Bladeren bron

Add status and position functions to AudioPlaySdWav

dds
PaulStoffregen 10 jaren geleden
bovenliggende
commit
636bba0723
2 gewijzigde bestanden met toevoegingen van 56 en 30 verwijderingen
  1. +51
    -28
      play_sd_wav.cpp
  2. +5
    -2
      play_sd_wav.h

+ 51
- 28
play_sd_wav.cpp Bestand weergeven

@@ -87,20 +87,6 @@ void AudioPlaySdWav::stop(void)
}
}

bool AudioPlaySdWav::start(void)
{
__disable_irq();
if (state == STATE_STOP) {
if (state_play == STATE_STOP) {
__enable_irq();
return false;
}
state = state_play;
}
__enable_irq();
return true;
}


void AudioPlaySdWav::update(void)
{
@@ -268,6 +254,7 @@ start:
block_right = allocate();
if (!block_right) return false;
}
total_length = data_length;
} else {
state = STATE_PARSE4;
}
@@ -427,16 +414,16 @@ start:
// 256 byte chunks, speed is 443272 bytes/sec
// 512 byte chunks, speed is 468023 bytes/sec

#define B2M_44100 (uint32_t)((double)4294967296000.0 / AUDIO_SAMPLE_RATE_EXACT) // 97352592
#define B2M_22050 (uint32_t)((double)4294967296000.0 / AUDIO_SAMPLE_RATE_EXACT * 2.0)
#define B2M_11025 (uint32_t)((double)4294967296000.0 / AUDIO_SAMPLE_RATE_EXACT * 4.0)

bool AudioPlaySdWav::parse_format(void)
{
uint8_t num = 0;
uint16_t format;
uint16_t channels;
uint32_t rate;
uint32_t rate, b2m;
uint16_t bits;

format = header[0];
@@ -444,12 +431,28 @@ bool AudioPlaySdWav::parse_format(void)
//Serial.println(format);
if (format != 1) return false;

rate = header[1];
//Serial.print(" rate = ");
//Serial.println(rate);
if (rate == AUDIO_SAMPLE_RATE) {
b2m = B2M_44100;
} else if (rate == 22050) {
b2m = B2M_22050;
num |= 4;
} else if (rate == 11025) {
b2m = B2M_11025;
num |= 4;
} else {
return false;
}

channels = header[0] >> 16;
//Serial.print(" channels = ");
//Serial.println(channels);
if (channels == 1) {
} else if (channels == 2) {
num = 1;
b2m >>= 1;
num |= 1;
} else {
return false;
}
@@ -459,20 +462,16 @@ bool AudioPlaySdWav::parse_format(void)
//Serial.println(bits);
if (bits == 8) {
} else if (bits == 16) {
b2m >>= 1;
num |= 2;
} else {
return false;
}

rate = header[1];
//Serial.print(" rate = ");
//Serial.println(rate);
if (rate == AUDIO_SAMPLE_RATE) {
} else if (rate >= 8000 && rate <= 48000) {
num |= 4;
} else {
return false;
}
bytes2millis = b2m;
//Serial.print(" bytes2millis = ");
//Serial.println(b2m);

// we're not checking the byte rate and block align fields
// if they're not the expected values, all we could do is
// return false. Do any real wav files have unexpected
@@ -482,3 +481,27 @@ bool AudioPlaySdWav::parse_format(void)
}


bool AudioPlaySdWav::isPlaying(void)
{
return (state < 8);
}

uint32_t AudioPlaySdWav::positionMillis(void)
{
if (state >= 8) return 0;
uint32_t offset = total_length - data_length;
return ((uint64_t)offset * bytes2millis) >> 32;
}


uint32_t AudioPlaySdWav::lengthMillis(void)
{
if (state >= 8) return 0;
return ((uint64_t)total_length * bytes2millis) >> 32;
}







+ 5
- 2
play_sd_wav.h Bestand weergeven

@@ -35,10 +35,11 @@ class AudioPlaySdWav : public AudioStream
public:
AudioPlaySdWav(void) : AudioStream(0, NULL) { begin(); }
void begin(void);

bool play(const char *filename);
void stop(void);
bool start(void);
bool isPlaying(void);
uint32_t positionMillis(void);
uint32_t lengthMillis(void);
virtual void update(void);
private:
File wavfile;
@@ -46,6 +47,8 @@ private:
bool parse_format(void);
uint32_t header[5];
uint32_t data_length; // number of bytes remaining in data section
uint32_t total_length; // number of audio data bytes in file
uint32_t bytes2millis;
audio_block_t *block_left;
audio_block_t *block_right;
uint16_t block_offset;

Laden…
Annuleren
Opslaan