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.
Fork of flash_audio_player by
main.cpp@1:bd7bef7df00b, 2017-10-22 (annotated)
- Committer:
- 4180_1
- Date:
- Sun Oct 22 16:12:03 2017 +0000
- Revision:
- 1:bd7bef7df00b
- Parent:
- 0:e4f991474a45
ver 1.0
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| 4180_1 | 0:e4f991474a45 | 1 | #include "mbed.h" |
| 4180_1 | 0:e4f991474a45 | 2 | AnalogOut speaker(p18); |
| 4180_1 | 0:e4f991474a45 | 3 | Ticker sampletick; |
| 4180_1 | 0:e4f991474a45 | 4 | DigitalOut myled(LED1); |
| 4180_1 | 0:e4f991474a45 | 5 | //Plays Audio Clip using Array in Flash |
| 4180_1 | 1:bd7bef7df00b | 6 | //8-bit audio samples used |
| 4180_1 | 0:e4f991474a45 | 7 | //setup const array in flash with audio values |
| 4180_1 | 0:e4f991474a45 | 8 | //from free wav file conversion tool at |
| 4180_1 | 0:e4f991474a45 | 9 | //http://ccgi.cjseymour.plus.com/wavtocode/wavtocode.htm |
| 4180_1 | 0:e4f991474a45 | 10 | //see https://os.mbed.com/users/4180_1/notebook/using-flash-to-play-audio-clips/ |
| 4180_1 | 1:bd7bef7df00b | 11 | #include "cylon8bit.h" |
| 4180_1 | 0:e4f991474a45 | 12 | |
| 4180_1 | 0:e4f991474a45 | 13 | #define sample_freq 11025.0 |
| 4180_1 | 0:e4f991474a45 | 14 | //get and set the frequency from wav conversion tool GUI |
| 4180_1 | 0:e4f991474a45 | 15 | int i=0; |
| 4180_1 | 0:e4f991474a45 | 16 | |
| 4180_1 | 1:bd7bef7df00b | 17 | union short_or_char { |
| 4180_1 | 1:bd7bef7df00b | 18 | short s; |
| 4180_1 | 1:bd7bef7df00b | 19 | char c[2]; |
| 4180_1 | 1:bd7bef7df00b | 20 | }; |
| 4180_1 | 1:bd7bef7df00b | 21 | |
| 4180_1 | 1:bd7bef7df00b | 22 | union short_or_char sample; |
| 4180_1 | 1:bd7bef7df00b | 23 | |
| 4180_1 | 1:bd7bef7df00b | 24 | |
| 4180_1 | 0:e4f991474a45 | 25 | //interrupt routine to play next audio sample from array in flash |
| 4180_1 | 0:e4f991474a45 | 26 | void audio_sample () |
| 4180_1 | 0:e4f991474a45 | 27 | { |
| 4180_1 | 1:bd7bef7df00b | 28 | sample.c[1] = sound_data[i]; |
| 4180_1 | 1:bd7bef7df00b | 29 | speaker.write_u16(sample.s); |
| 4180_1 | 0:e4f991474a45 | 30 | i++; |
| 4180_1 | 0:e4f991474a45 | 31 | if (i>= NUM_ELEMENTS) { |
| 4180_1 | 0:e4f991474a45 | 32 | i = 0; |
| 4180_1 | 0:e4f991474a45 | 33 | sampletick.detach(); |
| 4180_1 | 0:e4f991474a45 | 34 | myled = 0; |
| 4180_1 | 0:e4f991474a45 | 35 | } |
| 4180_1 | 0:e4f991474a45 | 36 | } |
| 4180_1 | 0:e4f991474a45 | 37 | int main() |
| 4180_1 | 0:e4f991474a45 | 38 | { |
| 4180_1 | 1:bd7bef7df00b | 39 | sample.c[0] = 0; //setup fast byte to short conversion using union |
| 4180_1 | 0:e4f991474a45 | 40 | while(1) { |
| 4180_1 | 0:e4f991474a45 | 41 | myled = 1; |
| 4180_1 | 0:e4f991474a45 | 42 | //use a ticker to play send next audio sample value to D/A |
| 4180_1 | 0:e4f991474a45 | 43 | sampletick.attach(&audio_sample, 1.0 / sample_freq); |
| 4180_1 | 0:e4f991474a45 | 44 | //can do other things while audio plays with timer interrupts |
| 4180_1 | 0:e4f991474a45 | 45 | wait(10.0); |
| 4180_1 | 0:e4f991474a45 | 46 | } |
| 4180_1 | 0:e4f991474a45 | 47 | } |
