microSDカードからWaveファイルを再生するサンプルです。

Dependencies:   mbed FATFileSystem

Fork of JBB_WavePlayer_test by Jksoft Blue mbed Board Developer

Committer:
jksoft
Date:
Fri May 23 09:41:26 2014 +0000
Revision:
1:9681a1526ecb
Parent:
0:e9f196d85a46
???????????

Who changed what in which revision?

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