Music Visualizer

Dependencies:   mbed SDFileSystem NeoStrip PinDetect

Committer:
spatel465
Date:
Thu Apr 30 04:00:33 2020 +0000
Revision:
9:0e16d1e33c4b
Parent:
0:adce77867281
cleaned up comments

Who changed what in which revision?

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