old demo that i want to try in mbed studio

Dependencies:   mbed SDFileSystem_Copy_of_mbed_version I2S

Revision:
1:aac37edee302
Parent:
0:e89d7a0bfa3b
Child:
2:957d3b2afff4
--- a/NotchingDemo.cpp	Sat Feb 16 17:19:43 2019 +0000
+++ b/NotchingDemo.cpp	Wed Feb 27 20:11:41 2019 +0000
@@ -1,4 +1,4 @@
- 
+
 //  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!!!
@@ -7,14 +7,15 @@
 //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 "I2S.h"
+#include "SDFileSystem.h"
 #include "wm8731_Config_setup.h"
 #include "WOLFSON_config_consts.h"
 #include <stdlib.h>
 #include <vector>
 #include <string>
 #define sample_freq 11025
+#pragma import __use_two_region_memory
 DigitalOut myled(LED1);
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
@@ -43,8 +44,8 @@
 
 class classFade
 {
-    public:
-    
+public:
+
     float powerval;
     float FadeIteration;
     float DecayFactor;
@@ -53,41 +54,35 @@
     float Natural_Exp;
     int LengthSecs;
     int Length;
-    
+
     //member Functions
     float FadeOut(void)
     {
         powerval = -FadeIteration/Denom;
-        if (FadeIteration >=Length)
-        {
-            FadeCoeff = 0;    
-            
-        }
-        else
-        {
+        if (FadeIteration >=Length) {
+            FadeCoeff = 0;
+
+        } else {
             FadeCoeff = (Length - FadeIteration)/Length;
         }
-        FadeIteration = FadeIteration + 1;  
-        return FadeCoeff;              
-                
-    } 
-    float FadeIn(void)   
+        FadeIteration = FadeIteration + 1;
+        return FadeCoeff;
+
+    }
+    float FadeIn(void)
     {
         powerval = FadeIteration/Denom;
-        if (FadeIteration >=Length)
-        {
-            FadeCoeff = 1;    
-            
-        }
-        else
-        {
+        if (FadeIteration >=Length) {
+            FadeCoeff = 1;
+
+        } else {
             FadeCoeff = FadeIteration/Length;
         }
 
-        FadeIteration = FadeIteration + 1;  
-        return FadeCoeff;              
-                
-    } 
+        FadeIteration = FadeIteration + 1;
+        return FadeCoeff;
+
+    }
 };
 
 classFade  IdleFadeOut;
@@ -97,70 +92,76 @@
 
 
 
-typedef struct uFMT_STRUCT{
-  short comp_code;
-  short num_channels;
-  unsigned sample_rate;
-  unsigned avg_Bps;
-  short block_align;
-  short sig_bps;
+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;
+    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; 
+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;    
-    
-    
+    FILE* WavFile;
+
+
 }WAV_FILE_STRUCT;*/
 class classSoundFile
 {
-    WAV_FILE_STRUCT WavFile;
-    string filename[30];
-    
-    WAV_FILE_STRUCT LoadFileStream()
-    {
-        //Declare RootFolder and the directory for the appropriate file.
-        //How we index into filename[] from the outside of this class is another
-        //issue...
-        string RootFolder = "/sd/mydir/SoundDecoder/";
-        string Directory =  RootFolder + filename[0];
-        printf("%s\n\r",Directory);
-        const char* DirectoryChar = Directory.c_str();
-        WavFile.WavFile = fopen(DirectoryChar,"rb");
-        fseek(WavFile,20,SEEK_SET);
-        fread(&FileInfo.FileFormat,sizeof(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);
-        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;  
-        printf("Number of Slices: %d\n\r",FileInfo.num_slices);
-        return FileInfo;     
-    }
-    
+public:
+
+//add a class constructor
+
+    WAV_FILE_STRUCT FileInfo;
+    //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 i = 0;
 int h = 0;
 short bufflen = 1;
 int buffer[1];
-int AudioFormat, NumChannels, SampleRate, BitsPerSample ; 
+int AudioFormat, NumChannels, SampleRate, BitsPerSample ;
 char *slice_buf;
 short *data_sptr;
 short *data_sptr_horn;
@@ -219,48 +220,45 @@
 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;
-void flip() {
+void flip()
+{
     led2  = !led2;
 }
 
 
 void isr()
 {
-    if(flag1 == 0)
-    { 
+    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))
-        {  
+        if( (place_hold1 >= BufferLen)) {
             led2  = !led2;
             place_hold1 = 0;
             place_hold2 = 0;
             flag1 = 1;
             flag2 = 0;
-        }          
-    }           
-    else if(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) )
-        {
+        if( (place_hold2 >= BufferLen) ) {
             led2  = !led2;
             place_hold1 = 0;
             place_hold2 = 0;
             flag1 = 0;
-            flag2 = 1; 
+            flag2 = 1;
             FLAGBUFF2 = 0;
-        }        
+        }
     }
 }
-    
+
 void horn_sound()
 {
     BellFlag = 1;
@@ -282,130 +280,180 @@
 
 void FadeOutIsr()
 {
-    FadeFlag = 1;  
-    fseek(IdleN2Wav,44,SEEK_SET); 
-    fseek(N2Wav,44,SEEK_SET); 
-    fseek(HornWav,44,SEEK_SET); 
+    FadeFlag = 1;
+    fseek(IdleN2Wav,44,SEEK_SET);
+    fseek(N2Wav,44,SEEK_SET);
+    fseek(HornWav,44,SEEK_SET);
     fseek(FlangeWav,44,SEEK_SET);
-    
+
 }
 
 
-/*void NotchUpIsr()
+void NotchUpIsr()
 {
-    if(1 <= Notch <= 8)
-    {
-        Notch = Notch + 1;  
-        NotchDirection = 1;     
-    }      
+    if(1 <= NotchingSet.Notch < 8) {
+
+        NotchingSet.Notch = NotchingSet.Notch + 1;
+        NotchingSet.NotchTransUp = NotchingSet.Notch + 7;
+
+        NotchingSet.NotchDirection = 1;
+    }
 }
 
 void NotchDownIsr()
 {
-    if(1 <= Notch <= 8)
-    {
-        Notch = Notch - 1; 
-        NotchDirection = 0;      
-    }      
-}*/
+    if(1 < NotchingSet.Notch <= 8) {
+        NotchingSet.Notch = NotchingSet.Notch - 1;
+        NotchingSet.NotchTransDown = NotchingSet.Notch + 15;;
+        NotchingSet.NotchDirection = 0;
+    }
+}
 
 
-    
+
 
 //function prototypes
 WAV_FILE_STRUCT ReadFileInfo(WAV_FILE_STRUCT FileInfo, FILE * wav_file);
+classSoundFile LoadFileStream(classSoundFile FileInfo, string filename);
 classFade FadeDataInitialise(classFade FadeData);
 float FadeOut(void);
 void Play_WaveFile(FILE * my_wav, WAV_FILE_STRUCT FileInfo);
 void Play_WaveFileLoop(FILE * my_wav, WAV_FILE_STRUCT FileInfo);
-int main() {
+int main()
+{
     //LocalFileSystem local("local");
     NotchUp.mode(PullUp);
 
 
     //classSoundFile Sounds[4];
-   /* for(int iii = 0; iii < 3; iii ++)
-    {
-        Sounds[iii].setX(iii);     
-    }*/
+    /* for(int iii = 0; iii < 3; iii ++)
+     {
+         Sounds[iii].setX(iii);
+     }*/
     WAV_FILE_STRUCT WavInfo_Startup;
     WAV_FILE_STRUCT WavInfo_Idle;
 
     pc.printf("Beginning of program\n");
     FILE *StartupWav;
     FILE *IdleWav;
-    
-    char * RootFolder = "/sd/mydir/SoundDecoder/";]]
+
+    char * RootFolder = "/sd/mydir/SoundDecoder/";
     DIR *dir;
     dirent *ent;
     int iterator = 0;
     string  filename[10];
-      if ((dir = opendir ("/sd/mydir/SoundDecoder")) != NULL) 
-      {
-        
+    if ((dir = opendir ("/sd/mydir/SoundDecoder")) != NULL) {
+
         // print all the files and directories within directory
-        while ((ent = readdir (dir)) != NULL) 
-        {
+        while ((ent = readdir (dir)) != NULL) {
             printf("%s\r\n",string(ent->d_name));
             filename[iterator] = string(ent->d_name);
-            
+
             //printf("%s\r\n",filename[iterator]);
             iterator  = iterator + 1;
             //printf("%s\r\n", ent->d_name);
-             //filenames.push_back(string(dirp->d_name));
+            //filenames.push_back(string(dirp->d_name));
         }
         closedir (dir);
-        }
-        for(iterator = 0;iterator < 10; iterator ++)
-        {
-                printf("All the Files: %s\r\n",filename[iterator]);
-                //printf("All the Files: %s\r\n",ent[iterator]);
-        }
-        //strcat(RootFolder,qdfilename[0]);
-        string folder =  RootFolder + filename[9];
-        printf("%s\n\r",folder);
-        
-  
+    }
+    for(iterator = 0; iterator < 10; iterator ++) {
+        printf("All the Files: %s\r\n",filename[iterator]);
+        //printf("All the Files: %s\r\n",ent[iterator]);
+    }
+    //strcat(RootFolder,qdfilename[0]);
+    string folder =  RootFolder + filename[9];
+    printf("Folder: %s\n\r",folder);
+
+
     //printf("Files and Folders contained here: %d\n\r",readdir(dp));
-    
-    
+
+
     //StartupWav = fopen("/sd/mydir/Startup.wav","rb");
     const char* folder2 = folder.c_str();
-    
-    
-        classSoundFile Sounds[27];
-    for(iii = 0; iii < 26; iii++)
-    {
-        filename[iii];
-        Sounds[iii].LoadFileStream();   
+
+
+    string FileName;
+    //vector <classSoundFile> Sound (27);
+    classSoundFile Sound[22];
+    for(int iii  = 0; iii < 21; iii++) {
+        //Sound[0].FileInfo;
+        string folder =  RootFolder + filename[3];
+        printf("Whole File Thing: %s\n\r",folder);
+        Sound[iii].FileInfo.WavFile = fopen("/sd/mydir/645Engine/Startup.wav","rb");
+        printf("Size of FILE* %d\n\r ",sizeof(Sound[iii].FileInfo.WavFile));
+        fseek(Sound[iii].FileInfo.WavFile,20,SEEK_SET);
+        fread(&Sound[iii].FileInfo.FileFormat,sizeof(Sound[iii].FileInfo.FileFormat),1,Sound[iii].FileInfo.WavFile);
+        //printf("wav_format.sample_rate: %d\n\r",Sound[iii].FileInfo.FileFormat.sample_rate);
+        fread(&Sound[iii].FileInfo.FileData,sizeof(Sound[iii].FileInfo.FileData),1,Sound[iii].FileInfo.WavFile);
+        //printf("wav_data.subchunk2_size: %d\n\r",Sound[iii].FileInfo.FileData.subchunk2_size);
+        Sound[iii].FileInfo.slice_buf = ( char *)malloc(Sound[iii].FileInfo.FileFormat.block_align);
+        fread(Sound[iii].FileInfo.slice_buf,Sound[iii].FileInfo.FileFormat.block_align,1,Sound[iii].FileInfo.WavFile);   //This isnt actually required, its just a test
+        Sound[iii].FileInfo.num_slices = Sound[iii].FileInfo.FileData.subchunk2_size/Sound[iii].FileInfo.FileFormat.block_align;
+        //printf("Number of Slices: %d\n\r",Sound[iii].FileInfo.num_slices);
+        printf("Iteration: %d\n\r",iii);
+        printf("SizeOf Sound[iii] %d\n\r",sizeof(Sound[iii]));
+        printf("SizeOf Sound[iii].FileInfo.FileFormat %d\n\r",sizeof(Sound[iii].FileInfo.FileFormat));
+        printf("SizeOf Sound[iii].FileInfo.FileFormat %d\n\r",sizeof(Sound[iii].FileInfo.FileData));
+        printf("SizeOf Sound[iii].FileInfo.FileFormat %d\n\r",sizeof(Sound[iii].FileInfo.WavFile));
+        printf("SizeOf Sound[iii].FileInfo.FileFormat %d\n\r",sizeof(Sound[iii].FileInfo.id_number));
+        printf("SizeOf Sound[iii].FileInfo.FileFormat %d\n\r",sizeof(Sound[iii].FileInfo.slice_buf));
+        printf("SizeOf Sound[iii].FileInfo.FileFormat %d\n\r",sizeof(Sound[iii].FileInfo.num_slices));
+        fclose(Sound[iii].FileInfo.WavFile);//close the file stream.  We will only reopen the file when we need to do it. Hopefully this should also free the memory...
+
+        /*
+          FILE *WavFile;
+          int id_number;
+          char *slice_buf;
+          int num_slices;
+        */
+        //Sounds[iii] =   LoadFileStream(Sounds[iii],FileName);
     }
-    
-    
-    StartupWav = fopen(folder2,"rb");
-    //IdleWav = fopen("/sd/mydir/Idle_11k_quiet.wav","rb");
+    //We still experience the exact same memory issue
+    //classSoundFile Sounds[10] = {classSoundFile(FileName),classSoundFile(FileName), classSoundFile(FileName),classSoundFile(FileName),classSoundFile(FileName),classSoundFile(FileName),classSoundFile(FileName), classSoundFile(FileName),classSoundFile(FileName),classSoundFile(FileName)};
+
+    //classSoundFile Sounds[27];
+    /* for(int iii = 0; iii < 6; iii++)
+     {
+         FileName = filename[0];
+         string teststring = "test string";
+         //printf("Before calling fnc: 2%s\n\r",FileName);
+         classSoundFile Sound(FileName);
+         Sounds[iii] = Sound;
+         printf("HELLO!\n\r");
+     }*/
+    printf("DO WE GET PAST THE ALLOCATION POINT\n\r");
+
+
+
+
+
+
+    StartupWav = fopen("/sd/mydir/645Engine/Startup.wav","rb");
     IdleWav = fopen("/sd/mydir/645Engine/N2_11k_minus10dB.wav","rb");
     IdleN2Wav = fopen("/sd/mydir/645Engine/N2N3_11k_minus10dB.wav","rb");
     HornWav = fopen("/sd/mydir/645Engine/K3H_1_A_short_quiet.wav","rb");
-    //HornWav = fopen("/sd/mydir/645Engine/N2_11k_minus10dB.wav","rb");
     N2Wav = fopen("/sd/mydir/645Engine/N3_11k_minus11dB.wav","rb");
     BellWav = fopen("/sd/mydir/645Engine/EMD_BRONZE_BELL_1_11k_minus10dB.wav","rb");
     FlangeWav = fopen("/sd/mydir/645Engine/EX_FlangeJoint1_11k_minus12dB.wav","rb");
-    
-    
+
+
     WavInfo_Startup = ReadFileInfo(WavInfo_Startup, StartupWav);
+    printf("WavInfo_Startup Size: %d\n\r",sizeof(WavInfo_Startup));
     WavInfo_Idle = ReadFileInfo(WavInfo_Idle, IdleWav);
     WavInfo_Horn = ReadFileInfo(WavInfo_Horn, HornWav);
     WavInfo_IdleN2 = ReadFileInfo(WavInfo_IdleN2, IdleN2Wav);
     WavInfo_N2 = ReadFileInfo(WavInfo_N2,  N2Wav);
     WavInfo_Bell = ReadFileInfo(WavInfo_Bell, BellWav);
     WavInfo_Flange = ReadFileInfo(WavInfo_Flange, FlangeWav);
+
+
     //Populate our class instances with some data (is there an implicit way to do this?)
     N2FadeIn.LengthSecs = 4;
     IdleFadeOut.LengthSecs = 2;
     N2FadeIn = FadeDataInitialise(N2FadeIn);
     IdleFadeOut = FadeDataInitialise(IdleFadeOut);
-    
-    
+
+
     //Set up the wolfson Audio Codec board
     wm8731_Config_setup();
     //i2s audio data transfer code??
@@ -427,48 +475,73 @@
     t.stop();
     printf("Time to Calcualte Fade Coeff:  %d\n\r",t.read_us());
     t.reset();
-    
+
     fseek(IdleN2Wav,44,SEEK_SET);
     t.start();
-    fread(WavInfo_IdleN2.slice_buf,WavInfo_IdleN2.FileFormat.block_align,1,IdleN2Wav);  
+    fread(WavInfo_IdleN2.slice_buf,WavInfo_IdleN2.FileFormat.block_align,1,IdleN2Wav);
     data_sptr_IdleN2 = (short *)WavInfo_IdleN2.slice_buf;
     t.stop();
     printf("Time to Read in a slice:  %dus\n\r",t.read_us());
     t.reset();
-    
+
     printf("point to start sound: %d\n\r",((WavInfo_IdleN2.FileData.subchunk2_size) - N2FadeIn.Length));
     printf("Size of Data: %d\n\r",WavInfo_IdleN2.FileData.subchunk2_size);
-    
+
     fseek(FlangeWav,44,SEEK_SET);
     Play_WaveFile(StartupWav,WavInfo_Startup);
     fseek(IdleN2Wav,44,SEEK_SET);//reset for use in the Loop code
     slice = 0;
     fseek(IdleWav,44,SEEK_SET);
-    Play_WaveFileLoop(IdleWav, WavInfo_Idle);
-    i2s.stop();    
+    //Play_WaveFileLoop(IdleWav, WavInfo_Idle);
+
+    i2s.stop();
 
 }
 
 
-
-
 WAV_FILE_STRUCT ReadFileInfo(WAV_FILE_STRUCT FileInfo, FILE * wav_file)
 {
     fseek(wav_file,20,SEEK_SET);
     fread(&FileInfo.FileFormat,sizeof(FileInfo.FileFormat),1,wav_file);
-    printf("wav_format.sample_rate: %d\n\r",FileInfo.FileFormat.sample_rate);
-    
+    //printf("wav_format.sample_rate: %d\n\r",FileInfo.FileFormat.sample_rate);
+
     fread(&FileInfo.FileData,sizeof(FileInfo.FileData),1,wav_file);
-    printf("wav_data.subchunk2_size: %d\n\r",FileInfo.FileData.subchunk2_size);
+    //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;  
-    printf("Number of Slices: %d\n\r",FileInfo.num_slices);
-    return FileInfo;  
+    FileInfo.num_slices = FileInfo.FileData.subchunk2_size/FileInfo.FileFormat.block_align;
+    //printf("Number of Slices: %d\n\r",FileInfo.num_slices);
+    return FileInfo;
 }
+
+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.DecayFactor = 1.3;
     FadeData.FadeIteration = 1;
     //FadeData.Denom = 11025*FadeData.DecayFactor;
     FadeData.Denom = 11025*FadeData.DecayFactor;
@@ -483,65 +556,54 @@
 {
     powervalFadeIn = FadeIterationIn/denom;
     FadeCoeffFadeIn
-    
-    
+
+
 }*/
 /*float FadeOut(void)
 {
     powerval = -FadeIteration/denom;
     FadeCoeff = pow(natural_exp,powerval);
-    FadeIteration = FadeIteration + 1;  
-    return FadeCoeff;              
-                
+    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) 
-                                {
+    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;
-                                }
-                            
-                            }
+                            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;
-                                }    
-                            }
-                        }                                      
+                }
+
+            } 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;
+
+        }
+        slice = slice + 1;
     }
 }
 
@@ -551,128 +613,105 @@
 
 
 void Play_WaveFileLoop(FILE * my_wav, WAV_FILE_STRUCT FileInfo)//(classSoundFile Sounds)
+//void Play_WaveFileLoop(classSoundFiles NotchSounds, classSoundFiles NotchTransitionSounds, classSoundFiles AdditSounds)
 {
-        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;
+    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;
+
+        if(BellFlag) {
+            if(feof(BellWav)) {
+                if(feof(HornWav)) {
 
-            if(BellFlag)
-            {
-                if(feof(BellWav))
-                {
-                    if(feof(HornWav))
-                    {
-                        
-                    }
-                    else
-                    {
-                        fseek(BellWav,44,SEEK_SET);
-                    }    
+                } else {
+                    fseek(BellWav,44,SEEK_SET);
                 }
-                fread(WavInfo_Horn.slice_buf,WavInfo_Horn.FileFormat.block_align,1,HornWav);    
-                fread(WavInfo_Bell.slice_buf,WavInfo_Bell.FileFormat.block_align,1,BellWav);
-                fread(WavInfo_Flange.slice_buf,WavInfo_Flange.FileFormat.block_align,1,FlangeWav);
-                fread(WavInfo_N2.slice_buf,WavInfo_N2.FileFormat.block_align,1,N2Wav);
             }
-            if( slice == (FileInfo.num_slices-1) )
-            {   
-                slice = 0;
-                fseek(my_wav,44,SEEK_SET);   
-            } 
-               
-            fread(FileInfo.slice_buf,FileInfo.FileFormat.block_align,1,my_wav);
-            data_sptr=(short *)FileInfo.slice_buf;     // 16 bit samples
-            if(DualEngineFlag)
-            {
-               fread(WavInfo_N2.slice_buf,WavInfo_N2.FileFormat.block_align,1,N2Wav);  
-               data_sptr_N2 = (short *)WavInfo_N2.slice_buf;  
-               *data_sptr = *data_sptr + *data_sptr_N2;     
+            fread(WavInfo_Horn.slice_buf,WavInfo_Horn.FileFormat.block_align,1,HornWav);
+            fread(WavInfo_Bell.slice_buf,WavInfo_Bell.FileFormat.block_align,1,BellWav);
+            fread(WavInfo_Flange.slice_buf,WavInfo_Flange.FileFormat.block_align,1,FlangeWav);
+            fread(WavInfo_N2.slice_buf,WavInfo_N2.FileFormat.block_align,1,N2Wav);
+        }
+        if( slice == (FileInfo.num_slices-1) ) {
+            slice = 0;
+            fseek(my_wav,44,SEEK_SET);
+        }
+
+        fread(FileInfo.slice_buf,FileInfo.FileFormat.block_align,1,my_wav);
+        data_sptr=(short *)FileInfo.slice_buf;     // 16 bit samples
+        if(DualEngineFlag) {
+            fread(WavInfo_N2.slice_buf,WavInfo_N2.FileFormat.block_align,1,N2Wav);
+            data_sptr_N2 = (short *)WavInfo_N2.slice_buf;
+            *data_sptr = *data_sptr + *data_sptr_N2;
+        }
+
+
+        if(FadeFlag) {
+            if(feof(N2Wav)) {
+                fseek(N2Wav,44,SEEK_SET);
             }
             
-            
-            if(FadeFlag)
+            //NotchFadeOut.FadeCoeff = NotchFadeOut.FadeOut();//compute new FadeOut Coeff value
+            IdleFadeOut.FadeCoeff = IdleFadeOut.FadeOut();
+            data_sptr_IdleN2 = (short *)WavInfo_IdleN2.slice_buf;
+            fread(WavInfo_IdleN2.slice_buf,WavInfo_IdleN2.FileFormat.block_align,1,IdleN2Wav);
+            if( ((WavInfo_IdleN2.FileData.subchunk2_size) - N2FadeIn.Length - 2*11025) <= (ftell(IdleN2Wav) + 44))
+                //if( (WavInfo_IdleN2.FileData.subchunk2_size)/8 <=ftell(IdleN2Wav) )
             {
-                if(feof(N2Wav))
-                {
-                    fseek(N2Wav,44,SEEK_SET);                        
-                }
-                IdleFadeOut.FadeCoeff = IdleFadeOut.FadeOut();
-                data_sptr_IdleN2 = (short *)WavInfo_IdleN2.slice_buf; 
-                fread(WavInfo_IdleN2.slice_buf,WavInfo_IdleN2.FileFormat.block_align,1,IdleN2Wav);   
-                if( ((WavInfo_IdleN2.FileData.subchunk2_size) - N2FadeIn.Length - 2*11025) <= (ftell(IdleN2Wav) + 44))
-                //if( (WavInfo_IdleN2.FileData.subchunk2_size)/8 <=ftell(IdleN2Wav) )
-                {   
-                    
-                    N2FadeIn.FadeCoeff = N2FadeIn.FadeIn();
-                    fread(WavInfo_N2.slice_buf,WavInfo_N2.FileFormat.block_align,1,N2Wav); 
-                    data_sptr_N2 = (short *)WavInfo_N2.slice_buf;
 
-                    if( (ftell(IdleN2Wav) + 44) >= WavInfo_IdleN2.FileData.subchunk2_size )
-                    {
-                        *data_sptr = *data_sptr_N2*N2FadeIn.FadeCoeff;        
-                    } 
-                    else
-                    {
-                        *data_sptr = *data_sptr*IdleFadeOut.FadeCoeff + *data_sptr_IdleN2 + *data_sptr_N2*N2FadeIn.FadeCoeff;// + *data_sptr_N2;    
-                    }
-                        
-                }
-                else
-                {
-                    *data_sptr = *data_sptr*IdleFadeOut.FadeCoeff + *data_sptr_IdleN2;    
-                }                 
-                
-            }
-            if(BellFlag)
-            {
-                data_sptr_horn = (short *)WavInfo_Horn.slice_buf;
-                data_sptr_bell = (short *)WavInfo_Bell.slice_buf;
-                data_sptr_Flange = (short *)WavInfo_Flange.slice_buf;
+                N2FadeIn.FadeCoeff = N2FadeIn.FadeIn();
+                fread(WavInfo_N2.slice_buf,WavInfo_N2.FileFormat.block_align,1,N2Wav);
                 data_sptr_N2 = (short *)WavInfo_N2.slice_buf;
-                *data_sptr = *data_sptr + *data_sptr_horn + *data_sptr_bell + *data_sptr_Flange + *data_sptr_N2;//use dereference operator
-                //*data_sptr = *data_sptr + *data_sptr_horn;
+
+                if( (ftell(IdleN2Wav) + 44) >= WavInfo_IdleN2.FileData.subchunk2_size ) {
+                    *data_sptr = *data_sptr_N2*N2FadeIn.FadeCoeff;
+                } else {
+                    *data_sptr = *data_sptr*IdleFadeOut.FadeCoeff + *data_sptr_IdleN2 + *data_sptr_N2*N2FadeIn.FadeCoeff;// + *data_sptr_N2;
+                }
+
+            } else {
+                *data_sptr = *data_sptr*IdleFadeOut.FadeCoeff + *data_sptr_IdleN2;
             }
-            for (channel=0;channel<FileInfo.FileFormat.num_channels;channel++)
-            {
-                switch (FileInfo.FileFormat.sig_bps) {
+
+        }
+        if(BellFlag) {
+            data_sptr_horn = (short *)WavInfo_Horn.slice_buf;
+            data_sptr_bell = (short *)WavInfo_Bell.slice_buf;
+            data_sptr_Flange = (short *)WavInfo_Flange.slice_buf;
+            data_sptr_N2 = (short *)WavInfo_N2.slice_buf;
+            *data_sptr = *data_sptr + *data_sptr_horn + *data_sptr_bell + *data_sptr_Flange + *data_sptr_N2;//use dereference operator
+            //*data_sptr = *data_sptr + *data_sptr_horn;
+        }
+        for (channel=0; channel<FileInfo.FileFormat.num_channels; channel++) {
+            switch (FileInfo.FileFormat.sig_bps) {
                 case 16:
-                if(flag1 == 1)
-                {
-                    Buffer1[place_hold1] = 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] = data_sptr[channel];
-                    place_hold2 = place_hold2 + 1;
-                    if(place_hold2 >= BufferLen)
-                    {
-                        while(1)
-                        {
+                    if(flag1 == 1) {
+                        Buffer1[place_hold1] = 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)
 
-                            if(flag2 == 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;