old demo that i want to try in mbed studio

Dependencies:   mbed SDFileSystem_Copy_of_mbed_version I2S

Revision:
49:ce6b36eb0d88
Parent:
48:01490448a05a
Child:
50:78ded157e9bb
--- a/NotchingDemo.cpp	Sun Sep 08 20:53:06 2019 +0000
+++ b/NotchingDemo.cpp	Mon Sep 09 21:06:07 2019 +0000
@@ -323,31 +323,33 @@
         fseek(wavfile3,44,SEEK_SET);
 
     } else if(BlockFlag == 1) {
-        BlockFlag = 2;
-        fclose(wavfile1);
-        fclose(wavfile2);
-        wavfile1 =   fopen("/sd/mydir/SoundDecoder_second/03.wav","rb");
+        //BlockFlag = 2;
+        //fclose(wavfile1);
+        //fclose(wavfile2);
+        wavfile1 =   fopen("/sd/mydir/SoundDecoder_second/03.wav","rb");//01
         if(wavfile1 == NULL) {
             printf("Cannot Open wavfile1\n\r");
         }
-        wavfile2 = fopen("/sd/mydir/SoundDecoder_second/10.wav","rb");
+        fseek(wavfile1,44,SEEK_SET);//go to beginning of audio segment
+        wavfile2 = fopen("/sd/mydir/SoundDecoder_second/10.wav","rb");//22
         if(wavfile2 == NULL) {
             printf("Cannot Open wavfile2\n\r");
         }
+        fseek(wavfile2,44,SEEK_SET);//go to beginning of audio segment
 
     } else {
 
-        BlockFlag = 1;
-        fclose(wavfile2);
-        fclose(wavfile3);
+        //BlockFlag = 1;
         wavfile2 =   fopen("/sd/mydir/SoundDecoder_second/09.wav","rb");
         if(wavfile2 == NULL) {
             printf("Cannot Open wavfile2\n\r");
         }
+        fseek(wavfile2,44,SEEK_SET);//go to beginning of audio segment
         wavfile3 = fopen("/sd/mydir/SoundDecoder_second/02.wav","rb");
         if(wavfile3 == NULL) {
             printf("Cannot Open wavfile3\n\r");
         }
+        fseek(wavfile3,44,SEEK_SET);//go to beginning of audio segment
     }
     FadeFlag = 1;
     //printf("We are inside the FadeOut interrupt routine\n\r");
@@ -722,126 +724,332 @@
         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
-//=============================================================================================
+
+
+        if(BlockFlag == 0) {
+            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) {
+                if( slice1 == (Sound1.FileInfo.num_slices-1) ) {
+                    slice1 = 0;
+                    fseek(wavfile1,44,SEEK_SET);
+                }
+                if(feof(wavfile1)) {
+                    fseek(wavfile1,44,SEEK_SET);
+                }
+//Sound1=======================================================================================
+                fread(Sound1.FileInfo.slice_buf,Sound1.FileInfo.FileFormat.block_align,1,wavfile1);
+                Sound1.data_sptr=(short *)Sound1.FileInfo.slice_buf;     // 16 bit samples
+//Sound1=======================================================================================
+
+
+                if(FadeFlag == 1) {
+
 //Sound2=======================================================================================
-        fread(Sound2.FileInfo.slice_buf,Sound2.FileInfo.FileFormat.block_align,1,wavfile2);
-        Sound2.data_sptr=(short *)Sound2.FileInfo.slice_buf;     // 16 bit samples
+                    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
+                    fread(Sound3.FileInfo.slice_buf,Sound3.FileInfo.FileFormat.block_align,1,wavfile3);
+                    Sound3.data_sptr=(short *)Sound3.FileInfo.slice_buf;     // 16 bit samples
 //=============================================================================================
-
-
-
-        /*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(BlockFlag == 1) {
-                if(feof(wavfile1)) {
-                    fseek(wavfile1,44,SEEK_SET);
-                }
-
-
-                //require this to get the fadeout coefficient for Sound1.
-                NotchFadeOut.FadeCoeff = NotchFadeOut.FadeOut();//compute new FadeOut Coeff value
+                    //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)) {
+                    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
+
+
+                        //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.  //Confirmed 8/9/2019 this works!
+
+                            *Sound1.data_sptr = *Sound3.data_sptr*NotchFadeIn.FadeCoeff;//Sound 3 by itself.
+                            fclose(wavfile1);
+                            fclose(wavfile2);
+                            BlockFlag = 1;
+                            FadeFlag = 0;
+                        } 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
+                    }
+
+
+                }
+                /*else
+                {
+                    //not convinced this is needed.  Check written logic.
+
+                }*/
+
+            } else if(BlockFlag == 2) {
+                if( slice3 == (Sound3.FileInfo.num_slices-1) ) {
+                    slice3 = 0;
+                    fseek(wavfile3,44,SEEK_SET);
+                }
+                if(feof(wavfile3)) {
+                    fseek(wavfile3,44,SEEK_SET);
+                }
 
 
-                    //required for calculating the fade In coefficient for Sound3
-                    NotchFadeIn.FadeCoeff = NotchFadeIn.FadeIn();
-
-                    //Read In the next Notch
+//Sound3=======================================================================================
+                fread(Sound3.FileInfo.slice_buf,Sound3.FileInfo.FileFormat.block_align,1,wavfile3);
+                Sound3.data_sptr=(short *)Sound3.FileInfo.slice_buf;     // 16 bit samples
+//=============================================================================================
+                if(FadeFlag == 1) {
+                    //Sound2=======================================================================================
+                    fread(Sound2.FileInfo.slice_buf,Sound2.FileInfo.FileFormat.block_align,1,wavfile2);
+                    Sound2.data_sptr=(short *)Sound2.FileInfo.slice_buf;     // 16 bit samples
+                    //=============================================================================================
+                    //Sound1=======================================================================================
+                    fread(Sound1.FileInfo.slice_buf,Sound1.FileInfo.FileFormat.block_align,1,wavfile1);
+                    Sound1.data_sptr=(short *)Sound1.FileInfo.slice_buf;     // 16 bit samples
+                    //=============================================================================================
 
-                    /*
-                    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).
+                    //require this to get the fadeout coefficient for Sound1.
+                    NotchFadeOut.FadeCoeff = NotchFadeOut.FadeOut();//compute new FadeOut Coeff value
+
+
 
-                    if( (ftell(wavfile2) + 44) >= Sound2.FileInfo.FileData.subchunk2_size ) {
+                    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();
 
-                        //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.*/  /*Confirmed 8/9/2019 this works!*/
+                        //Read In the next Notch
+
+
+                        //no longer require the above section as we are reading in data for Sound3 (above, at beginning of function).
 
-                        *Sound1.data_sptr = *Sound3.data_sptr*NotchFadeIn.FadeCoeff;//Sound 3 by itself.
-                    } else {
+                        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.  //Confirmed 8/9/2019 this works!
 
-                        *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.
-                    }
+                            *Sound1.data_sptr = *Sound1.data_sptr*NotchFadeIn.FadeCoeff;//Sound 1 by itself.
+                            fclose(wavfile3);
+                            fclose(wavfile2);
+                            BlockFlag = 2;
+                            FadeFlag = 0;
+                        } else {
 
-                } else {
-                    *Sound1.data_sptr = *Sound1.data_sptr*NotchFadeOut.FadeCoeff + *Sound2.data_sptr;//Sound1 and Sound 2 cross fade
-                }
+                            *Sound1.data_sptr = *Sound3.data_sptr*NotchFadeOut.FadeCoeff + *Sound2.data_sptr + *Sound1.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.
+                        }
 
-            }
-
-        }
-        /********************END OF DATA ASSIGNMENT SECTION*************************************************/
+                    } else {
+                        *Sound1.data_sptr = *Sound3.data_sptr*NotchFadeOut.FadeCoeff + *Sound2.data_sptr;//Sound3 and Sound 2 cross fade
+                    }
 
 
 
 
+                }
+                /*else
+                {
 
-        /**********************************************************************************************************/
-        /****************************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)
+                    //Again, not convinced this needs to be here.  check written logic
+                }  */
+
+
+            }
+
+//=============================================================================================
+
+            /*
+            //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
+            //=============================================================================================
+            */
+
+
+            /*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(BlockFlag == 1) {
+                            if(feof(wavfile1)) {
+                                fseek(wavfile1,44,SEEK_SET);
+                            }
+
+
+                            //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
+
+
+                                //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.  //Confirmed 8/9/2019 this works!
+
+                                    *Sound1.data_sptr = *Sound3.data_sptr*NotchFadeIn.FadeCoeff;//Sound 3 by itself.
+                                    fclose(wavfile1);
+                                    fclose(wavfile2);
+                                    BlockFlag = 1;
+                                    FadeFlag = 0;
+                                } 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
+                            }
+
+                        }
 
 
-                            }//while(1)
-                        }//if(place_hold1 > = BufferLen)
+                        //********************END OF DATA ASSIGNMENT SECTION*************************************************
+
+                        else if(BlockFlag == 2) {
+                            if(feof(wavfile3)) {
+                                fseek(wavfile3,44,SEEK_SET);
+                            }
+
+
+                            //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
+
+
+                                //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.  //Confirmed 8/9/2019 this works!
+
+                                    *Sound1.data_sptr = *Sound1.data_sptr*NotchFadeIn.FadeCoeff;//Sound 1 by itself.
+                                    fclose(wavfile3);
+                                    fclose(wavfile2);
+                                    BlockFlag = 2;
+                                    FadeFlag = 0;
+                                } else {
 
-                    } else if(flag2 == 1) {
-                        Buffer2[place_hold2] = Sound1.data_sptr[channel];
-                        place_hold2 = place_hold2 + 1;
-                        if(place_hold2 >= BufferLen) {
-                            while(1) {
+                                    *Sound1.data_sptr = *Sound3.data_sptr*NotchFadeOut.FadeCoeff + *Sound2.data_sptr + *Sound1.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 = *Sound3.data_sptr*NotchFadeOut.FadeCoeff + *Sound2.data_sptr;//Sound3 and Sound 2 cross fade
+                            }
+
+
+                        }
+
+                    }*/ //if(FadeFlag)
 
-                                if(flag2 == 0) {
-                                    break;
+            /**********************************************************************************************************/
+            /****************************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;
         }
-        slice1 = slice1 + 1;
-        slice2 = slice2 + 1;
-        slice3 = slice3 + 1;
     }
-}