wav: use dr_wav for wav decoding

Adds support for various wave formats.

Signed-off-by: Mahyar Koshkouei <mk@deltabeard.com>
This commit is contained in:
Mahyar Koshkouei
2023-07-08 14:38:55 +01:00
parent 0e79e61255
commit ca583a5487
2 changed files with 24 additions and 61 deletions

View File

@@ -55,17 +55,14 @@ enum file_types getFileType(const char *file)
{ {
// "RIFF" // "RIFF"
case 0x46464952: case 0x46464952:
if(fseek(ftest, 4, SEEK_CUR) != 0) // "riff"
break; case 0x66666972:
// "RIFX"
// "WAVE" case 0x58464952:
// Check required as AVI file format also uses "RIFF". // "RF64"
if(fread(&fileSig, 4, 1, ftest) == 0) case 0x34364652:
break; // "FORM"
case 0x4D524F46:
if(fileSig != 0x45564157)
break;
file_type = FILE_TYPE_WAV; file_type = FILE_TYPE_WAV;
break; break;

View File

@@ -2,13 +2,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define DR_WAV_IMPLEMENTATION
#include <dr_libs/dr_wav.h>
#include "wav.h" #include "wav.h"
#include "playback.h" #include "playback.h"
static const size_t buffSize = 16 * 1024; static drwav wav;
static FILE* pWav = NULL; static const size_t buffSize = 16 * 1024;
static char header[45];
static uint8_t channels;
static int initWav(const char* file); static int initWav(const char* file);
static uint32_t rateWav(void); static uint32_t rateWav(void);
@@ -39,47 +40,7 @@ void setWav(struct decoder_fn* decoder)
*/ */
int initWav(const char* file) int initWav(const char* file)
{ {
pWav = fopen(file, "rb"); return !drwav_init_file(&wav, file, NULL);
if(pWav == NULL)
return -1;
/* TODO: No need to read the first number of bytes */
if(fread(header, 1, 44, pWav) == 0)
return -1;
/**
* http://www.topherlee.com/software/pcm-tut-wavformat.html and
* http://soundfile.sapp.org/doc/WaveFormat/ helped a lot.
* format = (header[19]<<8) + (header[20]);
* channels = (header[23]<<8) + (header[22]);
* sample = (header[27]<<24) + (header[26]<<16) + (header[25]<<8) +
* (header[24]);
* byterate = (header[31]<<24) + (header[30]<<16) + (header[29]<<8) +
* (header[28]);
* blockalign = (header[33]<<8) + (header[32]);
* bitness = (header[35]<<8) + (header[34]);
*/
/* TODO: This should be moved to get file type */
/* Only support 16 bit PCM WAV */
if(((header[35]<<8) + (header[34])) != 16)
return -1;
channels = (header[23]<<8) + (header[22]);
switch(channels)
{
/* Only Mono and Stereo allowed */
case 1:
case 2:
break;
default:
return -1;
}
return 0;
} }
/** /**
@@ -89,8 +50,7 @@ int initWav(const char* file)
*/ */
uint32_t rateWav(void) uint32_t rateWav(void)
{ {
return (header[27]<<24) + (header[26]<<16) + (header[25]<<8) + return wav.sampleRate;
(header[24]);
} }
/** /**
@@ -100,7 +60,7 @@ uint32_t rateWav(void)
*/ */
uint8_t channelWav(void) uint8_t channelWav(void)
{ {
return channels; return wav.channels;
} }
/** /**
@@ -111,7 +71,13 @@ uint8_t channelWav(void)
*/ */
uint64_t readWav(void* buffer) uint64_t readWav(void* buffer)
{ {
return fread(buffer, 1, buffSize, pWav) / sizeof(int16_t); size_t buffSizeFrames;
uint64_t samplesRead;
buffSizeFrames = buffSize / (size_t)wav.channels;
samplesRead = drwav_read_pcm_frames_s16(&wav, buffSizeFrames, buffer);
samplesRead *= (uint64_t)wav.channels;
return samplesRead;
} }
/** /**
@@ -119,5 +85,5 @@ uint64_t readWav(void* buffer)
*/ */
void exitWav(void) void exitWav(void)
{ {
fclose(pWav); drwav_uninit(&wav);
} }