Browse Source

Play WAV files with junk sections

dds
PaulStoffregen 6 years ago
parent
commit
4a4d695cec
2 changed files with 42 additions and 13 deletions
  1. +41
    -12
      play_sd_wav.cpp
  2. +1
    -1
      play_sd_wav.h

+ 41
- 12
play_sd_wav.cpp View File

#define STATE_PARSE1 8 // looking for 20 byte ID header #define STATE_PARSE1 8 // looking for 20 byte ID header
#define STATE_PARSE2 9 // looking for 16 byte format header #define STATE_PARSE2 9 // looking for 16 byte format header
#define STATE_PARSE3 10 // looking for 8 byte data header #define STATE_PARSE3 10 // looking for 8 byte data header
#define STATE_PARSE4 11 // ignoring unknown chunk
#define STATE_STOP 12
#define STATE_PARSE4 11 // ignoring unknown chunk after "fmt "
#define STATE_PARSE5 12 // ignoring unknown chunk before "fmt "
#define STATE_STOP 13


void AudioPlaySdWav::begin(void) void AudioPlaySdWav::begin(void)
{ {
data_length -= len; data_length -= len;
if (data_length > 0) return false; if (data_length > 0) return false;
// parse the header... // parse the header...
if (header[0] == 0x46464952 && header[2] == 0x45564157
&& header[3] == 0x20746D66 && header[4] >= 16) {
if (header[4] > sizeof(header)) {
// if such .wav files exist, increasing the
// size of header[] should accomodate them...
//Serial.println("WAVEFORMATEXTENSIBLE too long");
break;
if (header[0] == 0x46464952 && header[2] == 0x45564157) {
//Serial.println("is wav file");
if (header[3] == 0x20746D66) {
// "fmt " header
if (header[4] < 16) {
// WAV "fmt " info must be at least 16 bytes
break;
}
if (header[4] > sizeof(header)) {
// if such .wav files exist, increasing the
// size of header[] should accomodate them...
//Serial.println("WAVEFORMATEXTENSIBLE too long");
break;
}
//Serial.println("header ok");
header_offset = 0;
state = STATE_PARSE2;
} else {
// first chuck is something other than "fmt "
//Serial.print("skipping \"");
//Serial.printf("\" (%08X), ", __builtin_bswap32(header[3]));
//Serial.print(header[4]);
//Serial.println(" bytes");
header_offset = 12;
state = STATE_PARSE5;
} }
//Serial.println("header ok");
p += len; p += len;
size -= len; size -= len;
data_length = header[4]; data_length = header[4];
header_offset = 0;
state = STATE_PARSE2;
goto start; goto start;
} }
//Serial.println("unknown WAV header"); //Serial.println("unknown WAV header");
//Serial.println("consumed unknown chunk"); //Serial.println("consumed unknown chunk");
goto start; goto start;


// skip past "junk" data before "fmt " header
case STATE_PARSE5:
len = data_length;
if (size < len) len = size;
buffer_offset += len;
data_length -= len;
if (data_length > 0) return false;
p += len;
size -= len;
data_length = 8;
state = STATE_PARSE1;
goto start;

// playing mono at native sample rate // playing mono at native sample rate
case STATE_DIRECT_8BIT_MONO: case STATE_DIRECT_8BIT_MONO:
return false; return false;

+ 1
- 1
play_sd_wav.h View File

File wavfile; File wavfile;
bool consume(uint32_t size); bool consume(uint32_t size);
bool parse_format(void); bool parse_format(void);
uint32_t header[6]; // temporary storage of wav header data
uint32_t header[10]; // temporary storage of wav header data
uint32_t data_length; // number of bytes remaining in current section uint32_t data_length; // number of bytes remaining in current section
uint32_t total_length; // number of audio data bytes in file uint32_t total_length; // number of audio data bytes in file
uint32_t bytes2millis; uint32_t bytes2millis;

Loading…
Cancel
Save