old demo that i want to try in mbed studio
Dependencies: mbed SDFileSystem_Copy_of_mbed_version I2S
NotchingDemo.cpp
- Committer:
- roryhand
- Date:
- 2020-05-16
- Branch:
- LargeFile_Tests
- Revision:
- 90:1dce46f0d9e5
- Parent:
- 87:f5c9beae06ac
File content as of revision 90:1dce46f0d9e5:
// 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 NotchUpTick; Ticker TickFadeOut; Timer t; Timer t2; Timer OperationsTimer; 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 class classFade { public: float powerval; float FadeIteration; float DecayFactor; float Denom; float FadeCoeff; float Natural_Exp; int LengthSecs; int Length; void FadeDataInitialise() { DecayFactor = 1.3; FadeIteration = 1; //FadeData.Denom = 11025*FadeData.DecayFactor; Denom = 11025*DecayFactor; Natural_Exp = 2.7183; Length = 11025*LengthSecs; //FadeData.Natural_Exp = 2.7; } //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; } }; //Do these variables have to be global?? Find out - may be better (safer) to make them local. classFade NotchFadeIn; classFade NotchFadeOut; //Dont require the class below. Turns out I had already made a Structure to hold the same information. //If in the futre I need to embed functions then I will switch to using this class 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; Notch_STRUCT NotchingSet; 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 classPositionIndicators { public: int notch1_indicator, notch2_indicator, notch3_indicator, notch4_indicator; int notch5_indicator, notch6_indicator, notch7_indicator, notch8_indicator; int notch1_start_pt, notch2_start_pt, notch3_start_pt, notch4_start_pt; int notch5_start_pt, notch6_start_pt, notch7_start_pt, notch8_start_pt; int N1N2_start_pt, N2N3_start_pt, N3N4_start_pt, N4N5_start_pt, N5N6_start_pt; int N6N7_start_pt, N7N8_start_pt, N8N7_start_pt, N7N6_start_pt, N6N5_start_pt; int N5N4_start_pt, N4N3_start_pt, N3N2_start_pt, N2N1_start_pt; int notch_start_pts [9]; int notch_position_indicators[9]; int notch_transitions_start_pts[15]; int notch_transitions_position_indicators[15]; int auxiliary_start_pts[5]; int auxiliary_position_indicators[5]; classPositionIndicators() { notch1_start_pt = 44; notch2_start_pt = 220884+44; notch3_start_pt = notch2_start_pt+217698; notch4_start_pt = notch3_start_pt + 193060; notch5_start_pt = notch4_start_pt + 92010; notch6_start_pt = notch5_start_pt + 216642; notch7_start_pt = notch6_start_pt + 250316; notch8_start_pt = notch7_start_pt + 150152; notch1_indicator = notch1_start_pt; notch2_indicator = notch2_start_pt; notch3_indicator = notch3_start_pt; notch4_indicator = notch4_start_pt; notch5_indicator = notch5_start_pt; notch6_indicator = notch6_start_pt; notch7_indicator = notch7_start_pt; notch8_indicator = notch8_start_pt; notch_start_pts[1] = notch1_start_pt; notch_start_pts[2] = notch2_start_pt; notch_start_pts[3] = notch3_start_pt; notch_start_pts[4] = notch4_start_pt; notch_start_pts[5] = notch5_start_pt; notch_start_pts[6] = notch6_start_pt; notch_start_pts[7] = notch7_start_pt; notch_start_pts[8] = notch8_start_pt; notch_position_indicators[1] = notch1_indicator; notch_position_indicators[2] = notch2_indicator; notch_position_indicators[3] = notch3_indicator; notch_position_indicators[4] = notch4_indicator; notch_position_indicators[5] = notch5_indicator; notch_position_indicators[6] = notch6_indicator; notch_position_indicators[7] = notch7_indicator; notch_position_indicators[8] = notch8_indicator; N1N2_start_pt = 44; N2N3_start_pt = N1N2_start_pt + 73220; N3N4_start_pt = N2N3_start_pt + 78164; N4N5_start_pt = N3N4_start_pt + 59432; N5N6_start_pt = N4N5_start_pt + 64984; N6N7_start_pt = N5N6_start_pt + 59924; N7N8_start_pt = N6N7_start_pt + 97874; N8N7_start_pt = N7N8_start_pt + 63992; N7N6_start_pt = N8N7_start_pt + 44506; N6N5_start_pt = N7N6_start_pt + 55052; N5N4_start_pt = N6N5_start_pt + 37038; N4N3_start_pt = N5N4_start_pt + 49692; N3N2_start_pt = N4N3_start_pt + 44100; N2N1_start_pt = N3N2_start_pt + 58346; notch_transitions_start_pts[1] = N1N2_start_pt; notch_transitions_start_pts[2] = N2N3_start_pt; notch_transitions_start_pts[3] = N3N4_start_pt; notch_transitions_start_pts[4] = N4N5_start_pt; notch_transitions_start_pts[5] = N5N6_start_pt; notch_transitions_start_pts[6] = N6N7_start_pt; notch_transitions_start_pts[7] = N7N8_start_pt; notch_transitions_start_pts[8] = N8N7_start_pt; notch_transitions_start_pts[9] = N7N6_start_pt; notch_transitions_start_pts[10] = N6N5_start_pt; notch_transitions_start_pts[11] = N5N4_start_pt; notch_transitions_start_pts[12] = N4N3_start_pt; notch_transitions_start_pts[13] = N3N2_start_pt; notch_transitions_start_pts[14] = N2N1_start_pt; notch_transitions_position_indicators[1] = N1N2_start_pt; notch_transitions_position_indicators[2] = N2N3_start_pt; notch_transitions_position_indicators[3] = N3N4_start_pt; notch_transitions_position_indicators[4] = N4N5_start_pt; notch_transitions_position_indicators[5] = N5N6_start_pt; notch_transitions_position_indicators[6] = N6N7_start_pt; notch_transitions_position_indicators[7] = N7N8_start_pt; notch_transitions_position_indicators[8] = N8N7_start_pt; notch_transitions_position_indicators[9] = N7N6_start_pt; notch_transitions_position_indicators[10] = N6N5_start_pt; notch_transitions_position_indicators[11] = N5N4_start_pt; notch_transitions_position_indicators[12] = N4N3_start_pt; notch_transitions_position_indicators[13] = N3N2_start_pt; notch_transitions_position_indicators[14] = N2N1_start_pt; } }; int TransitionFlag; void NotchUpIsr() { if(1 <= NotchingSet.Notch < 8) { NotchingSet.Notch = NotchingSet.Notch + 1; NotchingSet.NotchTransUp = NotchingSet.Notch + 7; NotchingSet.NotchDirection = 1; NotchFadeIn.FadeDataInitialise(); NotchFadeOut.FadeDataInitialise(); TransitionFlag = 1; //printf("We are in the NotchUpIsr() section \n\r"); //NotchFadeOut.FadeDataInitialise; } } void NotchDownIsr() { if(1 < NotchingSet.Notch <= 8) { NotchingSet.Notch = NotchingSet.Notch - 1; NotchingSet.NotchTransDown = NotchingSet.Notch + 15; NotchingSet.NotchDirection = 0; NotchFadeIn.FadeDataInitialise(); NotchFadeOut.FadeDataInitialise(); TransitionFlag = 1; } } short * data_sptr1; short * data_sptr2; short * data_sptr3; char *slice_buf1; char *slice_buf2; char *slice_buf3; int OneOff = 0; int notch_flag = 0; int i = 0; int h = 0; short bufflen = 1; int buffer[1]; char *slice_buf; short *data_sptr; unsigned channel; long slice, slice1, slice2, slice3, num_slices; 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; 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; short value[1]; //long long slice_value; int slice_value[1]; 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(); } //function prototypes //classFade FadeDataInitialise(classFade FadeData); classSoundFile ReadFileInfo(classSoundFile Sound, FILE * wav_file); classSoundFile LoadFileStream(classSoundFile FileInfo, string filename); void Play_WaveFile(FILE * my_wav, WAV_FILE_STRUCT FileInfo); void Play_WaveFileDual(FILE * my_wav, WAV_FILE_STRUCT FileInfo); void Play_WaveFileLoop(classSoundFile Sound1, classSoundFile Sound2, FILE* wavfile1, FILE* wavfile2,classPositionIndicators Positions); 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 NotchUpTick.attach(&NotchUpIsr,10.0); NotchFadeIn.LengthSecs = 2; NotchFadeOut.LengthSecs = 2; NotchFadeIn.FadeDataInitialise(); NotchFadeOut.FadeDataInitialise(); classPositionIndicators Positions; slice1 = Positions.notch_start_pts[1]; TransitionFlag = 0; FILE* wavfile1 = fopen("/sd/mydir/SoundDecoder_second/01.wav","rb"); FILE* wavfile2 = fopen("/sd/mydir/SoundDecoder_second/04.wav","rb"); FILE* wavfile3 = fopen("/sd/mydir/SoundDecoder_second/05.wav","rb"); //FILE* wavfile1 = fopen("/sd/mydir/SoundDecoder_second/01.wav","rb"); classSoundFile Sound1; classSoundFile Sound2; classSoundFile Sound4; Sound4 = ReadFileInfo(Sound4, wavfile3); Sound2 = ReadFileInfo(Sound2, wavfile2); Sound1 = ReadFileInfo(Sound1, wavfile1); printf("sizeof(Sound1): %d bytes\n\r",sizeof(Sound1)); NotchingSet.Notch = 1; //Positions.notch_position_indicators[1] = 44; fseek(wavfile1,Positions.notch_start_pts[1],SEEK_SET); fseek(wavfile2,Positions.notch_transitions_start_pts[1],SEEK_SET); printf("about to play wav file\n\r"); Play_WaveFileLoop(Sound1,Sound2, wavfile1,wavfile2, Positions); 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()); /************************************PLAY WAV FILE LOOP*******************/ /************************************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); printf("Wav File Block Align (number of bytes per sample!!: %d\n\r", 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; } //Function to initialise Data for classFade objects. Perhaps move this into a constructor for the class? (class still to be added) /* 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; } */ 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_WaveFileLoop(classSoundFile Sound1, classSoundFile Sound2, FILE *wavfile1, FILE* wavfile2, classPositionIndicators Positions) { while(1) { //might have to change this to a while(1) loop? //Sound1======================================================================================= if( slice1 == (Positions.notch_start_pts[NotchingSet.Notch + 1]) ) { slice1 = Positions.notch_start_pts[NotchingSet.Notch]; fseek(wavfile1,Positions.notch_start_pts[NotchingSet.Notch],SEEK_SET); } fread(Sound1.FileInfo.slice_buf,Sound1.FileInfo.FileFormat.block_align,1,wavfile1); /*OperationsTimer.stop(); printf("It took %d useconds to perform fread operation.\n\r",OperationsTimer.read_us()); Positions.notch_position_indicators[1] = Positions.notch_position_indicators[1] + 2;//i.e. 2 bytes, per sample, for 2-byte (16-bit) data */ data_sptr1=(short *)Sound1.FileInfo.slice_buf; *Sound1.data_sptr = *data_sptr1; //Sound1.data_sptr=(short *)Sound1.FileInfo.slice_buf; // 16 bit samples //============================================================================================= 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 + 2;//increment up by the number of bytes per Audio data sample! Makes sense it is 2 bytes, as this is 16-bit data. slice2 = slice2 + 1; slice3 = slice3 + 1; } }