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