ECE 4180 Project for Spring 2020

Dependencies:   mbed mbed-rtos SDFileSystem PinDetect ESP8266NodeMCUInterface

Committer:
kimberlylie99
Date:
Fri May 01 18:43:13 2020 +0000
Revision:
1:5ae291085f75
Parent:
0:b97c07227845
Edit Main.cpp;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kimberlylie99 0:b97c07227845 1 //-----------------------------------------------------------------------------
kimberlylie99 0:b97c07227845 2 // a sample mbed library to play back wave files.
kimberlylie99 0:b97c07227845 3 //
kimberlylie99 0:b97c07227845 4 // explanation of wave file format.
kimberlylie99 0:b97c07227845 5 // https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
kimberlylie99 0:b97c07227845 6
kimberlylie99 0:b97c07227845 7 // if VERBOSE is uncommented then the wave player will enter a verbose
kimberlylie99 0:b97c07227845 8 // mode that displays all data values as it reads them from the file
kimberlylie99 0:b97c07227845 9 // and writes them to the DAC. Very slow and unusable output on the DAC,
kimberlylie99 0:b97c07227845 10 // but useful for debugging wave files that don't work.
kimberlylie99 0:b97c07227845 11 //#define VERBOSE
kimberlylie99 0:b97c07227845 12
kimberlylie99 0:b97c07227845 13
kimberlylie99 0:b97c07227845 14 #include <mbed.h>
kimberlylie99 0:b97c07227845 15 #include <stdio.h>
kimberlylie99 0:b97c07227845 16 #include <wave_player.h>
kimberlylie99 0:b97c07227845 17
kimberlylie99 0:b97c07227845 18
kimberlylie99 0:b97c07227845 19 //-----------------------------------------------------------------------------
kimberlylie99 0:b97c07227845 20 // constructor -- accepts an mbed pin to use for AnalogOut. Only p18 will work
kimberlylie99 0:b97c07227845 21 wave_player::wave_player(AnalogOut *_dac)
kimberlylie99 0:b97c07227845 22 {
kimberlylie99 0:b97c07227845 23 wave_DAC=_dac;
kimberlylie99 0:b97c07227845 24 wave_DAC->write_u16(32768); //DAC is 0-3.3V, so idles at ~1.6V
kimberlylie99 0:b97c07227845 25 verbosity=0;
kimberlylie99 0:b97c07227845 26 }
kimberlylie99 0:b97c07227845 27
kimberlylie99 0:b97c07227845 28 //-----------------------------------------------------------------------------
kimberlylie99 0:b97c07227845 29 // if verbosity is set then wave player enters a mode where the wave file
kimberlylie99 0:b97c07227845 30 // is decoded and displayed to the screen, including sample values put into
kimberlylie99 0:b97c07227845 31 // the DAC FIFO, and values read out of the DAC FIFO by the ISR. The DAC output
kimberlylie99 0:b97c07227845 32 // itself is so slow as to be unusable, but this might be handy for debugging
kimberlylie99 0:b97c07227845 33 // wave files that don't play
kimberlylie99 0:b97c07227845 34 //-----------------------------------------------------------------------------
kimberlylie99 0:b97c07227845 35 void wave_player::set_verbosity(int v)
kimberlylie99 0:b97c07227845 36 {
kimberlylie99 0:b97c07227845 37 verbosity=v;
kimberlylie99 0:b97c07227845 38 }
kimberlylie99 0:b97c07227845 39
kimberlylie99 0:b97c07227845 40 //-----------------------------------------------------------------------------
kimberlylie99 0:b97c07227845 41 // player function. Takes a pointer to an opened wave file. The file needs
kimberlylie99 0:b97c07227845 42 // to be stored in a filesystem with enough bandwidth to feed the wave data.
kimberlylie99 0:b97c07227845 43 // LocalFileSystem isn't, but the SDcard is, at least for 22kHz files. The
kimberlylie99 0:b97c07227845 44 // SDcard filesystem can be hotrodded by increasing the SPI frequency it uses
kimberlylie99 0:b97c07227845 45 // internally.
kimberlylie99 0:b97c07227845 46 //-----------------------------------------------------------------------------
kimberlylie99 0:b97c07227845 47 void wave_player::play(FILE *wavefile)
kimberlylie99 0:b97c07227845 48 {
kimberlylie99 0:b97c07227845 49 unsigned chunk_id,chunk_size,channel;
kimberlylie99 0:b97c07227845 50 unsigned data,samp_int,i;
kimberlylie99 0:b97c07227845 51 short unsigned dac_data;
kimberlylie99 0:b97c07227845 52 long long slice_value;
kimberlylie99 0:b97c07227845 53 char *slice_buf;
kimberlylie99 0:b97c07227845 54 short *data_sptr;
kimberlylie99 0:b97c07227845 55 unsigned char *data_bptr;
kimberlylie99 0:b97c07227845 56 int *data_wptr;
kimberlylie99 0:b97c07227845 57 FMT_STRUCT wav_format;
kimberlylie99 0:b97c07227845 58 long slice,num_slices;
kimberlylie99 0:b97c07227845 59 DAC_wptr=0;
kimberlylie99 0:b97c07227845 60 DAC_rptr=0;
kimberlylie99 0:b97c07227845 61 for (i=0;i<256;i+=2) {
kimberlylie99 0:b97c07227845 62 DAC_fifo[i]=0;
kimberlylie99 0:b97c07227845 63 DAC_fifo[i+1]=3000;
kimberlylie99 0:b97c07227845 64 }
kimberlylie99 0:b97c07227845 65 DAC_wptr=4;
kimberlylie99 0:b97c07227845 66 DAC_on=0;
kimberlylie99 0:b97c07227845 67
kimberlylie99 0:b97c07227845 68 fread(&chunk_id,4,1,wavefile);
kimberlylie99 0:b97c07227845 69 fread(&chunk_size,4,1,wavefile);
kimberlylie99 0:b97c07227845 70 while (!feof(wavefile)) {
kimberlylie99 0:b97c07227845 71 if (verbosity)
kimberlylie99 0:b97c07227845 72 printf("Read chunk ID 0x%x, size 0x%x\n",chunk_id,chunk_size);
kimberlylie99 0:b97c07227845 73 switch (chunk_id) {
kimberlylie99 0:b97c07227845 74 case 0x46464952:
kimberlylie99 0:b97c07227845 75 fread(&data,4,1,wavefile);
kimberlylie99 0:b97c07227845 76 if (verbosity) {
kimberlylie99 0:b97c07227845 77 printf("RIFF chunk\n");
kimberlylie99 0:b97c07227845 78 printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size);
kimberlylie99 0:b97c07227845 79 printf(" RIFF type 0x%x\n",data);
kimberlylie99 0:b97c07227845 80 }
kimberlylie99 0:b97c07227845 81 break;
kimberlylie99 0:b97c07227845 82 case 0x20746d66:
kimberlylie99 0:b97c07227845 83 fread(&wav_format,sizeof(wav_format),1,wavefile);
kimberlylie99 0:b97c07227845 84 if (verbosity) {
kimberlylie99 0:b97c07227845 85 printf("FORMAT chunk\n");
kimberlylie99 0:b97c07227845 86 printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size);
kimberlylie99 0:b97c07227845 87 printf(" compression code %d\n",wav_format.comp_code);
kimberlylie99 0:b97c07227845 88 printf(" %d channels\n",wav_format.num_channels);
kimberlylie99 0:b97c07227845 89 printf(" %d samples/sec\n",wav_format.sample_rate);
kimberlylie99 0:b97c07227845 90 printf(" %d bytes/sec\n",wav_format.avg_Bps);
kimberlylie99 0:b97c07227845 91 printf(" block align %d\n",wav_format.block_align);
kimberlylie99 0:b97c07227845 92 printf(" %d bits per sample\n",wav_format.sig_bps);
kimberlylie99 0:b97c07227845 93 }
kimberlylie99 0:b97c07227845 94 if (chunk_size > sizeof(wav_format))
kimberlylie99 0:b97c07227845 95 fseek(wavefile,chunk_size-sizeof(wav_format),SEEK_CUR);
kimberlylie99 0:b97c07227845 96 break;
kimberlylie99 0:b97c07227845 97 case 0x61746164:
kimberlylie99 0:b97c07227845 98 // allocate a buffer big enough to hold a slice
kimberlylie99 0:b97c07227845 99 slice_buf=(char *)malloc(wav_format.block_align);
kimberlylie99 0:b97c07227845 100 if (!slice_buf) {
kimberlylie99 0:b97c07227845 101 printf("Unable to malloc slice buffer");
kimberlylie99 0:b97c07227845 102 exit(1);
kimberlylie99 0:b97c07227845 103 }
kimberlylie99 0:b97c07227845 104 num_slices=chunk_size/wav_format.block_align;
kimberlylie99 0:b97c07227845 105 samp_int=1000000/(wav_format.sample_rate);
kimberlylie99 0:b97c07227845 106 if (verbosity) {
kimberlylie99 0:b97c07227845 107 printf("DATA chunk\n");
kimberlylie99 0:b97c07227845 108 printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size);
kimberlylie99 0:b97c07227845 109 printf(" %d slices\n",num_slices);
kimberlylie99 0:b97c07227845 110 printf(" Ideal sample interval=%d\n",(unsigned)(1000000.0/wav_format.sample_rate));
kimberlylie99 0:b97c07227845 111 printf(" programmed interrupt tick interval=%d\n",samp_int);
kimberlylie99 0:b97c07227845 112 }
kimberlylie99 0:b97c07227845 113
kimberlylie99 0:b97c07227845 114 // starting up ticker to write samples out -- no printfs until tick.detach is called
kimberlylie99 0:b97c07227845 115 if (verbosity)
kimberlylie99 0:b97c07227845 116 tick.attach_us(this,&wave_player::dac_out, 500000);
kimberlylie99 0:b97c07227845 117 else
kimberlylie99 0:b97c07227845 118 tick.attach_us(this,&wave_player::dac_out, samp_int);
kimberlylie99 0:b97c07227845 119 DAC_on=1;
kimberlylie99 0:b97c07227845 120
kimberlylie99 0:b97c07227845 121 // start reading slices, which contain one sample each for however many channels
kimberlylie99 0:b97c07227845 122 // are in the wave file. one channel=mono, two channels=stereo, etc. Since
kimberlylie99 0:b97c07227845 123 // mbed only has a single AnalogOut, all of the channels present are averaged
kimberlylie99 0:b97c07227845 124 // to produce a single sample value. This summing and averaging happens in
kimberlylie99 0:b97c07227845 125 // a variable of type signed long long, to make sure that the data doesn't
kimberlylie99 0:b97c07227845 126 // overflow regardless of sample size (8 bits, 16 bits, 32 bits).
kimberlylie99 0:b97c07227845 127 //
kimberlylie99 0:b97c07227845 128 // note that from what I can find that 8 bit wave files use unsigned data,
kimberlylie99 0:b97c07227845 129 // while 16 and 32 bit wave files use signed data
kimberlylie99 0:b97c07227845 130 //
kimberlylie99 0:b97c07227845 131 for (slice=0;slice<num_slices;slice+=1) {
kimberlylie99 0:b97c07227845 132 fread(slice_buf,wav_format.block_align,1,wavefile);
kimberlylie99 0:b97c07227845 133 if (feof(wavefile)) {
kimberlylie99 0:b97c07227845 134 printf("Oops -- not enough slices in the wave file\n");
kimberlylie99 0:b97c07227845 135 exit(1);
kimberlylie99 0:b97c07227845 136 }
kimberlylie99 0:b97c07227845 137 data_sptr=(short *)slice_buf; // 16 bit samples
kimberlylie99 0:b97c07227845 138 data_bptr=(unsigned char *)slice_buf; // 8 bit samples
kimberlylie99 0:b97c07227845 139 data_wptr=(int *)slice_buf; // 32 bit samples
kimberlylie99 0:b97c07227845 140 slice_value=0;
kimberlylie99 0:b97c07227845 141 for (channel=0;channel<wav_format.num_channels;channel++) {
kimberlylie99 0:b97c07227845 142 switch (wav_format.sig_bps) {
kimberlylie99 0:b97c07227845 143 case 16:
kimberlylie99 0:b97c07227845 144 if (verbosity)
kimberlylie99 0:b97c07227845 145 printf("16 bit channel %d data=%d ",channel,data_sptr[channel]);
kimberlylie99 0:b97c07227845 146 slice_value+=data_sptr[channel];
kimberlylie99 0:b97c07227845 147 break;
kimberlylie99 0:b97c07227845 148 case 32:
kimberlylie99 0:b97c07227845 149 if (verbosity)
kimberlylie99 0:b97c07227845 150 printf("32 bit channel %d data=%d ",channel,data_wptr[channel]);
kimberlylie99 0:b97c07227845 151 slice_value+=data_wptr[channel];
kimberlylie99 0:b97c07227845 152 break;
kimberlylie99 0:b97c07227845 153 case 8:
kimberlylie99 0:b97c07227845 154 if (verbosity)
kimberlylie99 0:b97c07227845 155 printf("8 bit channel %d data=%d ",channel,(int)data_bptr[channel]);
kimberlylie99 0:b97c07227845 156 slice_value+=data_bptr[channel];
kimberlylie99 0:b97c07227845 157 break;
kimberlylie99 0:b97c07227845 158 }
kimberlylie99 0:b97c07227845 159 }
kimberlylie99 0:b97c07227845 160 slice_value/=wav_format.num_channels;
kimberlylie99 0:b97c07227845 161
kimberlylie99 0:b97c07227845 162 // slice_value is now averaged. Next it needs to be scaled to an unsigned 16 bit value
kimberlylie99 0:b97c07227845 163 // with DC offset so it can be written to the DAC.
kimberlylie99 0:b97c07227845 164 switch (wav_format.sig_bps) {
kimberlylie99 0:b97c07227845 165 case 8: slice_value<<=8;
kimberlylie99 0:b97c07227845 166 break;
kimberlylie99 0:b97c07227845 167 case 16: slice_value+=32768;
kimberlylie99 0:b97c07227845 168 break;
kimberlylie99 0:b97c07227845 169 case 32: slice_value>>=16;
kimberlylie99 0:b97c07227845 170 slice_value+=32768;
kimberlylie99 0:b97c07227845 171 break;
kimberlylie99 0:b97c07227845 172 }
kimberlylie99 0:b97c07227845 173 dac_data=(short unsigned)slice_value;
kimberlylie99 0:b97c07227845 174 if (verbosity)
kimberlylie99 0:b97c07227845 175 printf("sample %d wptr %d slice_value %d dac_data %u\n",slice,DAC_wptr,(int)slice_value,dac_data);
kimberlylie99 0:b97c07227845 176 DAC_fifo[DAC_wptr]=dac_data;
kimberlylie99 0:b97c07227845 177 DAC_wptr=(DAC_wptr+1) & 0xff;
kimberlylie99 0:b97c07227845 178 while (DAC_wptr==DAC_rptr) {
kimberlylie99 0:b97c07227845 179 }
kimberlylie99 0:b97c07227845 180 }
kimberlylie99 0:b97c07227845 181 DAC_on=0;
kimberlylie99 0:b97c07227845 182 tick.detach();
kimberlylie99 0:b97c07227845 183 free(slice_buf);
kimberlylie99 0:b97c07227845 184 break;
kimberlylie99 0:b97c07227845 185 case 0x5453494c:
kimberlylie99 0:b97c07227845 186 if (verbosity)
kimberlylie99 0:b97c07227845 187 printf("INFO chunk, size %d\n",chunk_size);
kimberlylie99 0:b97c07227845 188 fseek(wavefile,chunk_size,SEEK_CUR);
kimberlylie99 0:b97c07227845 189 break;
kimberlylie99 0:b97c07227845 190 default:
kimberlylie99 0:b97c07227845 191 printf("unknown chunk type 0x%x, size %d\n",chunk_id,chunk_size);
kimberlylie99 0:b97c07227845 192 data=fseek(wavefile,chunk_size,SEEK_CUR);
kimberlylie99 0:b97c07227845 193 break;
kimberlylie99 0:b97c07227845 194 }
kimberlylie99 0:b97c07227845 195 fread(&chunk_id,4,1,wavefile);
kimberlylie99 0:b97c07227845 196 fread(&chunk_size,4,1,wavefile);
kimberlylie99 0:b97c07227845 197 }
kimberlylie99 0:b97c07227845 198 }
kimberlylie99 0:b97c07227845 199
kimberlylie99 0:b97c07227845 200
kimberlylie99 0:b97c07227845 201 void wave_player::dac_out()
kimberlylie99 0:b97c07227845 202 {
kimberlylie99 0:b97c07227845 203 if (DAC_on) {
kimberlylie99 0:b97c07227845 204 #ifdef VERBOSE
kimberlylie99 0:b97c07227845 205 printf("ISR rdptr %d got %u\n",DAC_rptr,DAC_fifo[DAC_rptr]);
kimberlylie99 0:b97c07227845 206 #endif
kimberlylie99 0:b97c07227845 207 wave_DAC->write_u16(DAC_fifo[DAC_rptr]);
kimberlylie99 0:b97c07227845 208 DAC_rptr=(DAC_rptr+1) & 0xff;
kimberlylie99 0:b97c07227845 209 }
kimberlylie99 0:b97c07227845 210 }
kimberlylie99 0:b97c07227845 211