amended wave player

Fork of wave_player_appbd by jim hamblen

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