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@18:14e5391beccf, 2019-05-08 (annotated)
- Committer:
- rottenegg
- Date:
- Wed May 08 01:49:54 2019 +0000
- Revision:
- 18:14e5391beccf
- Parent:
- 17:7d4d8905b608
- Child:
- 21:f3b0ce18b44f
CaMove: 20 line per Function criteria met
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 | 2:964e19f2f3f1 | 18 | _fptr = fopen(path,"r"); |
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 | 11:7f3e9bc7366d | 31 | lock = false; |
rottenegg | 12:ff8d26124c38 | 32 | vtck = 4000; |
rottenegg | 11:7f3e9bc7366d | 33 | _dac->period( _pwmfreq / 4.0f); |
rottenegg | 11:7f3e9bc7366d | 34 | if (allocate) { |
rottenegg | 12:ff8d26124c38 | 35 | cache = (unsigned char*)std::calloc(4000,sizeof(unsigned char)); |
rottenegg | 11:7f3e9bc7366d | 36 | } |
rottenegg | 2:964e19f2f3f1 | 37 | }; |
rottenegg | 2:964e19f2f3f1 | 38 | |
rottenegg | 11:7f3e9bc7366d | 39 | //Inline Functions (Operates using main MCU) |
rottenegg | 11:7f3e9bc7366d | 40 | |
rottenegg | 11:7f3e9bc7366d | 41 | void WDplayer::WDplay() { |
rottenegg | 2:964e19f2f3f1 | 42 | _fptr = fopen(_path,"r"); |
rottenegg | 2:964e19f2f3f1 | 43 | fseek(_fptr,44,SEEK_SET); |
rottenegg | 3:63cdd5cab431 | 44 | //Sampling Loop- Sample Byte by Byte |
rottenegg | 3:63cdd5cab431 | 45 | //CORE ENGINE |
rottenegg | 2:964e19f2f3f1 | 46 | for (_tck = _length; _tck > 1; _tck--) { |
rottenegg | 2:964e19f2f3f1 | 47 | fread(buffer,1,1,_fptr); |
rottenegg | 11:7f3e9bc7366d | 48 | _dac->write((float)buffer[0] / 255.00f); |
rottenegg | 2:964e19f2f3f1 | 49 | wait(_pwmfreq / 2.0f); //I dont like this & Playback present here |
rottenegg | 2:964e19f2f3f1 | 50 | } |
rottenegg | 2:964e19f2f3f1 | 51 | _fptr = NULL; |
rottenegg | 2:964e19f2f3f1 | 52 | _tck = 0; |
rottenegg | 2:964e19f2f3f1 | 53 | }; |
rottenegg | 2:964e19f2f3f1 | 54 | |
rottenegg | 11:7f3e9bc7366d | 55 | void WDplayer::WDtck() { |
rottenegg | 3:63cdd5cab431 | 56 | //To Loop the Music File When it Ends |
rottenegg | 2:964e19f2f3f1 | 57 | if (_fptr == NULL) { |
rottenegg | 2:964e19f2f3f1 | 58 | _fptr = fopen(_path,"r"); |
rottenegg | 2:964e19f2f3f1 | 59 | fseek(_fptr,44,SEEK_SET); |
rottenegg | 2:964e19f2f3f1 | 60 | } |
rottenegg | 3:63cdd5cab431 | 61 | //Sampling If Statements |
rottenegg | 2:964e19f2f3f1 | 62 | if (_tck == _length) { |
rottenegg | 2:964e19f2f3f1 | 63 | _fptr = NULL; |
rottenegg | 2:964e19f2f3f1 | 64 | _tck = 0; |
rottenegg | 2:964e19f2f3f1 | 65 | } else { |
rottenegg | 3:63cdd5cab431 | 66 | //Plays One Sample |
rottenegg | 2:964e19f2f3f1 | 67 | fread(buffer,1,1,_fptr); |
rottenegg | 11:7f3e9bc7366d | 68 | _dac->write( (int)buffer[0] / 255.00 ); |
rottenegg | 2:964e19f2f3f1 | 69 | _tck++; |
rottenegg | 2:964e19f2f3f1 | 70 | } |
rottenegg | 2:964e19f2f3f1 | 71 | }; |
rottenegg | 2:964e19f2f3f1 | 72 | |
rottenegg | 11:7f3e9bc7366d | 73 | //Outline Functions (Uses ISR to operate) |
rottenegg | 11:7f3e9bc7366d | 74 | |
rottenegg | 11:7f3e9bc7366d | 75 | void WDplayer::ISRpreload() { |
rottenegg | 11:7f3e9bc7366d | 76 | //File Reset |
rottenegg | 11:7f3e9bc7366d | 77 | if (_fptr == NULL) { |
rottenegg | 11:7f3e9bc7366d | 78 | _fptr = fopen(_path,"r"); |
rottenegg | 11:7f3e9bc7366d | 79 | fseek(_fptr,44,SEEK_SET); |
rottenegg | 11:7f3e9bc7366d | 80 | //Reset State and Locational Variables |
rottenegg | 11:7f3e9bc7366d | 81 | lock = false; |
rottenegg | 12:ff8d26124c38 | 82 | vtck = 4000; |
rottenegg | 11:7f3e9bc7366d | 83 | _tck = 0; |
rottenegg | 11:7f3e9bc7366d | 84 | } |
rottenegg | 11:7f3e9bc7366d | 85 | //CORE ENGINE WRITING |
rottenegg | 12:ff8d26124c38 | 86 | if (_tck + 2000 >= _length) { |
rottenegg | 11:7f3e9bc7366d | 87 | _fptr = NULL; |
rottenegg | 11:7f3e9bc7366d | 88 | _tck = 0; |
rottenegg | 11:7f3e9bc7366d | 89 | vtck = 0; |
rottenegg | 12:ff8d26124c38 | 90 | } else if (vtck > 2001 && !lock) { |
rottenegg | 17:7d4d8905b608 | 91 | this->UpdateBlock1(); |
rottenegg | 12:ff8d26124c38 | 92 | } else if (vtck < 1999 && lock) { |
rottenegg | 17:7d4d8905b608 | 93 | this->UpdateBlock2(); |
rottenegg | 11:7f3e9bc7366d | 94 | } |
rottenegg | 11:7f3e9bc7366d | 95 | } |
rottenegg | 11:7f3e9bc7366d | 96 | |
rottenegg | 17:7d4d8905b608 | 97 | void WDplayer::UpdateBlock2() { |
rottenegg | 17:7d4d8905b608 | 98 | //Block 2 Update |
rottenegg | 17:7d4d8905b608 | 99 | for (int i = 2001; i <= 4000; i++) { |
rottenegg | 17:7d4d8905b608 | 100 | fread(buffer,1,1,_fptr); |
rottenegg | 17:7d4d8905b608 | 101 | cache[i] = ((int)buffer[0]); |
rottenegg | 17:7d4d8905b608 | 102 | } |
rottenegg | 17:7d4d8905b608 | 103 | lock = false; |
rottenegg | 17:7d4d8905b608 | 104 | _tck = _tck + 2000; |
rottenegg | 17:7d4d8905b608 | 105 | } |
rottenegg | 17:7d4d8905b608 | 106 | |
rottenegg | 17:7d4d8905b608 | 107 | void WDplayer::UpdateBlock1() { |
rottenegg | 17:7d4d8905b608 | 108 | //Block 1 Update |
rottenegg | 17:7d4d8905b608 | 109 | for (int i = 0; i <= 2000; i++) { |
rottenegg | 17:7d4d8905b608 | 110 | fread(buffer,1,1,_fptr); |
rottenegg | 17:7d4d8905b608 | 111 | cache[i] = ((unsigned char)buffer[0]); |
rottenegg | 17:7d4d8905b608 | 112 | } |
rottenegg | 17:7d4d8905b608 | 113 | lock = true; |
rottenegg | 17:7d4d8905b608 | 114 | _tck = _tck + 2000; |
rottenegg | 17:7d4d8905b608 | 115 | } |
rottenegg | 17:7d4d8905b608 | 116 | |
rottenegg | 11:7f3e9bc7366d | 117 | void WDplayer::ISRset() { |
rottenegg | 11:7f3e9bc7366d | 118 | this->ISRpreload(); |
rottenegg | 11:7f3e9bc7366d | 119 | ISR.attach(callback(this,&WDplayer::ISRtck),_pwmfreq); |
rottenegg | 11:7f3e9bc7366d | 120 | } |
rottenegg | 11:7f3e9bc7366d | 121 | |
rottenegg | 12:ff8d26124c38 | 122 | void WDplayer::ISRpause() { |
rottenegg | 12:ff8d26124c38 | 123 | ISR.detach(); |
rottenegg | 12:ff8d26124c38 | 124 | } |
rottenegg | 12:ff8d26124c38 | 125 | |
rottenegg | 12:ff8d26124c38 | 126 | void WDplayer::ISRresume() { |
rottenegg | 12:ff8d26124c38 | 127 | ISR.attach(callback(this,&WDplayer::ISRtck),_pwmfreq); |
rottenegg | 12:ff8d26124c38 | 128 | } |
rottenegg | 12:ff8d26124c38 | 129 | |
rottenegg | 11:7f3e9bc7366d | 130 | void WDplayer::ISRreset() { |
rottenegg | 11:7f3e9bc7366d | 131 | free(cache); |
rottenegg | 11:7f3e9bc7366d | 132 | lock = false; |
rottenegg | 12:ff8d26124c38 | 133 | vtck = 4000; |
rottenegg | 11:7f3e9bc7366d | 134 | _tck = 0; |
rottenegg | 11:7f3e9bc7366d | 135 | ISR.detach(); |
rottenegg | 11:7f3e9bc7366d | 136 | } |
rottenegg | 11:7f3e9bc7366d | 137 | |
rottenegg | 11:7f3e9bc7366d | 138 | void WDplayer::ISRtck() { |
rottenegg | 11:7f3e9bc7366d | 139 | _dac->write( cache[vtck] / 255.00 ); |
rottenegg | 12:ff8d26124c38 | 140 | if (vtck == 4000) { |
rottenegg | 11:7f3e9bc7366d | 141 | vtck = -1; |
rottenegg | 11:7f3e9bc7366d | 142 | } |
rottenegg | 11:7f3e9bc7366d | 143 | vtck++; |
rottenegg | 11:7f3e9bc7366d | 144 | }; |
rottenegg | 11:7f3e9bc7366d | 145 | |
rottenegg | 11:7f3e9bc7366d | 146 | |
rottenegg | 2:964e19f2f3f1 | 147 | float WDplayer::get_pwmfreq() { |
rottenegg | 2:964e19f2f3f1 | 148 | return _pwmfreq; |
rottenegg | 2:964e19f2f3f1 | 149 | }; |