ELEC2645 (2018/19) / Mbed 2 deprecated el17st

Dependencies:   mbed FATFileSystem

Committer:
rottenegg
Date:
Thu Apr 18 21:21:58 2019 +0000
Revision:
3:63cdd5cab431
Parent:
2:964e19f2f3f1
Child:
11:7f3e9bc7366d
Updated WDplayer Documentation and Removed all global Definitions and removed the Hardware Definitions. Added SDFileSystem library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rottenegg 2:964e19f2f3f1 1 #include "WDplayer.h"
rottenegg 2:964e19f2f3f1 2
rottenegg 3:63cdd5cab431 3 //Constructor
rottenegg 3:63cdd5cab431 4 WDplayer::WDplayer() {
rottenegg 3:63cdd5cab431 5 buffer = (unsigned char *)malloc(1);
rottenegg 3:63cdd5cab431 6 }
rottenegg 2:964e19f2f3f1 7
rottenegg 3:63cdd5cab431 8 //Deconstructor
rottenegg 3:63cdd5cab431 9 WDplayer::~WDplayer() {
rottenegg 3:63cdd5cab431 10 free(buffer);
rottenegg 3:63cdd5cab431 11 }
rottenegg 3:63cdd5cab431 12
rottenegg 3:63cdd5cab431 13 void WDplayer::intWD(const char *path, PwmOut &dac) {
rottenegg 3:63cdd5cab431 14 //Open File and Check
rottenegg 2:964e19f2f3f1 15 _fptr = fopen(path,"r");
rottenegg 2:964e19f2f3f1 16 if (_fptr == NULL) {
rottenegg 2:964e19f2f3f1 17 std::cerr << "Error File not Found" << std::endl;
rottenegg 2:964e19f2f3f1 18 }
rottenegg 3:63cdd5cab431 19 //Get Riff Information
rottenegg 2:964e19f2f3f1 20 unsigned int samplerate;
rottenegg 2:964e19f2f3f1 21 fseek(_fptr,24,SEEK_SET);
rottenegg 2:964e19f2f3f1 22 fread(&samplerate,4,1,_fptr);
rottenegg 2:964e19f2f3f1 23 fseek(_fptr,0,SEEK_END);
rottenegg 2:964e19f2f3f1 24 unsigned int len = ftell(_fptr);
rottenegg 3:63cdd5cab431 25 //Set Private Variables
rottenegg 2:964e19f2f3f1 26 _pwmfreq = (1.0f / samplerate);
rottenegg 2:964e19f2f3f1 27 _path = path;
rottenegg 2:964e19f2f3f1 28 _length = len - 44;
rottenegg 2:964e19f2f3f1 29 _fptr = NULL;
rottenegg 2:964e19f2f3f1 30 _tck = 0;
rottenegg 3:63cdd5cab431 31 dac.period( _pwmfreq / 4.0f);
rottenegg 2:964e19f2f3f1 32 };
rottenegg 2:964e19f2f3f1 33
rottenegg 2:964e19f2f3f1 34 void WDplayer::WDplay(PwmOut &dac) {
rottenegg 2:964e19f2f3f1 35 _fptr = fopen(_path,"r");
rottenegg 2:964e19f2f3f1 36 fseek(_fptr,44,SEEK_SET);
rottenegg 3:63cdd5cab431 37 //Sampling Loop- Sample Byte by Byte
rottenegg 3:63cdd5cab431 38 //CORE ENGINE
rottenegg 2:964e19f2f3f1 39 for (_tck = _length; _tck > 1; _tck--) {
rottenegg 2:964e19f2f3f1 40 fread(buffer,1,1,_fptr);
rottenegg 2:964e19f2f3f1 41 dac.write((float)buffer[0] / 255.00f);
rottenegg 2:964e19f2f3f1 42 wait(_pwmfreq / 2.0f); //I dont like this & Playback present here
rottenegg 2:964e19f2f3f1 43 }
rottenegg 2:964e19f2f3f1 44 _fptr = NULL;
rottenegg 2:964e19f2f3f1 45 _tck = 0;
rottenegg 2:964e19f2f3f1 46 };
rottenegg 2:964e19f2f3f1 47
rottenegg 2:964e19f2f3f1 48 void WDplayer::WDtck(PwmOut &dac) {
rottenegg 3:63cdd5cab431 49 //To Loop the Music File When it Ends
rottenegg 2:964e19f2f3f1 50 if (_fptr == NULL) {
rottenegg 2:964e19f2f3f1 51 _fptr = fopen(_path,"r");
rottenegg 2:964e19f2f3f1 52 fseek(_fptr,44,SEEK_SET);
rottenegg 2:964e19f2f3f1 53 }
rottenegg 3:63cdd5cab431 54 //Sampling If Statements
rottenegg 2:964e19f2f3f1 55 if (_tck == _length) {
rottenegg 2:964e19f2f3f1 56 _fptr = NULL;
rottenegg 2:964e19f2f3f1 57 _tck = 0;
rottenegg 2:964e19f2f3f1 58 } else {
rottenegg 3:63cdd5cab431 59 //Plays One Sample
rottenegg 2:964e19f2f3f1 60 fread(buffer,1,1,_fptr);
rottenegg 2:964e19f2f3f1 61 dac.write( (int)buffer[0] / 255.00 );
rottenegg 2:964e19f2f3f1 62 _tck++;
rottenegg 2:964e19f2f3f1 63 }
rottenegg 2:964e19f2f3f1 64 };
rottenegg 2:964e19f2f3f1 65
rottenegg 2:964e19f2f3f1 66 float WDplayer::get_pwmfreq() {
rottenegg 2:964e19f2f3f1 67 return _pwmfreq;
rottenegg 2:964e19f2f3f1 68 };