ELEC2645 (2018/19) / Mbed 2 deprecated el17st

Dependencies:   mbed FATFileSystem

WDplayer/WDplayer.cpp

Committer:
rottenegg
Date:
2019-04-18
Revision:
3:63cdd5cab431
Parent:
2:964e19f2f3f1
Child:
11:7f3e9bc7366d

File content as of revision 3:63cdd5cab431:

#include "WDplayer.h"

//Constructor
WDplayer::WDplayer() {
    buffer = (unsigned char *)malloc(1);
}

//Deconstructor
WDplayer::~WDplayer() {
    free(buffer);
}

void WDplayer::intWD(const char *path, PwmOut &dac) {
    //Open File and Check
    _fptr = fopen(path,"r");
    if (_fptr == NULL) {
    std::cerr << "Error File not Found" << std::endl;
    }
    //Get Riff Information
    unsigned int samplerate;
    fseek(_fptr,24,SEEK_SET);
    fread(&samplerate,4,1,_fptr);
    fseek(_fptr,0,SEEK_END);
    unsigned int len = ftell(_fptr);
    //Set Private Variables
    _pwmfreq = (1.0f / samplerate);
    _path = path;
    _length = len - 44;
    _fptr = NULL;
    _tck = 0;
    dac.period( _pwmfreq / 4.0f);
};

void WDplayer::WDplay(PwmOut &dac) {
    _fptr = fopen(_path,"r");
    fseek(_fptr,44,SEEK_SET);
    //Sampling Loop- Sample Byte by Byte
    //CORE ENGINE
    for (_tck = _length; _tck > 1; _tck--) {
        fread(buffer,1,1,_fptr);
        dac.write((float)buffer[0] / 255.00f);
        wait(_pwmfreq / 2.0f); //I dont like this & Playback present here        
    }
    _fptr = NULL;
    _tck = 0;
};

void WDplayer::WDtck(PwmOut &dac) {
    //To Loop the Music File When it Ends
    if (_fptr == NULL) {
    _fptr = fopen(_path,"r");
    fseek(_fptr,44,SEEK_SET);
    }
    //Sampling If Statements
    if (_tck == _length) {
        _fptr = NULL;
        _tck = 0;
    } else {
        //Plays One Sample
        fread(buffer,1,1,_fptr);
        dac.write( (int)buffer[0] / 255.00 );
        _tck++;
    }
};

float WDplayer::get_pwmfreq() {
    return _pwmfreq;
};