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:
- 7:fb47ff047ac7
- Parent:
- 6:fd6ae534d120
- Child:
- 8:40f37287589a
--- a/wolfson_3_wav.cpp Sun Oct 28 16:44:14 2018 +0000
+++ b/wolfson_3_wav.cpp Sun Oct 28 21:31:58 2018 +0000
@@ -41,7 +41,7 @@
short hello;
int i = 0;
int h = 0;
-int bufflen = 1;
+short bufflen = 1;
int buffer[1];
int AudioFormat, NumChannels, SampleRate, BitsPerSample ;
char *slice_buf;
@@ -52,12 +52,17 @@
int newvar;
long slice, num_slices;
int verbosity = 0;
-int interrupt_condition = 0;
+int interrupt_condition = 1;
int sampling_freq = 48000;
-int Buffer1[512];
-int Buffer2[512];
-int place_hold1;
-int place_hold2;
+short Buffer1[8];
+short Buffer2[8];
+short place_hold1 = 0;
+short place_hold2 = 0;
+int flag1 = 1;
+int flag2 = 0;
+int flag3 = 1;
+int flag4 = 0;
+short value[1];
//long long slice_value;
int slice_value[1];
FILE *my_wav;
@@ -73,27 +78,61 @@
void isr()
{
- buffer[0] = (*data_sptr)>>1;
- myled = 1;
- int bufflen = sizeof(buffer);
+ //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);
+ printf("data_sptr data: %d",data_sptr);
+ short value = Buffer1[0];
+ printf("value: %d",value);
}
+ //t.start();
- //buffer[0] = (int*)data_sptr;
- //buffer[0] = newvar;
- //buffer[0] = sine16lookup[i];//>>1;//sine16lookup[i];//scale down volume a bit on amp//how would this scale down the volume a bit??
- //buffer[0] = *fp;
- //printf("value: %d",data_sptr);
- t.start();
- i2s.write(data_sptr, bufflen);//Send next PWM value to amp
- t.stop();
- printf("The time taken was %f seconds\n", t.read());
- t.reset();
- myled = 0;
+ //t.stop();
+ //printf("The time taken was %f seconds\n", t.read());
+ //t.reset();
+ //myled = 0;
+ if(flag1 == 0)
+ {
+ //access double buffer pt1
+ value[0] = Buffer1[place_hold1];
+ i2s.write(value,1);//Send next PWM value to amp
+ place_hold1 = place_hold1 + 1;
+ if(place_hold1 == 8)
+ {
+ place_hold1 = 0;
+ place_hold2 = 0;
+ flag1 = 1;
+ flag2 = 0;
+ flag3 = 1;
+ }
+ if(verbosity == 1)
+ {
+ printf("value Buffer1: %d\n\r",value[0]);
+ }
+ }
+ else if(flag2 == 0)
+ {
+ //access double buffer pt2
+ value[0] = Buffer1[place_hold2];
+ i2s.write(value,1);//Send next PWM value to amp
+ place_hold2 = place_hold2 + 1;
+ if(place_hold2 == 8)
+ {
+ place_hold1 = 0;
+ place_hold2 = 0;
+ flag1 = 0;
+ flag2 = 1;
+ flag3 = 1;
+ }
+ if(verbosity == 1)
+ {
+ printf("value Buffer2: %d\n\r",value[0]);
+ }
}
-
+
+}
@@ -272,9 +311,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/gp40_8K.wav","rb");
+ //my_wav = fopen("/sd/mydir/gp40_8K_2018.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
@@ -346,27 +386,16 @@
printf("do we egt to this point:\n\r");
if (interrupt_condition == 1)
{
- sampletick.attach(&isr,10.0);
- }
-
- //sampletick.attach(&isr,1000000/48000);
- //while(!feof(my_wav)){
- //while(1){
- //if (wav_data.subchunk2_size > sizeof(wav_format)){
- //fseek(my_wav,wav_data.subchunk2_size-sizeof(wav_format),SEEK_CUR);
- /*if(wav_data.subchunk2_size > (num_slices*wav_format.block_align)){
- fseek(my_wav,wav_data.subchunk2_size - (num_slices*wav_format.block_align),SEEK_CUR);
- printf("chunk_size - sizeof(wav_format) %d\n\r",wav_data.subchunk2_size-sizeof(wav_format));
- printf("sizeof(wav_format): %d\n\r",sizeof(wav_format));
- }
- else {
-
- break;
- }*/
-
+ sampletick.attach(&isr,1.0/16000); //1/16000
+ }
slice_buf=(char *)malloc(wav_format.block_align);
- for (slice=0;slice<num_slices;slice+=1) {
- t2.start();
+ while(1){
+ slice = 0;
+ while(flag3 == 1)
+ {
+ //for (slice=0;slice<num_slices;slice+=1)
+ while(slice<num_slices){
+ //t2.start();
fread(slice_buf,wav_format.block_align,1,my_wav);//THIS IS WHERE HE READS IN THE DATA, TO SLICE_BUF. BUT USES WAV_FORMAT.BLOCKALIGN AS HIS SIZE GUIDE
//I do not understand why he is not "seeking" through the file first though...
if (feof(my_wav)){
@@ -380,8 +409,31 @@
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;
+ //int mybuffer[1];
+ //mybuffer[0] = data_sptr[channel];// - 32768;
+ if(flag1 == 1)
+ {
+ Buffer1[place_hold1] = data_sptr[channel];
+ place_hold1 = place_hold1 + 1;
+ if(place_hold1 == 8)
+ {
+ flag4 = 1;
+ break;
+ }
+
+ }
+ else if(flag2 == 0)
+ {
+ Buffer2[place_hold2] = data_sptr[channel];
+ place_hold2 = place_hold2 + 1;
+ if(place_hold2 == 8)
+ {
+ flag4 = 1;
+ break;
+ }
+
+ }
+
if(interrupt_condition==1)
{
@@ -435,6 +487,11 @@
break;
}
}
+ if(flag4 == 1)
+ {
+ flag4 = 0;
+ break;
+ }
slice_value[1]/=wav_format.num_channels;
// slice_value is now averaged. Next it needs to be scaled to an unsigned 16 bit value
@@ -453,12 +510,17 @@
printf("data_sptr data: %d \n\r",data_sptr);
printf("slice_value data: %d \n\r",slice_value);
}
- int mynewvar[1];
+ slice = slice + 1;
//i2s.write(data_sptr,2);
- t2.stop();
- printf("The time taken was %f seconds\n", t2.read());
- t2.reset();
+ //t2.stop();
+ //printf("The time taken was %f seconds\n", t2.read());
+ //t2.reset();
}
+ printf("we must have filled the buffer");
+}
+}
+
+
/* dac_data=(short unsigned)slice_value;
if (verbosity)
printf("sample %d wptr %d slice_value %d dac_data %u\n",slice,DAC_wptr,(int)slice_value,dac_data);