Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed FATFileSystem
WDplayer/WDplayer.cpp@11:7f3e9bc7366d, 2019-05-05 (annotated)
- Committer:
- rottenegg
- Date:
- Sun May 05 03:45:29 2019 +0000
- Revision:
- 11:7f3e9bc7366d
- Parent:
- 3:63cdd5cab431
- Child:
- 12:ff8d26124c38
WDplayer: Added new ISR Ticker compatible Functions and updated Documents.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rottenegg | 2:964e19f2f3f1 | 1 | #include "WDplayer.h" |
rottenegg | 11:7f3e9bc7366d | 2 | Ticker ISR; |
rottenegg | 2:964e19f2f3f1 | 3 | |
rottenegg | 3:63cdd5cab431 | 4 | //Constructor |
rottenegg | 11:7f3e9bc7366d | 5 | WDplayer::WDplayer(PinName buzzer) |
rottenegg | 11:7f3e9bc7366d | 6 | : _dac(new PwmOut(buzzer)) { |
rottenegg | 3:63cdd5cab431 | 7 | buffer = (unsigned char *)malloc(1); |
rottenegg | 3:63cdd5cab431 | 8 | } |
rottenegg | 2:964e19f2f3f1 | 9 | |
rottenegg | 3:63cdd5cab431 | 10 | //Deconstructor |
rottenegg | 3:63cdd5cab431 | 11 | WDplayer::~WDplayer() { |
rottenegg | 3:63cdd5cab431 | 12 | free(buffer); |
rottenegg | 11:7f3e9bc7366d | 13 | free(cache); |
rottenegg | 11:7f3e9bc7366d | 14 | delete _dac; |
rottenegg | 3:63cdd5cab431 | 15 | } |
rottenegg | 3:63cdd5cab431 | 16 | |
rottenegg | 11:7f3e9bc7366d | 17 | void WDplayer::intWD(const char *path, bool allocate) { |
rottenegg | 3:63cdd5cab431 | 18 | //Open File and Check |
rottenegg | 2:964e19f2f3f1 | 19 | _fptr = fopen(path,"r"); |
rottenegg | 2:964e19f2f3f1 | 20 | if (_fptr == NULL) { |
rottenegg | 2:964e19f2f3f1 | 21 | std::cerr << "Error File not Found" << std::endl; |
rottenegg | 2:964e19f2f3f1 | 22 | } |
rottenegg | 3:63cdd5cab431 | 23 | //Get Riff Information |
rottenegg | 2:964e19f2f3f1 | 24 | unsigned int samplerate; |
rottenegg | 2:964e19f2f3f1 | 25 | fseek(_fptr,24,SEEK_SET); |
rottenegg | 2:964e19f2f3f1 | 26 | fread(&samplerate,4,1,_fptr); |
rottenegg | 2:964e19f2f3f1 | 27 | fseek(_fptr,0,SEEK_END); |
rottenegg | 2:964e19f2f3f1 | 28 | unsigned int len = ftell(_fptr); |
rottenegg | 3:63cdd5cab431 | 29 | //Set Private Variables |
rottenegg | 2:964e19f2f3f1 | 30 | _pwmfreq = (1.0f / samplerate); |
rottenegg | 2:964e19f2f3f1 | 31 | _path = path; |
rottenegg | 2:964e19f2f3f1 | 32 | _length = len - 44; |
rottenegg | 2:964e19f2f3f1 | 33 | _fptr = NULL; |
rottenegg | 2:964e19f2f3f1 | 34 | _tck = 0; |
rottenegg | 11:7f3e9bc7366d | 35 | lock = false; |
rottenegg | 11:7f3e9bc7366d | 36 | vtck = 8000; |
rottenegg | 11:7f3e9bc7366d | 37 | cache = NULL; |
rottenegg | 11:7f3e9bc7366d | 38 | _dac->period( _pwmfreq / 4.0f); |
rottenegg | 11:7f3e9bc7366d | 39 | if (allocate) { |
rottenegg | 11:7f3e9bc7366d | 40 | cache = (unsigned char*)std::calloc(8000,sizeof(unsigned char)); |
rottenegg | 11:7f3e9bc7366d | 41 | } |
rottenegg | 2:964e19f2f3f1 | 42 | }; |
rottenegg | 2:964e19f2f3f1 | 43 | |
rottenegg | 11:7f3e9bc7366d | 44 | //Inline Functions (Operates using main MCU) |
rottenegg | 11:7f3e9bc7366d | 45 | |
rottenegg | 11:7f3e9bc7366d | 46 | void WDplayer::WDplay() { |
rottenegg | 2:964e19f2f3f1 | 47 | _fptr = fopen(_path,"r"); |
rottenegg | 2:964e19f2f3f1 | 48 | fseek(_fptr,44,SEEK_SET); |
rottenegg | 3:63cdd5cab431 | 49 | //Sampling Loop- Sample Byte by Byte |
rottenegg | 3:63cdd5cab431 | 50 | //CORE ENGINE |
rottenegg | 2:964e19f2f3f1 | 51 | for (_tck = _length; _tck > 1; _tck--) { |
rottenegg | 2:964e19f2f3f1 | 52 | fread(buffer,1,1,_fptr); |
rottenegg | 11:7f3e9bc7366d | 53 | _dac->write((float)buffer[0] / 255.00f); |
rottenegg | 2:964e19f2f3f1 | 54 | wait(_pwmfreq / 2.0f); //I dont like this & Playback present here |
rottenegg | 2:964e19f2f3f1 | 55 | } |
rottenegg | 2:964e19f2f3f1 | 56 | _fptr = NULL; |
rottenegg | 2:964e19f2f3f1 | 57 | _tck = 0; |
rottenegg | 2:964e19f2f3f1 | 58 | }; |
rottenegg | 2:964e19f2f3f1 | 59 | |
rottenegg | 11:7f3e9bc7366d | 60 | void WDplayer::WDtck() { |
rottenegg | 3:63cdd5cab431 | 61 | //To Loop the Music File When it Ends |
rottenegg | 2:964e19f2f3f1 | 62 | if (_fptr == NULL) { |
rottenegg | 2:964e19f2f3f1 | 63 | _fptr = fopen(_path,"r"); |
rottenegg | 2:964e19f2f3f1 | 64 | fseek(_fptr,44,SEEK_SET); |
rottenegg | 2:964e19f2f3f1 | 65 | } |
rottenegg | 3:63cdd5cab431 | 66 | //Sampling If Statements |
rottenegg | 2:964e19f2f3f1 | 67 | if (_tck == _length) { |
rottenegg | 2:964e19f2f3f1 | 68 | _fptr = NULL; |
rottenegg | 2:964e19f2f3f1 | 69 | _tck = 0; |
rottenegg | 2:964e19f2f3f1 | 70 | } else { |
rottenegg | 3:63cdd5cab431 | 71 | //Plays One Sample |
rottenegg | 2:964e19f2f3f1 | 72 | fread(buffer,1,1,_fptr); |
rottenegg | 11:7f3e9bc7366d | 73 | _dac->write( (int)buffer[0] / 255.00 ); |
rottenegg | 2:964e19f2f3f1 | 74 | _tck++; |
rottenegg | 2:964e19f2f3f1 | 75 | } |
rottenegg | 2:964e19f2f3f1 | 76 | }; |
rottenegg | 2:964e19f2f3f1 | 77 | |
rottenegg | 11:7f3e9bc7366d | 78 | //Outline Functions (Uses ISR to operate) |
rottenegg | 11:7f3e9bc7366d | 79 | |
rottenegg | 11:7f3e9bc7366d | 80 | void WDplayer::ISRpreload() { |
rottenegg | 11:7f3e9bc7366d | 81 | //File Reset |
rottenegg | 11:7f3e9bc7366d | 82 | if (_fptr == NULL) { |
rottenegg | 11:7f3e9bc7366d | 83 | _fptr = fopen(_path,"r"); |
rottenegg | 11:7f3e9bc7366d | 84 | fseek(_fptr,44,SEEK_SET); |
rottenegg | 11:7f3e9bc7366d | 85 | //Reset State and Locational Variables |
rottenegg | 11:7f3e9bc7366d | 86 | lock = false; |
rottenegg | 11:7f3e9bc7366d | 87 | vtck = 8000; |
rottenegg | 11:7f3e9bc7366d | 88 | _tck = 0; |
rottenegg | 11:7f3e9bc7366d | 89 | } |
rottenegg | 11:7f3e9bc7366d | 90 | //CORE ENGINE WRITING |
rottenegg | 11:7f3e9bc7366d | 91 | if (_tck + 4000 >= _length) { |
rottenegg | 11:7f3e9bc7366d | 92 | _fptr = NULL; |
rottenegg | 11:7f3e9bc7366d | 93 | _tck = 0; |
rottenegg | 11:7f3e9bc7366d | 94 | vtck = 0; |
rottenegg | 11:7f3e9bc7366d | 95 | } else if (vtck > 4001 && !lock) { |
rottenegg | 11:7f3e9bc7366d | 96 | //Block 1 Update |
rottenegg | 11:7f3e9bc7366d | 97 | for (int i = 0; i <= 4000; i++) { |
rottenegg | 11:7f3e9bc7366d | 98 | fread(buffer,1,1,_fptr); |
rottenegg | 11:7f3e9bc7366d | 99 | cache[i] = ((unsigned char)buffer[0]); |
rottenegg | 11:7f3e9bc7366d | 100 | } |
rottenegg | 11:7f3e9bc7366d | 101 | lock = true; |
rottenegg | 11:7f3e9bc7366d | 102 | _tck = _tck + 4000; |
rottenegg | 11:7f3e9bc7366d | 103 | } else if (vtck < 3999 && lock) { |
rottenegg | 11:7f3e9bc7366d | 104 | //Block 2 Update |
rottenegg | 11:7f3e9bc7366d | 105 | for (int i = 4001; i <= 8000; i++) { |
rottenegg | 11:7f3e9bc7366d | 106 | fread(buffer,1,1,_fptr); |
rottenegg | 11:7f3e9bc7366d | 107 | cache[i] = ((int)buffer[0]); |
rottenegg | 11:7f3e9bc7366d | 108 | } |
rottenegg | 11:7f3e9bc7366d | 109 | lock = false; |
rottenegg | 11:7f3e9bc7366d | 110 | _tck = _tck + 4000; |
rottenegg | 11:7f3e9bc7366d | 111 | } |
rottenegg | 11:7f3e9bc7366d | 112 | } |
rottenegg | 11:7f3e9bc7366d | 113 | |
rottenegg | 11:7f3e9bc7366d | 114 | void WDplayer::ISRset() { |
rottenegg | 11:7f3e9bc7366d | 115 | this->ISRpreload(); |
rottenegg | 11:7f3e9bc7366d | 116 | ISR.attach(callback(this,&WDplayer::ISRtck),_pwmfreq); |
rottenegg | 11:7f3e9bc7366d | 117 | } |
rottenegg | 11:7f3e9bc7366d | 118 | |
rottenegg | 11:7f3e9bc7366d | 119 | void WDplayer::ISRreset() { |
rottenegg | 11:7f3e9bc7366d | 120 | free(cache); |
rottenegg | 11:7f3e9bc7366d | 121 | lock = false; |
rottenegg | 11:7f3e9bc7366d | 122 | vtck = 8000; |
rottenegg | 11:7f3e9bc7366d | 123 | _tck = 0; |
rottenegg | 11:7f3e9bc7366d | 124 | ISR.detach(); |
rottenegg | 11:7f3e9bc7366d | 125 | } |
rottenegg | 11:7f3e9bc7366d | 126 | |
rottenegg | 11:7f3e9bc7366d | 127 | void WDplayer::ISRtck() { |
rottenegg | 11:7f3e9bc7366d | 128 | _dac->write( cache[vtck] / 255.00 ); |
rottenegg | 11:7f3e9bc7366d | 129 | if (vtck == 8000) { |
rottenegg | 11:7f3e9bc7366d | 130 | vtck = -1; |
rottenegg | 11:7f3e9bc7366d | 131 | } |
rottenegg | 11:7f3e9bc7366d | 132 | vtck++; |
rottenegg | 11:7f3e9bc7366d | 133 | }; |
rottenegg | 11:7f3e9bc7366d | 134 | |
rottenegg | 11:7f3e9bc7366d | 135 | |
rottenegg | 2:964e19f2f3f1 | 136 | float WDplayer::get_pwmfreq() { |
rottenegg | 2:964e19f2f3f1 | 137 | return _pwmfreq; |
rottenegg | 2:964e19f2f3f1 | 138 | }; |