Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed SDFileSystem_Copy_of_mbed_version I2S
NotchingDemo.cpp
- Committer:
- roryhand
- Date:
- 2019-05-26
- Revision:
- 30:4a8e80b243c4
- Parent:
- 28:6b2353fad12d
- Child:
- 31:0f8c3adf09c3
File content as of revision 30:4a8e80b243c4:
// 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 <stdlib.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#define sample_freq 11025
#pragma import __use_two_region_memory
DigitalOut myled(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalIn NotchUp(p16);
InterruptIn Horn(p16);
Ticker sampletick;
Ticker BellTick;
Ticker EdTick;
Ticker TickFadeOut;
Ticker TestTick;
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
WAV_FILE_STRUCT FileInfo;
short * data_sptr;
//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 ;
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, 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 DualEngineFlag = 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];
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_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;
void flip()
{
led2 = !led2;
}
void isr()
{
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;
}
}
}
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 N2SoundIsr()
{
DualEngineFlag = 1;
fseek(N2Wav,44,SEEK_SET);
fseek(HornWav,44,SEEK_SET);
//TickFadeOut.detach(&N2SoundIsr);//,5.0);
}
void FadeOutIsr()
{
FadeFlag = 1;
fseek(IdleN2Wav,44,SEEK_SET);
fseek(N2Wav,44,SEEK_SET);
fseek(HornWav,44,SEEK_SET);
fseek(FlangeWav,44,SEEK_SET);
}
classFade FadeDataInitialise(classFade FadeData);
//function prototypes
WAV_FILE_STRUCT ReadFileInfo(WAV_FILE_STRUCT FileInfo, FILE * wav_file);
classSoundFile LoadFileStream(classSoundFile FileInfo, string filename);
float FadeOut(void);
void Play_WaveFile(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 Sounds[], Notch_STRUCT NotchingSet);
int main()
{
//LocalFileSystem local("local");
NotchUp.mode(PullUp);
WAV_FILE_STRUCT WavInfo_Startup;
WAV_FILE_STRUCT WavInfo_Idle;
pc.printf("Beginning of program\n");
FILE *StartupWav;
FILE *IdleWav;
DIR *dir;
dirent *ent;
int iterator = 0;
printf("Directory is about to be opened? Still dont really Get this concept\n\r");
//if i try to open folder SoundDecoder2, it gets stuck after file 13... WHY
if ((dir = opendir ("/sd/mydir/SoundDecoder_second")) != NULL) {
// print all the files and directories within directory
while ((ent = readdir (dir)) != NULL) {
//printf("%s\r\n",string(ent->d_name));
filename[iterator] = (ent->d_name);
printf("Filename: %s\n\r",filename[iterator]);
//printf("%s\r\n",filename[iterator]);
iterator = iterator + 1;
//printf("%s\r\n", ent->d_name);
//filenames.push_back(string(dirp->d_name));
}
closedir (dir);
}
timer_open.reset();
timer_open.start();
StartupWav = fopen("/sd/mydir/Startup.wav","rb");
timer_open.reset();
printf("It took %d useconds to open file\n\r",timer_open.read_us());
//const char* folder2 = folder.c_str();
timer_open.reset();
timer_open.start();
fclose(StartupWav);
timer_open.stop();
printf("It took %d useconds to close file\n\r",timer_open.read_us());
string FileName;
//vector <classSoundFile> Sound (27);
const char* FOLDER;
int aaa = 0;
string folder = RootFolder + filename[0];
FOLDER = folder.c_str();
WAV_FILE_STRUCT WavStruct21;
WAV_FILE_STRUCT WavStruct22;
HeyWav = fopen("/sd/mydir/645Engine/EX_FlangeJoint1_11k_minus12dB.wav","rb");
if(HeyWav == NULL){
printf("Cannot Open HeyWav\n\r");
}
printf("About to Read HeyWav\n\r");
WavInfo_Hey = ReadFileInfo(WavInfo_Hey,HeyWav);
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 starrted!");
i2s.start();
sampletick.attach(&isr,1.0/sampling_freq); //1/16000
StartupWav = fopen("/sd/mydir/Startup.wav","rb");
fseek(StartupWav,44,SEEK_SET);
WavInfo_Startup = ReadFileInfo(WavInfo_Startup,StartupWav);
printf("about to play wav file\n\r");
Play_WaveFile(StartupWav,WavInfo_Startup);
printf("finished playing Wav file\n\r");
/************************************PLAY WAV FILE LOOP*******************/
//Play_WaveFileLoop(Sound, NotchingSet);
/************************************END OF PLAY WAV FILE LOOP*************/
fseek(IdleN2Wav,44,SEEK_SET);//reset for use in the Loop code
slice = 0;
fseek(IdleWav,44,SEEK_SET);
NotchingSet.Notch = 1;
//Play_WaveFileLoop(IdleWav, WavInfo_Idle);
i2s.stop();
}
WAV_FILE_STRUCT ReadFileInfo(WAV_FILE_STRUCT FileInfo, FILE * wav_file)
{
fseek(wav_file,20,SEEK_SET);
printf("We have just seeked through this file\n\r");
fread(&FileInfo.FileFormat,sizeof(FileInfo.FileFormat),1,wav_file);
//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;
}
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_WaveFileLoop(FILE * my_wav, WAV_FILE_STRUCT FileInfo)//(classSoundFile Sounds)
void Play_WaveFileLoop(classSoundFile Sounds[], Notch_STRUCT NotchingSet)
{
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( slice == (Sounds[NotchingSet.Notch].FileInfo.num_slices-1) ) {
slice = 0;
fseek(Sounds[NotchingSet.Notch].FileInfo.WavFile,44,SEEK_SET);
}
//fread(FileInfo.slice_buf,FileInfo.FileFormat.block_align,1,my_wav);
data_sptr=(short *)Sounds[NotchingSet.Notch].FileInfo.slice_buf; // 16 bit samples
//make sure we are reading in the correct "notch" here
if(FadeFlag) {
if(feof(Sounds[NotchingSet.Notch].FileInfo.WavFile)) {
fseek(Sounds[NotchingSet.Notch].FileInfo.WavFile,44,SEEK_SET);
}
//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;
NotchFadeOut.FadeCoeff = NotchFadeOut.FadeOut();//compute new FadeOut Coeff value
//Read in the notch transition file for transitioning up
fread(Sounds[NotchingSet.NotchTransUp].FileInfo.slice_buf,Sounds[NotchingSet.NotchTransUp].FileInfo.FileFormat.block_align,1,Sounds[NotchingSet.NotchTransUp].FileInfo.WavFile);
Sounds[NotchingSet.NotchTransUp].data_sptr = (short*)Sounds[NotchingSet.NotchTransUp].FileInfo.slice_buf;
if( ((Sounds[NotchingSet.NotchTransUp].FileInfo.FileData.subchunk2_size) - NotchFadeIn.Length - 2*11025) <= (ftell(Sounds[NotchingSet.NotchTransUp].FileInfo.WavFile) + 44))
//if( (WavInfo_IdleN2.FileData.subchunk2_size)/8 <=ftell(IdleN2Wav) )
{
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;
if( (ftell(Sounds[NotchingSet.NotchTransUp].FileInfo.WavFile) + 44) >= Sounds[NotchingSet.NotchTransUp].FileInfo.FileData.subchunk2_size ) {
//need to explicitly test if this notation/syntax works for pointers....
*Sounds[NotchingSet.Notch].data_sptr = *Sounds[NotchingSet.Notch].data_sptr*NotchFadeIn.FadeCoeff;
} else {
*Sounds[NotchingSet.Notch-1].data_sptr = *Sounds[NotchingSet.Notch - 1].data_sptr*NotchFadeOut.FadeCoeff + *Sounds[NotchingSet.NotchTransUp].data_sptr + *Sounds[NotchingSet.Notch].data_sptr*NotchFadeIn.FadeCoeff;// + *data_sptr_N2;
}
} else {
*Sounds[NotchingSet.Notch-1].data_sptr = *Sounds[NotchingSet.Notch-1].data_sptr*NotchFadeOut.FadeCoeff + *Sounds[NotchingSet.NotchTransUp].data_sptr;
}
}
/********************END OF DATA ASSIGNMENT SECTION*************************************************/
for (channel=0; channel<Sounds[NotchingSet.Notch].FileInfo.FileFormat.num_channels; channel++) {
switch (Sounds[NotchingSet.Notch].FileInfo.FileFormat.sig_bps) {
case 16:
if(flag1 == 1) {
Buffer1[place_hold1] = Sounds[NotchingSet.Notch].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] = Sounds[NotchingSet.Notch].data_sptr[channel];
place_hold2 = place_hold2 + 1;
if(place_hold2 >= BufferLen) {
while(1) {
if(flag2 == 0) {
break;
}
}
}
}
}
}
slice = slice + 1;
}
}