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

Revision:
2:05b426733282
Parent:
1:ec4e2020522c
Child:
3:6fba0a044d85
--- a/wolfson_3_wav.cpp	Mon Oct 01 19:04:12 2018 +0000
+++ b/wolfson_3_wav.cpp	Mon Oct 01 20:33:33 2018 +0000
@@ -305,7 +305,7 @@
     fread(&wav_format,sizeof(wav_format),1,my_wav);
     fseek(my_wav,36,SEEK_SET);
     fread(&wav_data,sizeof(wav_data),1,my_wav);
-    num_slices = wav_data.subchunk2_size/wav_format.block_align;
+    int num_slices = wav_data.subchunk2_size/wav_format.block_align;
     printf("wav_data.subchunk2_size: %d\n\r",wav_data.subchunk2_size);
     printf("wav_format.block_align: %d\n\r",wav_format.block_align);
     printf("num_slices: %d\n\r",num_slices);
@@ -328,46 +328,78 @@
             break;
             }*/
             
-    slice_buf=(char *)malloc(wav_format.block_align);
-    for(int slice = 0; slice<num_slices; slice+=1)
-    {       
-      fread(&slice_buf,wav_format.block_align,1,my_wav);
-      data_sptr=(int *)slice_buf;  
-      //printf("data_sptr:%d\n\r",data_sptr);
-      //printf("wav_format.num_channels: %d\n\r",wav_format.num_channels);
-      //}
-      slice_value = 0;
+//    slice_buf=(char *)malloc(wav_format.block_align);
 
-                //for (int channel=0;channel<wav_format.num_channels;channel++) 
-               // {
-                    int channel = 0;
-                    //printf("channel: %d\n\r",channel);
-                    //printf("wav_format.num_channels: %d\n\r",wav_format.num_channels);
-                    int var = 1;//data_sptr[channel];
-                    printf("var: %d\n\r", var);
-                    //slice_value+=data_sptr[channel];
-                    //printf("slice_value %d\n\r",slice_value);
-                    //printf("data_sptr[channel] %d\n\r",data_sptr[channel] );
-                    //slice_value = 21;//
-                    //malloc(sizeof(data_sptr[channel]));
-                    //printf("SIZEOF DATASPTR %d:\n\r",sizeof(data_sptr[channel]));
-                    //newvar = data_sptr[channel];
-                    //printf("newvar %d\n\r",newvar);
-                    channel = channel + 1;
-                    newvar = data_sptr[channel];
-                    //printf("newvar %d\n\r",newvar);
-                    //printf("channel: %d\n\r",channel);
-                    //slice_value+=data_sptr[channel];
-                  
-                    //printf("did it get to this point\n\r");
-                
-                
-                //printf("channel: %d  data %d\n\r",channel,data_sptr[channel]);
-                //printf("slice_value: %d\n\r",slice_value);
-                //}
-      
-     }
-      
+              for (slice=0;slice<num_slices;slice+=1) {
+          fread(slice_buf,wav_format.block_align,1,wavefile);//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(wavefile)) {
+            printf("Oops -- not enough slices in the wave file\n");
+            exit(1);
+          }
+          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;
+          for (channel=0;channel<wav_format.num_channels;channel++) {
+            switch (wav_format.sig_bps) {
+              case 16:
+                if (verbosity)
+                  printf("16 bit channel %d data=%d ",channel,data_sptr[channel]);
+                slice_value+=data_sptr[channel];
+                break;
+              case 32:
+                if (verbosity)
+                  printf("32 bit channel %d data=%d ",channel,data_wptr[channel]);
+                slice_value+=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];
+                break;
+            }
+          }
+          slice_value/=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;
+                        break;
+            case 16:    slice_value+=32768;
+                        break;
+            case 32:    slice_value>>=16;
+                        slice_value+=32768;
+                        break;
+          }
+          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);
+          DAC_fifo[DAC_wptr]=dac_data;
+          DAC_wptr=(DAC_wptr+1) & 0xff;
+          while (DAC_wptr==DAC_rptr) {
+          }
+        }
+        DAC_on=0;
+        tick.detach();
+        free(slice_buf);
+        break;
+      case 0x5453494c:
+        if (verbosity)
+          printf("INFO chunk, size %d\n",chunk_size);
+        fseek(wavefile,chunk_size,SEEK_CUR);
+        break;
+      default:
+        printf("unknown chunk type 0x%x, size %d\n",chunk_id,chunk_size);
+        data=fseek(wavefile,chunk_size,SEEK_CUR);
+        break;
+    }
+    fread(&chunk_id,4,1,wavefile);
+    fread(&chunk_size,4,1,wavefile);
+  }
+}
+
     
         sampletick.detach();
     i2s.stop();