Fork of wave_player by
Revision 2:1a9dbc4d2b46, committed 2014-02-02
- Comitter:
- aos
- Date:
- Sun Feb 02 10:48:11 2014 +0000
- Parent:
- 1:acc3e18e77ad
- Commit message:
- Modified for adjusting volume
Changed in this revision
wave_player.cpp | Show annotated file Show diff for this revision Revisions of this file |
wave_player.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r acc3e18e77ad -r 1a9dbc4d2b46 wave_player.cpp --- a/wave_player.cpp Tue Jan 18 03:57:27 2011 +0000 +++ b/wave_player.cpp Sun Feb 02 10:48:11 2014 +0000 @@ -1,21 +1,10 @@ -//----------------------------------------------------------------------------- -// a sample mbed library to play back wave files. -// -// explanation of wave file format. -// https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ -// if VERBOSE is uncommented then the wave player will enter a verbose -// mode that displays all data values as it reads them from the file -// and writes them to the DAC. Very slow and unusable output on the DAC, -// but useful for debugging wave files that don't work. -//#define VERBOSE - - + #include <mbed.h> #include <stdio.h> #include <wave_player.h> - - + +Serial pc(USBTX, USBRX); //----------------------------------------------------------------------------- // constructor -- accepts an mbed pin to use for AnalogOut. Only p18 will work wave_player::wave_player(AnalogOut *_dac) @@ -24,7 +13,7 @@ wave_DAC->write_u16(32768); //DAC is 0-3.3V, so idles at ~1.6V verbosity=0; } - + //----------------------------------------------------------------------------- // if verbosity is set then wave player enters a mode where the wave file // is decoded and displayed to the screen, including sample values put into @@ -36,7 +25,7 @@ { verbosity=v; } - + //----------------------------------------------------------------------------- // player function. Takes a pointer to an opened wave file. The file needs // to be stored in a filesystem with enough bandwidth to feed the wave data. @@ -64,60 +53,62 @@ } DAC_wptr=4; DAC_on=0; - + 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); + pc.printf("Read chunk ID 0x%x, size 0x%x\n",chunk_id,chunk_size); switch (chunk_id) { case 0x46464952: fread(&data,4,1,wavefile); if (verbosity) { - printf("RIFF chunk\n"); - printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size); - printf(" RIFF type 0x%x\n",data); + pc.printf("RIFF chunk\n"); + pc.printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size); + pc.printf(" RIFF type 0x%x\n",data); } + pc.printf("case1\n"); break; case 0x20746d66: fread(&wav_format,sizeof(wav_format),1,wavefile); if (verbosity) { - printf("FORMAT chunk\n"); - printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size); - printf(" compression code %d\n",wav_format.comp_code); - printf(" %d channels\n",wav_format.num_channels); - printf(" %d samples/sec\n",wav_format.sample_rate); - printf(" %d bytes/sec\n",wav_format.avg_Bps); - printf(" block align %d\n",wav_format.block_align); - printf(" %d bits per sample\n",wav_format.sig_bps); + pc.printf("FORMAT chunk\n"); + pc.printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size); + pc.printf(" compression code %d\n",wav_format.comp_code); + pc.printf(" %d channels\n",wav_format.num_channels); + pc.printf(" %d samples/sec\n",wav_format.sample_rate); + pc.printf(" %d bytes/sec\n",wav_format.avg_Bps); + pc.printf(" block align %d\n",wav_format.block_align); + pc.printf(" %d bits per sample\n",wav_format.sig_bps); } if (chunk_size > sizeof(wav_format)) fseek(wavefile,chunk_size-sizeof(wav_format),SEEK_CUR); + pc.printf("case2\n"); break; case 0x61746164: // allocate a buffer big enough to hold a slice slice_buf=(char *)malloc(wav_format.block_align); if (!slice_buf) { - printf("Unable to malloc slice buffer"); + pc.printf("Unable to malloc slice buffer"); exit(1); } num_slices=chunk_size/wav_format.block_align; samp_int=1000000/(wav_format.sample_rate); if (verbosity) { - printf("DATA chunk\n"); - printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size); - printf(" %d slices\n",num_slices); - printf(" Ideal sample interval=%d\n",(unsigned)(1000000.0/wav_format.sample_rate)); - printf(" programmed interrupt tick interval=%d\n",samp_int); + pc.printf("DATA chunk\n"); + pc.printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size); + pc.printf(" %d slices\n",num_slices); + pc.printf(" Ideal sample interval=%d\n",(unsigned)(1000000.0/wav_format.sample_rate)); + pc.printf(" programmed interrupt tick interval=%d\n",samp_int); } - + // starting up ticker to write samples out -- no printfs until tick.detach is called if (verbosity) tick.attach_us(this,&wave_player::dac_out, 500000); else tick.attach_us(this,&wave_player::dac_out, samp_int); DAC_on=1; - + // start reading slices, which contain one sample each for however many channels // are in the wave file. one channel=mono, two channels=stereo, etc. Since // mbed only has a single AnalogOut, all of the channels present are averaged @@ -131,7 +122,7 @@ for (slice=0;slice<num_slices;slice+=1) { fread(slice_buf,wav_format.block_align,1,wavefile); if (feof(wavefile)) { - printf("Oops -- not enough slices in the wave file\n"); + pc.printf("Oops -- not enough slices in the wave file\n"); exit(1); } data_sptr=(short *)slice_buf; // 16 bit samples @@ -139,20 +130,21 @@ data_wptr=(int *)slice_buf; // 32 bit samples slice_value=0; for (channel=0;channel<wav_format.num_channels;channel++) { + switch (wav_format.sig_bps) { case 16: if (verbosity) - printf("16 bit channel %d data=%d ",channel,data_sptr[channel]); + pc.printf("16 bit channel %d data=%d ",channel,data_sptr[channel]); slice_value+=data_sptr[channel]; break; case 32: if (verbosity) - printf("32 bit channel %d data=%d ",channel,data_wptr[channel]); + pc.printf("32 bit channel %d data=%d ",channel,data_wptr[channel]); slice_value+=data_wptr[channel]; break; case 8: if (verbosity) - printf("8 bit channel %d data=%d ",channel,(int)data_bptr[channel]); + pc.printf("8 bit channel %d data=%d ",channel,(int)data_bptr[channel]); slice_value+=data_bptr[channel]; break; } @@ -172,8 +164,10 @@ } 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; + pc.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*volume; //--> modified: adjusting the volume + + //controls end DAC_wptr=(DAC_wptr+1) & 0xff; while (DAC_wptr==DAC_rptr) { } @@ -181,14 +175,16 @@ DAC_on=0; tick.detach(); free(slice_buf); + pc.printf("case3\n"); break; case 0x5453494c: if (verbosity) - printf("INFO chunk, size %d\n",chunk_size); + pc.printf("INFO chunk, size %d\n",chunk_size); fseek(wavefile,chunk_size,SEEK_CUR); + pc.printf("case4\n"); break; default: - printf("unknown chunk type 0x%x, size %d\n",chunk_id,chunk_size); + pc.printf("unknown chunk type 0x%x, size %d\n",chunk_id,chunk_size); data=fseek(wavefile,chunk_size,SEEK_CUR); break; } @@ -196,16 +192,15 @@ fread(&chunk_size,4,1,wavefile); } } - - + + void wave_player::dac_out() { if (DAC_on) { #ifdef VERBOSE - printf("ISR rdptr %d got %u\n",DAC_rptr,DAC_fifo[DAC_rptr]); + pc.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; } -} - +} \ No newline at end of file
diff -r acc3e18e77ad -r 1a9dbc4d2b46 wave_player.h --- a/wave_player.h Tue Jan 18 03:57:27 2011 +0000 +++ b/wave_player.h Sun Feb 02 10:48:11 2014 +0000 @@ -30,8 +30,8 @@ * } * @endcode */ + class wave_player { - public: /** Create a wave player using a pointer to the given AnalogOut object. * @@ -58,6 +58,10 @@ */ void set_verbosity(int v); +float volume; +//public: +//AnalogIn position_player; + private: void dac_out(void); int verbosity;