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@26:716bcd47f3ca, 2019-05-10 (annotated)
- Committer:
- rottenegg
- Date:
- Fri May 10 21:25:27 2019 +0000
- Revision:
- 26:716bcd47f3ca
- Parent:
- 21:f3b0ce18b44f
FINAL_SUBMISSION; ; Changes:; WDplayer: Major Memory Leek fixed related to fclose(); Game_Manager: Tuned Scene Order and Improved Random number generator.; SceneFuctions: Added a Randomly changing Object to Scene 4.
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 | _tck = 0; |
rottenegg | 11:7f3e9bc7366d | 30 | lock = false; |
rottenegg | 12:ff8d26124c38 | 31 | vtck = 4000; |
rottenegg | 11:7f3e9bc7366d | 32 | _dac->period( _pwmfreq / 4.0f); |
rottenegg | 11:7f3e9bc7366d | 33 | if (allocate) { |
rottenegg | 12:ff8d26124c38 | 34 | cache = (unsigned char*)std::calloc(4000,sizeof(unsigned char)); |
rottenegg | 11:7f3e9bc7366d | 35 | } |
rottenegg | 26:716bcd47f3ca | 36 | fclose(_fptr); |
rottenegg | 26:716bcd47f3ca | 37 | _fptr = NULL; |
rottenegg | 2:964e19f2f3f1 | 38 | }; |
rottenegg | 2:964e19f2f3f1 | 39 | |
rottenegg | 11:7f3e9bc7366d | 40 | //Inline Functions (Operates using main MCU) |
rottenegg | 11:7f3e9bc7366d | 41 | |
rottenegg | 11:7f3e9bc7366d | 42 | void WDplayer::WDplay() { |
rottenegg | 2:964e19f2f3f1 | 43 | _fptr = fopen(_path,"r"); |
rottenegg | 2:964e19f2f3f1 | 44 | fseek(_fptr,44,SEEK_SET); |
rottenegg | 3:63cdd5cab431 | 45 | //Sampling Loop- Sample Byte by Byte |
rottenegg | 3:63cdd5cab431 | 46 | //CORE ENGINE |
rottenegg | 2:964e19f2f3f1 | 47 | for (_tck = _length; _tck > 1; _tck--) { |
rottenegg | 2:964e19f2f3f1 | 48 | fread(buffer,1,1,_fptr); |
rottenegg | 11:7f3e9bc7366d | 49 | _dac->write((float)buffer[0] / 255.00f); |
rottenegg | 26:716bcd47f3ca | 50 | wait(_pwmfreq / 2.0f); |
rottenegg | 2:964e19f2f3f1 | 51 | } |
rottenegg | 26:716bcd47f3ca | 52 | fclose(_fptr); |
rottenegg | 2:964e19f2f3f1 | 53 | _fptr = NULL; |
rottenegg | 2:964e19f2f3f1 | 54 | _tck = 0; |
rottenegg | 2:964e19f2f3f1 | 55 | }; |
rottenegg | 2:964e19f2f3f1 | 56 | |
rottenegg | 11:7f3e9bc7366d | 57 | void WDplayer::WDtck() { |
rottenegg | 3:63cdd5cab431 | 58 | //To Loop the Music File When it Ends |
rottenegg | 2:964e19f2f3f1 | 59 | if (_fptr == NULL) { |
rottenegg | 2:964e19f2f3f1 | 60 | _fptr = fopen(_path,"r"); |
rottenegg | 2:964e19f2f3f1 | 61 | fseek(_fptr,44,SEEK_SET); |
rottenegg | 2:964e19f2f3f1 | 62 | } |
rottenegg | 21:f3b0ce18b44f | 63 | //end of file reset |
rottenegg | 2:964e19f2f3f1 | 64 | if (_tck == _length) { |
rottenegg | 26:716bcd47f3ca | 65 | fclose(_fptr); |
rottenegg | 2:964e19f2f3f1 | 66 | _fptr = NULL; |
rottenegg | 2:964e19f2f3f1 | 67 | _tck = 0; |
rottenegg | 2:964e19f2f3f1 | 68 | } else { |
rottenegg | 3:63cdd5cab431 | 69 | //Plays One Sample |
rottenegg | 2:964e19f2f3f1 | 70 | fread(buffer,1,1,_fptr); |
rottenegg | 11:7f3e9bc7366d | 71 | _dac->write( (int)buffer[0] / 255.00 ); |
rottenegg | 2:964e19f2f3f1 | 72 | _tck++; |
rottenegg | 2:964e19f2f3f1 | 73 | } |
rottenegg | 2:964e19f2f3f1 | 74 | }; |
rottenegg | 2:964e19f2f3f1 | 75 | |
rottenegg | 11:7f3e9bc7366d | 76 | //Outline Functions (Uses ISR to operate) |
rottenegg | 11:7f3e9bc7366d | 77 | |
rottenegg | 11:7f3e9bc7366d | 78 | void WDplayer::ISRpreload() { |
rottenegg | 11:7f3e9bc7366d | 79 | //File Reset |
rottenegg | 11:7f3e9bc7366d | 80 | if (_fptr == NULL) { |
rottenegg | 11:7f3e9bc7366d | 81 | _fptr = fopen(_path,"r"); |
rottenegg | 11:7f3e9bc7366d | 82 | fseek(_fptr,44,SEEK_SET); |
rottenegg | 11:7f3e9bc7366d | 83 | //Reset State and Locational Variables |
rottenegg | 11:7f3e9bc7366d | 84 | lock = false; |
rottenegg | 12:ff8d26124c38 | 85 | vtck = 4000; |
rottenegg | 11:7f3e9bc7366d | 86 | _tck = 0; |
rottenegg | 11:7f3e9bc7366d | 87 | } |
rottenegg | 21:f3b0ce18b44f | 88 | //CORE ENGINE - Opertes on a two data Block system one to read from and one to write to. |
rottenegg | 12:ff8d26124c38 | 89 | if (_tck + 2000 >= _length) { |
rottenegg | 26:716bcd47f3ca | 90 | fclose(_fptr); |
rottenegg | 11:7f3e9bc7366d | 91 | _fptr = NULL; |
rottenegg | 11:7f3e9bc7366d | 92 | _tck = 0; |
rottenegg | 11:7f3e9bc7366d | 93 | vtck = 0; |
rottenegg | 26:716bcd47f3ca | 94 | } else if (vtck > 2001 && !lock) { //updates dependent on where Vtck is in reading 0 to 1999 is block 1 and 2001 to 4000 block 2 |
rottenegg | 17:7d4d8905b608 | 95 | this->UpdateBlock1(); |
rottenegg | 12:ff8d26124c38 | 96 | } else if (vtck < 1999 && lock) { |
rottenegg | 17:7d4d8905b608 | 97 | this->UpdateBlock2(); |
rottenegg | 11:7f3e9bc7366d | 98 | } |
rottenegg | 11:7f3e9bc7366d | 99 | } |
rottenegg | 11:7f3e9bc7366d | 100 | |
rottenegg | 17:7d4d8905b608 | 101 | void WDplayer::UpdateBlock2() { |
rottenegg | 17:7d4d8905b608 | 102 | //Block 2 Update |
rottenegg | 17:7d4d8905b608 | 103 | for (int i = 2001; i <= 4000; i++) { |
rottenegg | 17:7d4d8905b608 | 104 | fread(buffer,1,1,_fptr); |
rottenegg | 17:7d4d8905b608 | 105 | cache[i] = ((int)buffer[0]); |
rottenegg | 17:7d4d8905b608 | 106 | } |
rottenegg | 17:7d4d8905b608 | 107 | lock = false; |
rottenegg | 17:7d4d8905b608 | 108 | _tck = _tck + 2000; |
rottenegg | 17:7d4d8905b608 | 109 | } |
rottenegg | 17:7d4d8905b608 | 110 | |
rottenegg | 17:7d4d8905b608 | 111 | void WDplayer::UpdateBlock1() { |
rottenegg | 17:7d4d8905b608 | 112 | //Block 1 Update |
rottenegg | 17:7d4d8905b608 | 113 | for (int i = 0; i <= 2000; i++) { |
rottenegg | 17:7d4d8905b608 | 114 | fread(buffer,1,1,_fptr); |
rottenegg | 17:7d4d8905b608 | 115 | cache[i] = ((unsigned char)buffer[0]); |
rottenegg | 17:7d4d8905b608 | 116 | } |
rottenegg | 17:7d4d8905b608 | 117 | lock = true; |
rottenegg | 17:7d4d8905b608 | 118 | _tck = _tck + 2000; |
rottenegg | 17:7d4d8905b608 | 119 | } |
rottenegg | 17:7d4d8905b608 | 120 | |
rottenegg | 11:7f3e9bc7366d | 121 | void WDplayer::ISRset() { |
rottenegg | 11:7f3e9bc7366d | 122 | this->ISRpreload(); |
rottenegg | 11:7f3e9bc7366d | 123 | ISR.attach(callback(this,&WDplayer::ISRtck),_pwmfreq); |
rottenegg | 11:7f3e9bc7366d | 124 | } |
rottenegg | 11:7f3e9bc7366d | 125 | |
rottenegg | 12:ff8d26124c38 | 126 | void WDplayer::ISRpause() { |
rottenegg | 12:ff8d26124c38 | 127 | ISR.detach(); |
rottenegg | 12:ff8d26124c38 | 128 | } |
rottenegg | 12:ff8d26124c38 | 129 | |
rottenegg | 12:ff8d26124c38 | 130 | void WDplayer::ISRresume() { |
rottenegg | 12:ff8d26124c38 | 131 | ISR.attach(callback(this,&WDplayer::ISRtck),_pwmfreq); |
rottenegg | 12:ff8d26124c38 | 132 | } |
rottenegg | 12:ff8d26124c38 | 133 | |
rottenegg | 11:7f3e9bc7366d | 134 | void WDplayer::ISRreset() { |
rottenegg | 21:f3b0ce18b44f | 135 | ISR.detach(); |
rottenegg | 26:716bcd47f3ca | 136 | fclose(_fptr); |
rottenegg | 26:716bcd47f3ca | 137 | _fptr = NULL; |
rottenegg | 11:7f3e9bc7366d | 138 | free(cache); |
rottenegg | 11:7f3e9bc7366d | 139 | lock = false; |
rottenegg | 12:ff8d26124c38 | 140 | vtck = 4000; |
rottenegg | 11:7f3e9bc7366d | 141 | _tck = 0; |
rottenegg | 11:7f3e9bc7366d | 142 | } |
rottenegg | 11:7f3e9bc7366d | 143 | |
rottenegg | 21:f3b0ce18b44f | 144 | //ISR attached Function |
rottenegg | 11:7f3e9bc7366d | 145 | void WDplayer::ISRtck() { |
rottenegg | 11:7f3e9bc7366d | 146 | _dac->write( cache[vtck] / 255.00 ); |
rottenegg | 12:ff8d26124c38 | 147 | if (vtck == 4000) { |
rottenegg | 11:7f3e9bc7366d | 148 | vtck = -1; |
rottenegg | 11:7f3e9bc7366d | 149 | } |
rottenegg | 11:7f3e9bc7366d | 150 | vtck++; |
rottenegg | 11:7f3e9bc7366d | 151 | }; |
rottenegg | 11:7f3e9bc7366d | 152 | |
rottenegg | 11:7f3e9bc7366d | 153 | |
rottenegg | 2:964e19f2f3f1 | 154 | float WDplayer::get_pwmfreq() { |
rottenegg | 2:964e19f2f3f1 | 155 | return _pwmfreq; |
rottenegg | 2:964e19f2f3f1 | 156 | }; |