Plays 8-bit Mono Wav Files for PWM
WDplayer.cpp@0:c2f3d752d98f, 2019-03-25 (annotated)
- Committer:
- rottenegg
- Date:
- Mon Mar 25 10:27:56 2019 +0000
- Revision:
- 0:c2f3d752d98f
- Child:
- 3:574fdfeb949e
Added Documentation Comments;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rottenegg | 0:c2f3d752d98f | 1 | #include "WDplayer.h" |
rottenegg | 0:c2f3d752d98f | 2 | |
rottenegg | 0:c2f3d752d98f | 3 | PwmOut dac(PTC10); |
rottenegg | 0:c2f3d752d98f | 4 | unsigned char *buffer = (unsigned char *)malloc(1); |
rottenegg | 0:c2f3d752d98f | 5 | |
rottenegg | 0:c2f3d752d98f | 6 | void WDplayer::intWD(const char *path) { |
rottenegg | 0:c2f3d752d98f | 7 | //open File |
rottenegg | 0:c2f3d752d98f | 8 | _fptr = fopen(path,"r"); |
rottenegg | 0:c2f3d752d98f | 9 | if (_fptr == NULL) { |
rottenegg | 0:c2f3d752d98f | 10 | std::cerr << "Error File not Found" << std::endl; |
rottenegg | 0:c2f3d752d98f | 11 | } |
rottenegg | 0:c2f3d752d98f | 12 | //get info |
rottenegg | 0:c2f3d752d98f | 13 | unsigned int samplerate; |
rottenegg | 0:c2f3d752d98f | 14 | fseek(_fptr,24,SEEK_SET); |
rottenegg | 0:c2f3d752d98f | 15 | fread(&samplerate,4,1,_fptr); |
rottenegg | 0:c2f3d752d98f | 16 | fseek(_fptr,0,SEEK_END); |
rottenegg | 0:c2f3d752d98f | 17 | unsigned int len = ftell(_fptr); |
rottenegg | 0:c2f3d752d98f | 18 | _pwmfreq = (1.0f / samplerate); |
rottenegg | 0:c2f3d752d98f | 19 | dac.period(_pwmfreq / (8.0f * 1.75f));//wave control and playback factor control |
rottenegg | 0:c2f3d752d98f | 20 | _path = path; |
rottenegg | 0:c2f3d752d98f | 21 | _length = len - 44; |
rottenegg | 0:c2f3d752d98f | 22 | _fptr = NULL; |
rottenegg | 0:c2f3d752d98f | 23 | _tck = 0; |
rottenegg | 0:c2f3d752d98f | 24 | }; |
rottenegg | 0:c2f3d752d98f | 25 | |
rottenegg | 0:c2f3d752d98f | 26 | void WDplayer::WDplay() { |
rottenegg | 0:c2f3d752d98f | 27 | _fptr = fopen(_path,"r"); |
rottenegg | 0:c2f3d752d98f | 28 | fseek(_fptr,44,SEEK_SET); |
rottenegg | 0:c2f3d752d98f | 29 | //sampling loop _ source of main performance issues |
rottenegg | 0:c2f3d752d98f | 30 | for (_tck = _length; _tck > 1; _tck--) { |
rottenegg | 0:c2f3d752d98f | 31 | fread(buffer,1,1,_fptr); |
rottenegg | 0:c2f3d752d98f | 32 | dac.write((int)buffer[0] / 255.00); |
rottenegg | 0:c2f3d752d98f | 33 | wait(_pwmfreq / 1.75f); //I dont like this & Playback present here |
rottenegg | 0:c2f3d752d98f | 34 | } |
rottenegg | 0:c2f3d752d98f | 35 | _fptr = NULL; |
rottenegg | 0:c2f3d752d98f | 36 | _tck = 0; |
rottenegg | 0:c2f3d752d98f | 37 | }; |
rottenegg | 0:c2f3d752d98f | 38 | |
rottenegg | 0:c2f3d752d98f | 39 | void WDplayer::WDtck() { |
rottenegg | 0:c2f3d752d98f | 40 | if (_fptr == NULL) { |
rottenegg | 0:c2f3d752d98f | 41 | _fptr = fopen(_path,"r"); |
rottenegg | 0:c2f3d752d98f | 42 | fseek(_fptr,44,SEEK_SET); |
rottenegg | 0:c2f3d752d98f | 43 | } |
rottenegg | 0:c2f3d752d98f | 44 | //sampling loop |
rottenegg | 0:c2f3d752d98f | 45 | if (_tck == _length) { |
rottenegg | 0:c2f3d752d98f | 46 | _fptr = NULL; |
rottenegg | 0:c2f3d752d98f | 47 | _tck = 0; |
rottenegg | 0:c2f3d752d98f | 48 | } else { |
rottenegg | 0:c2f3d752d98f | 49 | fread(buffer,1,1,_fptr); |
rottenegg | 0:c2f3d752d98f | 50 | dac.write( (int)buffer[0] / 255.00 ); |
rottenegg | 0:c2f3d752d98f | 51 | wait(_pwmfreq / 1.75f); //I dont like this remove to tick mode |
rottenegg | 0:c2f3d752d98f | 52 | |
rottenegg | 0:c2f3d752d98f | 53 | } |
rottenegg | 0:c2f3d752d98f | 54 | }; |
rottenegg | 0:c2f3d752d98f | 55 | |
rottenegg | 0:c2f3d752d98f | 56 | unsigned long WDplayer::get_pwmfreq() { |
rottenegg | 0:c2f3d752d98f | 57 | return _pwmfreq; |
rottenegg | 0:c2f3d752d98f | 58 | }; |