this hurts

Dependencies:   FFT

Committer:
shyamgatech
Date:
Thu Dec 03 18:15:35 2020 +0000
Revision:
7:0d62545e6d73
Parent:
0:d6c9b09b4042
addded gui mbed code;

Who changed what in which revision?

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