f303k8 wav player

Dependencies:   SDFileSystem mbed

Revision:
0:1561c4efda0e
Child:
1:7a3f34b2d18b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wavPlayer.cpp	Sun Jul 31 12:13:36 2016 +0000
@@ -0,0 +1,95 @@
+#include "mbed.h"
+#include "wavChunks.h"
+#include "wavPlayer.h"
+#include <stdio.h>
+#include "SDFileSystem.h"
+
+
+wavPlayerOnDAC::wavPlayerOnDAC(PinName pinDAC) : 
+    DACout(pinDAC) 
+{
+}
+
+void wavPlayerOnDAC::setFile(FIL* tgtFile)
+{
+    uint16_t i;
+    wavfil = *tgtFile;
+    f_read(&wavfil,&wavReadData,sizeof(wavReadData),&wavReadByte);
+    for(i=0;i<WAV_READ_SIZE;i++)
+    {
+        DACData[0][i] = wavReadData[i]+32768;
+    }
+    dac_flag[0] = 1;
+    
+    f_read(&wavfil,&wavReadData,sizeof(wavReadData),&wavReadByte);
+    for(i=0;i<WAV_READ_SIZE;i++)
+    {
+        DACData[1][i] = wavReadData[i]+32768;
+    }
+    dac_flag[1] = 1;
+    fWavPlaying = 1;
+    dac_c = 0;
+}
+
+//return 0:playing 1:stop
+uint8_t wavPlayerOnDAC::readProc(void)
+{
+    uint16_t i,j;
+
+    if(f_eof(&wavfil) != 0)
+    {
+        fWavPlaying = 0;
+    }
+
+    if(fWavPlaying == 0)
+    {
+        dac_on = 0;
+        return 1;
+    }
+    dac_on = 1;
+    for(j=0;j<2;j++)
+    {
+        if(dac_flag[j] == 0)
+        {
+            
+            f_read(&wavfil,&wavReadData,sizeof(wavReadData),&wavReadByte);
+            for(i=0;i<WAV_READ_SIZE;i++)
+            {
+                DACData[j][i] = wavReadData[i]+32768;
+            }
+            dac_flag[j] = 1;
+        }
+    }
+    return 0;
+}
+
+void wavPlayerOnDAC::rewind(void)
+{
+    f_lseek(&wavfil,0);
+    fWavPlaying = 1;
+}
+void wavPlayerOnDAC::stop(void)
+{
+    fWavPlaying = 0;
+}
+
+void wavPlayerOnDAC::DACOutProc(void)
+{
+    if(dac_on == 1) {
+        if(dac_flag[dac_c] == 1)
+        {
+            DACout.write_u16(DACData[dac_c][rp]);
+            if( rp == WAV_READ_SIZE-1)
+            {
+                dac_flag[dac_c] = 0;
+                rp = 0;
+                dac_c = (dac_c+1)%2;
+            }
+            else
+            {
+                rp++;
+            }
+        }
+    }
+}
+