Junichi Katsu / Mbed 2 deprecated OnlyYesterday

Dependencies:   mbed FATFileSystem

Fork of OnlyYestaerday by Junichi Katsu

Committer:
jksoft
Date:
Wed Apr 02 01:43:55 2014 +0000
Revision:
0:5975af170e43
1st Prototype

Who changed what in which revision?

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