Demo using DMA to play recorded audio samples to the DAC (p18) on the mbed LPC1768 using the MODDMA library for the LPC1768.
main.cpp@0:e4f991474a45, 2017-10-21 (annotated)
- Committer:
- 4180_1
- Date:
- Sat Oct 21 20:53:36 2017 +0000
- Revision:
- 0:e4f991474a45
- Child:
- 1:a61a49613606
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 | 0:e4f991474a45 | 6 | // |
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 | 0:e4f991474a45 | 11 | #include "cylonbyc.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 | 0:e4f991474a45 | 17 | //interrupt routine to play next audio sample from array in flash |
4180_1 | 0:e4f991474a45 | 18 | void audio_sample () |
4180_1 | 0:e4f991474a45 | 19 | { |
4180_1 | 0:e4f991474a45 | 20 | speaker.write_u16(sound_data[i]); |
4180_1 | 0:e4f991474a45 | 21 | i++; |
4180_1 | 0:e4f991474a45 | 22 | if (i>= NUM_ELEMENTS) { |
4180_1 | 0:e4f991474a45 | 23 | i = 0; |
4180_1 | 0:e4f991474a45 | 24 | sampletick.detach(); |
4180_1 | 0:e4f991474a45 | 25 | myled = 0; |
4180_1 | 0:e4f991474a45 | 26 | } |
4180_1 | 0:e4f991474a45 | 27 | } |
4180_1 | 0:e4f991474a45 | 28 | int main() |
4180_1 | 0:e4f991474a45 | 29 | { |
4180_1 | 0:e4f991474a45 | 30 | while(1) { |
4180_1 | 0:e4f991474a45 | 31 | myled = 1; |
4180_1 | 0:e4f991474a45 | 32 | //use a ticker to play send next audio sample value to D/A |
4180_1 | 0:e4f991474a45 | 33 | sampletick.attach(&audio_sample, 1.0 / sample_freq); |
4180_1 | 0:e4f991474a45 | 34 | //can do other things while audio plays with timer interrupts |
4180_1 | 0:e4f991474a45 | 35 | wait(10.0); |
4180_1 | 0:e4f991474a45 | 36 | } |
4180_1 | 0:e4f991474a45 | 37 | } |