does not work yet. Sound starts but then stops after a few seconds, whole thing hangs. Published so as I can import through mbed CLI.
Dependencies: mbed sinelookup SDFileSystem_Copy_of_mbed_version I2S
Diff: wolfson_3_wav.cpp
- Revision:
- 5:5b120a2d9249
- Parent:
- 4:1a9482a07ee2
- Child:
- 6:fd6ae534d120
--- a/wolfson_3_wav.cpp Sat Oct 06 11:51:23 2018 +0000 +++ b/wolfson_3_wav.cpp Tue Oct 23 19:02:45 2018 +0000 @@ -44,7 +44,7 @@ int buffer[1]; int AudioFormat, NumChannels, SampleRate, BitsPerSample ; char *slice_buf; -int *data_sptr; +short *data_sptr; unsigned char *data_bptr; int *data_wptr; unsigned channel; @@ -52,7 +52,7 @@ long slice, num_slices; int verbosity = 0; //long long slice_value; -int slice_value; +int slice_value[1]; FILE *my_wav; FMT_STRUCT wav_format; DATA_STRUCT wav_data; @@ -69,8 +69,10 @@ buffer[0] = (*data_sptr)>>1; myled = 1; int bufflen = sizeof(buffer); + if(verbosity){ printf("buffer data: %d \n\r",buffer); printf("data_sptr data: %d \n\r",data_sptr); + } //t.start(); //buffer[0] = (int*)data_sptr; //buffer[0] = newvar; @@ -261,7 +263,10 @@ data[1] = 0x00; FILE *my_wav; //my_wav = fopen("/sd/mydir/gp40.wav", "rb");//i think that it is "w" for write, "r" for read. - my_wav = fopen("/sd/mydir/gp40_48K.wav", "rb");//i think that it is "w" for write, "r" for read. + //my_wav = fopen("/sd/mydir/gp40_48K.wav", "rb");//i think that it is "w" for write, "r" for read. + //my_wav = fopen("/sd/mydir/gp40_48K_quieter.wav", "rb");//i think that it is "w" for write, "r" for read. + my_wav = fopen("/sd/mydir/gp40_8K.wav","rb"); + //my_wav = fopen("/sd/mydir/emd_645.wav", "rb"); fseek(my_wav, 20, SEEK_SET); // set pointer to byte 20 fread(&AudioFormat, 2, 1, my_wav); // check file is PCM if (AudioFormat==0x01) { @@ -331,6 +336,7 @@ printf("sizeof(wav_format): %d\n\r",sizeof(wav_format)); printf("do we egt to this point:\n\r"); //sampletick.attach(&isr,1/96000); + //sampletick.attach(&isr,1000000/48000); //while(!feof(my_wav)){ //while(1){ //if (wav_data.subchunk2_size > sizeof(wav_format)){ @@ -354,43 +360,53 @@ printf("Oops -- not enough slices in the wave file\n"); exit(1); } - data_sptr=(int *)slice_buf; // 16 bit samples + data_sptr=(short *)slice_buf; // 16 bit samples data_bptr=(unsigned char *)slice_buf; // 8 bit samples data_wptr=(int *)slice_buf; // 32 bit samples - slice_value=0; + slice_value[1]=0; for (channel=0;channel<wav_format.num_channels;channel++) { switch (wav_format.sig_bps) { case 16: + int mybuffer[1]; + mybuffer[0] = data_sptr[channel];// - 32768; + i2s.write(data_sptr,2); + wait_us(0.1); if (verbosity) - printf("16 bit channel %d data=%d ",channel,data_sptr[channel]); - slice_value+=data_sptr[channel]; + printf("16 bit channel %d data=%d\n\r ",channel,data_sptr[channel]); + slice_value[1]+=data_sptr[channel]; break; case 32: if (verbosity) printf("32 bit channel %d data=%d ",channel,data_wptr[channel]); - slice_value+=data_wptr[channel]; + slice_value[1]+=data_wptr[channel]; break; case 8: if (verbosity) printf("8 bit channel %d data=%d ",channel,(int)data_bptr[channel]); - slice_value+=data_bptr[channel]; + slice_value[1]+=data_bptr[channel]; break; } } - slice_value/=wav_format.num_channels; + slice_value[1]/=wav_format.num_channels; // slice_value is now averaged. Next it needs to be scaled to an unsigned 16 bit value // with DC offset so it can be written to the DAC. switch (wav_format.sig_bps) { - case 8: slice_value<<=8; + case 8: slice_value[1]<<=8; break; - case 16: slice_value+=32768; + case 16: slice_value[1]+=32768; break; - case 32: slice_value>>=16; - slice_value+=32768; + case 32: slice_value[1]>>=16; + slice_value[1]+=32768; break; } - i2s.write(data_sptr,2); + if (verbosity) + { + printf("data_sptr data: %d \n\r",data_sptr); + printf("slice_value data: %d \n\r",slice_value); + } + int mynewvar[1]; + //i2s.write(data_sptr,2); //t.stop(); //printf("The time taken was %f seconds\n", t.read()); } @@ -405,7 +421,7 @@ - sampletick.detach(); + //sampletick.detach(); i2s.stop(); fclose(my_wav);