old demo that i want to try in mbed studio
Dependencies: mbed SDFileSystem_Copy_of_mbed_version I2S
Diff: NotchingDemo.cpp
- Revision:
- 45:0e8e1f2ec5d2
- Parent:
- 44:a9e84d333a6a
- Child:
- 46:de390e45c2af
--- a/NotchingDemo.cpp Sun Sep 08 19:23:09 2019 +0000 +++ b/NotchingDemo.cpp Sun Sep 08 20:02:46 2019 +0000 @@ -26,11 +26,8 @@ DigitalIn NotchDown(p17);//check the pin!!! Dont know if this will actually work... InterruptIn Horn(p16); Ticker sampletick; -Ticker fileswitch_tick; -Ticker BellTick; -Ticker EdTick; + Ticker TickFadeOut; -Ticker TestTick; Timer t; Timer t2; Timer NotchTimer; @@ -184,7 +181,7 @@ unsigned char *data_bptr; int *data_wptr; unsigned channel; -long slice, num_slices; +long slice, slice1, slice2, slice3, num_slices; int verbosity = 0; int verbosity2 = 0; int verbosity3 = 0; @@ -229,6 +226,14 @@ int slice_value[1]; +FILE *wavfile1; +FILE *wavfile2; +FILE *wavfile3; + +classSoundFile Sound1; +classSoundFile Sound2; +classSoundFile Sound3; + WAV_FILE_STRUCT WavInfo_Horn; WAV_FILE_STRUCT WavInfo_IdleN2; WAV_FILE_STRUCT WavInfo_N2; @@ -311,28 +316,11 @@ void FadeOutIsr() { FadeFlag = 1; - fseek(IdleN2Wav,44,SEEK_SET); - fseek(N2Wav,44,SEEK_SET); - fseek(HornWav,44,SEEK_SET); - fseek(FlangeWav,44,SEEK_SET); + fseek(wavfile2,44,SEEK_SET); + fseek(wavfile3,44,SEEK_SET); } -/* -void filenotch_isr() -{ - if(up) { - notch = notch+1; - notchtrans_up = notch + 8; - } else if(down) { - notch = notch-1; - notchtrans_down = notch + 15; - } - notch_flag = 1; - - -} -*/ @@ -344,7 +332,7 @@ //function prototypes -WAV_FILE_STRUCT ReadFileInfo(WAV_FILE_STRUCT FileInfo, FILE * wav_file); +classSoundFile ReadFileInfo(classSoundFile Sound, FILE * wav_file); classSoundFile LoadFileStream(classSoundFile FileInfo, string filename); float FadeOut(void); @@ -391,186 +379,8 @@ NotchUp.mode(PullUp); NotchDown.mode(PullUp); - WAV_FILE_STRUCT WavInfo_Startup; - WAV_FILE_STRUCT WavInfo_Idle; - pc.printf("Beginning of program\n"); - FILE *IdleWav; - - - char buf[10]; - int a = 1; - sprintf(buf, "%d", a); - - string foo( "hello" ); - string test( "how are" ); - string hello; - hello = foo + test; - printf("%s\n\r",hello); - - string RootFolder = "sd/mydir/SoundDecoder_second/"; - string folder = RootFolder + "0" + buf + ".wav"; - printf("FullFile location: %s\n\r",folder); - - - - classSoundFile Sound[22]; - string file_names[22]; - const char* FileName; - /* - for(int aaa = 0; aaa < 20; aaa ++) { - a = aaa+1; - sprintf(buf, "%d", a); - Sound[aaa].file_location = RootFolder + "0" + buf + ".wav"; - FileName = Sound[aaa].file_location.c_str(); - Sound[aaa].FileInfo.WavFile = fopen(FileName,"rb"); - if(Sound[aaa].FileInfo.WavFile == NULL) { - printf("Cannot Open Sound[%d].FileInfo.WavFile\n\r",aaa); - - } - */ - - /* - printf("FileName: %s\n\r",Sound[aaa].file_location); - fclose(Sound[aaa].FileInfo.WavFile); - free(Sound[aaa].FileInfo.WavFile); - */ - //wait(1); - // } - - /* - string RootFolder = "sd/mydir/SoundDecoder_second/"; - string folder = RootFolder + "01.wav"; - printf("Full file location: %s\n\r",folder); - */ - - - - /* - for(int aaa = 0;aaa < 21; aaa ++ ) - { - folder = RootFolder + "\01.wav";//filename[aaa]; - FOLDER = folder.c_str(); - Sound[aaa].FileInfo.WavFile = fopen(FOLDER,"rb"); - //MyNewWav.WavFile = fopen("/sd/mydir/Startup.wav","rb"); - printf("we opened This file %d\n\r",aaa); - - } - */ - - - - printf("About to Start ReadFileInfo Operations\n\r"); - - FILE* mywav1 = fopen("/sd/mydir/SoundDecoder_second/01.wav","rb"); - if(mywav1 == NULL) { - printf("Cannot Open mywav1\n\r"); - } - fclose(mywav1); - - FILE* mywav2 = fopen("/sd/mydir/SoundDecoder_second/02.wav","rb"); - if(mywav2 == NULL) { - printf("Cannot Open mywav2\n\r"); - } - fclose(mywav2); - - FILE* mywav3 = fopen("/sd/mydir/SoundDecoder_second/03.wav","rb"); - if(mywav3 == NULL) { - printf("Cannot Open mywav3\n\r"); - } - fclose(mywav3); - - FILE* mywav4 = fopen("/sd/mydir/SoundDecoder_second/04.wav","rb"); - if(mywav4 == NULL) { - printf("Cannot Open mywav4\n\r"); - } - fclose(mywav4); - - FILE* mywav5 = fopen("/sd/mydir/SoundDecoder_second/05.wav","rb"); - if(mywav5 == NULL) { - printf("Cannot Open mywav5\n\r"); - } - fclose(mywav5); - - FILE* mywav6 = fopen("/sd/mydir/SoundDecoder_second/06.wav","rb"); - if(mywav6 == NULL) { - printf("Cannot Open mywav6\n\r"); - } - fclose(mywav6); - - FILE* mywav7 = fopen("/sd/mydir/SoundDecoder_second/07.wav","rb"); - if(mywav7 == NULL) { - printf("Cannot Open mywav7\n\r"); - } - fclose(mywav7); - - FILE* mywav8 = fopen("/sd/mydir/SoundDecoder_second/08.wav","rb"); - if(mywav8 == NULL) { - printf("Cannot Open mywav8\n\r"); - } - fclose(mywav8); - - FILE* mywav9 = fopen("/sd/mydir/SoundDecoder_second/09.wav","rb"); - if(mywav9 == NULL) { - printf("Cannot Open mywav9\n\r"); - } - fclose(mywav9); - - FILE* mywav10 = fopen("/sd/mydir/SoundDecoder_second/10.wav","rb"); - if(mywav10 == NULL) { - printf("Cannot Open mywav10\n\r"); - } - fclose(mywav10); - - FILE* mywav11= fopen("/sd/mydir/SoundDecoder_second/11.wav","rb"); - if(mywav11 == NULL) { - printf("Cannot Open mywav11\n\r"); - } - fclose(mywav11); - - FILE* mywav12 = fopen("/sd/mydir/SoundDecoder_second/12.wav","rb"); - if(mywav12 == NULL) { - printf("Cannot Open mywav12\n\r"); - } - fclose(mywav12); - - FILE* mywav13 = fopen("/sd/mydir/SoundDecoder_second/13.wav","rb"); - if(mywav13 == NULL) { - printf("Cannot Open mywav13\n\r"); - } - fclose(mywav13); - - FILE* mywav14 = fopen("/sd/mydir/SoundDecoder_second/14.wav","rb"); - if(mywav14 == NULL) { - printf("Cannot Open mywav14\n\r"); - } - fclose(mywav14); - - - - - - HeyWav = fopen("/sd/mydir/645Engine/EX_FlangeJoint1_11k_minus12dB.wav","rb"); - if(HeyWav == NULL) { - printf("Cannot Open HeyWav\n\r"); - } - - N3Wav = fopen("/sd/mydir/SoundDecoder_second/03.wav","rb"); - if(N3Wav == NULL) { - printf("Cannot Open N3Wav\n\r"); - } - WavInfo_N3 = ReadFileInfo(WavInfo_N3,N3Wav); - fclose(N3Wav); - - - - printf("About to Read HeyWav\n\r"); - - - - WavInfo_Hey = ReadFileInfo(WavInfo_Hey,HeyWav); - @@ -586,59 +396,86 @@ i2s.masterslave(I2S_MASTER); led3 = 1; led2 = 1; - printf("Hello i2s has starrted!"); + printf("Hello i2s has started!"); i2s.start(); sampletick.attach(&isr,1.0/sampling_freq); //1/16000 - fileswitch_tick.attach(&FileSwitch_isr,6.0);//So we can switch between playing file 1 and file 2, including opening and closing - + //fileswitch_tick.attach(&FileSwitch_isr,6.0);//So we can switch between playing file 1 and file 2, including opening and closing + //not convinced we need this above line at the moment!! timer_open.reset(); timer_open.start(); - N3Wav = fopen("/sd/mydir/SoundDecoder_second/03.wav","rb"); - if(N3Wav == NULL) { - printf("Cannot Open N3Wav\n\r"); + wavfile1 = fopen("/sd/mydir/SoundDecoder_second/01.wav","rb"); + if(wavfile1 == NULL) { + printf("Cannot Open wavfile1\n\r"); } - fclose(N3Wav); - StartupWav = fopen("/sd/mydir/Startup.wav","rb"); + timer_open.reset(); printf("It took %d useconds to open file\n\r",timer_open.read_us()); - //const char* folder2 = folder.c_str(); - - - - fseek(StartupWav,44,SEEK_SET); + + + timer_open.start(); + wavfile2 = fopen("/sd/mydir/SoundDecoder_second/02.wav","rb"); + if(wavfile2 == NULL) { + printf("Cannot Open wavfile2\n\r"); + } timer_open.reset(); + printf("It took %d useconds to open file\n\r",timer_open.read_us()); + timer_open.start(); - WavInfo_Startup = ReadFileInfo(WavInfo_Startup,StartupWav); - timer_open.stop(); - printf("It took %d useconds to Read In File Info\n\r",timer_open.read_us()); + wavfile3 = fopen("/sd/mydir/SoundDecoder_second/03.wav","rb"); + if(wavfile3 == NULL) { + printf("Cannot Open wavfile3\n\r"); + } + timer_open.reset(); + printf("It took %d useconds to open file\n\r",timer_open.read_us()); + + + Sound1 = ReadFileInfo(Sound1,wavfile1); + Sound2 = ReadFileInfo(Sound2,wavfile2); + Sound3 = ReadFileInfo(Sound3,wavfile3); + + + + + TickFadeOut.attach(&FadeOutIsr,10.0); + printf("about to play wav file\n\r"); - Play_WaveFileDual(StartupWav,WavInfo_Startup); + Play_WaveFileLoop(Sound1,Sound2,Sound3,wavfile1,wavfile2,wavfile3); + //Play_WaveFileDual(StartupWav,WavInfo_Startup); printf("finished playing Wav file\n\r"); + + timer_open.reset(); timer_open.start(); - fclose(StartupWav); + fclose(wavfile1); timer_open.stop(); printf("It took %d useconds to close file\n\r",timer_open.read_us()); + + timer_open.reset(); + timer_open.start(); + fclose(wavfile2); + timer_open.stop(); + printf("It took %d useconds to close file\n\r",timer_open.read_us()); + + + timer_open.reset(); + timer_open.start(); + fclose(wavfile3); + timer_open.stop(); + printf("It took %d useconds to close file\n\r",timer_open.read_us()); + + + /************************************PLAY WAV FILE LOOP*******************/ //Play_WaveFileLoop(Sound, NotchingSet); /************************************END OF PLAY WAV FILE LOOP*************/ - - - - fseek(IdleN2Wav,44,SEEK_SET);//reset for use in the Loop code - slice = 0; - fseek(IdleWav,44,SEEK_SET); - NotchingSet.Notch = 1; - //Play_WaveFileLoop(IdleWav, WavInfo_Idle); - i2s.stop(); } @@ -648,20 +485,20 @@ -WAV_FILE_STRUCT ReadFileInfo(WAV_FILE_STRUCT FileInfo, FILE * wav_file) +classSoundFile ReadFileInfo(classSoundFile Sound, FILE * wav_file) { fseek(wav_file,20,SEEK_SET); printf("We have just seeked through this file\n\r"); - fread(&FileInfo.FileFormat,sizeof(FileInfo.FileFormat),1,wav_file); + fread(&Sound.FileInfo.FileFormat,sizeof(Sound.FileInfo.FileFormat),1,wav_file); //printf("wav_format.sample_rate: %d\n\r",FileInfo.FileFormat.sample_rate); - fread(&FileInfo.FileData,sizeof(FileInfo.FileData),1,wav_file); + fread(&Sound.FileInfo.FileData,sizeof(Sound.FileInfo.FileData),1,wav_file); //printf("wav_data.subchunk2_size: %d\n\r",FileInfo.FileData.subchunk2_size); - FileInfo.slice_buf = ( char *)malloc(FileInfo.FileFormat.block_align); - fread(FileInfo.slice_buf,FileInfo.FileFormat.block_align,1,wav_file); //This isnt actually required, its just a test - FileInfo.num_slices = FileInfo.FileData.subchunk2_size/FileInfo.FileFormat.block_align; + Sound.FileInfo.slice_buf = ( char *)malloc(Sound.FileInfo.FileFormat.block_align); + fread(Sound.FileInfo.slice_buf,Sound.FileInfo.FileFormat.block_align,1,wav_file); //This isnt actually required, its just a test + Sound.FileInfo.num_slices = Sound.FileInfo.FileData.subchunk2_size/Sound.FileInfo.FileFormat.block_align; //printf("Number of Slices: %d\n\r",FileInfo.num_slices); - return FileInfo; + return Sound; } classSoundFile LoadFileStream(classSoundFile Sound, string filename) @@ -702,21 +539,21 @@ } //Playing Files Code -/*float FadeIn(void) +float FadeIn(void) { powervalFadeIn = FadeIterationIn/denom; FadeCoeffFadeIn -}*/ -/*float FadeOut(void) +} +float FadeOut(void) { powerval = -FadeIteration/denom; FadeCoeff = pow(natural_exp,powerval); FadeIteration = FadeIteration + 1; return FadeCoeff; -}*/ +} void Play_WaveFile(FILE * my_wav, WAV_FILE_STRUCT FileInfo) @@ -814,87 +651,25 @@ } //***************************************************************************// -/* -void Play_WaveFileNotchTest(FILE * my_wav, WAV_FILE_STRUCT FileInfo) -{ - while(slice<FileInfo.num_slices) { - if(notch_flag == 1) { - if(temp == 1) { - //printf("Does it go to this point\n\r"); - SoundFile[notch].FileInfo.WavFile = fopen(Sound[aaa].file_location,"rb"); - if(SoundFile[notch].FileInfo.WavFile == NULL) { - printf("Cannot Open testwav\n\r"); - } - fclose(StartupWav); - temp = 0; - } - fread(WavInfo_N3.slice_buf,WavInfo_N3.FileFormat.block_align,1,N3Wav); - data_sptr=(short*)WavInfo_N3.slice_buf; - } else { - fread(FileInfo.slice_buf,FileInfo.FileFormat.block_align,1,my_wav); - data_sptr=(short *)FileInfo.slice_buf; // 16 bit samples - } - for (channel=0; channel<FileInfo.FileFormat.num_channels; channel++) { - if(flag1 == 1) { - Buffer1[place_hold1] = data_sptr[channel]; - place_hold1 = place_hold1 + 1; - if(place_hold1 >= BufferLen) { - while(1) { - if(flag1 == 0) { - - break; - } - - } - } - - } else if(flag2 == 1) { - Buffer2[place_hold2] = data_sptr[channel]; - place_hold2 = place_hold2 + 1; - if(place_hold2 >= BufferLen) { - - while(1) { - if(flag2 == 0) { - - break; - } - } - } - } - - } - slice = slice + 1; - } -} -*/ //**************************************************************************// -//void Play_WaveFileLoop(FILE * my_wav, WAV_FILE_STRUCT FileInfo)//(classSoundFile Sounds) - -//void Play_WaveFileLoop(classSoundFile Sounds[], Notch_STRUCT NotchingSet) -void Play_WaveFileLoop(classSoundFile Sound1, classSoundFile Sound2, classSoundFile Sound3, FIlE *wavfile1, FILE *wavfile2, FILE *wavfile3, FILE_NAME_STRUCT, filenames) -{ - while(1) { //might have to change this to a while(1) loop? - //New format!! This should be (roughly) the way that this is done, with the new structures and classes - ////fread(Sound[Notch].FileData.slice_buf,Sound[Notch].FileFormat.block_align,1,Sound[notch].WavFile); - //data_sptr=(short *)Sound[Notch].FileInfo.slice_buf; -//Block 1 of code. We need to pass in 3x classSoundFile objects, and 3x FILE* pointer objects. -//This will allow us to switch between the 3 different files (using "cross-fades") as needed. - /*Remember that these will need to be global varaibles to work properly*/ - - - - - +void Play_WaveFileLoop(classSoundFile Sound1, classSoundFile Sound2, classSoundFile Sound3, FILE *wavfile1, FILE *wavfile2, FILE *wavfile3) +{ + while(1) { //might have to change this to a while(1) loop? + //New format!! This should be (roughly) the way that this is done, with the new structures and classes - //data_sptr=(short *)Sounds[NotchingSet.Notch].FileInfo.slice_buf; // 16 bit samples +//Block 1 of code. We need to pass in 3x classSoundFile objects, and 3x FILE* pointer objects. +//This will allow us to switch between the 3 different files (using "cross-fades") as needed. + /*Remember that these will need to be global varaibles to work properly*/ + + //make sure we are reading in the correct "notch" here if(FadeFlag) { @@ -942,12 +717,7 @@ //require this to get the fadeout coefficient for Sound1. NotchFadeOut.FadeCoeff = NotchFadeOut.FadeOut();//compute new FadeOut Coeff value - //Read in the notch transition file for transitioning up - /* - fread(Sounds[NotchingSet.NotchTransUp].FileInfo.slice_buf,Sounds[NotchingSet.NotchTransUp].FileInfo.FileFormat.block_align,1,Sounds[NotchingSet.NotchTransUp].FileInfo.WavFile); - Sounds[NotchingSet.NotchTransUp].data_sptr = (short*)Sounds[NotchingSet.NotchTransUp].FileInfo.slice_buf; - */ - //No longer need the above section as we are doing it above, for Sound2 + if( ((Sound2.FileInfo.FileData.subchunk2_size) - NotchFadeIn.Length - 2*11025) <= (ftell(wavfile2) + 44)) //if( (WavInfo_IdleN2.FileData.subchunk2_size)/8 <=ftell(IdleN2Wav) ) @@ -971,9 +741,9 @@ /*Was this ever tested?? no idea... If not then we need to pass in the 3 different "data_sptr" variables in separately, and not tied to a specific class or structure.*/ - *Sound3.data_sptr = *Sound3.data_sptr*NotchFadeIn.FadeCoeff;//Sound 3 by itself. + *Sound1.data_sptr = *Sound3.data_sptr*NotchFadeIn.FadeCoeff;//Sound 3 by itself. } else { - *Sound1.data_sptr = *Sound1.data_sptr*NotchFadeOut.FadeCoeff + *Sound2.data_sptr + *Sounds[NotchingSet.Notch].data_sptr*NotchFadeIn.FadeCoeff;//Sounds 1, 2 and 3 Cross-fade. + *Sound1.data_sptr = *Sound1.data_sptr*NotchFadeOut.FadeCoeff + *Sound2.data_sptr + *Sound3.data_sptr*NotchFadeIn.FadeCoeff;//Sounds 1, 2 and 3 Cross-fade. //probably later on redesign this to work with flags - that way it may be a little easier to debug any issues. } @@ -993,11 +763,11 @@ /**********************************************************************************************************/ /****************************DATA OUTPUT SECTION***********************************************************/ /**********************************************************************************************************/ - for (channel=0; channel<Sounds[NotchingSet.Notch].FileInfo.FileFormat.num_channels; channel++) { - switch (Sounds[NotchingSet.Notch].FileInfo.FileFormat.sig_bps) { + for (channel=0; channel<Sound1.FileInfo.FileFormat.num_channels; channel++) { + switch (Sound1.FileInfo.FileFormat.sig_bps) { case 16: if(flag1 == 1) { - Buffer1[place_hold1] = Sounds[NotchingSet.Notch].data_sptr[channel]; + Buffer1[place_hold1] = Sound1.data_sptr[channel]; place_hold1 = place_hold1 + 1; if(place_hold1 >= BufferLen) { while(1) { @@ -1010,7 +780,7 @@ }//if(place_hold1 > = BufferLen) } else if(flag2 == 1) { - Buffer2[place_hold2] = Sounds[NotchingSet.Notch].data_sptr[channel]; + Buffer2[place_hold2] = Sound1.data_sptr[channel]; place_hold2 = place_hold2 + 1; if(place_hold2 >= BufferLen) { while(1) { @@ -1024,7 +794,9 @@ } } } - slice = slice + 1; + slice1 = slice1 + 1; + slice2 = slice2 + 1; + slice3 = slice3 + 1; } }