old demo that i want to try in mbed studio

Dependencies:   mbed SDFileSystem_Copy_of_mbed_version I2S

Committer:
roryhand
Date:
Wed Oct 02 20:37:50 2019 +0000
Revision:
88:76a4563ee9c7
Parent:
62:ac63dfe15ef9
Child:
89:936f37f90cc2
WIP - branch.  Have performed tests where I have the exact same number of classSoundFile objects, but have no issues unlike in the top branch.  Makes absolutely no sense whatsoever.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
roryhand 1:aac37edee302 1
roryhand 0:e89d7a0bfa3b 2 // 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.
roryhand 0:e89d7a0bfa3b 3 //however if i use the write function with 4 arguments (as opposed to just 1 argument) then it doesnt work
roryhand 0:e89d7a0bfa3b 4 //only works with the 1 argument version!!!
roryhand 0:e89d7a0bfa3b 5
roryhand 0:e89d7a0bfa3b 6
roryhand 0:e89d7a0bfa3b 7 //THIS VERSION WORKED, CHANGED SOME THINGS, THEN CHANGED THEM BACK. NOW IT NO LONGER WORKS!!!!
roryhand 0:e89d7a0bfa3b 8 #include "mbed.h"
roryhand 0:e89d7a0bfa3b 9 #include "math.h"
roryhand 1:aac37edee302 10 #include "I2S.h"
roryhand 1:aac37edee302 11 #include "SDFileSystem.h"
roryhand 0:e89d7a0bfa3b 12 #include "wm8731_Config_setup.h"
roryhand 0:e89d7a0bfa3b 13 #include "WOLFSON_config_consts.h"
roryhand 54:606a83fff291 14 //#include "BlockDevice.h"
roryhand 37:a563899ac0df 15 #include <string>
roryhand 0:e89d7a0bfa3b 16 #include <stdlib.h>
roryhand 27:a378f1f937ee 17 #include <fstream>
roryhand 27:a378f1f937ee 18 #include <iostream>
roryhand 0:e89d7a0bfa3b 19 #include <vector>
roryhand 0:e89d7a0bfa3b 20 #include <string>
roryhand 0:e89d7a0bfa3b 21 #define sample_freq 11025
roryhand 1:aac37edee302 22 #pragma import __use_two_region_memory
roryhand 54:606a83fff291 23 //BlockDevice *bd = BlockDevice::get_default_instance();
roryhand 54:606a83fff291 24 //#include "LittleFileSystem.h"
roryhand 54:606a83fff291 25 //LittleFileSystem fs("fs");
roryhand 54:606a83fff291 26
roryhand 0:e89d7a0bfa3b 27 DigitalOut myled(LED1);
roryhand 0:e89d7a0bfa3b 28 DigitalOut led2(LED2);
roryhand 0:e89d7a0bfa3b 29 DigitalOut led3(LED3);
roryhand 0:e89d7a0bfa3b 30 DigitalIn NotchUp(p16);
roryhand 38:3b4c05af5f36 31 DigitalIn NotchDown(p17);//check the pin!!! Dont know if this will actually work...
roryhand 0:e89d7a0bfa3b 32 InterruptIn Horn(p16);
roryhand 0:e89d7a0bfa3b 33 Ticker sampletick;
roryhand 45:0e8e1f2ec5d2 34
roryhand 0:e89d7a0bfa3b 35 Ticker TickFadeOut;
roryhand 0:e89d7a0bfa3b 36 Timer t;
roryhand 0:e89d7a0bfa3b 37 Timer t2;
roryhand 3:6169aeeaeeb4 38 Timer NotchTimer;
roryhand 27:a378f1f937ee 39 Timer timer_open;
roryhand 0:e89d7a0bfa3b 40
roryhand 0:e89d7a0bfa3b 41 Serial pc(USBTX, USBRX); // tx, rx //FOR DEBUGGING PROGRAM USING GNU SCREEN
roryhand 0:e89d7a0bfa3b 42 DigitalOut cs(p8);
roryhand 0:e89d7a0bfa3b 43 I2S i2s(I2S_TRANSMIT, p5, p6, p7);
roryhand 0:e89d7a0bfa3b 44 SDFileSystem sd(p11, p12, p13, p8, "sd"); // the new pinout that i am using
roryhand 0:e89d7a0bfa3b 45
roryhand 0:e89d7a0bfa3b 46
roryhand 0:e89d7a0bfa3b 47 /*struct A {
roryhand 0:e89d7a0bfa3b 48 int data;
roryhand 0:e89d7a0bfa3b 49 B b;
roryhand 0:e89d7a0bfa3b 50 };*/
roryhand 0:e89d7a0bfa3b 51
roryhand 0:e89d7a0bfa3b 52
roryhand 0:e89d7a0bfa3b 53
roryhand 0:e89d7a0bfa3b 54
roryhand 0:e89d7a0bfa3b 55 class classFade
roryhand 0:e89d7a0bfa3b 56 {
roryhand 1:aac37edee302 57 public:
roryhand 1:aac37edee302 58
roryhand 0:e89d7a0bfa3b 59 float powerval;
roryhand 0:e89d7a0bfa3b 60 float FadeIteration;
roryhand 0:e89d7a0bfa3b 61 float DecayFactor;
roryhand 0:e89d7a0bfa3b 62 float Denom;
roryhand 0:e89d7a0bfa3b 63 float FadeCoeff;
roryhand 0:e89d7a0bfa3b 64 float Natural_Exp;
roryhand 0:e89d7a0bfa3b 65 int LengthSecs;
roryhand 0:e89d7a0bfa3b 66 int Length;
roryhand 1:aac37edee302 67
roryhand 0:e89d7a0bfa3b 68 //member Functions
roryhand 0:e89d7a0bfa3b 69 float FadeOut(void)
roryhand 0:e89d7a0bfa3b 70 {
roryhand 0:e89d7a0bfa3b 71 powerval = -FadeIteration/Denom;
roryhand 1:aac37edee302 72 if (FadeIteration >=Length) {
roryhand 1:aac37edee302 73 FadeCoeff = 0;
roryhand 1:aac37edee302 74
roryhand 1:aac37edee302 75 } else {
roryhand 0:e89d7a0bfa3b 76 FadeCoeff = (Length - FadeIteration)/Length;
roryhand 0:e89d7a0bfa3b 77 }
roryhand 1:aac37edee302 78 FadeIteration = FadeIteration + 1;
roryhand 1:aac37edee302 79 return FadeCoeff;
roryhand 1:aac37edee302 80
roryhand 1:aac37edee302 81 }
roryhand 1:aac37edee302 82 float FadeIn(void)
roryhand 0:e89d7a0bfa3b 83 {
roryhand 0:e89d7a0bfa3b 84 powerval = FadeIteration/Denom;
roryhand 1:aac37edee302 85 if (FadeIteration >=Length) {
roryhand 1:aac37edee302 86 FadeCoeff = 1;
roryhand 1:aac37edee302 87
roryhand 1:aac37edee302 88 } else {
roryhand 0:e89d7a0bfa3b 89 FadeCoeff = FadeIteration/Length;
roryhand 0:e89d7a0bfa3b 90 }
roryhand 0:e89d7a0bfa3b 91
roryhand 1:aac37edee302 92 FadeIteration = FadeIteration + 1;
roryhand 1:aac37edee302 93 return FadeCoeff;
roryhand 1:aac37edee302 94
roryhand 1:aac37edee302 95 }
roryhand 0:e89d7a0bfa3b 96 };
roryhand 0:e89d7a0bfa3b 97
roryhand 0:e89d7a0bfa3b 98 classFade IdleFadeOut;
roryhand 0:e89d7a0bfa3b 99 classFade N2FadeIn;
roryhand 0:e89d7a0bfa3b 100
roryhand 2:957d3b2afff4 101 classFade NotchFadeOut;
roryhand 2:957d3b2afff4 102 classFade NotchFadeIn;
roryhand 2:957d3b2afff4 103
roryhand 0:e89d7a0bfa3b 104
roryhand 0:e89d7a0bfa3b 105
roryhand 0:e89d7a0bfa3b 106
roryhand 0:e89d7a0bfa3b 107
roryhand 1:aac37edee302 108 typedef struct uFMT_STRUCT {
roryhand 1:aac37edee302 109 short comp_code;
roryhand 1:aac37edee302 110 short num_channels;
roryhand 1:aac37edee302 111 unsigned sample_rate;
roryhand 1:aac37edee302 112 unsigned avg_Bps;
roryhand 1:aac37edee302 113 short block_align;
roryhand 1:aac37edee302 114 short sig_bps;
roryhand 0:e89d7a0bfa3b 115 } FMT_STRUCT;
roryhand 0:e89d7a0bfa3b 116
roryhand 1:aac37edee302 117 typedef struct uNotch_STRUCT {
roryhand 1:aac37edee302 118 short Notch;
roryhand 1:aac37edee302 119 short NotchTransUp;
roryhand 1:aac37edee302 120 short NotchTransDown;
roryhand 1:aac37edee302 121 short NotchDirection;
roryhand 1:aac37edee302 122 } Notch_STRUCT;
roryhand 1:aac37edee302 123
roryhand 0:e89d7a0bfa3b 124 typedef struct uDATA_STRUCT {
roryhand 1:aac37edee302 125 unsigned subchunk2_ID;
roryhand 1:aac37edee302 126 unsigned subchunk2_size;
roryhand 1:aac37edee302 127 char * data_buf;
roryhand 1:aac37edee302 128 } DATA_STRUCT;
roryhand 0:e89d7a0bfa3b 129
roryhand 1:aac37edee302 130 typedef struct uWAV_FILE_STRUCT {
roryhand 1:aac37edee302 131 FILE *WavFile;
roryhand 1:aac37edee302 132 int id_number;
roryhand 1:aac37edee302 133 char *slice_buf;
roryhand 1:aac37edee302 134 int num_slices;
roryhand 1:aac37edee302 135 FMT_STRUCT FileFormat;
roryhand 1:aac37edee302 136 DATA_STRUCT FileData;
roryhand 0:e89d7a0bfa3b 137 } WAV_FILE_STRUCT;
roryhand 0:e89d7a0bfa3b 138
roryhand 0:e89d7a0bfa3b 139 /*typedef struct uWAV_FILE_STRUCT{
roryhand 1:aac37edee302 140 FILE* WavFile;
roryhand 1:aac37edee302 141
roryhand 1:aac37edee302 142
roryhand 0:e89d7a0bfa3b 143 }WAV_FILE_STRUCT;*/
roryhand 0:e89d7a0bfa3b 144 class classSoundFile
roryhand 0:e89d7a0bfa3b 145 {
roryhand 1:aac37edee302 146 public:
roryhand 1:aac37edee302 147
roryhand 44:a9e84d333a6a 148 //add a class constructor at some point in the future (perform tests in visual studio)
roryhand 1:aac37edee302 149 WAV_FILE_STRUCT FileInfo;
roryhand 2:957d3b2afff4 150 short * data_sptr;
roryhand 36:569ec4335f92 151 string file_location;
roryhand 1:aac37edee302 152 //classSoundFile(string filename);//this is the constructor
roryhand 1:aac37edee302 153 //string filename;
roryhand 0:e89d7a0bfa3b 154 };
roryhand 0:e89d7a0bfa3b 155
roryhand 1:aac37edee302 156 //class constructor;
roryhand 1:aac37edee302 157 /*classSoundFile::classSoundFile(string filename)
roryhand 1:aac37edee302 158 {
roryhand 1:aac37edee302 159 //Declare RootFolder and the directory for the appropriate file.
roryhand 1:aac37edee302 160 //How we index into filename[] from the outside of this class is another
roryhand 1:aac37edee302 161 //issue...
roryhand 1:aac37edee302 162 FileInfo.WavFile = fopen("/sd/mydir/645Engine/Startup.wav","rb");
roryhand 1:aac37edee302 163 fseek(FileInfo.WavFile,20,SEEK_SET);
roryhand 1:aac37edee302 164 fread(&FileInfo.FileFormat,sizeof(FileInfo.FileFormat),1,FileInfo.WavFile);
roryhand 1:aac37edee302 165 printf("wav_format.sample_rate: %d\n\r",FileInfo.FileFormat.sample_rate);
roryhand 1:aac37edee302 166 fread(&FileInfo.FileData,sizeof(FileInfo.FileData),1,FileInfo.WavFile);
roryhand 1:aac37edee302 167 printf("wav_data.subchunk2_size: %d\n\r",FileInfo.FileData.subchunk2_size);
roryhand 1:aac37edee302 168 FileInfo.slice_buf = ( char *)malloc(FileInfo.FileFormat.block_align);
roryhand 1:aac37edee302 169 fread(FileInfo.slice_buf,FileInfo.FileFormat.block_align,1,FileInfo.WavFile); //This isnt actually required, its just a test
roryhand 1:aac37edee302 170 FileInfo.num_slices = FileInfo.FileData.subchunk2_size/FileInfo.FileFormat.block_align;
roryhand 1:aac37edee302 171 }*/
roryhand 54:606a83fff291 172 int OneOff = 0;
roryhand 38:3b4c05af5f36 173 int notch_flag = 0;
roryhand 0:e89d7a0bfa3b 174 int i = 0;
roryhand 0:e89d7a0bfa3b 175 int h = 0;
roryhand 0:e89d7a0bfa3b 176 short bufflen = 1;
roryhand 0:e89d7a0bfa3b 177 int buffer[1];
roryhand 1:aac37edee302 178 int AudioFormat, NumChannels, SampleRate, BitsPerSample ;
roryhand 0:e89d7a0bfa3b 179 char *slice_buf;
roryhand 0:e89d7a0bfa3b 180 short *data_sptr;
roryhand 0:e89d7a0bfa3b 181 short *data_sptr_horn;
roryhand 0:e89d7a0bfa3b 182 short *data_sptr_IdleN2;
roryhand 0:e89d7a0bfa3b 183 short * data_sptr_bell;
roryhand 0:e89d7a0bfa3b 184 short * data_sptr_N2;
roryhand 0:e89d7a0bfa3b 185 short * data_sptr_Flange;
roryhand 0:e89d7a0bfa3b 186 unsigned char *data_bptr;
roryhand 0:e89d7a0bfa3b 187 int *data_wptr;
roryhand 0:e89d7a0bfa3b 188 unsigned channel;
roryhand 45:0e8e1f2ec5d2 189 long slice, slice1, slice2, slice3, num_slices;
roryhand 0:e89d7a0bfa3b 190 int verbosity = 0;
roryhand 0:e89d7a0bfa3b 191 int verbosity2 = 0;
roryhand 0:e89d7a0bfa3b 192 int verbosity3 = 0;
roryhand 0:e89d7a0bfa3b 193 int verbosity4 = 0;
roryhand 0:e89d7a0bfa3b 194 int verbosity5 = 0;
roryhand 0:e89d7a0bfa3b 195 int interrupt_condition = 1;
roryhand 0:e89d7a0bfa3b 196 int sampling_freq = 11025;
roryhand 0:e89d7a0bfa3b 197 const int BufferLen = 2000;
roryhand 0:e89d7a0bfa3b 198 short Buffer1[BufferLen];
roryhand 0:e89d7a0bfa3b 199 short Buffer2[BufferLen];
roryhand 0:e89d7a0bfa3b 200 short place_hold1 = 0;
roryhand 0:e89d7a0bfa3b 201 short place_hold2 = 0;
roryhand 0:e89d7a0bfa3b 202
roryhand 3:6169aeeaeeb4 203
roryhand 3:6169aeeaeeb4 204 string FOLDER;
roryhand 3:6169aeeaeeb4 205 string RootFolder = "/sd/mydir/SoundDecoder/";
roryhand 3:6169aeeaeeb4 206 string filename[25];
roryhand 3:6169aeeaeeb4 207 classSoundFile Sound[22];
roryhand 3:6169aeeaeeb4 208
roryhand 0:e89d7a0bfa3b 209 volatile int flag1 = 1;
roryhand 0:e89d7a0bfa3b 210 volatile int flag2 = 0;
roryhand 0:e89d7a0bfa3b 211 volatile int flag3 = 1;
roryhand 0:e89d7a0bfa3b 212 volatile int flag4 = 0;
roryhand 0:e89d7a0bfa3b 213 int FLAGBUFF1 = 0;
roryhand 0:e89d7a0bfa3b 214 int FLAGBUFF2 = 0;
roryhand 0:e89d7a0bfa3b 215 int BellFlag = 0;
roryhand 0:e89d7a0bfa3b 216 int BellFlag2 = 0;
roryhand 0:e89d7a0bfa3b 217 int FadeFlag = 0;
roryhand 54:606a83fff291 218 int BlockFlag = 0;
roryhand 32:6ee488c97dcc 219 int FileSwitchFlag = 0;
roryhand 0:e89d7a0bfa3b 220
roryhand 0:e89d7a0bfa3b 221
roryhand 0:e89d7a0bfa3b 222 short value[1];
roryhand 0:e89d7a0bfa3b 223 FILE *HornWav;
roryhand 0:e89d7a0bfa3b 224 FILE *edsheeran_wav;
roryhand 0:e89d7a0bfa3b 225 FILE *Startup_wav;
roryhand 0:e89d7a0bfa3b 226 FILE *IdleN2Wav;
roryhand 0:e89d7a0bfa3b 227 FILE *N2Wav;
roryhand 0:e89d7a0bfa3b 228 FILE *BellWav;
roryhand 0:e89d7a0bfa3b 229 FILE *FlangeWav;
roryhand 16:5e3420d0509b 230 FILE *HeyWav;
roryhand 0:e89d7a0bfa3b 231 //long long slice_value;
roryhand 0:e89d7a0bfa3b 232 int slice_value[1];
roryhand 0:e89d7a0bfa3b 233
roryhand 0:e89d7a0bfa3b 234
roryhand 45:0e8e1f2ec5d2 235 FILE *wavfile1;
roryhand 45:0e8e1f2ec5d2 236 FILE *wavfile2;
roryhand 45:0e8e1f2ec5d2 237 FILE *wavfile3;
roryhand 45:0e8e1f2ec5d2 238
roryhand 45:0e8e1f2ec5d2 239 classSoundFile Sound1;
roryhand 45:0e8e1f2ec5d2 240 classSoundFile Sound2;
roryhand 45:0e8e1f2ec5d2 241 classSoundFile Sound3;
roryhand 45:0e8e1f2ec5d2 242
roryhand 0:e89d7a0bfa3b 243 WAV_FILE_STRUCT WavInfo_Horn;
roryhand 0:e89d7a0bfa3b 244 WAV_FILE_STRUCT WavInfo_IdleN2;
roryhand 0:e89d7a0bfa3b 245 WAV_FILE_STRUCT WavInfo_N2;
roryhand 0:e89d7a0bfa3b 246 WAV_FILE_STRUCT WavInfo_Bell;
roryhand 0:e89d7a0bfa3b 247 WAV_FILE_STRUCT WavInfo_Flange;
roryhand 20:9cc7d825c07b 248 WAV_FILE_STRUCT WavInfo_Hey;
roryhand 32:6ee488c97dcc 249 WAV_FILE_STRUCT WavInfo_N3;
roryhand 16:5e3420d0509b 250 WAV_FILE_STRUCT WavInfo_Frustration;
roryhand 0:e89d7a0bfa3b 251 Ticker flipper;
roryhand 0:e89d7a0bfa3b 252 char * slice_buf_bell;
roryhand 0:e89d7a0bfa3b 253 char * slice_buf_ed;
roryhand 0:e89d7a0bfa3b 254 char * slice_buf_startup;
roryhand 0:e89d7a0bfa3b 255 char * slice_buf_N2;
roryhand 1:aac37edee302 256 Notch_STRUCT NotchingSet;
roryhand 0:e89d7a0bfa3b 257 //test
roryhand 0:e89d7a0bfa3b 258 //short *data_sptr_bell = 0;
roryhand 0:e89d7a0bfa3b 259 short *data_sptr_ed = 0;
roryhand 0:e89d7a0bfa3b 260 short *data_sptr_startup = 0;
roryhand 32:6ee488c97dcc 261 FILE *StartupWav;
roryhand 32:6ee488c97dcc 262 FILE *N3Wav;
roryhand 33:a75f0a30fbdc 263 int temp = 0;
roryhand 1:aac37edee302 264 void flip()
roryhand 1:aac37edee302 265 {
roryhand 0:e89d7a0bfa3b 266 led2 = !led2;
roryhand 0:e89d7a0bfa3b 267 }
roryhand 0:e89d7a0bfa3b 268
roryhand 0:e89d7a0bfa3b 269
roryhand 0:e89d7a0bfa3b 270 void isr()
roryhand 0:e89d7a0bfa3b 271 {
roryhand 54:606a83fff291 272 //timer_interrupt.start();
roryhand 1:aac37edee302 273 if(flag1 == 0) {
roryhand 0:e89d7a0bfa3b 274 value[0] = Buffer1[place_hold1]>>4;
roryhand 0:e89d7a0bfa3b 275 i2s.write(value,1);//Send next PWM value to amp
roryhand 0:e89d7a0bfa3b 276 place_hold1 = place_hold1 + 1;
roryhand 1:aac37edee302 277 if( (place_hold1 >= BufferLen)) {
roryhand 0:e89d7a0bfa3b 278 led2 = !led2;
roryhand 0:e89d7a0bfa3b 279 place_hold1 = 0;
roryhand 0:e89d7a0bfa3b 280 place_hold2 = 0;
roryhand 0:e89d7a0bfa3b 281 flag1 = 1;
roryhand 0:e89d7a0bfa3b 282 flag2 = 0;
roryhand 1:aac37edee302 283 }
roryhand 1:aac37edee302 284 } else if(flag2 == 0) {
roryhand 0:e89d7a0bfa3b 285 value[0] = Buffer2[place_hold2]>>4;
roryhand 0:e89d7a0bfa3b 286 i2s.write(value,1);//Send next PWM value to amp
roryhand 0:e89d7a0bfa3b 287 place_hold2 = place_hold2 + 1;
roryhand 1:aac37edee302 288 if( (place_hold2 >= BufferLen) ) {
roryhand 0:e89d7a0bfa3b 289 led2 = !led2;
roryhand 0:e89d7a0bfa3b 290 place_hold1 = 0;
roryhand 0:e89d7a0bfa3b 291 place_hold2 = 0;
roryhand 0:e89d7a0bfa3b 292 flag1 = 0;
roryhand 1:aac37edee302 293 flag2 = 1;
roryhand 0:e89d7a0bfa3b 294 FLAGBUFF2 = 0;
roryhand 1:aac37edee302 295 }
roryhand 0:e89d7a0bfa3b 296 }
roryhand 54:606a83fff291 297
roryhand 54:606a83fff291 298 //timer_interrupt.stop();
roryhand 54:606a83fff291 299
roryhand 0:e89d7a0bfa3b 300 }
roryhand 1:aac37edee302 301
roryhand 0:e89d7a0bfa3b 302 void horn_sound()
roryhand 0:e89d7a0bfa3b 303 {
roryhand 0:e89d7a0bfa3b 304 BellFlag = 1;
roryhand 0:e89d7a0bfa3b 305 fseek(HornWav,44,SEEK_SET);
roryhand 0:e89d7a0bfa3b 306 fseek(BellWav,44,SEEK_SET);
roryhand 0:e89d7a0bfa3b 307 fseek(FlangeWav,44,SEEK_SET);
roryhand 0:e89d7a0bfa3b 308 fseek(N2Wav,44,SEEK_SET);
roryhand 0:e89d7a0bfa3b 309 }
roryhand 0:e89d7a0bfa3b 310
roryhand 0:e89d7a0bfa3b 311
roryhand 32:6ee488c97dcc 312 void FileSwitch_isr()
roryhand 0:e89d7a0bfa3b 313 {
roryhand 32:6ee488c97dcc 314 FileSwitchFlag = 1;
roryhand 33:a75f0a30fbdc 315 temp = 1;
roryhand 33:a75f0a30fbdc 316 /*FILE* testwav = fopen("/sd/mydir/SoundDecoder_second/05.wav","rb");
roryhand 33:a75f0a30fbdc 317 if(N3Wav == NULL) {
roryhand 33:a75f0a30fbdc 318 printf("Cannot Open testwav\n\r");
roryhand 33:a75f0a30fbdc 319 }
roryhand 33:a75f0a30fbdc 320 */
roryhand 32:6ee488c97dcc 321 fseek(N3Wav,44,SEEK_SET);
roryhand 32:6ee488c97dcc 322 // fclose(StartupWav);
roryhand 0:e89d7a0bfa3b 323 }
roryhand 0:e89d7a0bfa3b 324
roryhand 0:e89d7a0bfa3b 325
roryhand 0:e89d7a0bfa3b 326 void FadeOutIsr()
roryhand 0:e89d7a0bfa3b 327 {
roryhand 1:aac37edee302 328 FadeFlag = 1;
roryhand 54:606a83fff291 329 OneOff = 1;
roryhand 54:606a83fff291 330 /*
roryhand 54:606a83fff291 331 wavfile2 = fopen("/sd/mydir/SoundDecoder_second/09.wav","rb");
roryhand 54:606a83fff291 332 if(wavfile2 == NULL) {
roryhand 54:606a83fff291 333 printf("Cannot Open wavfile2\n\r");
roryhand 54:606a83fff291 334 }
roryhand 54:606a83fff291 335 */
roryhand 54:606a83fff291 336
roryhand 54:606a83fff291 337
roryhand 54:606a83fff291 338
roryhand 54:606a83fff291 339 /*
roryhand 54:606a83fff291 340 wavfile3 = fopen("/sd/mydir/SoundDecoder_second/02.wav","rb");
roryhand 54:606a83fff291 341 if(wavfile3 == NULL) {
roryhand 54:606a83fff291 342 printf("Cannot Open wavfile3\n\r");
roryhand 54:606a83fff291 343 }
roryhand 54:606a83fff291 344 */
roryhand 54:606a83fff291 345 /*
roryhand 54:606a83fff291 346 FILE* wavfile4 = fopen("/sd/mydir/SoundDecoder_second/04.wav","rb");
roryhand 54:606a83fff291 347 if(wavfile4 == NULL) {
roryhand 54:606a83fff291 348 printf("Cannot Open wavfile3\n\r");
roryhand 54:606a83fff291 349 }
roryhand 54:606a83fff291 350 */
roryhand 54:606a83fff291 351 //fseek(wavfile2,44,SEEK_SET);
roryhand 54:606a83fff291 352 //fseek(wavfile3,44,SEEK_SET);
roryhand 54:606a83fff291 353 slice2 = 0;
roryhand 54:606a83fff291 354 slice3 = 0;
roryhand 54:606a83fff291 355 BlockFlag = 1;
roryhand 47:9334a8f54997 356 //printf("We are inside the FadeOut interrupt routine\n\r");
roryhand 1:aac37edee302 357
roryhand 0:e89d7a0bfa3b 358 }
roryhand 38:3b4c05af5f36 359
roryhand 38:3b4c05af5f36 360
roryhand 38:3b4c05af5f36 361
roryhand 38:3b4c05af5f36 362
roryhand 0:e89d7a0bfa3b 363
roryhand 3:6169aeeaeeb4 364 classFade FadeDataInitialise(classFade FadeData);
roryhand 3:6169aeeaeeb4 365
roryhand 3:6169aeeaeeb4 366
roryhand 3:6169aeeaeeb4 367
roryhand 3:6169aeeaeeb4 368
roryhand 3:6169aeeaeeb4 369
roryhand 3:6169aeeaeeb4 370 //function prototypes
roryhand 45:0e8e1f2ec5d2 371 classSoundFile ReadFileInfo(classSoundFile Sound, FILE * wav_file);
roryhand 3:6169aeeaeeb4 372 classSoundFile LoadFileStream(classSoundFile FileInfo, string filename);
roryhand 3:6169aeeaeeb4 373
roryhand 3:6169aeeaeeb4 374 float FadeOut(void);
roryhand 3:6169aeeaeeb4 375 void Play_WaveFile(FILE * my_wav, WAV_FILE_STRUCT FileInfo);
roryhand 32:6ee488c97dcc 376 void Play_WaveFileDual(FILE * my_wav, WAV_FILE_STRUCT FileInfo);
roryhand 3:6169aeeaeeb4 377 //void Play_WaveFileLoop(FILE * my_wav, WAV_FILE_STRUCT FileInfo);
roryhand 3:6169aeeaeeb4 378 //*********************INTERRUPT ROUTINE FOR NOTCHING***************************
roryhand 0:e89d7a0bfa3b 379
roryhand 1:aac37edee302 380 void NotchUpIsr()
roryhand 0:e89d7a0bfa3b 381 {
roryhand 1:aac37edee302 382 if(1 <= NotchingSet.Notch < 8) {
roryhand 1:aac37edee302 383
roryhand 1:aac37edee302 384 NotchingSet.Notch = NotchingSet.Notch + 1;
roryhand 1:aac37edee302 385 NotchingSet.NotchTransUp = NotchingSet.Notch + 7;
roryhand 1:aac37edee302 386 NotchingSet.NotchDirection = 1;
roryhand 3:6169aeeaeeb4 387 FadeDataInitialise(NotchFadeIn);
roryhand 3:6169aeeaeeb4 388 FadeDataInitialise(NotchFadeOut);
roryhand 1:aac37edee302 389 }
roryhand 0:e89d7a0bfa3b 390 }
roryhand 0:e89d7a0bfa3b 391
roryhand 0:e89d7a0bfa3b 392 void NotchDownIsr()
roryhand 0:e89d7a0bfa3b 393 {
roryhand 1:aac37edee302 394 if(1 < NotchingSet.Notch <= 8) {
roryhand 1:aac37edee302 395 NotchingSet.Notch = NotchingSet.Notch - 1;
roryhand 1:aac37edee302 396 NotchingSet.NotchTransDown = NotchingSet.Notch + 15;;
roryhand 1:aac37edee302 397 NotchingSet.NotchDirection = 0;
roryhand 3:6169aeeaeeb4 398 FadeDataInitialise(NotchFadeIn);
roryhand 3:6169aeeaeeb4 399 FadeDataInitialise(NotchFadeOut);
roryhand 1:aac37edee302 400 }
roryhand 1:aac37edee302 401 }
roryhand 0:e89d7a0bfa3b 402
roryhand 0:e89d7a0bfa3b 403
roryhand 3:6169aeeaeeb4 404 /**********************END OF INTERRUPT ROUTINE FOR NOTCHING*******************/
roryhand 3:6169aeeaeeb4 405
roryhand 3:6169aeeaeeb4 406
roryhand 1:aac37edee302 407
roryhand 0:e89d7a0bfa3b 408
roryhand 3:6169aeeaeeb4 409
roryhand 3:6169aeeaeeb4 410
roryhand 44:a9e84d333a6a 411 void Play_WaveFileLoop(classSoundFile Sound1, classSoundFile Sound2, classSoundFile Sound3, FILE* wavfile1,FILE* wavfile2, FILE* wavfile3);
roryhand 1:aac37edee302 412 int main()
roryhand 1:aac37edee302 413 {
roryhand 44:a9e84d333a6a 414
roryhand 0:e89d7a0bfa3b 415 NotchUp.mode(PullUp);
roryhand 38:3b4c05af5f36 416 NotchDown.mode(PullUp);
roryhand 0:e89d7a0bfa3b 417
roryhand 0:e89d7a0bfa3b 418 pc.printf("Beginning of program\n");
roryhand 32:6ee488c97dcc 419
roryhand 13:8e93396a27c5 420
roryhand 13:8e93396a27c5 421
roryhand 4:55fbbb049bae 422
roryhand 25:5336e1cf38d6 423 printf("Do we even get to this stupid bloody point\n\r");
roryhand 0:e89d7a0bfa3b 424 //Populate our class instances with some data (is there an implicit way to do this?)
roryhand 28:6b2353fad12d 425
roryhand 1:aac37edee302 426
roryhand 3:6169aeeaeeb4 427 printf("hello\n\r");
roryhand 0:e89d7a0bfa3b 428 //Set up the wolfson Audio Codec board
roryhand 0:e89d7a0bfa3b 429 wm8731_Config_setup();
roryhand 0:e89d7a0bfa3b 430 //i2s audio data transfer code??
roryhand 0:e89d7a0bfa3b 431 i2s.stereomono(I2S_STEREO);
roryhand 0:e89d7a0bfa3b 432 i2s.masterslave(I2S_MASTER);
roryhand 0:e89d7a0bfa3b 433 led3 = 1;
roryhand 0:e89d7a0bfa3b 434 led2 = 1;
roryhand 45:0e8e1f2ec5d2 435 printf("Hello i2s has started!");
roryhand 30:4a8e80b243c4 436 i2s.start();
roryhand 0:e89d7a0bfa3b 437 sampletick.attach(&isr,1.0/sampling_freq); //1/16000
roryhand 45:0e8e1f2ec5d2 438 //fileswitch_tick.attach(&FileSwitch_isr,6.0);//So we can switch between playing file 1 and file 2, including opening and closing
roryhand 45:0e8e1f2ec5d2 439 //not convinced we need this above line at the moment!!
roryhand 31:0f8c3adf09c3 440
roryhand 56:3755c53ac4c6 441
roryhand 62:ac63dfe15ef9 442
roryhand 31:0f8c3adf09c3 443 timer_open.reset();
roryhand 32:6ee488c97dcc 444 timer_open.start();
roryhand 88:76a4563ee9c7 445 wavfile1 = fopen("/sd/mydir/SoundDecoder_second/All_eight_notches.wav","rb");
roryhand 54:606a83fff291 446 if(wavfile1 == NULL) {
roryhand 45:0e8e1f2ec5d2 447 printf("Cannot Open wavfile1\n\r");
roryhand 32:6ee488c97dcc 448 }
roryhand 54:606a83fff291 449 printf("size of wavfile1 %d\n\r",sizeof(wavfile1));
roryhand 54:606a83fff291 450 timer_open.stop();
roryhand 31:0f8c3adf09c3 451 printf("It took %d useconds to open file\n\r",timer_open.read_us());
roryhand 54:606a83fff291 452 timer_open.reset();
roryhand 54:606a83fff291 453
roryhand 54:606a83fff291 454
roryhand 54:606a83fff291 455 printf("Size of wavfile 1: %d bytes\n\r",sizeof(wavfile1));
roryhand 45:0e8e1f2ec5d2 456 timer_open.start();
roryhand 88:76a4563ee9c7 457 wavfile2 = fopen("/sd/mydir/SoundDecoder_second/All_14_Notch_Transitions.wav","rb");
roryhand 54:606a83fff291 458 if(wavfile2 == NULL) {
roryhand 45:0e8e1f2ec5d2 459 printf("Cannot Open wavfile2\n\r");
roryhand 45:0e8e1f2ec5d2 460 }
roryhand 54:606a83fff291 461 timer_open.stop();
roryhand 45:0e8e1f2ec5d2 462 printf("It took %d useconds to open file\n\r",timer_open.read_us());
roryhand 54:606a83fff291 463 timer_open.reset();
roryhand 54:606a83fff291 464
roryhand 32:6ee488c97dcc 465 timer_open.start();
roryhand 88:76a4563ee9c7 466 wavfile3 = fopen("/sd/mydir/SoundDecoder_second/All_eight_notches.wav","rb");
roryhand 54:606a83fff291 467 if(wavfile3 == NULL) {
roryhand 45:0e8e1f2ec5d2 468 printf("Cannot Open wavfile3\n\r");
roryhand 45:0e8e1f2ec5d2 469 }
roryhand 54:606a83fff291 470 timer_open.stop();
roryhand 45:0e8e1f2ec5d2 471 printf("It took %d useconds to open file\n\r",timer_open.read_us());
roryhand 54:606a83fff291 472 timer_open.reset();
roryhand 54:606a83fff291 473
roryhand 54:606a83fff291 474
roryhand 45:0e8e1f2ec5d2 475 Sound1 = ReadFileInfo(Sound1,wavfile1);
roryhand 45:0e8e1f2ec5d2 476 Sound2 = ReadFileInfo(Sound2,wavfile2);
roryhand 45:0e8e1f2ec5d2 477 Sound3 = ReadFileInfo(Sound3,wavfile3);
roryhand 88:76a4563ee9c7 478
roryhand 13:8e93396a27c5 479
roryhand 88:76a4563ee9c7 480 //fclose(wavfile2);
roryhand 88:76a4563ee9c7 481 //fclose(wavfile3);
roryhand 47:9334a8f54997 482 NotchFadeIn.LengthSecs = 2;
roryhand 46:de390e45c2af 483 NotchFadeOut.LengthSecs = 2;
roryhand 46:de390e45c2af 484 NotchFadeIn = FadeDataInitialise(NotchFadeIn);
roryhand 46:de390e45c2af 485 NotchFadeOut = FadeDataInitialise(NotchFadeOut);
roryhand 45:0e8e1f2ec5d2 486
roryhand 45:0e8e1f2ec5d2 487
roryhand 45:0e8e1f2ec5d2 488
roryhand 45:0e8e1f2ec5d2 489 TickFadeOut.attach(&FadeOutIsr,10.0);
roryhand 45:0e8e1f2ec5d2 490
roryhand 54:606a83fff291 491
roryhand 54:606a83fff291 492 //timer_interrupt.reset();
roryhand 54:606a83fff291 493
roryhand 20:9cc7d825c07b 494 printf("about to play wav file\n\r");
roryhand 45:0e8e1f2ec5d2 495 Play_WaveFileLoop(Sound1,Sound2,Sound3,wavfile1,wavfile2,wavfile3);
roryhand 45:0e8e1f2ec5d2 496 //Play_WaveFileDual(StartupWav,WavInfo_Startup);
roryhand 20:9cc7d825c07b 497 printf("finished playing Wav file\n\r");
roryhand 54:606a83fff291 498
roryhand 54:606a83fff291 499
roryhand 31:0f8c3adf09c3 500 timer_open.reset();
roryhand 31:0f8c3adf09c3 501 timer_open.start();
roryhand 45:0e8e1f2ec5d2 502 fclose(wavfile1);
roryhand 31:0f8c3adf09c3 503 timer_open.stop();
roryhand 31:0f8c3adf09c3 504 printf("It took %d useconds to close file\n\r",timer_open.read_us());
roryhand 54:606a83fff291 505
roryhand 45:0e8e1f2ec5d2 506 timer_open.reset();
roryhand 54:606a83fff291 507 timer_open.start();
roryhand 45:0e8e1f2ec5d2 508 fclose(wavfile2);
roryhand 45:0e8e1f2ec5d2 509 timer_open.stop();
roryhand 54:606a83fff291 510 printf("It took %d useconds to close file\n\r",timer_open.read_us());
roryhand 54:606a83fff291 511
roryhand 54:606a83fff291 512
roryhand 45:0e8e1f2ec5d2 513 timer_open.reset();
roryhand 54:606a83fff291 514 timer_open.start();
roryhand 45:0e8e1f2ec5d2 515 fclose(wavfile3);
roryhand 45:0e8e1f2ec5d2 516 timer_open.stop();
roryhand 45:0e8e1f2ec5d2 517 printf("It took %d useconds to close file\n\r",timer_open.read_us());
roryhand 54:606a83fff291 518
roryhand 54:606a83fff291 519
roryhand 54:606a83fff291 520
roryhand 9:dd9cae06b202 521 /************************************PLAY WAV FILE LOOP*******************/
roryhand 20:9cc7d825c07b 522 //Play_WaveFileLoop(Sound, NotchingSet);
roryhand 9:dd9cae06b202 523 /************************************END OF PLAY WAV FILE LOOP*************/
roryhand 13:8e93396a27c5 524
roryhand 13:8e93396a27c5 525
roryhand 1:aac37edee302 526 i2s.stop();
roryhand 0:e89d7a0bfa3b 527
roryhand 0:e89d7a0bfa3b 528 }
roryhand 0:e89d7a0bfa3b 529
roryhand 0:e89d7a0bfa3b 530
roryhand 3:6169aeeaeeb4 531
roryhand 3:6169aeeaeeb4 532
roryhand 3:6169aeeaeeb4 533
roryhand 3:6169aeeaeeb4 534
roryhand 45:0e8e1f2ec5d2 535 classSoundFile ReadFileInfo(classSoundFile Sound, FILE * wav_file)
roryhand 0:e89d7a0bfa3b 536 {
roryhand 0:e89d7a0bfa3b 537 fseek(wav_file,20,SEEK_SET);
roryhand 22:706e86dc0d45 538 printf("We have just seeked through this file\n\r");
roryhand 45:0e8e1f2ec5d2 539 fread(&Sound.FileInfo.FileFormat,sizeof(Sound.FileInfo.FileFormat),1,wav_file);
roryhand 1:aac37edee302 540 //printf("wav_format.sample_rate: %d\n\r",FileInfo.FileFormat.sample_rate);
roryhand 1:aac37edee302 541
roryhand 45:0e8e1f2ec5d2 542 fread(&Sound.FileInfo.FileData,sizeof(Sound.FileInfo.FileData),1,wav_file);
roryhand 55:5a441d3b0d57 543 printf("wav_data.subchunk2_size: %d\n\r",Sound.FileInfo.FileData.subchunk2_size);
roryhand 45:0e8e1f2ec5d2 544 Sound.FileInfo.slice_buf = ( char *)malloc(Sound.FileInfo.FileFormat.block_align);
roryhand 45:0e8e1f2ec5d2 545 fread(Sound.FileInfo.slice_buf,Sound.FileInfo.FileFormat.block_align,1,wav_file); //This isnt actually required, its just a test
roryhand 45:0e8e1f2ec5d2 546 Sound.FileInfo.num_slices = Sound.FileInfo.FileData.subchunk2_size/Sound.FileInfo.FileFormat.block_align;
roryhand 1:aac37edee302 547 //printf("Number of Slices: %d\n\r",FileInfo.num_slices);
roryhand 45:0e8e1f2ec5d2 548 return Sound;
roryhand 0:e89d7a0bfa3b 549 }
roryhand 1:aac37edee302 550
roryhand 1:aac37edee302 551 classSoundFile LoadFileStream(classSoundFile Sound, string filename)
roryhand 1:aac37edee302 552 {
roryhand 1:aac37edee302 553 //Declare RootFolder and the directory for the appropriate file.
roryhand 1:aac37edee302 554 //How we index into filename[] from the outside of this class is another
roryhand 1:aac37edee302 555 //issue...
roryhand 1:aac37edee302 556 //printf("FileName: %s\n\r",filename);
roryhand 1:aac37edee302 557 //string RootFolder = "/sd/mydir/SoundDecoder/";
roryhand 1:aac37edee302 558 //string Directory = RootFolder + "01.wav";// + filename[0];
roryhand 1:aac37edee302 559 //printf("%s\n\r",Directory);
roryhand 1:aac37edee302 560 //const char* DirectoryChar = Directory.c_str();
roryhand 1:aac37edee302 561 //Sound.FileInfo.WavFile = fopen(DirectoryChar,"rb");
roryhand 1:aac37edee302 562 Sound.FileInfo.WavFile = fopen("/sd/mydir/645Engine/Startup.wav","rb");
roryhand 1:aac37edee302 563 fseek(Sound.FileInfo.WavFile,20,SEEK_SET);
roryhand 1:aac37edee302 564 fread(&Sound.FileInfo.FileFormat,sizeof(Sound.FileInfo.FileFormat),1,Sound.FileInfo.WavFile);
roryhand 1:aac37edee302 565 printf("wav_format.sample_rate: %d\n\r",Sound.FileInfo.FileFormat.sample_rate);
roryhand 1:aac37edee302 566 fread(&Sound.FileInfo.FileData,sizeof(Sound.FileInfo.FileData),1,Sound.FileInfo.WavFile);
roryhand 1:aac37edee302 567 printf("wav_data.subchunk2_size: %d\n\r",Sound.FileInfo.FileData.subchunk2_size);
roryhand 1:aac37edee302 568 Sound.FileInfo.slice_buf = ( char *)malloc(Sound.FileInfo.FileFormat.block_align);
roryhand 1:aac37edee302 569 fread(Sound.FileInfo.slice_buf,Sound.FileInfo.FileFormat.block_align,1,Sound.FileInfo.WavFile); //This isnt actually required, its just a test
roryhand 1:aac37edee302 570 Sound.FileInfo.num_slices = Sound.FileInfo.FileData.subchunk2_size/Sound.FileInfo.FileFormat.block_align;
roryhand 1:aac37edee302 571 printf("Number of Slices: %d\n\r",Sound.FileInfo.num_slices);
roryhand 1:aac37edee302 572 return Sound;
roryhand 1:aac37edee302 573 }
roryhand 1:aac37edee302 574
roryhand 1:aac37edee302 575
roryhand 0:e89d7a0bfa3b 576 classFade FadeDataInitialise(classFade FadeData)
roryhand 0:e89d7a0bfa3b 577 {
roryhand 1:aac37edee302 578 FadeData.DecayFactor = 1.3;
roryhand 0:e89d7a0bfa3b 579 FadeData.FadeIteration = 1;
roryhand 0:e89d7a0bfa3b 580 //FadeData.Denom = 11025*FadeData.DecayFactor;
roryhand 0:e89d7a0bfa3b 581 FadeData.Denom = 11025*FadeData.DecayFactor;
roryhand 0:e89d7a0bfa3b 582 FadeData.Natural_Exp = 2.7183;
roryhand 0:e89d7a0bfa3b 583 FadeData.Length = 11025*FadeData.LengthSecs;
roryhand 0:e89d7a0bfa3b 584 //FadeData.Natural_Exp = 2.7;
roryhand 0:e89d7a0bfa3b 585 return FadeData;
roryhand 0:e89d7a0bfa3b 586 }
roryhand 0:e89d7a0bfa3b 587
roryhand 0:e89d7a0bfa3b 588 //Playing Files Code
roryhand 46:de390e45c2af 589 /*
roryhand 45:0e8e1f2ec5d2 590 float FadeIn(void)
roryhand 0:e89d7a0bfa3b 591 {
roryhand 0:e89d7a0bfa3b 592 powervalFadeIn = FadeIterationIn/denom;
roryhand 0:e89d7a0bfa3b 593 FadeCoeffFadeIn
roryhand 1:aac37edee302 594
roryhand 1:aac37edee302 595
roryhand 45:0e8e1f2ec5d2 596 }
roryhand 45:0e8e1f2ec5d2 597 float FadeOut(void)
roryhand 0:e89d7a0bfa3b 598 {
roryhand 0:e89d7a0bfa3b 599 powerval = -FadeIteration/denom;
roryhand 0:e89d7a0bfa3b 600 FadeCoeff = pow(natural_exp,powerval);
roryhand 1:aac37edee302 601 FadeIteration = FadeIteration + 1;
roryhand 1:aac37edee302 602 return FadeCoeff;
roryhand 1:aac37edee302 603
roryhand 45:0e8e1f2ec5d2 604 }
roryhand 46:de390e45c2af 605 */
roryhand 0:e89d7a0bfa3b 606
roryhand 0:e89d7a0bfa3b 607 void Play_WaveFile(FILE * my_wav, WAV_FILE_STRUCT FileInfo)
roryhand 0:e89d7a0bfa3b 608 {
roryhand 1:aac37edee302 609 while(slice<FileInfo.num_slices) {
roryhand 1:aac37edee302 610 fread(FileInfo.slice_buf,FileInfo.FileFormat.block_align,1,my_wav);
roryhand 1:aac37edee302 611 data_sptr=(short *)FileInfo.slice_buf; // 16 bit samples
roryhand 1:aac37edee302 612 for (channel=0; channel<FileInfo.FileFormat.num_channels; channel++) {
roryhand 1:aac37edee302 613 if(flag1 == 1) {
roryhand 1:aac37edee302 614 Buffer1[place_hold1] = data_sptr[channel];
roryhand 1:aac37edee302 615 place_hold1 = place_hold1 + 1;
roryhand 1:aac37edee302 616 if(place_hold1 >= BufferLen) {
roryhand 1:aac37edee302 617 while(1) {
roryhand 1:aac37edee302 618 if(flag1 == 0) {
roryhand 0:e89d7a0bfa3b 619
roryhand 1:aac37edee302 620 break;
roryhand 0:e89d7a0bfa3b 621 }
roryhand 1:aac37edee302 622
roryhand 0:e89d7a0bfa3b 623 }
roryhand 1:aac37edee302 624 }
roryhand 1:aac37edee302 625
roryhand 1:aac37edee302 626 } else if(flag2 == 1) {
roryhand 1:aac37edee302 627 Buffer2[place_hold2] = data_sptr[channel];
roryhand 1:aac37edee302 628 place_hold2 = place_hold2 + 1;
roryhand 1:aac37edee302 629 if(place_hold2 >= BufferLen) {
roryhand 1:aac37edee302 630
roryhand 1:aac37edee302 631 while(1) {
roryhand 1:aac37edee302 632 if(flag2 == 0) {
roryhand 1:aac37edee302 633
roryhand 1:aac37edee302 634 break;
roryhand 1:aac37edee302 635 }
roryhand 0:e89d7a0bfa3b 636 }
roryhand 1:aac37edee302 637 }
roryhand 0:e89d7a0bfa3b 638 }
roryhand 1:aac37edee302 639
roryhand 1:aac37edee302 640 }
roryhand 1:aac37edee302 641 slice = slice + 1;
roryhand 0:e89d7a0bfa3b 642 }
roryhand 0:e89d7a0bfa3b 643 }
roryhand 0:e89d7a0bfa3b 644
roryhand 0:e89d7a0bfa3b 645
roryhand 0:e89d7a0bfa3b 646
roryhand 0:e89d7a0bfa3b 647
roryhand 32:6ee488c97dcc 648 void Play_WaveFileDual(FILE * my_wav, WAV_FILE_STRUCT FileInfo)
roryhand 32:6ee488c97dcc 649 {
roryhand 32:6ee488c97dcc 650 while(slice<FileInfo.num_slices) {
roryhand 32:6ee488c97dcc 651 if(FileSwitchFlag == 1) {
roryhand 35:4469b508dda3 652
roryhand 33:a75f0a30fbdc 653 if(temp == 1) {
roryhand 34:26118c8f2e48 654 printf("Does it go to this point\n\r");
roryhand 35:4469b508dda3 655 N3Wav = fopen("/sd/mydir/SoundDecoder_second/03.wav","rb");
roryhand 33:a75f0a30fbdc 656 if(N3Wav == NULL) {
roryhand 33:a75f0a30fbdc 657 printf("Cannot Open testwav\n\r");
roryhand 33:a75f0a30fbdc 658 }
roryhand 33:a75f0a30fbdc 659 fclose(StartupWav);
roryhand 33:a75f0a30fbdc 660 temp = 0;
roryhand 33:a75f0a30fbdc 661 }
roryhand 35:4469b508dda3 662 fread(WavInfo_N3.slice_buf,WavInfo_N3.FileFormat.block_align,1,N3Wav);
roryhand 35:4469b508dda3 663 data_sptr=(short*)WavInfo_N3.slice_buf;
roryhand 32:6ee488c97dcc 664 } else {
roryhand 32:6ee488c97dcc 665 fread(FileInfo.slice_buf,FileInfo.FileFormat.block_align,1,my_wav);
roryhand 32:6ee488c97dcc 666 data_sptr=(short *)FileInfo.slice_buf; // 16 bit samples
roryhand 32:6ee488c97dcc 667 }
roryhand 32:6ee488c97dcc 668 for (channel=0; channel<FileInfo.FileFormat.num_channels; channel++) {
roryhand 32:6ee488c97dcc 669 if(flag1 == 1) {
roryhand 32:6ee488c97dcc 670 Buffer1[place_hold1] = data_sptr[channel];
roryhand 32:6ee488c97dcc 671 place_hold1 = place_hold1 + 1;
roryhand 32:6ee488c97dcc 672 if(place_hold1 >= BufferLen) {
roryhand 32:6ee488c97dcc 673 while(1) {
roryhand 32:6ee488c97dcc 674 if(flag1 == 0) {
roryhand 0:e89d7a0bfa3b 675
roryhand 32:6ee488c97dcc 676 break;
roryhand 32:6ee488c97dcc 677 }
roryhand 32:6ee488c97dcc 678
roryhand 32:6ee488c97dcc 679 }
roryhand 32:6ee488c97dcc 680 }
roryhand 32:6ee488c97dcc 681
roryhand 32:6ee488c97dcc 682 } else if(flag2 == 1) {
roryhand 32:6ee488c97dcc 683 Buffer2[place_hold2] = data_sptr[channel];
roryhand 32:6ee488c97dcc 684 place_hold2 = place_hold2 + 1;
roryhand 32:6ee488c97dcc 685 if(place_hold2 >= BufferLen) {
roryhand 32:6ee488c97dcc 686
roryhand 32:6ee488c97dcc 687 while(1) {
roryhand 32:6ee488c97dcc 688 if(flag2 == 0) {
roryhand 32:6ee488c97dcc 689
roryhand 32:6ee488c97dcc 690 break;
roryhand 32:6ee488c97dcc 691 }
roryhand 32:6ee488c97dcc 692 }
roryhand 32:6ee488c97dcc 693 }
roryhand 32:6ee488c97dcc 694 }
roryhand 32:6ee488c97dcc 695
roryhand 32:6ee488c97dcc 696 }
roryhand 32:6ee488c97dcc 697 slice = slice + 1;
roryhand 32:6ee488c97dcc 698 }
roryhand 32:6ee488c97dcc 699 }
roryhand 0:e89d7a0bfa3b 700
roryhand 38:3b4c05af5f36 701 //***************************************************************************//
roryhand 38:3b4c05af5f36 702
roryhand 38:3b4c05af5f36 703 //**************************************************************************//
roryhand 38:3b4c05af5f36 704
roryhand 38:3b4c05af5f36 705
roryhand 38:3b4c05af5f36 706
roryhand 38:3b4c05af5f36 707
roryhand 38:3b4c05af5f36 708
roryhand 1:aac37edee302 709
roryhand 44:a9e84d333a6a 710
roryhand 45:0e8e1f2ec5d2 711 void Play_WaveFileLoop(classSoundFile Sound1, classSoundFile Sound2, classSoundFile Sound3, FILE *wavfile1, FILE *wavfile2, FILE *wavfile3)
roryhand 45:0e8e1f2ec5d2 712 {
roryhand 45:0e8e1f2ec5d2 713 while(1) { //might have to change this to a while(1) loop?
roryhand 45:0e8e1f2ec5d2 714 //New format!! This should be (roughly) the way that this is done, with the new structures and classes
roryhand 44:a9e84d333a6a 715
roryhand 45:0e8e1f2ec5d2 716 //Block 1 of code. We need to pass in 3x classSoundFile objects, and 3x FILE* pointer objects.
roryhand 45:0e8e1f2ec5d2 717 //This will allow us to switch between the 3 different files (using "cross-fades") as needed.
roryhand 45:0e8e1f2ec5d2 718 /*Remember that these will need to be global varaibles to work properly*/
roryhand 45:0e8e1f2ec5d2 719
roryhand 45:0e8e1f2ec5d2 720
roryhand 2:957d3b2afff4 721 //make sure we are reading in the correct "notch" here
roryhand 2:957d3b2afff4 722
roryhand 44:a9e84d333a6a 723
roryhand 44:a9e84d333a6a 724
roryhand 54:606a83fff291 725 /*Block1*/
roryhand 54:606a83fff291 726 /*Read in data for current sound files. We only have 3 active engine noises at a time.
roryhand 54:606a83fff291 727 When it becomes time to transition up again, we then flip to the opposite block, and perform
roryhand 54:606a83fff291 728 the opposite operation.*/
roryhand 54:606a83fff291 729
roryhand 54:606a83fff291 730
roryhand 54:606a83fff291 731 if(BlockFlag == 0) {
roryhand 44:a9e84d333a6a 732 //Sound1=======================================================================================
roryhand 44:a9e84d333a6a 733 if( slice1 == (Sound1.FileInfo.num_slices-1) ) {
roryhand 44:a9e84d333a6a 734 slice1 = 0;
roryhand 44:a9e84d333a6a 735 fseek(wavfile1,44,SEEK_SET);
roryhand 2:957d3b2afff4 736 }
roryhand 47:9334a8f54997 737 if(feof(wavfile1)) {
roryhand 47:9334a8f54997 738 fseek(wavfile1,44,SEEK_SET);
roryhand 47:9334a8f54997 739 }
roryhand 44:a9e84d333a6a 740 fread(Sound1.FileInfo.slice_buf,Sound1.FileInfo.FileFormat.block_align,1,wavfile1);
roryhand 44:a9e84d333a6a 741 Sound1.data_sptr=(short *)Sound1.FileInfo.slice_buf; // 16 bit samples
roryhand 44:a9e84d333a6a 742 //=============================================================================================
roryhand 54:606a83fff291 743 }
roryhand 44:a9e84d333a6a 744
roryhand 54:606a83fff291 745 else if(BlockFlag == 1) {
roryhand 44:a9e84d333a6a 746
roryhand 44:a9e84d333a6a 747
roryhand 44:a9e84d333a6a 748
roryhand 44:a9e84d333a6a 749
roryhand 44:a9e84d333a6a 750 /*Block2*/
roryhand 3:6169aeeaeeb4 751
roryhand 2:957d3b2afff4 752 //Read in data for current (i.e. now the previous notch, according to the index!!)
roryhand 2:957d3b2afff4 753 //We might not need this code here...as its probably done somewhere else??
roryhand 44:a9e84d333a6a 754 //fread(Sounds[NotchingSet.Notch-1].FileInfo.slice_buf,Sounds[NotchingSet.Notch-1].FileInfo.FileFormat.block_align,1,Sounds[NotchingSet.Notch-1].FileInfo.WavFile);
roryhand 44:a9e84d333a6a 755 //Sounds[NotchingSet.Notch-1].data_sptr = (short *)Sounds[NotchingSet.Notch-1].FileInfo.slice_buf;
roryhand 44:a9e84d333a6a 756 //We are now performing this section above.
roryhand 3:6169aeeaeeb4 757
roryhand 47:9334a8f54997 758
roryhand 54:606a83fff291 759 if(FadeFlag) {
roryhand 54:606a83fff291 760 if( slice1 == (Sound1.FileInfo.num_slices-1) ) {
roryhand 54:606a83fff291 761 slice1 = 0;
roryhand 54:606a83fff291 762 fseek(wavfile1,44,SEEK_SET);
roryhand 54:606a83fff291 763 }
roryhand 54:606a83fff291 764 if(OneOff == 1) {
roryhand 47:9334a8f54997 765
roryhand 47:9334a8f54997 766
roryhand 54:606a83fff291 767 wavfile2 = fopen("/sd/mydir/SoundDecoder_second/09.wav","rb");
roryhand 54:606a83fff291 768 if(wavfile2 == NULL) {
roryhand 54:606a83fff291 769 printf("Cannot Open wavfile2\n\r");
roryhand 54:606a83fff291 770 }
roryhand 54:606a83fff291 771
roryhand 54:606a83fff291 772 wavfile3 = fopen("/sd/mydir/SoundDecoder_second/02.wav","rb");
roryhand 54:606a83fff291 773 if(wavfile3 == NULL) {
roryhand 54:606a83fff291 774 printf("Cannot Open wavfile3\n\r");
roryhand 54:606a83fff291 775 }
roryhand 54:606a83fff291 776 fseek(wavfile2,44,SEEK_SET);
roryhand 54:606a83fff291 777 fseek(wavfile3,44,SEEK_SET);
roryhand 54:606a83fff291 778 /*
roryhand 54:606a83fff291 779 FILE* wavfile4 = fopen("/sd/mydir/SoundDecoder_second/04.wav","rb");
roryhand 54:606a83fff291 780 if(wavfile4 == NULL) {
roryhand 54:606a83fff291 781 printf("Cannot Open wavfile3\n\r");
roryhand 54:606a83fff291 782 }
roryhand 54:606a83fff291 783 */
roryhand 54:606a83fff291 784 OneOff = 0;
roryhand 54:606a83fff291 785 }
roryhand 54:606a83fff291 786
roryhand 54:606a83fff291 787 fread(Sound1.FileInfo.slice_buf,Sound1.FileInfo.FileFormat.block_align,1,wavfile1);
roryhand 54:606a83fff291 788 Sound1.data_sptr=(short *)Sound1.FileInfo.slice_buf; // 16 bit samples
roryhand 54:606a83fff291 789
roryhand 54:606a83fff291 790 //Sound2=======================================================================================
roryhand 54:606a83fff291 791 fread(Sound2.FileInfo.slice_buf,Sound2.FileInfo.FileFormat.block_align,1,wavfile2);
roryhand 54:606a83fff291 792 Sound2.data_sptr=(short *)Sound2.FileInfo.slice_buf; // 16 bit samples
roryhand 54:606a83fff291 793 //=============================================================================================
roryhand 2:957d3b2afff4 794
roryhand 45:0e8e1f2ec5d2 795
roryhand 3:6169aeeaeeb4 796
roryhand 54:606a83fff291 797 //Sound3=======================================================================================
roryhand 54:606a83fff291 798 fread(Sound3.FileInfo.slice_buf,Sound3.FileInfo.FileFormat.block_align,1,wavfile3);
roryhand 54:606a83fff291 799 Sound3.data_sptr=(short *)Sound3.FileInfo.slice_buf; // 16 bit samples
roryhand 54:606a83fff291 800 //=============================================================================================
roryhand 54:606a83fff291 801
roryhand 54:606a83fff291 802 //require this to get the fadeout coefficient for Sound1.
roryhand 54:606a83fff291 803 NotchFadeOut.FadeCoeff = NotchFadeOut.FadeOut();//compute new FadeOut Coeff value
roryhand 54:606a83fff291 804
roryhand 54:606a83fff291 805
roryhand 54:606a83fff291 806
roryhand 54:606a83fff291 807 if( ((Sound2.FileInfo.FileData.subchunk2_size) - NotchFadeIn.Length - 2*11025) <= (ftell(wavfile2) + 44)) {
roryhand 2:957d3b2afff4 808
roryhand 44:a9e84d333a6a 809
roryhand 54:606a83fff291 810 //required for calculating the fade In coefficient for Sound3
roryhand 54:606a83fff291 811 NotchFadeIn.FadeCoeff = NotchFadeIn.FadeIn();
roryhand 3:6169aeeaeeb4 812
roryhand 54:606a83fff291 813 //Read In the next Notch
roryhand 44:a9e84d333a6a 814
roryhand 54:606a83fff291 815 /*
roryhand 54:606a83fff291 816 fread(Sounds[NotchingSet.Notch].FileInfo.slice_buf,Sounds[NotchingSet.Notch].FileInfo.FileFormat.block_align,1,Sounds[NotchingSet.Notch].FileInfo.WavFile);
roryhand 54:606a83fff291 817 Sounds[NotchingSet.Notch].data_sptr = (short *)Sounds[NotchingSet.Notch].FileInfo.slice_buf;
roryhand 54:606a83fff291 818 */
roryhand 54:606a83fff291 819 //no longer require the above section as we are reading in data for Sound3 (above, at beginning of function).
roryhand 54:606a83fff291 820
roryhand 54:606a83fff291 821 if( (ftell(wavfile2) + 44) >= Sound2.FileInfo.FileData.subchunk2_size ) {
roryhand 2:957d3b2afff4 822
roryhand 54:606a83fff291 823 //need to explicitly test if this notation/syntax works for pointers....
roryhand 54:606a83fff291 824 /*Was this ever tested?? no idea...
roryhand 54:606a83fff291 825 If not then we need to pass in the 3 different "data_sptr" variables in separately, and not tied to a specific class
roryhand 54:606a83fff291 826 or structure.*/
roryhand 54:606a83fff291 827 *Sound1.data_sptr = *Sound3.data_sptr*NotchFadeIn.FadeCoeff;//Sound 3 by itself.
roryhand 54:606a83fff291 828 //fclose(wavfile2);
roryhand 54:606a83fff291 829 //fclose(wavfile1);
roryhand 54:606a83fff291 830 } else {
roryhand 54:606a83fff291 831 *Sound1.data_sptr = *Sound1.data_sptr*NotchFadeOut.FadeCoeff + *Sound2.data_sptr + *Sound3.data_sptr*NotchFadeIn.FadeCoeff;//Sounds 1, 2 and 3 Cross-fade.
roryhand 54:606a83fff291 832 //probably later on redesign this to work with flags - that way it may be a little easier to debug any issues.
roryhand 54:606a83fff291 833 }
roryhand 3:6169aeeaeeb4 834
roryhand 2:957d3b2afff4 835 } else {
roryhand 54:606a83fff291 836 *Sound1.data_sptr = *Sound1.data_sptr*NotchFadeOut.FadeCoeff + *Sound2.data_sptr;//Sound1 and Sound 2 cross fade
roryhand 2:957d3b2afff4 837 }
roryhand 2:957d3b2afff4 838
roryhand 2:957d3b2afff4 839 }
roryhand 2:957d3b2afff4 840
roryhand 54:606a83fff291 841
roryhand 54:606a83fff291 842 /********************END OF DATA ASSIGNMENT SECTION*************************************************/
roryhand 54:606a83fff291 843
roryhand 1:aac37edee302 844 }
roryhand 1:aac37edee302 845
roryhand 1:aac37edee302 846
roryhand 44:a9e84d333a6a 847
roryhand 44:a9e84d333a6a 848 /**********************************************************************************************************/
roryhand 44:a9e84d333a6a 849 /****************************DATA OUTPUT SECTION***********************************************************/
roryhand 44:a9e84d333a6a 850 /**********************************************************************************************************/
roryhand 45:0e8e1f2ec5d2 851 for (channel=0; channel<Sound1.FileInfo.FileFormat.num_channels; channel++) {
roryhand 45:0e8e1f2ec5d2 852 switch (Sound1.FileInfo.FileFormat.sig_bps) {
roryhand 0:e89d7a0bfa3b 853 case 16:
roryhand 1:aac37edee302 854 if(flag1 == 1) {
roryhand 45:0e8e1f2ec5d2 855 Buffer1[place_hold1] = Sound1.data_sptr[channel];
roryhand 1:aac37edee302 856 place_hold1 = place_hold1 + 1;
roryhand 1:aac37edee302 857 if(place_hold1 >= BufferLen) {
roryhand 1:aac37edee302 858 while(1) {
roryhand 1:aac37edee302 859 if(flag1 == 0) {
roryhand 1:aac37edee302 860 break;
roryhand 1:aac37edee302 861 }//if(flag1 == 0)
roryhand 1:aac37edee302 862
roryhand 1:aac37edee302 863
roryhand 1:aac37edee302 864 }//while(1)
roryhand 1:aac37edee302 865 }//if(place_hold1 > = BufferLen)
roryhand 0:e89d7a0bfa3b 866
roryhand 1:aac37edee302 867 } else if(flag2 == 1) {
roryhand 45:0e8e1f2ec5d2 868 Buffer2[place_hold2] = Sound1.data_sptr[channel];
roryhand 1:aac37edee302 869 place_hold2 = place_hold2 + 1;
roryhand 1:aac37edee302 870 if(place_hold2 >= BufferLen) {
roryhand 1:aac37edee302 871 while(1) {
roryhand 1:aac37edee302 872
roryhand 1:aac37edee302 873 if(flag2 == 0) {
roryhand 1:aac37edee302 874 break;
roryhand 1:aac37edee302 875 }
roryhand 1:aac37edee302 876 }
roryhand 0:e89d7a0bfa3b 877 }
roryhand 1:aac37edee302 878
roryhand 1:aac37edee302 879 }
roryhand 0:e89d7a0bfa3b 880 }
roryhand 0:e89d7a0bfa3b 881 }
roryhand 45:0e8e1f2ec5d2 882 slice1 = slice1 + 1;
roryhand 45:0e8e1f2ec5d2 883 slice2 = slice2 + 1;
roryhand 45:0e8e1f2ec5d2 884 slice3 = slice3 + 1;
roryhand 0:e89d7a0bfa3b 885 }
roryhand 0:e89d7a0bfa3b 886 }
roryhand 0:e89d7a0bfa3b 887
roryhand 0:e89d7a0bfa3b 888
roryhand 0:e89d7a0bfa3b 889
roryhand 0:e89d7a0bfa3b 890
roryhand 0:e89d7a0bfa3b 891
roryhand 0:e89d7a0bfa3b 892
roryhand 0:e89d7a0bfa3b 893
roryhand 9:dd9cae06b202 894