Kevin Chen / Mbed 2 deprecated drue

Dependencies:   mbed SDFileSystem

Revision:
7:af45a10fdfb6
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sd_card_player.cpp	Sat Apr 27 21:47:02 2019 +0000
@@ -0,0 +1,141 @@
+#include <mbed.h>
+#include <sd_card_player.h>
+
+// must pass p18 on mbed (only one w/ AnalogOut) 
+sd_card_player::sd_card_player(AnalogOut *_dac) {
+    wave_DAC=_dac;
+    wave_DAC->write_u16(32768);        //DAC is 0-3.3V, so idles at ~1.6V
+}
+
+//-----------------------------------------------------------------------------
+// player function.  Takes a pointer to an opened wave file.  The file needs
+// to be stored in a filesystem with enough bandwidth to feed the wave data.
+// LocalFileSystem isn't, but the SDcard is, at least for 22kHz files.  The
+// SDcard filesystem can be hotrodded by increasing the SPI frequency it uses
+// internally.
+//-----------------------------------------------------------------------------
+void sd_card_player::play(FILE *wavefile)
+{
+        unsigned chunk_id,chunk_size,channel;
+        unsigned data,samp_int,i;
+        short unsigned dac_data;
+        long long slice_value;
+        char *slice_buf;
+        short *data_sptr;
+        unsigned char *data_bptr;
+        int *data_wptr;
+        FMT_STRUCT wav_format;
+        long slice,num_slices;
+  DAC_wptr=0;
+  DAC_rptr=0;
+  for (i=0;i<256;i+=2) {
+    DAC_fifo[i]=0;
+    DAC_fifo[i+1]=3000;
+  }
+  DAC_wptr=4;
+  DAC_on=0;
+
+  fread(&chunk_id,4,1,wavefile);
+  fread(&chunk_size,4,1,wavefile);
+  while (!feof(wavefile)) {
+    switch (chunk_id) {
+      case 0x46464952:
+        fread(&data,4,1,wavefile);
+        break;
+      case 0x20746d66:
+        fread(&wav_format,sizeof(wav_format),1,wavefile);
+        if (chunk_size > sizeof(wav_format))
+          fseek(wavefile,chunk_size-sizeof(wav_format),SEEK_CUR);
+        break;
+      case 0x61746164:
+// allocate a buffer big enough to hold a slice
+        slice_buf=(char *)malloc(wav_format.block_align);
+        if (!slice_buf) {
+          printf("Unable to malloc slice buffer");
+          exit(1);
+        }
+        num_slices=chunk_size/wav_format.block_align;
+        samp_int=1000000/(wav_format.sample_rate);
+
+// starting up ticker to write samples out -- no printfs until tick.detach is called
+        tick.attach_us(this,&sd_card_player::dac_out, samp_int); 
+        DAC_on=1; 
+
+// start reading slices, which contain one sample each for however many channels
+// are in the wave file.  one channel=mono, two channels=stereo, etc.  Since
+// mbed only has a single AnalogOut, all of the channels present are averaged
+// to produce a single sample value.  This summing and averaging happens in
+// a variable of type signed long long, to make sure that the data doesn't
+// overflow regardless of sample size (8 bits, 16 bits, 32 bits).
+//
+// note that from what I can find that 8 bit wave files use unsigned data,
+// while 16 and 32 bit wave files use signed data
+//
+        for (slice=0;slice<num_slices;slice+=1) {
+          fread(slice_buf,wav_format.block_align,1,wavefile);
+          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:
+                slice_value+=data_sptr[channel];
+                break;
+              case 32:
+                slice_value+=data_wptr[channel];
+                break;
+              case 8:
+                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;
+          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:
+        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);
+  }
+}
+
+
+void sd_card_player::dac_out()
+{
+  if (DAC_on) {
+    wave_DAC->write_u16(DAC_fifo[DAC_rptr]);
+    DAC_rptr=(DAC_rptr+1) & 0xff;
+  }
+}
+