Plays 8-bit Mono Wav Files for PWM

Revision:
0:c2f3d752d98f
Child:
3:574fdfeb949e
diff -r 000000000000 -r c2f3d752d98f WDplayer.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WDplayer.cpp	Mon Mar 25 10:27:56 2019 +0000
@@ -0,0 +1,58 @@
+#include "WDplayer.h"
+
+PwmOut dac(PTC10);
+unsigned char *buffer = (unsigned char *)malloc(1);
+
+void WDplayer::intWD(const char *path) {
+    //open File
+    _fptr = fopen(path,"r");
+    if (_fptr == NULL) {
+    std::cerr << "Error File not Found" << std::endl;
+    }
+    //get info
+    unsigned int samplerate;
+    fseek(_fptr,24,SEEK_SET);
+    fread(&samplerate,4,1,_fptr);
+    fseek(_fptr,0,SEEK_END);
+    unsigned int len = ftell(_fptr);
+    _pwmfreq = (1.0f / samplerate);
+    dac.period(_pwmfreq / (8.0f * 1.75f));//wave control and playback factor control
+    _path = path;
+    _length = len - 44;
+    _fptr = NULL;
+    _tck = 0;
+};
+
+void WDplayer::WDplay() {
+    _fptr = fopen(_path,"r");
+    fseek(_fptr,44,SEEK_SET);
+    //sampling loop _ source of main performance issues
+    for (_tck = _length; _tck > 1; _tck--) {
+        fread(buffer,1,1,_fptr);
+        dac.write((int)buffer[0] / 255.00);
+        wait(_pwmfreq / 1.75f); //I dont like this & Playback present here        
+    }
+    _fptr = NULL;
+    _tck = 0;
+};
+
+void WDplayer::WDtck() {
+    if (_fptr == NULL) {
+    _fptr = fopen(_path,"r");
+    fseek(_fptr,44,SEEK_SET);
+    }
+    //sampling loop
+    if (_tck == _length) {
+        _fptr = NULL;
+        _tck = 0;
+    } else {
+        fread(buffer,1,1,_fptr);
+        dac.write( (int)buffer[0] / 255.00 );
+        wait(_pwmfreq / 1.75f); //I dont like this remove to tick mode
+        
+    }
+};
+
+unsigned long WDplayer::get_pwmfreq() {
+    return _pwmfreq;
+};
\ No newline at end of file