ELEC2645 (2018/19) / Mbed 2 deprecated el17st

Dependencies:   mbed FATFileSystem

WDplayer/WDplayer.cpp

Committer:
rottenegg
Date:
2019-05-05
Revision:
11:7f3e9bc7366d
Parent:
3:63cdd5cab431
Child:
12:ff8d26124c38

File content as of revision 11:7f3e9bc7366d:

#include "WDplayer.h"
Ticker ISR;

//Constructor
WDplayer::WDplayer(PinName buzzer) 
    : _dac(new PwmOut(buzzer)) {
    buffer = (unsigned char *)malloc(1);
}

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

void WDplayer::intWD(const char *path, bool allocate) {
    //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;
    lock = false;
    vtck = 8000;
    cache = NULL;    
    _dac->period( _pwmfreq / 4.0f);
    if (allocate) {
        cache = (unsigned char*)std::calloc(8000,sizeof(unsigned char));
    }
};

//Inline Functions (Operates using main MCU)

void WDplayer::WDplay() {
    _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() {
    //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++;
    }
};

//Outline Functions (Uses ISR to operate)

void WDplayer::ISRpreload() {
    //File Reset
    if (_fptr == NULL) {
        _fptr = fopen(_path,"r");
        fseek(_fptr,44,SEEK_SET);
        //Reset State and Locational Variables
        lock = false;
        vtck = 8000;
        _tck = 0;
    }
    //CORE ENGINE WRITING
    if (_tck + 4000 >= _length) {
        _fptr = NULL;
        _tck = 0;
        vtck = 0;
    } else if (vtck > 4001 && !lock) {
        //Block 1 Update
        for (int i = 0; i <= 4000; i++) {
                fread(buffer,1,1,_fptr);
                cache[i] = ((unsigned char)buffer[0]);
        }
        lock = true;
        _tck = _tck + 4000;
    } else if (vtck < 3999 && lock) {
        //Block 2 Update
        for (int i = 4001; i <= 8000; i++) {
            fread(buffer,1,1,_fptr);
            cache[i] = ((int)buffer[0]);
        }
        lock = false;
        _tck = _tck + 4000;
    } 
}

void WDplayer::ISRset() {
    this->ISRpreload();
    ISR.attach(callback(this,&WDplayer::ISRtck),_pwmfreq);
}

void WDplayer::ISRreset() {
    free(cache);
    lock = false;
    vtck = 8000;
    _tck = 0;
    ISR.detach();
}

void WDplayer::ISRtck() {
    _dac->write( cache[vtck] / 255.00 );
    if (vtck == 8000) {
        vtck = -1;
    }
    vtck++;
};


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