Version of wave_player that allows music player to be interrupted in order to enable "STOP" or "PAUSE" functionality.
Dependents: Portable_Surface_Transducer_Jukebox
Revision 2:b3124879f219, committed 2018-12-12
- Comitter:
- jrod1096
- Date:
- Wed Dec 12 05:16:22 2018 +0000
- Parent:
- 1:acc3e18e77ad
- Commit message:
- Allows wave player to be interrupted to add "STOP" or "PAUSE" functionality.
Changed in this revision
wave_player.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/wave_player.cpp Tue Jan 18 03:57:27 2011 +0000 +++ b/wave_player.cpp Wed Dec 12 05:16:22 2018 +0000 @@ -14,7 +14,7 @@ #include <mbed.h> #include <stdio.h> #include <wave_player.h> - +#include <stdlib.h> //----------------------------------------------------------------------------- // constructor -- accepts an mbed pin to use for AnalogOut. Only p18 will work @@ -56,6 +56,10 @@ int *data_wptr; FMT_STRUCT wav_format; long slice,num_slices; + extern bool play; + extern bool old_play; + + DAC_wptr=0; DAC_rptr=0; for (i=0;i<256;i+=2) { @@ -68,6 +72,7 @@ fread(&chunk_id,4,1,wavefile); fread(&chunk_size,4,1,wavefile); while (!feof(wavefile)) { + if (verbosity) printf("Read chunk ID 0x%x, size 0x%x\n",chunk_id,chunk_size); switch (chunk_id) { @@ -129,11 +134,17 @@ // while 16 and 32 bit wave files use signed data // for (slice=0;slice<num_slices;slice+=1) { + if (play == false){ + + break; + } fread(slice_buf,wav_format.block_align,1,wavefile); if (feof(wavefile)) { printf("Oops -- not enough slices in the wave file\n"); exit(1); } + + data_sptr=(short *)slice_buf; // 16 bit samples data_bptr=(unsigned char *)slice_buf; // 8 bit samples data_wptr=(int *)slice_buf; // 32 bit samples @@ -173,7 +184,9 @@ dac_data=(short unsigned)slice_value; if (verbosity) printf("sample %d wptr %d slice_value %d dac_data %u\n",slice,DAC_wptr,(int)slice_value,dac_data); - DAC_fifo[DAC_wptr]=dac_data; + DAC_fifo[DAC_wptr]=dac_data; + + DAC_wptr=(DAC_wptr+1) & 0xff; while (DAC_wptr==DAC_rptr) { } @@ -205,7 +218,6 @@ printf("ISR rdptr %d got %u\n",DAC_rptr,DAC_fifo[DAC_rptr]); #endif wave_DAC->write_u16(DAC_fifo[DAC_rptr]); - DAC_rptr=(DAC_rptr+1) & 0xff; + DAC_rptr=(DAC_rptr+1) & 0xff; } -} - + } \ No newline at end of file