waveplayer with play pause

Dependents:   WavePlayer_HelloWorld_RTOS5

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wave_player.cpp Source File

wave_player.cpp

00001 //-----------------------------------------------------------------------------
00002 // a sample mbed library to play back wave files.
00003 //
00004 // explanation of wave file format.
00005 // https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
00006 
00007 // if VERBOSE is uncommented then the wave player will enter a verbose
00008 // mode that displays all data values as it reads them from the file
00009 // and writes them to the DAC.  Very slow and unusable output on the DAC,
00010 // but useful for debugging wave files that don't work.
00011 //#define VERBOSE
00012 
00013 
00014 #include <mbed.h>
00015 #include <stdio.h>
00016 #include <wave_player.h>
00017 
00018         extern bool playing;
00019         short unsigned dac_data;
00020 
00021 
00022 //-----------------------------------------------------------------------------
00023 // constructor -- accepts an mbed pin to use for AnalogOut.  Only p18 will work
00024 wave_player::wave_player(AnalogOut *_dac)
00025 {
00026   wave_DAC=_dac;
00027   wave_DAC->write_u16(32768);        //DAC is 0-3.3V, so idles at ~1.6V
00028   verbosity=0;
00029 }
00030 
00031 //-----------------------------------------------------------------------------
00032 // if verbosity is set then wave player enters a mode where the wave file
00033 // is decoded and displayed to the screen, including sample values put into
00034 // the DAC FIFO, and values read out of the DAC FIFO by the ISR.  The DAC output
00035 // itself is so slow as to be unusable, but this might be handy for debugging
00036 // wave files that don't play
00037 //-----------------------------------------------------------------------------
00038 void wave_player::set_verbosity(int v)
00039 {
00040   verbosity=v;
00041 }
00042 
00043 //-----------------------------------------------------------------------------
00044 // player function.  Takes a pointer to an opened wave file.  The file needs
00045 // to be stored in a filesystem with enough bandwidth to feed the wave data.
00046 // LocalFileSystem isn't, but the SDcard is, at least for 22kHz files.  The
00047 // SDcard filesystem can be hotrodded by increasing the SPI frequency it uses
00048 // internally.
00049 //-----------------------------------------------------------------------------
00050 void wave_player::play(FILE *wavefile)
00051 {
00052         unsigned chunk_id,chunk_size,channel;
00053         unsigned data,samp_int,i;
00054         //short unsigned dac_data;
00055         long long slice_value;
00056         char *slice_buf;
00057         short *data_sptr;
00058         unsigned char *data_bptr;
00059         int *data_wptr;
00060         FMT_STRUCT wav_format;
00061         long slice,num_slices;
00062   DAC_wptr=0;
00063   DAC_rptr=0;
00064   for (i=0;i<256;i+=2) {
00065     DAC_fifo[i]=0;
00066     DAC_fifo[i+1]=3000;
00067   }
00068   DAC_wptr=4;
00069   DAC_on=0;
00070 
00071   fread(&chunk_id,4,1,wavefile);
00072   fread(&chunk_size,4,1,wavefile);
00073   while (!feof(wavefile)) {
00074     if (verbosity)
00075       printf("Read chunk ID 0x%x, size 0x%x\n",chunk_id,chunk_size);
00076     switch (chunk_id) {
00077       case 0x46464952:
00078         fread(&data,4,1,wavefile);
00079         if (verbosity) {
00080           printf("RIFF chunk\n");
00081           printf("  chunk size %d (0x%x)\n",chunk_size,chunk_size);
00082           printf("  RIFF type 0x%x\n",data);
00083         }
00084         break;
00085       case 0x20746d66:
00086         fread(&wav_format,sizeof(wav_format),1,wavefile);
00087         if (verbosity) {
00088           printf("FORMAT chunk\n");
00089           printf("  chunk size %d (0x%x)\n",chunk_size,chunk_size);
00090           printf("  compression code %d\n",wav_format.comp_code);
00091           printf("  %d channels\n",wav_format.num_channels);
00092           printf("  %d samples/sec\n",wav_format.sample_rate);
00093           printf("  %d bytes/sec\n",wav_format.avg_Bps);
00094           printf("  block align %d\n",wav_format.block_align);
00095           printf("  %d bits per sample\n",wav_format.sig_bps);
00096         }
00097         if (chunk_size > sizeof(wav_format))
00098           fseek(wavefile,chunk_size-sizeof(wav_format),SEEK_CUR);
00099         break;
00100       case 0x61746164:
00101 // allocate a buffer big enough to hold a slice
00102         slice_buf=(char *)malloc(wav_format.block_align);
00103         if (!slice_buf) {
00104           printf("Unable to malloc slice buffer");
00105           exit(1);
00106         }
00107         num_slices=chunk_size/wav_format.block_align;
00108         samp_int=1000000/(wav_format.sample_rate);
00109         if (verbosity) {
00110           printf("DATA chunk\n");
00111           printf("  chunk size %d (0x%x)\n",chunk_size,chunk_size);
00112           printf("  %d slices\n",num_slices);
00113           printf("  Ideal sample interval=%d\n",(unsigned)(1000000.0/wav_format.sample_rate));
00114           printf("  programmed interrupt tick interval=%d\n",samp_int);
00115         }
00116 
00117 // starting up ticker to write samples out -- no printfs until tick.detach is called
00118         if (verbosity)
00119           tick.attach_us(this,&wave_player::dac_out, 500000); 
00120         else
00121           tick.attach_us(this,&wave_player::dac_out, samp_int); 
00122         DAC_on=1; 
00123 
00124 // start reading slices, which contain one sample each for however many channels
00125 // are in the wave file.  one channel=mono, two channels=stereo, etc.  Since
00126 // mbed only has a single AnalogOut, all of the channels present are averaged
00127 // to produce a single sample value.  This summing and averaging happens in
00128 // a variable of type signed long long, to make sure that the data doesn't
00129 // overflow regardless of sample size (8 bits, 16 bits, 32 bits).
00130 //
00131 // note that from what I can find that 8 bit wave files use unsigned data,
00132 // while 16 and 32 bit wave files use signed data
00133 //
00134         for (slice=0;slice<num_slices;slice+=1) {
00135                       if (playing == false){
00136          
00137           break;
00138         } 
00139           fread(slice_buf,wav_format.block_align,1,wavefile);
00140           if (feof(wavefile)) {
00141             printf("Oops -- not enough slices in the wave file\n");
00142             exit(1);
00143           }
00144           data_sptr=(short *)slice_buf;     // 16 bit samples
00145           data_bptr=(unsigned char *)slice_buf;     // 8 bit samples
00146           data_wptr=(int *)slice_buf;     // 32 bit samples
00147           slice_value=0;
00148           for (channel=0;channel<wav_format.num_channels;channel++) {
00149             switch (wav_format.sig_bps) {
00150               case 16:
00151                 if (verbosity)
00152                   printf("16 bit channel %d data=%d ",channel,data_sptr[channel]);
00153                 slice_value+=data_sptr[channel];
00154                 break;
00155               case 32:
00156                 if (verbosity)
00157                   printf("32 bit channel %d data=%d ",channel,data_wptr[channel]);
00158                 slice_value+=data_wptr[channel];
00159                 break;
00160               case 8:
00161                 if (verbosity)
00162                   printf("8 bit channel %d data=%d ",channel,(int)data_bptr[channel]);
00163                 slice_value+=data_bptr[channel];
00164                 break;
00165             }
00166           }
00167           slice_value/=wav_format.num_channels;
00168           
00169 // slice_value is now averaged.  Next it needs to be scaled to an unsigned 16 bit value
00170 // with DC offset so it can be written to the DAC.
00171           switch (wav_format.sig_bps) {
00172             case 8:     slice_value<<=8;
00173                         break;
00174             case 16:    slice_value+=32768;
00175                         break;
00176             case 32:    slice_value>>=16;
00177                         slice_value+=32768;
00178                         break;
00179           }
00180           dac_data=(short unsigned)slice_value;
00181           if (verbosity)
00182             printf("sample %d wptr %d slice_value %d dac_data %u\n",slice,DAC_wptr,(int)slice_value,dac_data);
00183           DAC_fifo[DAC_wptr]=dac_data;
00184           DAC_wptr=(DAC_wptr+1) & 0xff;
00185           while (DAC_wptr==DAC_rptr) {
00186           }
00187         }
00188         DAC_on=0;
00189         tick.detach();
00190         free(slice_buf);
00191         break;
00192       case 0x5453494c:
00193         if (verbosity)
00194           printf("INFO chunk, size %d\n",chunk_size);
00195         fseek(wavefile,chunk_size,SEEK_CUR);
00196         break;
00197       default:
00198         printf("unknown chunk type 0x%x, size %d\n",chunk_id,chunk_size);
00199         data=fseek(wavefile,chunk_size,SEEK_CUR);
00200         break;
00201     }
00202     fread(&chunk_id,4,1,wavefile);
00203     fread(&chunk_size,4,1,wavefile);
00204   }
00205 }
00206 
00207 
00208 void wave_player::dac_out()
00209 {
00210   if (DAC_on) {
00211 #ifdef VERBOSE
00212   printf("ISR rdptr %d got %u\n",DAC_rptr,DAC_fifo[DAC_rptr]);
00213 #endif
00214     wave_DAC->write_u16(DAC_fifo[DAC_rptr]);
00215     DAC_rptr=(DAC_rptr+1) & 0xff;
00216   }
00217 }
00218 void wave_player::update_level()
00219 {
00220     dac_data_pub=dac_data;
00221 }
00222