Plays 8-bit Mono Wav Files for PWM
WDplayer.h@2:41149130ecf4, 2019-04-09 (annotated)
- Committer:
- rottenegg
- Date:
- Tue Apr 09 04:32:52 2019 +0000
- Revision:
- 2:41149130ecf4
- Parent:
- 1:f24e1ca9c03a
- Child:
- 3:574fdfeb949e
Changed Example code;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rottenegg | 0:c2f3d752d98f | 1 | #ifndef WDPLAYER_H |
rottenegg | 0:c2f3d752d98f | 2 | #define WDPLAYER_H |
rottenegg | 0:c2f3d752d98f | 3 | |
rottenegg | 2:41149130ecf4 | 4 | /** WDplayer Class |
rottenegg | 2:41149130ecf4 | 5 | * A PWM Audio Player for .Wav runs for 8-bit Mono Audio samples |
rottenegg | 2:41149130ecf4 | 6 | * |
rottenegg | 2:41149130ecf4 | 7 | * Example: |
rottenegg | 2:41149130ecf4 | 8 | *@code |
rottenegg | 2:41149130ecf4 | 9 | *#include "mbed.h" |
rottenegg | 2:41149130ecf4 | 10 | *#include "SDFileSystem.h" |
rottenegg | 2:41149130ecf4 | 11 | *#include "WDplayer.h" |
rottenegg | 2:41149130ecf4 | 12 | * |
rottenegg | 2:41149130ecf4 | 13 | *SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); |
rottenegg | 2:41149130ecf4 | 14 | * |
rottenegg | 2:41149130ecf4 | 15 | *int main() { |
rottenegg | 2:41149130ecf4 | 16 | * //Defining a File Path |
rottenegg | 2:41149130ecf4 | 17 | * const char *mp3p; |
rottenegg | 2:41149130ecf4 | 18 | * mp3p = "/sd/Game-Files/AudBin/play1.wav"; |
rottenegg | 2:41149130ecf4 | 19 | * |
rottenegg | 2:41149130ecf4 | 20 | * //Creating an Object |
rottenegg | 2:41149130ecf4 | 21 | * WDplayer mp3; |
rottenegg | 2:41149130ecf4 | 22 | * |
rottenegg | 2:41149130ecf4 | 23 | * //Intialize with File Path |
rottenegg | 2:41149130ecf4 | 24 | * mp3.intWD(mp3p); |
rottenegg | 2:41149130ecf4 | 25 | * |
rottenegg | 2:41149130ecf4 | 26 | * //Play the File |
rottenegg | 2:41149130ecf4 | 27 | * mp3.WDplay(); |
rottenegg | 2:41149130ecf4 | 28 | * |
rottenegg | 2:41149130ecf4 | 29 | * //Changing Path |
rottenegg | 2:41149130ecf4 | 30 | * mp3p = "/sd/Game-Files/AudBin/play2.wav"; |
rottenegg | 2:41149130ecf4 | 31 | * |
rottenegg | 2:41149130ecf4 | 32 | * //Using the One Tick One Sample Playing |
rottenegg | 2:41149130ecf4 | 33 | * while(1) { |
rottenegg | 2:41149130ecf4 | 34 | * mp3.WDtck(); |
rottenegg | 2:41149130ecf4 | 35 | * } |
rottenegg | 2:41149130ecf4 | 36 | * |
rottenegg | 2:41149130ecf4 | 37 | *} |
rottenegg | 2:41149130ecf4 | 38 | *@endcode |
rottenegg | 0:c2f3d752d98f | 39 | */ |
rottenegg | 0:c2f3d752d98f | 40 | |
rottenegg | 0:c2f3d752d98f | 41 | |
rottenegg | 0:c2f3d752d98f | 42 | //Best Audio |
rottenegg | 0:c2f3d752d98f | 43 | //.wav sampled at 16000hz |
rottenegg | 0:c2f3d752d98f | 44 | // mono channel only |
rottenegg | 0:c2f3d752d98f | 45 | |
rottenegg | 0:c2f3d752d98f | 46 | #include <iostream> |
rottenegg | 0:c2f3d752d98f | 47 | #include "mbed.h" |
rottenegg | 0:c2f3d752d98f | 48 | |
rottenegg | 0:c2f3d752d98f | 49 | class WDplayer{ |
rottenegg | 0:c2f3d752d98f | 50 | |
rottenegg | 0:c2f3d752d98f | 51 | public: |
rottenegg | 0:c2f3d752d98f | 52 | /** Intialize WDplayer with a File Path |
rottenegg | 0:c2f3d752d98f | 53 | * |
rottenegg | 0:c2f3d752d98f | 54 | *@param path File Path Pointer |
rottenegg | 0:c2f3d752d98f | 55 | */ |
rottenegg | 0:c2f3d752d98f | 56 | void intWD(const char *path); |
rottenegg | 0:c2f3d752d98f | 57 | /** Plays the Wav File (mbed waits till File has finished playing) |
rottenegg | 0:c2f3d752d98f | 58 | * |
rottenegg | 0:c2f3d752d98f | 59 | */ |
rottenegg | 0:c2f3d752d98f | 60 | void WDplay(); |
rottenegg | 0:c2f3d752d98f | 61 | /** Plays the Wav File one Sample at a time |
rottenegg | 0:c2f3d752d98f | 62 | * |
rottenegg | 0:c2f3d752d98f | 63 | */ |
rottenegg | 0:c2f3d752d98f | 64 | void WDtck(); |
rottenegg | 0:c2f3d752d98f | 65 | /** Returns the created PWM period. |
rottenegg | 0:c2f3d752d98f | 66 | * |
rottenegg | 0:c2f3d752d98f | 67 | *@returns A unsigned long of the Period the PWM is working on |
rottenegg | 0:c2f3d752d98f | 68 | */ |
rottenegg | 0:c2f3d752d98f | 69 | unsigned long get_pwmfreq(); |
rottenegg | 0:c2f3d752d98f | 70 | |
rottenegg | 0:c2f3d752d98f | 71 | |
rottenegg | 0:c2f3d752d98f | 72 | private: |
rottenegg | 0:c2f3d752d98f | 73 | unsigned int _length; |
rottenegg | 0:c2f3d752d98f | 74 | float _pwmfreq; |
rottenegg | 0:c2f3d752d98f | 75 | const char *_path; |
rottenegg | 0:c2f3d752d98f | 76 | FILE *_fptr; |
rottenegg | 0:c2f3d752d98f | 77 | unsigned int _tck; |
rottenegg | 0:c2f3d752d98f | 78 | }; |
rottenegg | 0:c2f3d752d98f | 79 | #endif |