Plays 8-bit Mono Wav Files for PWM

Committer:
rottenegg
Date:
Wed Apr 17 05:07:15 2019 +0000
Revision:
3:574fdfeb949e
Parent:
0:c2f3d752d98f
Changed the PwmOut for internal pin declaration to external + changed the WDtick to manually wait from main

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rottenegg 0:c2f3d752d98f 1 #include "WDplayer.h"
rottenegg 0:c2f3d752d98f 2
rottenegg 0:c2f3d752d98f 3 unsigned char *buffer = (unsigned char *)malloc(1);
rottenegg 0:c2f3d752d98f 4
rottenegg 0:c2f3d752d98f 5 void WDplayer::intWD(const char *path) {
rottenegg 0:c2f3d752d98f 6 //open File
rottenegg 0:c2f3d752d98f 7 _fptr = fopen(path,"r");
rottenegg 0:c2f3d752d98f 8 if (_fptr == NULL) {
rottenegg 0:c2f3d752d98f 9 std::cerr << "Error File not Found" << std::endl;
rottenegg 0:c2f3d752d98f 10 }
rottenegg 0:c2f3d752d98f 11 //get info
rottenegg 0:c2f3d752d98f 12 unsigned int samplerate;
rottenegg 0:c2f3d752d98f 13 fseek(_fptr,24,SEEK_SET);
rottenegg 0:c2f3d752d98f 14 fread(&samplerate,4,1,_fptr);
rottenegg 0:c2f3d752d98f 15 fseek(_fptr,0,SEEK_END);
rottenegg 0:c2f3d752d98f 16 unsigned int len = ftell(_fptr);
rottenegg 0:c2f3d752d98f 17 _pwmfreq = (1.0f / samplerate);
rottenegg 0:c2f3d752d98f 18 _path = path;
rottenegg 0:c2f3d752d98f 19 _length = len - 44;
rottenegg 0:c2f3d752d98f 20 _fptr = NULL;
rottenegg 0:c2f3d752d98f 21 _tck = 0;
rottenegg 0:c2f3d752d98f 22 };
rottenegg 0:c2f3d752d98f 23
rottenegg 3:574fdfeb949e 24 void WDplayer::WDplay(PwmOut &dac) {
rottenegg 0:c2f3d752d98f 25 _fptr = fopen(_path,"r");
rottenegg 0:c2f3d752d98f 26 fseek(_fptr,44,SEEK_SET);
rottenegg 0:c2f3d752d98f 27 //sampling loop _ source of main performance issues
rottenegg 0:c2f3d752d98f 28 for (_tck = _length; _tck > 1; _tck--) {
rottenegg 0:c2f3d752d98f 29 fread(buffer,1,1,_fptr);
rottenegg 3:574fdfeb949e 30 dac.write((float)buffer[0] / 255.00f);
rottenegg 3:574fdfeb949e 31 wait(_pwmfreq / 2.0f); //I dont like this & Playback present here
rottenegg 0:c2f3d752d98f 32 }
rottenegg 0:c2f3d752d98f 33 _fptr = NULL;
rottenegg 0:c2f3d752d98f 34 _tck = 0;
rottenegg 0:c2f3d752d98f 35 };
rottenegg 0:c2f3d752d98f 36
rottenegg 3:574fdfeb949e 37 void WDplayer::WDtck(PwmOut &dac) {
rottenegg 0:c2f3d752d98f 38 if (_fptr == NULL) {
rottenegg 0:c2f3d752d98f 39 _fptr = fopen(_path,"r");
rottenegg 0:c2f3d752d98f 40 fseek(_fptr,44,SEEK_SET);
rottenegg 0:c2f3d752d98f 41 }
rottenegg 0:c2f3d752d98f 42 //sampling loop
rottenegg 0:c2f3d752d98f 43 if (_tck == _length) {
rottenegg 0:c2f3d752d98f 44 _fptr = NULL;
rottenegg 0:c2f3d752d98f 45 _tck = 0;
rottenegg 0:c2f3d752d98f 46 } else {
rottenegg 0:c2f3d752d98f 47 fread(buffer,1,1,_fptr);
rottenegg 0:c2f3d752d98f 48 dac.write( (int)buffer[0] / 255.00 );
rottenegg 3:574fdfeb949e 49 //wait(_pwmfreq / 2.0f); //I dont like this remove to tick mode
rottenegg 3:574fdfeb949e 50 _tck++;
rottenegg 0:c2f3d752d98f 51 }
rottenegg 0:c2f3d752d98f 52 };
rottenegg 0:c2f3d752d98f 53
rottenegg 3:574fdfeb949e 54 float WDplayer::get_pwmfreq() {
rottenegg 0:c2f3d752d98f 55 return _pwmfreq;
rottenegg 0:c2f3d752d98f 56 };