Browse Source

Improve interrupt handling for simultaneous WAV file playing

dds
PaulStoffregen 4 years ago
parent
commit
6369a6ae1f
1 changed files with 25 additions and 17 deletions
  1. +25
    -17
      play_sd_wav.cpp

+ 25
- 17
play_sd_wav.cpp View File

#include "play_sd_wav.h" #include "play_sd_wav.h"
#include "spi_interrupt.h" #include "spi_interrupt.h"



#define STATE_DIRECT_8BIT_MONO 0 // playing mono at native sample rate #define STATE_DIRECT_8BIT_MONO 0 // playing mono at native sample rate
#define STATE_DIRECT_8BIT_STEREO 1 // playing stereo at native sample rate #define STATE_DIRECT_8BIT_STEREO 1 // playing stereo at native sample rate
#define STATE_DIRECT_16BIT_MONO 2 // playing mono at native sample rate #define STATE_DIRECT_16BIT_MONO 2 // playing mono at native sample rate
bool AudioPlaySdWav::play(const char *filename) bool AudioPlaySdWav::play(const char *filename)
{ {
stop(); stop();
#if defined(HAS_KINETIS_SDHC)
bool irq = false;
if (NVIC_IS_ENABLED(IRQ_SOFTWARE)) {
NVIC_DISABLE_IRQ(IRQ_SOFTWARE);
irq = true;
}
#if defined(HAS_KINETIS_SDHC)
if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStartUsingSPI(); if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStartUsingSPI();
#else
#else
AudioStartUsingSPI(); AudioStartUsingSPI();
#endif #endif
__disable_irq();
wavfile = SD.open(filename); wavfile = SD.open(filename);
__enable_irq();
if (!wavfile) { if (!wavfile) {
#if defined(HAS_KINETIS_SDHC)
#if defined(HAS_KINETIS_SDHC)
if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI(); if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI();
#else
#else
AudioStopUsingSPI(); AudioStopUsingSPI();
#endif
#endif
if (irq) NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
return false; return false;
} }
buffer_length = 0; buffer_length = 0;
data_length = 20; data_length = 20;
header_offset = 0; header_offset = 0;
state = STATE_PARSE1; state = STATE_PARSE1;
if (irq) NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
return true; return true;
} }


void AudioPlaySdWav::stop(void) void AudioPlaySdWav::stop(void)
{ {
__disable_irq();
bool irq = false;
if (NVIC_IS_ENABLED(IRQ_SOFTWARE)) {
NVIC_DISABLE_IRQ(IRQ_SOFTWARE);
irq = true;
}
if (state != STATE_STOP) { if (state != STATE_STOP) {
audio_block_t *b1 = block_left; audio_block_t *b1 = block_left;
block_left = NULL; block_left = NULL;
audio_block_t *b2 = block_right; audio_block_t *b2 = block_right;
block_right = NULL; block_right = NULL;
state = STATE_STOP; state = STATE_STOP;
__enable_irq();
if (b1) release(b1); if (b1) release(b1);
if (b2) release(b2); if (b2) release(b2);
wavfile.close(); wavfile.close();
#if defined(HAS_KINETIS_SDHC)
#if defined(HAS_KINETIS_SDHC)
if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI(); if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI();
#else
#else
AudioStopUsingSPI(); AudioStopUsingSPI();
#endif
} else {
__enable_irq();
#endif
} }
if (irq) NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
} }




} }
end: // end of file reached or other reason to stop end: // end of file reached or other reason to stop
wavfile.close(); wavfile.close();
#if defined(HAS_KINETIS_SDHC)
#if defined(HAS_KINETIS_SDHC)
if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI(); if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI();
#else
#else
AudioStopUsingSPI(); AudioStopUsingSPI();
#endif
#endif
state_play = STATE_STOP; state_play = STATE_STOP;
state = STATE_STOP; state = STATE_STOP;
cleanup: cleanup:

Loading…
Cancel
Save