microSDカードからWaveファイルを再生するサンプルです。
Dependencies: mbed FATFileSystem
Fork of JBB_WavePlayer_test by
wave_player.cpp
00001 //----------------------------------------------------------------------------- 00002 // a sample mbed library to play back wave files. 00003 // 00004 // explanation of wave file format. 00005 // https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ 00006 00007 // if VERBOSE is uncommented then the wave player will enter a verbose 00008 // mode that displays all data values as it reads them from the file 00009 // and writes them to the DAC. Very slow and unusable output on the DAC, 00010 // but useful for debugging wave files that don't work. 00011 //#define VERBOSE 00012 00013 00014 #include <mbed.h> 00015 #include <stdio.h> 00016 #include <wave_player.h> 00017 00018 extern void send_led2(); 00019 00020 00021 00022 //----------------------------------------------------------------------------- 00023 // constructor -- accepts an mbed pin to use for AnalogOut. Only p18 will work 00024 wave_player::wave_player(AnalogOut *_dac, DigitalOut *_amp_enable) 00025 { 00026 wave_DAC=_dac; 00027 wave_DAC->write_u16(32768); //DAC is 0-3.3V, so idles at ~1.6V 00028 verbosity=0; 00029 sound_stop = 0; 00030 Amp_Enable= _amp_enable; 00031 *Amp_Enable = 1; 00032 } 00033 00034 //----------------------------------------------------------------------------- 00035 // if verbosity is set then wave player enters a mode where the wave file 00036 // is decoded and displayed to the screen, including sample values put into 00037 // the DAC FIFO, and values read out of the DAC FIFO by the ISR. The DAC output 00038 // itself is so slow as to be unusable, but this might be handy for debugging 00039 // wave files that don't play 00040 //----------------------------------------------------------------------------- 00041 void wave_player::set_verbosity(int v) 00042 { 00043 verbosity=v; 00044 } 00045 00046 //----------------------------------------------------------------------------- 00047 // player function. Takes a pointer to an opened wave file. The file needs 00048 // to be stored in a filesystem with enough bandwidth to feed the wave data. 00049 // LocalFileSystem isn't, but the SDcard is, at least for 22kHz files. The 00050 // SDcard filesystem can be hotrodded by increasing the SPI frequency it uses 00051 // internally. 00052 //----------------------------------------------------------------------------- 00053 void wave_player::play(FILE *wavefile) 00054 { 00055 unsigned chunk_id,chunk_size,channel; 00056 unsigned data,samp_int,i; 00057 short unsigned dac_data; 00058 long long slice_value; 00059 char *slice_buf; 00060 short *data_sptr; 00061 unsigned char *data_bptr; 00062 int *data_wptr; 00063 FMT_STRUCT wav_format; 00064 long slice,num_slices; 00065 DAC_wptr=0; 00066 DAC_rptr=0; 00067 for (i=0;i<256;i+=2) { 00068 DAC_fifo[i]=0; 00069 DAC_fifo[i+1]=3000; 00070 } 00071 DAC_wptr=4; 00072 DAC_on=0; 00073 play_count = 0; 00074 *Amp_Enable = 1; 00075 00076 fread(&chunk_id,4,1,wavefile); 00077 fread(&chunk_size,4,1,wavefile); 00078 while ((!feof(wavefile))&&(sound_stop==0)) { 00079 if (verbosity) 00080 printf("Read chunk ID 0x%x, size 0x%x\n",chunk_id,chunk_size); 00081 switch (chunk_id) { 00082 case 0x46464952: 00083 fread(&data,4,1,wavefile); 00084 if (verbosity) { 00085 printf("RIFF chunk\n"); 00086 printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size); 00087 printf(" RIFF type 0x%x\n",data); 00088 } 00089 break; 00090 case 0x20746d66: 00091 fread(&wav_format,sizeof(wav_format),1,wavefile); 00092 if (verbosity) { 00093 printf("FORMAT chunk\n"); 00094 printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size); 00095 printf(" compression code %d\n",wav_format.comp_code); 00096 printf(" %d channels\n",wav_format.num_channels); 00097 printf(" %d samples/sec\n",wav_format.sample_rate); 00098 printf(" %d bytes/sec\n",wav_format.avg_Bps); 00099 printf(" block align %d\n",wav_format.block_align); 00100 printf(" %d bits per sample\n",wav_format.sig_bps); 00101 } 00102 if (chunk_size > sizeof(wav_format)) 00103 fseek(wavefile,chunk_size-sizeof(wav_format),SEEK_CUR); 00104 break; 00105 case 0x61746164: 00106 // allocate a buffer big enough to hold a slice 00107 slice_buf=(char *)malloc(wav_format.block_align); 00108 if (!slice_buf) { 00109 printf("Unable to malloc slice buffer"); 00110 exit(1); 00111 } 00112 num_slices=chunk_size/wav_format.block_align; 00113 samp_int=1000000/(wav_format.sample_rate); 00114 if (verbosity) { 00115 printf("DATA chunk\n"); 00116 printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size); 00117 printf(" %d slices\n",num_slices); 00118 printf(" Ideal sample interval=%d\n",(unsigned)(1000000.0/wav_format.sample_rate)); 00119 printf(" programmed interrupt tick interval=%d\n",samp_int); 00120 } 00121 00122 // starting up ticker to write samples out -- no printfs until tick.detach is called 00123 if (verbosity) 00124 tick.attach_us(this,&wave_player::dac_out, 500000); 00125 else 00126 tick.attach_us(this,&wave_player::dac_out, samp_int); 00127 DAC_on=1; 00128 00129 // start reading slices, which contain one sample each for however many channels 00130 // are in the wave file. one channel=mono, two channels=stereo, etc. Since 00131 // mbed only has a single AnalogOut, all of the channels present are averaged 00132 // to produce a single sample value. This summing and averaging happens in 00133 // a variable of type signed long long, to make sure that the data doesn't 00134 // overflow regardless of sample size (8 bits, 16 bits, 32 bits). 00135 // 00136 // note that from what I can find that 8 bit wave files use unsigned data, 00137 // while 16 and 32 bit wave files use signed data 00138 // 00139 for (slice=0;slice<num_slices;slice+=1) { 00140 fread(slice_buf,wav_format.block_align,1,wavefile); 00141 if (feof(wavefile)) { 00142 printf("Oops -- not enough slices in the wave file\n"); 00143 exit(1); 00144 } 00145 play_count++; 00146 if( play_count > 100 ) 00147 { 00148 *Amp_Enable = 0; 00149 } 00150 if( sound_stop == 1 ) 00151 { 00152 tick.detach(); 00153 free(slice_buf); 00154 sound_stop = 0; 00155 return; 00156 } 00157 00158 data_sptr=(short *)slice_buf; // 16 bit samples 00159 data_bptr=(unsigned char *)slice_buf; // 8 bit samples 00160 data_wptr=(int *)slice_buf; // 32 bit samples 00161 slice_value=0; 00162 for (channel=0;channel<wav_format.num_channels;channel++) { 00163 00164 if( sound_stop == 1 ) 00165 { 00166 tick.detach(); 00167 free(slice_buf); 00168 sound_stop = 0; 00169 return; 00170 } 00171 switch (wav_format.sig_bps) { 00172 case 16: 00173 if (verbosity) 00174 printf("16 bit channel %d data=%d ",channel,data_sptr[channel]); 00175 slice_value+=data_sptr[channel]; 00176 break; 00177 case 32: 00178 if (verbosity) 00179 printf("32 bit channel %d data=%d ",channel,data_wptr[channel]); 00180 slice_value+=data_wptr[channel]; 00181 break; 00182 case 8: 00183 if (verbosity) 00184 printf("8 bit channel %d data=%d ",channel,(int)data_bptr[channel]); 00185 slice_value+=data_bptr[channel]; 00186 break; 00187 } 00188 } 00189 slice_value/=wav_format.num_channels; 00190 00191 // slice_value is now averaged. Next it needs to be scaled to an unsigned 16 bit value 00192 // with DC offset so it can be written to the DAC. 00193 switch (wav_format.sig_bps) { 00194 case 8: slice_value<<=8; 00195 break; 00196 case 16: slice_value+=32768; 00197 break; 00198 case 32: slice_value>>=16; 00199 slice_value+=32768; 00200 break; 00201 } 00202 dac_data=(short unsigned)slice_value; 00203 if (verbosity) 00204 printf("sample %d wptr %d slice_value %d dac_data %u\n",slice,DAC_wptr,(int)slice_value,dac_data); 00205 DAC_fifo[DAC_wptr]=dac_data; 00206 DAC_wptr=(DAC_wptr+1) & 0xff; 00207 while (DAC_wptr==DAC_rptr) { 00208 } 00209 } 00210 DAC_on=0; 00211 tick.detach(); 00212 free(slice_buf); 00213 break; 00214 case 0x5453494c: 00215 if (verbosity) 00216 printf("INFO chunk, size %d\n",chunk_size); 00217 fseek(wavefile,chunk_size,SEEK_CUR); 00218 break; 00219 default: 00220 printf("unknown chunk type 0x%x, size %d\n",chunk_id,chunk_size); 00221 data=fseek(wavefile,chunk_size,SEEK_CUR); 00222 break; 00223 } 00224 fread(&chunk_id,4,1,wavefile); 00225 fread(&chunk_size,4,1,wavefile); 00226 } 00227 sound_stop = 0; 00228 } 00229 00230 00231 void wave_player::dac_out() 00232 { 00233 if (DAC_on) { 00234 #ifdef VERBOSE 00235 printf("ISR rdptr %d got %u\n",DAC_rptr,DAC_fifo[DAC_rptr]); 00236 #endif 00237 wave_DAC->write_u16(DAC_fifo[DAC_rptr]); 00238 DAC_rptr=(DAC_rptr+1) & 0xff; 00239 } 00240 } 00241 00242 void wave_player::set_s_stop(void) 00243 { 00244 sound_stop = 1; 00245 } 00246 00247 00248
Generated on Wed Jul 13 2022 03:29:22 by 1.7.2