f

Dependencies:   mbed 4DGL-uLCD-SE MMA8452

Committer:
lballard9
Date:
Thu Mar 10 05:18:13 2022 +0000
Revision:
0:8e3b9bb1084a
shell rev b;

Who changed what in which revision?

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