Steam engine sound effects using data from flash. Hitting a pushbutton adds a whistle sound to the engine noise. A speaker with a driver plays audio.

Dependencies:   mbed

https://os.mbed.com/users/4180_1/notebook/using-flash-to-play-audio-clips/ has additional details

Committer:
4180_1
Date:
Fri Jan 17 13:18:11 2020 +0000
Revision:
0:0b19409c9805
ver 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:0b19409c9805 1 #include "mbed.h"
4180_1 0:0b19409c9805 2 #include "steam-train.h" //setup recorded sound data in flash
4180_1 0:0b19409c9805 3 #include "steam-whistle.h"
4180_1 0:0b19409c9805 4 //Mixes two sounds, steam engine and whistle,
4180_1 0:0b19409c9805 5 //using flash audio player
4180_1 0:0b19409c9805 6 //pushbutton on p12 blows whistle
4180_1 0:0b19409c9805 7 //Speaker with amp on p18 DAC to play audio
4180_1 0:0b19409c9805 8
4180_1 0:0b19409c9805 9 AnalogOut speaker(p18);//output to audio amp to drive speaker
4180_1 0:0b19409c9805 10 DigitalOut led(LED1);
4180_1 0:0b19409c9805 11 DigitalOut led2(LED2);
4180_1 0:0b19409c9805 12 DigitalIn whistle(p12,PullUp);//pushbutton to ground for whistle
4180_1 0:0b19409c9805 13 Ticker audio_tick; //audio sample rate interrupt
4180_1 0:0b19409c9805 14 int i=0;
4180_1 0:0b19409c9805 15 int j=0;
4180_1 0:0b19409c9805 16
4180_1 0:0b19409c9805 17 //interrupt routine to play next audio sample from array in flash
4180_1 0:0b19409c9805 18 void audio_sample ()
4180_1 0:0b19409c9805 19 {
4180_1 0:0b19409c9805 20 unsigned short data;
4180_1 0:0b19409c9805 21 data = steam_train_data[i]>>1;
4180_1 0:0b19409c9805 22 //add whistle sound if button hit
4180_1 0:0b19409c9805 23 //since two audio signals are added, scaling down using >>1
4180_1 0:0b19409c9805 24 //(i.e., shift is faster than /2) avoids overflow (i.e., audio distortion)
4180_1 0:0b19409c9805 25 //As an alternative, data could also be scaled down
4180_1 0:0b19409c9805 26 //in the .h flash data files using the audio tools
4180_1 0:0b19409c9805 27 if (!whistle) {
4180_1 0:0b19409c9805 28 data = data+(steam_whistle_data[j]>>1);
4180_1 0:0b19409c9805 29 j++;
4180_1 0:0b19409c9805 30 if (j>NUM_ELEMENTS_WHISTLE) j=0;
4180_1 0:0b19409c9805 31 } else j=0;
4180_1 0:0b19409c9805 32 //scale down by 2 to avoid amp saturation and audio distortion
4180_1 0:0b19409c9805 33 speaker.write_u16(data);
4180_1 0:0b19409c9805 34 i++;
4180_1 0:0b19409c9805 35 if (i>NUM_ELEMENTS_TRAIN) i = 0;
4180_1 0:0b19409c9805 36 }
4180_1 0:0b19409c9805 37
4180_1 0:0b19409c9805 38 int main()
4180_1 0:0b19409c9805 39 {
4180_1 0:0b19409c9805 40 //enable audio interrupt using timer - 8Khz sample rate
4180_1 0:0b19409c9805 41 audio_tick.attach(&audio_sample, 1.0 / 8000.0);
4180_1 0:0b19409c9805 42 while(1) {
4180_1 0:0b19409c9805 43 //LED blink
4180_1 0:0b19409c9805 44 led = !led;
4180_1 0:0b19409c9805 45 //Whistle pb status
4180_1 0:0b19409c9805 46 led2 = !whistle;
4180_1 0:0b19409c9805 47 wait(0.5);
4180_1 0:0b19409c9805 48 }
4180_1 0:0b19409c9805 49 }