ESE519 / wave_player_appbd

Dependents:   AppBoard_Waveplayer

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