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.
main.cpp
00001 #include "mbed.h" 00002 #include "steam-train.h" //setup recorded sound data in flash 00003 #include "steam-whistle.h" 00004 //Mixes two sounds, steam engine and whistle, 00005 //using flash audio player 00006 //pushbutton on p12 blows whistle 00007 //Speaker with amp on p18 DAC to play audio 00008 00009 AnalogOut speaker(p18);//output to audio amp to drive speaker 00010 DigitalOut led(LED1); 00011 DigitalOut led2(LED2); 00012 DigitalIn whistle(p12,PullUp);//pushbutton to ground for whistle 00013 Ticker audio_tick; //audio sample rate interrupt 00014 int i=0; 00015 int j=0; 00016 00017 //interrupt routine to play next audio sample from array in flash 00018 void audio_sample () 00019 { 00020 unsigned short data; 00021 data = steam_train_data[i]>>1; 00022 //add whistle sound if button hit 00023 //since two audio signals are added, scaling down using >>1 00024 //(i.e., shift is faster than /2) avoids overflow (i.e., audio distortion) 00025 //As an alternative, data could also be scaled down 00026 //in the .h flash data files using the audio tools 00027 if (!whistle) { 00028 data = data+(steam_whistle_data[j]>>1); 00029 j++; 00030 if (j>NUM_ELEMENTS_WHISTLE) j=0; 00031 } else j=0; 00032 //scale down by 2 to avoid amp saturation and audio distortion 00033 speaker.write_u16(data); 00034 i++; 00035 if (i>NUM_ELEMENTS_TRAIN) i = 0; 00036 } 00037 00038 int main() 00039 { 00040 //enable audio interrupt using timer - 8Khz sample rate 00041 audio_tick.attach(&audio_sample, 1.0 / 8000.0); 00042 while(1) { 00043 //LED blink 00044 led = !led; 00045 //Whistle pb status 00046 led2 = !whistle; 00047 wait(0.5); 00048 } 00049 }
Generated on Fri Jul 15 2022 05:45:16 by 1.7.2