old demo that i want to try in mbed studio
Dependencies: mbed SDFileSystem_Copy_of_mbed_version I2S
NotchingDemo.cpp
- Committer:
- roryhand
- Date:
- 2019-09-23
- Revision:
- 55:5a441d3b0d57
- Parent:
- 54:606a83fff291
- Child:
- 56:3755c53ac4c6
File content as of revision 55:5a441d3b0d57:
// 24/03/2018 update - I appear to be able to address the device and write something, as I am getting an ACK returned from the i2c write() function. //however if i use the write function with 4 arguments (as opposed to just 1 argument) then it doesnt work //only works with the 1 argument version!!! //THIS VERSION WORKED, CHANGED SOME THINGS, THEN CHANGED THEM BACK. NOW IT NO LONGER WORKS!!!! #include "mbed.h" #include "math.h" #include "I2S.h" #include "SDFileSystem.h" #include "wm8731_Config_setup.h" #include "WOLFSON_config_consts.h" //#include "BlockDevice.h" #include <string> #include <stdlib.h> #include <fstream> #include <iostream> #include <vector> #include <string> #define sample_freq 11025 #pragma import __use_two_region_memory //BlockDevice *bd = BlockDevice::get_default_instance(); //#include "LittleFileSystem.h" //LittleFileSystem fs("fs"); DigitalOut myled(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalIn NotchUp(p16); DigitalIn NotchDown(p17);//check the pin!!! Dont know if this will actually work... InterruptIn Horn(p16); Ticker sampletick; Ticker TickFadeOut; Timer t; Timer t2; Timer NotchTimer; Timer timer_open; Serial pc(USBTX, USBRX); // tx, rx //FOR DEBUGGING PROGRAM USING GNU SCREEN DigitalOut cs(p8); I2S i2s(I2S_TRANSMIT, p5, p6, p7); SDFileSystem sd(p11, p12, p13, p8, "sd"); // the new pinout that i am using /*struct A { int data; B b; };*/ class classFade { public: float powerval; float FadeIteration; float DecayFactor; float Denom; float FadeCoeff; float Natural_Exp; int LengthSecs; int Length; //member Functions float FadeOut(void) { powerval = -FadeIteration/Denom; if (FadeIteration >=Length) { FadeCoeff = 0; } else { FadeCoeff = (Length - FadeIteration)/Length; } FadeIteration = FadeIteration + 1; return FadeCoeff; } float FadeIn(void) { powerval = FadeIteration/Denom; if (FadeIteration >=Length) { FadeCoeff = 1; } else { FadeCoeff = FadeIteration/Length; } FadeIteration = FadeIteration + 1; return FadeCoeff; } }; classFade IdleFadeOut; classFade N2FadeIn; classFade NotchFadeOut; classFade NotchFadeIn; typedef struct uFMT_STRUCT { short comp_code; short num_channels; unsigned sample_rate; unsigned avg_Bps; short block_align; short sig_bps; } FMT_STRUCT; typedef struct uNotch_STRUCT { short Notch; short NotchTransUp; short NotchTransDown; short NotchDirection; } Notch_STRUCT; typedef struct uDATA_STRUCT { unsigned subchunk2_ID; unsigned subchunk2_size; char * data_buf; } DATA_STRUCT; typedef struct uWAV_FILE_STRUCT { FILE *WavFile; int id_number; char *slice_buf; int num_slices; FMT_STRUCT FileFormat; DATA_STRUCT FileData; } WAV_FILE_STRUCT; /*typedef struct uWAV_FILE_STRUCT{ FILE* WavFile; }WAV_FILE_STRUCT;*/ class classSoundFile { public: //add a class constructor at some point in the future (perform tests in visual studio) WAV_FILE_STRUCT FileInfo; short * data_sptr; string file_location; //classSoundFile(string filename);//this is the constructor //string filename; }; //class constructor; /*classSoundFile::classSoundFile(string filename) { //Declare RootFolder and the directory for the appropriate file. //How we index into filename[] from the outside of this class is another //issue... FileInfo.WavFile = fopen("/sd/mydir/645Engine/Startup.wav","rb"); fseek(FileInfo.WavFile,20,SEEK_SET); fread(&FileInfo.FileFormat,sizeof(FileInfo.FileFormat),1,FileInfo.WavFile); printf("wav_format.sample_rate: %d\n\r",FileInfo.FileFormat.sample_rate); fread(&FileInfo.FileData,sizeof(FileInfo.FileData),1,FileInfo.WavFile); 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,FileInfo.WavFile); //This isnt actually required, its just a test FileInfo.num_slices = FileInfo.FileData.subchunk2_size/FileInfo.FileFormat.block_align; }*/ int OneOff = 0; int notch_flag = 0; int i = 0; int h = 0; short bufflen = 1; int buffer[1]; int AudioFormat, NumChannels, SampleRate, BitsPerSample ; char *slice_buf; short *data_sptr; short *data_sptr_horn; short *data_sptr_IdleN2; short * data_sptr_bell; short * data_sptr_N2; short * data_sptr_Flange; unsigned char *data_bptr; int *data_wptr; unsigned channel; long slice, slice1, slice2, slice3, num_slices; int verbosity = 0; int verbosity2 = 0; int verbosity3 = 0; int verbosity4 = 0; int verbosity5 = 0; int interrupt_condition = 1; int sampling_freq = 11025; const int BufferLen = 2000; short Buffer1[BufferLen]; short Buffer2[BufferLen]; short place_hold1 = 0; short place_hold2 = 0; string FOLDER; string RootFolder = "/sd/mydir/SoundDecoder/"; string filename[25]; classSoundFile Sound[22]; volatile int flag1 = 1; volatile int flag2 = 0; volatile int flag3 = 1; volatile int flag4 = 0; int FLAGBUFF1 = 0; int FLAGBUFF2 = 0; int BellFlag = 0; int BellFlag2 = 0; int FadeFlag = 0; int BlockFlag = 0; int FileSwitchFlag = 0; short value[1]; FILE *HornWav; FILE *edsheeran_wav; FILE *Startup_wav; FILE *IdleN2Wav; FILE *N2Wav; FILE *BellWav; FILE *FlangeWav; FILE *HeyWav; //long long slice_value; 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; WAV_FILE_STRUCT WavInfo_Bell; WAV_FILE_STRUCT WavInfo_Flange; WAV_FILE_STRUCT WavInfo_Hey; WAV_FILE_STRUCT WavInfo_N3; WAV_FILE_STRUCT WavInfo_Frustration; Ticker flipper; char * slice_buf_bell; char * slice_buf_ed; char * slice_buf_startup; char * slice_buf_N2; Notch_STRUCT NotchingSet; //test //short *data_sptr_bell = 0; short *data_sptr_ed = 0; short *data_sptr_startup = 0; FILE *StartupWav; FILE *N3Wav; int temp = 0; void flip() { led2 = !led2; } void isr() { //timer_interrupt.start(); if(flag1 == 0) { value[0] = Buffer1[place_hold1]>>4; i2s.write(value,1);//Send next PWM value to amp place_hold1 = place_hold1 + 1; if( (place_hold1 >= BufferLen)) { led2 = !led2; place_hold1 = 0; place_hold2 = 0; flag1 = 1; flag2 = 0; } } else if(flag2 == 0) { value[0] = Buffer2[place_hold2]>>4; i2s.write(value,1);//Send next PWM value to amp place_hold2 = place_hold2 + 1; if( (place_hold2 >= BufferLen) ) { led2 = !led2; place_hold1 = 0; place_hold2 = 0; flag1 = 0; flag2 = 1; FLAGBUFF2 = 0; } } //timer_interrupt.stop(); } void horn_sound() { BellFlag = 1; fseek(HornWav,44,SEEK_SET); fseek(BellWav,44,SEEK_SET); fseek(FlangeWav,44,SEEK_SET); fseek(N2Wav,44,SEEK_SET); } void FileSwitch_isr() { FileSwitchFlag = 1; temp = 1; /*FILE* testwav = fopen("/sd/mydir/SoundDecoder_second/05.wav","rb"); if(N3Wav == NULL) { printf("Cannot Open testwav\n\r"); } */ fseek(N3Wav,44,SEEK_SET); // fclose(StartupWav); } void FadeOutIsr() { FadeFlag = 1; OneOff = 1; /* wavfile2 = fopen("/sd/mydir/SoundDecoder_second/09.wav","rb"); if(wavfile2 == NULL) { printf("Cannot Open wavfile2\n\r"); } */ /* wavfile3 = fopen("/sd/mydir/SoundDecoder_second/02.wav","rb"); if(wavfile3 == NULL) { printf("Cannot Open wavfile3\n\r"); } */ /* FILE* wavfile4 = fopen("/sd/mydir/SoundDecoder_second/04.wav","rb"); if(wavfile4 == NULL) { printf("Cannot Open wavfile3\n\r"); } */ //fseek(wavfile2,44,SEEK_SET); //fseek(wavfile3,44,SEEK_SET); slice2 = 0; slice3 = 0; BlockFlag = 1; //printf("We are inside the FadeOut interrupt routine\n\r"); } classFade FadeDataInitialise(classFade FadeData); //function prototypes classSoundFile ReadFileInfo(classSoundFile Sound, FILE * wav_file); classSoundFile LoadFileStream(classSoundFile FileInfo, string filename); float FadeOut(void); void Play_WaveFile(FILE * my_wav, WAV_FILE_STRUCT FileInfo); void Play_WaveFileDual(FILE * my_wav, WAV_FILE_STRUCT FileInfo); //void Play_WaveFileLoop(FILE * my_wav, WAV_FILE_STRUCT FileInfo); //*********************INTERRUPT ROUTINE FOR NOTCHING*************************** void NotchUpIsr() { if(1 <= NotchingSet.Notch < 8) { NotchingSet.Notch = NotchingSet.Notch + 1; NotchingSet.NotchTransUp = NotchingSet.Notch + 7; NotchingSet.NotchDirection = 1; FadeDataInitialise(NotchFadeIn); FadeDataInitialise(NotchFadeOut); } } void NotchDownIsr() { if(1 < NotchingSet.Notch <= 8) { NotchingSet.Notch = NotchingSet.Notch - 1; NotchingSet.NotchTransDown = NotchingSet.Notch + 15;; NotchingSet.NotchDirection = 0; FadeDataInitialise(NotchFadeIn); FadeDataInitialise(NotchFadeOut); } } /**********************END OF INTERRUPT ROUTINE FOR NOTCHING*******************/ void Play_WaveFileLoop(classSoundFile Sound1, classSoundFile Sound2, classSoundFile Sound3, FILE* wavfile1,FILE* wavfile2, FILE* wavfile3); int main() { NotchUp.mode(PullUp); NotchDown.mode(PullUp); pc.printf("Beginning of program\n"); printf("Do we even get to this stupid bloody point\n\r"); //Populate our class instances with some data (is there an implicit way to do this?) printf("hello\n\r"); //Set up the wolfson Audio Codec board wm8731_Config_setup(); //i2s audio data transfer code?? i2s.stereomono(I2S_STEREO); i2s.masterslave(I2S_MASTER); led3 = 1; led2 = 1; 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 //not convinced we need this above line at the moment!! /* FILE* f1 = fopen("/sd/mydir/SoundDecoder_second/01.wav","rb"); if(f1 == NULL) { printf("Cannot Open f1\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f1)); FILE* f2 = fopen("/sd/mydir/SoundDecoder_second/02.wav","rb"); if(f2 == NULL) { printf("Cannot Open f2\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f2)); FILE* f3 = fopen("/sd/mydir/SoundDecoder_second/03.wav","rb"); if(f3 == NULL) { printf("Cannot Open f3\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f3)); FILE* f4 = fopen("/sd/mydir/SoundDecoder_second/04.wav","rb"); if(f4 == NULL) { printf("Cannot Open f4\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f4)); FILE* f5 = fopen("/sd/mydir/SoundDecoder_second/05.wav","rb"); if(f5 == NULL) { printf("Cannot Open f5\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f5)); FILE* f6 = fopen("/sd/mydir/SoundDecoder_second/06.wav","rb"); if(f6 == NULL) { printf("Cannot Open f6\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f6)); FILE* f7 = fopen("/sd/mydir/SoundDecoder_second/07.wav","rb"); if(f7 == NULL) { printf("Cannot Open f7\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f7)); FILE* f8 = fopen("/sd/mydir/SoundDecoder_second/08.wav","rb"); if(f8 == NULL) { printf("Cannot Open f8\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f8)); FILE* f9 = fopen("/sd/mydir/SoundDecoder_second/09.wav","rb"); if(f9 == NULL) { printf("Cannot Open f9\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f9)); FILE* f10 = fopen("/sd/mydir/SoundDecoder_second/10.wav","rb"); if(f10 == NULL) { printf("Cannot Open f10\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f10)); FILE* f11 = fopen("/sd/mydir/SoundDecoder_second/11.wav","rb"); if(f11 == NULL) { printf("Cannot Open f11\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f11)); FILE* f12 = fopen("/sd/mydir/SoundDecoder_second/12.wav","rb"); if(f12 == NULL) { printf("Cannot Open f12\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f12)); FILE* f13 = fopen("/sd/mydir/SoundDecoder_second/13.wav","rb"); if(f13 == NULL) { printf("Cannot Open f13\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f13)); FILE* f14 = fopen("/sd/mydir/SoundDecoder_second/14.wav","rb"); if(f14 == NULL) { printf("Cannot Open f14\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f14)); FILE* f15 = fopen("/sd/mydir/SoundDecoder_second/15.wav","rb"); if(f15 == NULL) { printf("Cannot Open f15\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f15)); FILE* f16 = fopen("/sd/mydir/SoundDecoder_second/16.wav","rb"); if(f16 == NULL) { printf("Cannot Open f16\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f16)); FILE* f17 = fopen("/sd/mydir/SoundDecoder_second/17.wav","rb"); if(f17 == NULL) { printf("Cannot Open f17\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f17)); FILE* f18 = fopen("/sd/mydir/SoundDecoder_second/18.wav","rb"); if(f18 == NULL) { printf("Cannot Open f18\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f18)); FILE* f19 = fopen("/sd/mydir/SoundDecoder_second/19.wav","rb"); if(f19 == NULL) { printf("Cannot Open f19\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f19)); FILE* f20 = fopen("/sd/mydir/SoundDecoder_second/20.wav","rb"); if(f20 == NULL) { printf("Cannot Open f20\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f20)); FILE* f21 = fopen("/sd/mydir/SoundDecoder_second/21.wav","rb"); if(f21 == NULL) { printf("Cannot Open f21\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f21)); FILE* f22 = fopen("/sd/mydir/SoundDecoder_second/22.wav","rb"); if(f22 == NULL) { printf("Cannot Open f22\n\r"); } printf("size of file pointer: %d\n\r",sizeof(f22)); */ timer_open.reset(); timer_open.start(); wavfile1 = fopen("/sd/mydir/SoundDecoder_second/01.wav","rb"); if(wavfile1 == NULL) { printf("Cannot Open wavfile1\n\r"); } printf("size of wavfile1 %d\n\r",sizeof(wavfile1)); timer_open.stop(); printf("It took %d useconds to open file\n\r",timer_open.read_us()); timer_open.reset(); printf("Size of wavfile 1: %d bytes\n\r",sizeof(wavfile1)); timer_open.start(); wavfile2 = fopen("/sd/mydir/SoundDecoder_second/09.wav","rb"); if(wavfile2 == NULL) { printf("Cannot Open wavfile2\n\r"); } timer_open.stop(); printf("It took %d useconds to open file\n\r",timer_open.read_us()); timer_open.reset(); timer_open.start(); wavfile3 = fopen("/sd/mydir/SoundDecoder_second/02.wav","rb"); if(wavfile3 == NULL) { printf("Cannot Open wavfile3\n\r"); } timer_open.stop(); printf("It took %d useconds to open file\n\r",timer_open.read_us()); timer_open.reset(); Sound1 = ReadFileInfo(Sound1,wavfile1); Sound2 = ReadFileInfo(Sound2,wavfile2); Sound3 = ReadFileInfo(Sound3,wavfile3); FILE* wavfile4 = fopen("/sd/mydir/SoundDecoder_second/04.wav","rb"); if(wavfile4 == NULL) { printf("Cannot Open wavfile4\n\r"); } classSoundFile Sound4; Sound4 = ReadFileInfo(Sound4,wavfile4); fclose(wavfile4); FILE* wavfile5 = fopen("/sd/mydir/SoundDecoder_second/05.wav","rb"); if(wavfile5 == NULL) { printf("Cannot Open wavfile5\n\r"); } classSoundFile Sound5; Sound5 = ReadFileInfo(Sound5,wavfile5); fclose(wavfile5); FILE* wavfile6 = fopen("/sd/mydir/SoundDecoder_second/06.wav","rb"); if(wavfile6 == NULL) { printf("Cannot Open wavfile6\n\r"); } classSoundFile Sound6; Sound6 = ReadFileInfo(Sound6,wavfile6); fclose(wavfile6); FILE* wavfile7 = fopen("/sd/mydir/SoundDecoder_second/07.wav","rb"); if(wavfile7 == NULL) { printf("Cannot Open wavfile7\n\r"); } classSoundFile Sound7; Sound7 = ReadFileInfo(Sound7,wavfile7); fclose(wavfile7); FILE* wavfile8 = fopen("/sd/mydir/SoundDecoder_second/08.wav","rb"); if(wavfile8 == NULL) { printf("Cannot Open wavfile8\n\r"); } classSoundFile Sound8; Sound8 = ReadFileInfo(Sound8,wavfile8); fclose(wavfile8); FILE* wavfile9 = fopen("/sd/mydir/SoundDecoder_second/09.wav","rb"); if(wavfile9 == NULL) { printf("Cannot Open wavfile9\n\r"); } classSoundFile Sound9; Sound9 = ReadFileInfo(Sound9,wavfile9); fclose(wavfile9); FILE* wavfile10 = fopen("/sd/mydir/SoundDecoder_second/10.wav","rb"); if(wavfile10 == NULL) { printf("Cannot Open wavfile10\n\r"); } classSoundFile Sound10; Sound10 = ReadFileInfo(Sound10,wavfile10); fclose(wavfile10); FILE* wavfile11 = fopen("/sd/mydir/SoundDecoder_second/11.wav","rb"); if(wavfile11 == NULL) { printf("Cannot Open wavfile11\n\r"); } classSoundFile Sound11; Sound11 = ReadFileInfo(Sound11,wavfile11); fclose(wavfile11); FILE* wavfile12 = fopen("/sd/mydir/SoundDecoder_second/12.wav","rb"); if(wavfile12 == NULL) { printf("Cannot Open wavfile12\n\r"); } classSoundFile Sound12; Sound12 = ReadFileInfo(Sound12,wavfile12); fclose(wavfile12); FILE* wavfile13 = fopen("/sd/mydir/SoundDecoder_second/13.wav","rb"); if(wavfile13 == NULL) { printf("Cannot Open wavfile13\n\r"); } classSoundFile Sound13; Sound13 = ReadFileInfo(Sound13,wavfile13); fclose(wavfile13); FILE* wavfile14 = fopen("/sd/mydir/SoundDecoder_second/14.wav","rb"); if(wavfile14 == NULL) { printf("Cannot Open wavfile14\n\r"); } classSoundFile Sound14; Sound14 = ReadFileInfo(Sound14,wavfile14); fclose(wavfile14); FILE* wavfile15 = fopen("/sd/mydir/SoundDecoder_second/15.wav","rb"); if(wavfile15 == NULL) { printf("Cannot Open wavfile15\n\r"); } classSoundFile Sound15; Sound15 = ReadFileInfo(Sound15,wavfile15); fclose(wavfile15); FILE* wavfile16 = fopen("/sd/mydir/SoundDecoder_second/16.wav","rb"); if(wavfile16 == NULL) { printf("Cannot Open wavfile16\n\r"); } classSoundFile Sound16; Sound16 = ReadFileInfo(Sound16,wavfile16); fclose(wavfile16); FILE* wavfile17 = fopen("/sd/mydir/SoundDecoder_second/17.wav","rb"); if(wavfile17 == NULL) { printf("Cannot Open wavfile17\n\r"); } classSoundFile Sound17; Sound17 = ReadFileInfo(Sound17,wavfile17); fclose(wavfile17); FILE* wavfile18 = fopen("/sd/mydir/SoundDecoder_second/18.wav","rb"); if(wavfile18 == NULL) { printf("Cannot Open wavfile18\n\r"); } classSoundFile Sound18; Sound18 = ReadFileInfo(Sound18,wavfile18); fclose(wavfile18); FILE* wavfile19 = fopen("/sd/mydir/SoundDecoder_second/19.wav","rb"); if(wavfile19 == NULL) { printf("Cannot Open wavfile19\n\r"); } classSoundFile Sound19; Sound19 = ReadFileInfo(Sound19,wavfile19); fclose(wavfile19); FILE* f1 = fopen("/sd/mydir/SoundDecoder_second/01.txt","rb"); fprintf(f1, Sound1.FileInfo.FileData.subchunk2_size); fclose(wavfile2); fclose(wavfile3); NotchFadeIn.LengthSecs = 2; NotchFadeOut.LengthSecs = 2; NotchFadeIn = FadeDataInitialise(NotchFadeIn); NotchFadeOut = FadeDataInitialise(NotchFadeOut); TickFadeOut.attach(&FadeOutIsr,10.0); //timer_interrupt.reset(); printf("about to play wav file\n\r"); 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(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*************/ i2s.stop(); } classSoundFile ReadFileInfo(classSoundFile Sound, FILE * wav_file) { fseek(wav_file,20,SEEK_SET); printf("We have just seeked through this file\n\r"); fread(&Sound.FileInfo.FileFormat,sizeof(Sound.FileInfo.FileFormat),1,wav_file); //printf("wav_format.sample_rate: %d\n\r",FileInfo.FileFormat.sample_rate); fread(&Sound.FileInfo.FileData,sizeof(Sound.FileInfo.FileData),1,wav_file); printf("wav_data.subchunk2_size: %d\n\r",Sound.FileInfo.FileData.subchunk2_size); 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 Sound; } classSoundFile LoadFileStream(classSoundFile Sound, string filename) { //Declare RootFolder and the directory for the appropriate file. //How we index into filename[] from the outside of this class is another //issue... //printf("FileName: %s\n\r",filename); //string RootFolder = "/sd/mydir/SoundDecoder/"; //string Directory = RootFolder + "01.wav";// + filename[0]; //printf("%s\n\r",Directory); //const char* DirectoryChar = Directory.c_str(); //Sound.FileInfo.WavFile = fopen(DirectoryChar,"rb"); Sound.FileInfo.WavFile = fopen("/sd/mydir/645Engine/Startup.wav","rb"); fseek(Sound.FileInfo.WavFile,20,SEEK_SET); fread(&Sound.FileInfo.FileFormat,sizeof(Sound.FileInfo.FileFormat),1,Sound.FileInfo.WavFile); printf("wav_format.sample_rate: %d\n\r",Sound.FileInfo.FileFormat.sample_rate); fread(&Sound.FileInfo.FileData,sizeof(Sound.FileInfo.FileData),1,Sound.FileInfo.WavFile); printf("wav_data.subchunk2_size: %d\n\r",Sound.FileInfo.FileData.subchunk2_size); Sound.FileInfo.slice_buf = ( char *)malloc(Sound.FileInfo.FileFormat.block_align); fread(Sound.FileInfo.slice_buf,Sound.FileInfo.FileFormat.block_align,1,Sound.FileInfo.WavFile); //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",Sound.FileInfo.num_slices); return Sound; } classFade FadeDataInitialise(classFade FadeData) { FadeData.DecayFactor = 1.3; FadeData.FadeIteration = 1; //FadeData.Denom = 11025*FadeData.DecayFactor; FadeData.Denom = 11025*FadeData.DecayFactor; FadeData.Natural_Exp = 2.7183; FadeData.Length = 11025*FadeData.LengthSecs; //FadeData.Natural_Exp = 2.7; return FadeData; } //Playing Files Code /* float FadeIn(void) { powervalFadeIn = FadeIterationIn/denom; FadeCoeffFadeIn } 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) { while(slice<FileInfo.num_slices) { 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_WaveFileDual(FILE * my_wav, WAV_FILE_STRUCT FileInfo) { while(slice<FileInfo.num_slices) { if(FileSwitchFlag == 1) { if(temp == 1) { printf("Does it go to this point\n\r"); N3Wav = fopen("/sd/mydir/SoundDecoder_second/03.wav","rb"); if(N3Wav == 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(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 //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 /*Block1*/ /*Read in data for current sound files. We only have 3 active engine noises at a time. When it becomes time to transition up again, we then flip to the opposite block, and perform the opposite operation.*/ if(BlockFlag == 0) { //Sound1======================================================================================= if( slice1 == (Sound1.FileInfo.num_slices-1) ) { slice1 = 0; fseek(wavfile1,44,SEEK_SET); } if(feof(wavfile1)) { fseek(wavfile1,44,SEEK_SET); } fread(Sound1.FileInfo.slice_buf,Sound1.FileInfo.FileFormat.block_align,1,wavfile1); Sound1.data_sptr=(short *)Sound1.FileInfo.slice_buf; // 16 bit samples //============================================================================================= } else if(BlockFlag == 1) { /*Block2*/ //Read in data for current (i.e. now the previous notch, according to the index!!) //We might not need this code here...as its probably done somewhere else?? //fread(Sounds[NotchingSet.Notch-1].FileInfo.slice_buf,Sounds[NotchingSet.Notch-1].FileInfo.FileFormat.block_align,1,Sounds[NotchingSet.Notch-1].FileInfo.WavFile); //Sounds[NotchingSet.Notch-1].data_sptr = (short *)Sounds[NotchingSet.Notch-1].FileInfo.slice_buf; //We are now performing this section above. if(FadeFlag) { if( slice1 == (Sound1.FileInfo.num_slices-1) ) { slice1 = 0; fseek(wavfile1,44,SEEK_SET); } if(OneOff == 1) { wavfile2 = fopen("/sd/mydir/SoundDecoder_second/09.wav","rb"); if(wavfile2 == NULL) { printf("Cannot Open wavfile2\n\r"); } wavfile3 = fopen("/sd/mydir/SoundDecoder_second/02.wav","rb"); if(wavfile3 == NULL) { printf("Cannot Open wavfile3\n\r"); } fseek(wavfile2,44,SEEK_SET); fseek(wavfile3,44,SEEK_SET); /* FILE* wavfile4 = fopen("/sd/mydir/SoundDecoder_second/04.wav","rb"); if(wavfile4 == NULL) { printf("Cannot Open wavfile3\n\r"); } */ OneOff = 0; } fread(Sound1.FileInfo.slice_buf,Sound1.FileInfo.FileFormat.block_align,1,wavfile1); Sound1.data_sptr=(short *)Sound1.FileInfo.slice_buf; // 16 bit samples //Sound2======================================================================================= fread(Sound2.FileInfo.slice_buf,Sound2.FileInfo.FileFormat.block_align,1,wavfile2); Sound2.data_sptr=(short *)Sound2.FileInfo.slice_buf; // 16 bit samples //============================================================================================= //Sound3======================================================================================= fread(Sound3.FileInfo.slice_buf,Sound3.FileInfo.FileFormat.block_align,1,wavfile3); Sound3.data_sptr=(short *)Sound3.FileInfo.slice_buf; // 16 bit samples //============================================================================================= //require this to get the fadeout coefficient for Sound1. NotchFadeOut.FadeCoeff = NotchFadeOut.FadeOut();//compute new FadeOut Coeff value if( ((Sound2.FileInfo.FileData.subchunk2_size) - NotchFadeIn.Length - 2*11025) <= (ftell(wavfile2) + 44)) { //required for calculating the fade In coefficient for Sound3 NotchFadeIn.FadeCoeff = NotchFadeIn.FadeIn(); //Read In the next Notch /* fread(Sounds[NotchingSet.Notch].FileInfo.slice_buf,Sounds[NotchingSet.Notch].FileInfo.FileFormat.block_align,1,Sounds[NotchingSet.Notch].FileInfo.WavFile); Sounds[NotchingSet.Notch].data_sptr = (short *)Sounds[NotchingSet.Notch].FileInfo.slice_buf; */ //no longer require the above section as we are reading in data for Sound3 (above, at beginning of function). if( (ftell(wavfile2) + 44) >= Sound2.FileInfo.FileData.subchunk2_size ) { //need to explicitly test if this notation/syntax works for pointers.... /*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.*/ *Sound1.data_sptr = *Sound3.data_sptr*NotchFadeIn.FadeCoeff;//Sound 3 by itself. //fclose(wavfile2); //fclose(wavfile1); } else { *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. } } else { *Sound1.data_sptr = *Sound1.data_sptr*NotchFadeOut.FadeCoeff + *Sound2.data_sptr;//Sound1 and Sound 2 cross fade } } /********************END OF DATA ASSIGNMENT SECTION*************************************************/ } /**********************************************************************************************************/ /****************************DATA OUTPUT SECTION***********************************************************/ /**********************************************************************************************************/ for (channel=0; channel<Sound1.FileInfo.FileFormat.num_channels; channel++) { switch (Sound1.FileInfo.FileFormat.sig_bps) { case 16: if(flag1 == 1) { Buffer1[place_hold1] = Sound1.data_sptr[channel]; place_hold1 = place_hold1 + 1; if(place_hold1 >= BufferLen) { while(1) { if(flag1 == 0) { break; }//if(flag1 == 0) }//while(1) }//if(place_hold1 > = BufferLen) } else if(flag2 == 1) { Buffer2[place_hold2] = Sound1.data_sptr[channel]; place_hold2 = place_hold2 + 1; if(place_hold2 >= BufferLen) { while(1) { if(flag2 == 0) { break; } } } } } } slice1 = slice1 + 1; slice2 = slice2 + 1; slice3 = slice3 + 1; } }