old demo that i want to try in mbed studio

Dependencies:   mbed SDFileSystem_Copy_of_mbed_version I2S

Committer:
roryhand
Date:
Sun Sep 08 20:33:59 2019 +0000
Revision:
47:9334a8f54997
Parent:
46:de390e45c2af
Child:
48:01490448a05a
Child:
54:606a83fff291
FINISHED - a basic Fade In demo.  We aren't even using a switch on the input, and we have not yet written block 2.  We also need to add in appropriate code to make fclose of different files work (hopefully)

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