old demo that i want to try in mbed studio

Dependencies:   mbed SDFileSystem_Copy_of_mbed_version I2S

Committer:
roryhand
Date:
Sun Sep 29 20:54:34 2019 +0000
Branch:
LargeFile_Tests
Revision:
68:55e26c5e837b
Parent:
67:043fe0b81343
Child:
69:df10ee3ad523
WIP - more minor changes

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 65:8b6a4e307941 46 class classFade
roryhand 65:8b6a4e307941 47 {
roryhand 65:8b6a4e307941 48 public:
roryhand 65:8b6a4e307941 49
roryhand 65:8b6a4e307941 50 float powerval;
roryhand 65:8b6a4e307941 51 float FadeIteration;
roryhand 65:8b6a4e307941 52 float DecayFactor;
roryhand 65:8b6a4e307941 53 float Denom;
roryhand 65:8b6a4e307941 54 float FadeCoeff;
roryhand 65:8b6a4e307941 55 float Natural_Exp;
roryhand 65:8b6a4e307941 56 int LengthSecs;
roryhand 65:8b6a4e307941 57 int Length;
roryhand 67:043fe0b81343 58 void FadeDataInitialise()
roryhand 67:043fe0b81343 59 {
roryhand 67:043fe0b81343 60 DecayFactor = 1.3;
roryhand 67:043fe0b81343 61 FadeIteration = 1;
roryhand 67:043fe0b81343 62 //FadeData.Denom = 11025*FadeData.DecayFactor;
roryhand 67:043fe0b81343 63 Denom = 11025*DecayFactor;
roryhand 67:043fe0b81343 64 Natural_Exp = 2.7183;
roryhand 67:043fe0b81343 65 Length = 11025*LengthSecs;
roryhand 67:043fe0b81343 66 //FadeData.Natural_Exp = 2.7;
roryhand 67:043fe0b81343 67 }
roryhand 65:8b6a4e307941 68 //member Functions
roryhand 65:8b6a4e307941 69 float FadeOut(void)
roryhand 65:8b6a4e307941 70 {
roryhand 65:8b6a4e307941 71 powerval = -FadeIteration/Denom;
roryhand 65:8b6a4e307941 72 if (FadeIteration >=Length) {
roryhand 65:8b6a4e307941 73 FadeCoeff = 0;
roryhand 65:8b6a4e307941 74
roryhand 65:8b6a4e307941 75 } else {
roryhand 65:8b6a4e307941 76 FadeCoeff = (Length - FadeIteration)/Length;
roryhand 65:8b6a4e307941 77 }
roryhand 65:8b6a4e307941 78 FadeIteration = FadeIteration + 1;
roryhand 65:8b6a4e307941 79 return FadeCoeff;
roryhand 65:8b6a4e307941 80
roryhand 65:8b6a4e307941 81 }
roryhand 65:8b6a4e307941 82 float FadeIn(void)
roryhand 65:8b6a4e307941 83 {
roryhand 65:8b6a4e307941 84 powerval = FadeIteration/Denom;
roryhand 65:8b6a4e307941 85 if (FadeIteration >=Length) {
roryhand 65:8b6a4e307941 86 FadeCoeff = 1;
roryhand 65:8b6a4e307941 87
roryhand 65:8b6a4e307941 88 } else {
roryhand 65:8b6a4e307941 89 FadeCoeff = FadeIteration/Length;
roryhand 65:8b6a4e307941 90 }
roryhand 65:8b6a4e307941 91
roryhand 65:8b6a4e307941 92 FadeIteration = FadeIteration + 1;
roryhand 65:8b6a4e307941 93 return FadeCoeff;
roryhand 65:8b6a4e307941 94
roryhand 65:8b6a4e307941 95 }
roryhand 65:8b6a4e307941 96 };
roryhand 65:8b6a4e307941 97
roryhand 65:8b6a4e307941 98
roryhand 67:043fe0b81343 99 //Do these variables have to be global?? Find out - may be better (safer) to make them local.
roryhand 67:043fe0b81343 100 classFade NotchFadeIn;
roryhand 67:043fe0b81343 101 classFade NotchFadeOut;
roryhand 67:043fe0b81343 102
roryhand 67:043fe0b81343 103
roryhand 67:043fe0b81343 104 //Dont require the class below. Turns out I had already made a Structure to hold the same information.
roryhand 67:043fe0b81343 105 //If in the futre I need to embed functions then I will switch to using this class
roryhand 67:043fe0b81343 106 /*
roryhand 67:043fe0b81343 107 class classNotchingValue{
roryhand 67:043fe0b81343 108 public:
roryhand 67:043fe0b81343 109
roryhand 67:043fe0b81343 110 int Notch, Previous_Notch;
roryhand 67:043fe0b81343 111 int NotchDirection;
roryhand 67:043fe0b81343 112 int NotchTransUp;
roryhand 67:043fe0b81343 113 classNotchingValue()
roryhand 67:043fe0b81343 114 {
roryhand 67:043fe0b81343 115 Notch = 1;
roryhand 67:043fe0b81343 116 Previous_Notch = 0;
roryhand 67:043fe0b81343 117 NotchDirection = 1;
roryhand 67:043fe0b81343 118 NotchTransUp = 0;
roryhand 67:043fe0b81343 119 }
roryhand 67:043fe0b81343 120
roryhand 67:043fe0b81343 121 };
roryhand 67:043fe0b81343 122
roryhand 67:043fe0b81343 123
roryhand 67:043fe0b81343 124
roryhand 67:043fe0b81343 125 classNotchingValue NotchingSet;
roryhand 67:043fe0b81343 126
roryhand 67:043fe0b81343 127 */
roryhand 67:043fe0b81343 128
roryhand 1:aac37edee302 129 typedef struct uFMT_STRUCT {
roryhand 1:aac37edee302 130 short comp_code;
roryhand 1:aac37edee302 131 short num_channels;
roryhand 1:aac37edee302 132 unsigned sample_rate;
roryhand 1:aac37edee302 133 unsigned avg_Bps;
roryhand 1:aac37edee302 134 short block_align;
roryhand 1:aac37edee302 135 short sig_bps;
roryhand 0:e89d7a0bfa3b 136 } FMT_STRUCT;
roryhand 0:e89d7a0bfa3b 137
roryhand 1:aac37edee302 138 typedef struct uNotch_STRUCT {
roryhand 1:aac37edee302 139 short Notch;
roryhand 1:aac37edee302 140 short NotchTransUp;
roryhand 1:aac37edee302 141 short NotchTransDown;
roryhand 1:aac37edee302 142 short NotchDirection;
roryhand 1:aac37edee302 143 } Notch_STRUCT;
roryhand 1:aac37edee302 144
roryhand 67:043fe0b81343 145 Notch_STRUCT NotchingSet;
roryhand 67:043fe0b81343 146
roryhand 67:043fe0b81343 147
roryhand 0:e89d7a0bfa3b 148 typedef struct uDATA_STRUCT {
roryhand 1:aac37edee302 149 unsigned subchunk2_ID;
roryhand 1:aac37edee302 150 unsigned subchunk2_size;
roryhand 1:aac37edee302 151 char * data_buf;
roryhand 1:aac37edee302 152 } DATA_STRUCT;
roryhand 0:e89d7a0bfa3b 153
roryhand 1:aac37edee302 154 typedef struct uWAV_FILE_STRUCT {
roryhand 1:aac37edee302 155 FILE *WavFile;
roryhand 1:aac37edee302 156 int id_number;
roryhand 1:aac37edee302 157 char *slice_buf;
roryhand 1:aac37edee302 158 int num_slices;
roryhand 1:aac37edee302 159 FMT_STRUCT FileFormat;
roryhand 1:aac37edee302 160 DATA_STRUCT FileData;
roryhand 0:e89d7a0bfa3b 161 } WAV_FILE_STRUCT;
roryhand 0:e89d7a0bfa3b 162
roryhand 0:e89d7a0bfa3b 163 /*typedef struct uWAV_FILE_STRUCT{
roryhand 1:aac37edee302 164 FILE* WavFile;
roryhand 1:aac37edee302 165
roryhand 1:aac37edee302 166
roryhand 0:e89d7a0bfa3b 167 }WAV_FILE_STRUCT;*/
roryhand 59:8e7c25a915a0 168
roryhand 59:8e7c25a915a0 169
roryhand 59:8e7c25a915a0 170
roryhand 59:8e7c25a915a0 171
roryhand 59:8e7c25a915a0 172
roryhand 59:8e7c25a915a0 173 class classSoundFile
roryhand 59:8e7c25a915a0 174 {
roryhand 59:8e7c25a915a0 175 public:
roryhand 59:8e7c25a915a0 176
roryhand 59:8e7c25a915a0 177 //add a class constructor at some point in the future (perform tests in visual studio)
roryhand 59:8e7c25a915a0 178 WAV_FILE_STRUCT FileInfo;
roryhand 59:8e7c25a915a0 179 short * data_sptr;
roryhand 59:8e7c25a915a0 180 string file_location;
roryhand 59:8e7c25a915a0 181 //classSoundFile(string filename);//this is the constructor
roryhand 59:8e7c25a915a0 182 //string filename;
roryhand 59:8e7c25a915a0 183 };
roryhand 58:a174e7a8f5f2 184 class classPositionIndicators
roryhand 57:0c76b15cabaf 185 {
roryhand 57:0c76b15cabaf 186 public:
roryhand 57:0c76b15cabaf 187 int notch1_indicator, notch2_indicator, notch3_indicator, notch4_indicator;
roryhand 57:0c76b15cabaf 188 int notch5_indicator, notch6_indicator, notch7_indicator, notch8_indicator;
roryhand 57:0c76b15cabaf 189 int notch1_start_pt, notch2_start_pt, notch3_start_pt, notch4_start_pt;
roryhand 57:0c76b15cabaf 190 int notch5_start_pt, notch6_start_pt, notch7_start_pt, notch8_start_pt;
roryhand 63:a8c1971d3d42 191 int N1N2_start_pt, N2N3_start_pt, N3N4_start_pt, N4N5_start_pt, N5N6_start_pt;
roryhand 63:a8c1971d3d42 192 int N6N7_start_pt, N7N8_start_pt, N8N7_start_pt, N7N6_start_pt, N6N5_start_pt;
roryhand 63:a8c1971d3d42 193 int N5N4_start_pt, N4N3_start_pt, N3N2_start_pt, N2N1_start_pt;
roryhand 67:043fe0b81343 194
roryhand 61:212d2db45c56 195 int notch_start_pts [9];
roryhand 61:212d2db45c56 196 int notch_position_indicators[9];
roryhand 61:212d2db45c56 197 int notch_transitions_start_pts[15];
roryhand 61:212d2db45c56 198 int notch_transitions_position_indicators[15];
roryhand 61:212d2db45c56 199 int auxiliary_start_pts[5];
roryhand 61:212d2db45c56 200 int auxiliary_position_indicators[5];
roryhand 58:a174e7a8f5f2 201 classPositionIndicators()
roryhand 57:0c76b15cabaf 202 {
roryhand 57:0c76b15cabaf 203 notch1_start_pt = 44;
roryhand 57:0c76b15cabaf 204 notch2_start_pt = 220884+44;
roryhand 57:0c76b15cabaf 205 notch3_start_pt = notch2_start_pt+217698;
roryhand 57:0c76b15cabaf 206 notch4_start_pt = notch3_start_pt + 193060;
roryhand 57:0c76b15cabaf 207 notch5_start_pt = notch4_start_pt + 92010;
roryhand 57:0c76b15cabaf 208 notch6_start_pt = notch5_start_pt + 216642;
roryhand 57:0c76b15cabaf 209 notch7_start_pt = notch6_start_pt + 250316;
roryhand 61:212d2db45c56 210 notch8_start_pt = notch7_start_pt + 150152;
roryhand 57:0c76b15cabaf 211 notch1_indicator = notch1_start_pt;
roryhand 57:0c76b15cabaf 212 notch2_indicator = notch2_start_pt;
roryhand 57:0c76b15cabaf 213 notch3_indicator = notch3_start_pt;
roryhand 57:0c76b15cabaf 214 notch4_indicator = notch4_start_pt;
roryhand 57:0c76b15cabaf 215 notch5_indicator = notch5_start_pt;
roryhand 57:0c76b15cabaf 216 notch6_indicator = notch6_start_pt;
roryhand 57:0c76b15cabaf 217 notch7_indicator = notch7_start_pt;
roryhand 57:0c76b15cabaf 218 notch8_indicator = notch8_start_pt;
roryhand 61:212d2db45c56 219 notch_start_pts[1] = notch1_start_pt;
roryhand 61:212d2db45c56 220 notch_start_pts[2] = notch2_start_pt;
roryhand 61:212d2db45c56 221 notch_start_pts[3] = notch3_start_pt;
roryhand 61:212d2db45c56 222 notch_start_pts[4] = notch4_start_pt;
roryhand 61:212d2db45c56 223 notch_start_pts[5] = notch5_start_pt;
roryhand 61:212d2db45c56 224 notch_start_pts[6] = notch6_start_pt;
roryhand 61:212d2db45c56 225 notch_start_pts[7] = notch7_start_pt;
roryhand 61:212d2db45c56 226 notch_start_pts[8] = notch8_start_pt;
roryhand 61:212d2db45c56 227 notch_position_indicators[1] = notch1_indicator;
roryhand 61:212d2db45c56 228 notch_position_indicators[2] = notch2_indicator;
roryhand 61:212d2db45c56 229 notch_position_indicators[3] = notch3_indicator;
roryhand 61:212d2db45c56 230 notch_position_indicators[4] = notch4_indicator;
roryhand 61:212d2db45c56 231 notch_position_indicators[5] = notch5_indicator;
roryhand 61:212d2db45c56 232 notch_position_indicators[6] = notch6_indicator;
roryhand 61:212d2db45c56 233 notch_position_indicators[7] = notch7_indicator;
roryhand 61:212d2db45c56 234 notch_position_indicators[8] = notch8_indicator;
roryhand 67:043fe0b81343 235
roryhand 63:a8c1971d3d42 236 N1N2_start_pt = 44;
roryhand 63:a8c1971d3d42 237 N2N3_start_pt = N1N2_start_pt + 73220;
roryhand 63:a8c1971d3d42 238 N3N4_start_pt = N2N3_start_pt + 78164;
roryhand 63:a8c1971d3d42 239 N4N5_start_pt = N3N4_start_pt + 59432;
roryhand 63:a8c1971d3d42 240 N5N6_start_pt = N4N5_start_pt + 64984;
roryhand 63:a8c1971d3d42 241 N6N7_start_pt = N5N6_start_pt + 59924;
roryhand 63:a8c1971d3d42 242 N7N8_start_pt = N6N7_start_pt + 97874;
roryhand 63:a8c1971d3d42 243 N8N7_start_pt = N7N8_start_pt + 63992;
roryhand 63:a8c1971d3d42 244 N7N6_start_pt = N8N7_start_pt + 44506;
roryhand 63:a8c1971d3d42 245 N6N5_start_pt = N7N6_start_pt + 55052;
roryhand 63:a8c1971d3d42 246 N5N4_start_pt = N6N5_start_pt + 37038;
roryhand 63:a8c1971d3d42 247 N4N3_start_pt = N5N4_start_pt + 49692;
roryhand 63:a8c1971d3d42 248 N3N2_start_pt = N4N3_start_pt + 44100;
roryhand 63:a8c1971d3d42 249 N2N1_start_pt = N3N2_start_pt + 58346;
roryhand 67:043fe0b81343 250
roryhand 67:043fe0b81343 251
roryhand 63:a8c1971d3d42 252 notch_transitions_start_pts[1] = N1N2_start_pt;
roryhand 63:a8c1971d3d42 253 notch_transitions_start_pts[2] = N2N3_start_pt;
roryhand 63:a8c1971d3d42 254 notch_transitions_start_pts[3] = N3N4_start_pt;
roryhand 63:a8c1971d3d42 255 notch_transitions_start_pts[4] = N4N5_start_pt;
roryhand 63:a8c1971d3d42 256 notch_transitions_start_pts[5] = N5N6_start_pt;
roryhand 63:a8c1971d3d42 257 notch_transitions_start_pts[6] = N6N7_start_pt;
roryhand 63:a8c1971d3d42 258 notch_transitions_start_pts[7] = N7N8_start_pt;
roryhand 63:a8c1971d3d42 259 notch_transitions_start_pts[8] = N8N7_start_pt;
roryhand 63:a8c1971d3d42 260 notch_transitions_start_pts[9] = N7N6_start_pt;
roryhand 63:a8c1971d3d42 261 notch_transitions_start_pts[10] = N6N5_start_pt;
roryhand 63:a8c1971d3d42 262 notch_transitions_start_pts[11] = N5N4_start_pt;
roryhand 63:a8c1971d3d42 263 notch_transitions_start_pts[12] = N4N3_start_pt;
roryhand 63:a8c1971d3d42 264 notch_transitions_start_pts[13] = N3N2_start_pt;
roryhand 63:a8c1971d3d42 265 notch_transitions_start_pts[14] = N2N1_start_pt;
roryhand 67:043fe0b81343 266
roryhand 67:043fe0b81343 267
roryhand 63:a8c1971d3d42 268 notch_transitions_position_indicators[1] = N1N2_start_pt;
roryhand 63:a8c1971d3d42 269 notch_transitions_position_indicators[2] = N2N3_start_pt;
roryhand 63:a8c1971d3d42 270 notch_transitions_position_indicators[3] = N3N4_start_pt;
roryhand 63:a8c1971d3d42 271 notch_transitions_position_indicators[4] = N4N5_start_pt;
roryhand 63:a8c1971d3d42 272 notch_transitions_position_indicators[5] = N5N6_start_pt;
roryhand 63:a8c1971d3d42 273 notch_transitions_position_indicators[6] = N6N7_start_pt;
roryhand 63:a8c1971d3d42 274 notch_transitions_position_indicators[7] = N7N8_start_pt;
roryhand 63:a8c1971d3d42 275 notch_transitions_position_indicators[8] = N8N7_start_pt;
roryhand 63:a8c1971d3d42 276 notch_transitions_position_indicators[9] = N7N6_start_pt;
roryhand 63:a8c1971d3d42 277 notch_transitions_position_indicators[10] = N6N5_start_pt;
roryhand 63:a8c1971d3d42 278 notch_transitions_position_indicators[11] = N5N4_start_pt;
roryhand 63:a8c1971d3d42 279 notch_transitions_position_indicators[12] = N4N3_start_pt;
roryhand 63:a8c1971d3d42 280 notch_transitions_position_indicators[13] = N3N2_start_pt;
roryhand 63:a8c1971d3d42 281 notch_transitions_position_indicators[14] = N2N1_start_pt;
roryhand 57:0c76b15cabaf 282 }
roryhand 57:0c76b15cabaf 283 };
roryhand 57:0c76b15cabaf 284
roryhand 66:edf370edd21c 285
roryhand 66:edf370edd21c 286 void NotchUpIsr()
roryhand 66:edf370edd21c 287 {
roryhand 66:edf370edd21c 288 if(1 <= NotchingSet.Notch < 8) {
roryhand 66:edf370edd21c 289
roryhand 66:edf370edd21c 290 NotchingSet.Notch = NotchingSet.Notch + 1;
roryhand 66:edf370edd21c 291 NotchingSet.NotchTransUp = NotchingSet.Notch + 7;
roryhand 66:edf370edd21c 292 NotchingSet.NotchDirection = 1;
roryhand 67:043fe0b81343 293 NotchFadeIn.FadeDataInitialise();
roryhand 67:043fe0b81343 294 NotchFadeOut.FadeDataInitialise();
roryhand 67:043fe0b81343 295 //NotchFadeOut.FadeDataInitialise;
roryhand 66:edf370edd21c 296 }
roryhand 66:edf370edd21c 297 }
roryhand 66:edf370edd21c 298
roryhand 66:edf370edd21c 299 void NotchDownIsr()
roryhand 66:edf370edd21c 300 {
roryhand 66:edf370edd21c 301 if(1 < NotchingSet.Notch <= 8) {
roryhand 66:edf370edd21c 302 NotchingSet.Notch = NotchingSet.Notch - 1;
roryhand 67:043fe0b81343 303 NotchingSet.NotchTransDown = NotchingSet.Notch + 15;
roryhand 66:edf370edd21c 304 NotchingSet.NotchDirection = 0;
roryhand 67:043fe0b81343 305 NotchFadeIn.FadeDataInitialise();
roryhand 67:043fe0b81343 306 NotchFadeOut.FadeDataInitialise();
roryhand 66:edf370edd21c 307 }
roryhand 66:edf370edd21c 308 }
roryhand 67:043fe0b81343 309
roryhand 67:043fe0b81343 310
roryhand 54:606a83fff291 311 int OneOff = 0;
roryhand 38:3b4c05af5f36 312 int notch_flag = 0;
roryhand 0:e89d7a0bfa3b 313 int i = 0;
roryhand 0:e89d7a0bfa3b 314 int h = 0;
roryhand 0:e89d7a0bfa3b 315 short bufflen = 1;
roryhand 0:e89d7a0bfa3b 316 int buffer[1];
roryhand 1:aac37edee302 317 int AudioFormat, NumChannels, SampleRate, BitsPerSample ;
roryhand 0:e89d7a0bfa3b 318 char *slice_buf;
roryhand 0:e89d7a0bfa3b 319 short *data_sptr;
roryhand 0:e89d7a0bfa3b 320 short *data_sptr_horn;
roryhand 0:e89d7a0bfa3b 321 short *data_sptr_IdleN2;
roryhand 0:e89d7a0bfa3b 322 short * data_sptr_bell;
roryhand 0:e89d7a0bfa3b 323 short * data_sptr_N2;
roryhand 0:e89d7a0bfa3b 324 short * data_sptr_Flange;
roryhand 0:e89d7a0bfa3b 325 unsigned char *data_bptr;
roryhand 0:e89d7a0bfa3b 326 int *data_wptr;
roryhand 0:e89d7a0bfa3b 327 unsigned channel;
roryhand 45:0e8e1f2ec5d2 328 long slice, slice1, slice2, slice3, num_slices;
roryhand 0:e89d7a0bfa3b 329 int verbosity = 0;
roryhand 0:e89d7a0bfa3b 330 int verbosity2 = 0;
roryhand 0:e89d7a0bfa3b 331 int verbosity3 = 0;
roryhand 0:e89d7a0bfa3b 332 int verbosity4 = 0;
roryhand 0:e89d7a0bfa3b 333 int verbosity5 = 0;
roryhand 0:e89d7a0bfa3b 334 int interrupt_condition = 1;
roryhand 0:e89d7a0bfa3b 335 int sampling_freq = 11025;
roryhand 0:e89d7a0bfa3b 336 const int BufferLen = 2000;
roryhand 0:e89d7a0bfa3b 337 short Buffer1[BufferLen];
roryhand 0:e89d7a0bfa3b 338 short Buffer2[BufferLen];
roryhand 0:e89d7a0bfa3b 339 short place_hold1 = 0;
roryhand 0:e89d7a0bfa3b 340 short place_hold2 = 0;
roryhand 0:e89d7a0bfa3b 341
roryhand 3:6169aeeaeeb4 342
roryhand 3:6169aeeaeeb4 343 string FOLDER;
roryhand 3:6169aeeaeeb4 344 string RootFolder = "/sd/mydir/SoundDecoder/";
roryhand 3:6169aeeaeeb4 345 string filename[25];
roryhand 3:6169aeeaeeb4 346 classSoundFile Sound[22];
roryhand 3:6169aeeaeeb4 347
roryhand 0:e89d7a0bfa3b 348 volatile int flag1 = 1;
roryhand 0:e89d7a0bfa3b 349 volatile int flag2 = 0;
roryhand 0:e89d7a0bfa3b 350 volatile int flag3 = 1;
roryhand 0:e89d7a0bfa3b 351 volatile int flag4 = 0;
roryhand 0:e89d7a0bfa3b 352 int FLAGBUFF1 = 0;
roryhand 0:e89d7a0bfa3b 353 int FLAGBUFF2 = 0;
roryhand 0:e89d7a0bfa3b 354 int BellFlag = 0;
roryhand 0:e89d7a0bfa3b 355 int BellFlag2 = 0;
roryhand 0:e89d7a0bfa3b 356 int FadeFlag = 0;
roryhand 54:606a83fff291 357 int BlockFlag = 0;
roryhand 32:6ee488c97dcc 358 int FileSwitchFlag = 0;
roryhand 0:e89d7a0bfa3b 359
roryhand 0:e89d7a0bfa3b 360
roryhand 0:e89d7a0bfa3b 361 short value[1];
roryhand 59:8e7c25a915a0 362
roryhand 0:e89d7a0bfa3b 363 //long long slice_value;
roryhand 0:e89d7a0bfa3b 364 int slice_value[1];
roryhand 0:e89d7a0bfa3b 365
roryhand 0:e89d7a0bfa3b 366
roryhand 45:0e8e1f2ec5d2 367 FILE *wavfile1;
roryhand 45:0e8e1f2ec5d2 368 FILE *wavfile2;
roryhand 45:0e8e1f2ec5d2 369 FILE *wavfile3;
roryhand 45:0e8e1f2ec5d2 370
roryhand 45:0e8e1f2ec5d2 371 classSoundFile Sound1;
roryhand 45:0e8e1f2ec5d2 372 classSoundFile Sound2;
roryhand 45:0e8e1f2ec5d2 373 classSoundFile Sound3;
roryhand 45:0e8e1f2ec5d2 374
roryhand 0:e89d7a0bfa3b 375
roryhand 0:e89d7a0bfa3b 376 void isr()
roryhand 0:e89d7a0bfa3b 377 {
roryhand 54:606a83fff291 378 //timer_interrupt.start();
roryhand 1:aac37edee302 379 if(flag1 == 0) {
roryhand 0:e89d7a0bfa3b 380 value[0] = Buffer1[place_hold1]>>4;
roryhand 0:e89d7a0bfa3b 381 i2s.write(value,1);//Send next PWM value to amp
roryhand 0:e89d7a0bfa3b 382 place_hold1 = place_hold1 + 1;
roryhand 1:aac37edee302 383 if( (place_hold1 >= BufferLen)) {
roryhand 0:e89d7a0bfa3b 384 led2 = !led2;
roryhand 0:e89d7a0bfa3b 385 place_hold1 = 0;
roryhand 0:e89d7a0bfa3b 386 place_hold2 = 0;
roryhand 0:e89d7a0bfa3b 387 flag1 = 1;
roryhand 0:e89d7a0bfa3b 388 flag2 = 0;
roryhand 1:aac37edee302 389 }
roryhand 1:aac37edee302 390 } else if(flag2 == 0) {
roryhand 0:e89d7a0bfa3b 391 value[0] = Buffer2[place_hold2]>>4;
roryhand 0:e89d7a0bfa3b 392 i2s.write(value,1);//Send next PWM value to amp
roryhand 0:e89d7a0bfa3b 393 place_hold2 = place_hold2 + 1;
roryhand 1:aac37edee302 394 if( (place_hold2 >= BufferLen) ) {
roryhand 0:e89d7a0bfa3b 395 led2 = !led2;
roryhand 0:e89d7a0bfa3b 396 place_hold1 = 0;
roryhand 0:e89d7a0bfa3b 397 place_hold2 = 0;
roryhand 0:e89d7a0bfa3b 398 flag1 = 0;
roryhand 1:aac37edee302 399 flag2 = 1;
roryhand 0:e89d7a0bfa3b 400 FLAGBUFF2 = 0;
roryhand 1:aac37edee302 401 }
roryhand 0:e89d7a0bfa3b 402 }
roryhand 54:606a83fff291 403
roryhand 54:606a83fff291 404 //timer_interrupt.stop();
roryhand 54:606a83fff291 405
roryhand 0:e89d7a0bfa3b 406 }
roryhand 1:aac37edee302 407
roryhand 38:3b4c05af5f36 408
roryhand 38:3b4c05af5f36 409
roryhand 38:3b4c05af5f36 410
roryhand 38:3b4c05af5f36 411
roryhand 0:e89d7a0bfa3b 412
roryhand 59:8e7c25a915a0 413
roryhand 59:8e7c25a915a0 414
roryhand 59:8e7c25a915a0 415
roryhand 59:8e7c25a915a0 416
roryhand 59:8e7c25a915a0 417
roryhand 3:6169aeeaeeb4 418
roryhand 3:6169aeeaeeb4 419
roryhand 3:6169aeeaeeb4 420
roryhand 3:6169aeeaeeb4 421
roryhand 3:6169aeeaeeb4 422
roryhand 3:6169aeeaeeb4 423 //function prototypes
roryhand 67:043fe0b81343 424 //classFade FadeDataInitialise(classFade FadeData);
roryhand 45:0e8e1f2ec5d2 425 classSoundFile ReadFileInfo(classSoundFile Sound, FILE * wav_file);
roryhand 3:6169aeeaeeb4 426 classSoundFile LoadFileStream(classSoundFile FileInfo, string filename);
roryhand 3:6169aeeaeeb4 427 void Play_WaveFile(FILE * my_wav, WAV_FILE_STRUCT FileInfo);
roryhand 32:6ee488c97dcc 428 void Play_WaveFileDual(FILE * my_wav, WAV_FILE_STRUCT FileInfo);
roryhand 59:8e7c25a915a0 429 void Play_WaveFileLoop(classSoundFile Sound1, FILE* wavfile1,classPositionIndicators Positions);
roryhand 0:e89d7a0bfa3b 430
roryhand 1:aac37edee302 431 int main()
roryhand 1:aac37edee302 432 {
roryhand 44:a9e84d333a6a 433
roryhand 0:e89d7a0bfa3b 434 NotchUp.mode(PullUp);
roryhand 38:3b4c05af5f36 435 NotchDown.mode(PullUp);
roryhand 0:e89d7a0bfa3b 436
roryhand 0:e89d7a0bfa3b 437 pc.printf("Beginning of program\n");
roryhand 32:6ee488c97dcc 438
roryhand 13:8e93396a27c5 439
roryhand 13:8e93396a27c5 440
roryhand 4:55fbbb049bae 441
roryhand 25:5336e1cf38d6 442 printf("Do we even get to this stupid bloody point\n\r");
roryhand 0:e89d7a0bfa3b 443 //Populate our class instances with some data (is there an implicit way to do this?)
roryhand 28:6b2353fad12d 444
roryhand 1:aac37edee302 445
roryhand 3:6169aeeaeeb4 446 printf("hello\n\r");
roryhand 0:e89d7a0bfa3b 447 //Set up the wolfson Audio Codec board
roryhand 0:e89d7a0bfa3b 448 wm8731_Config_setup();
roryhand 0:e89d7a0bfa3b 449 //i2s audio data transfer code??
roryhand 0:e89d7a0bfa3b 450 i2s.stereomono(I2S_STEREO);
roryhand 0:e89d7a0bfa3b 451 i2s.masterslave(I2S_MASTER);
roryhand 0:e89d7a0bfa3b 452 led3 = 1;
roryhand 0:e89d7a0bfa3b 453 led2 = 1;
roryhand 45:0e8e1f2ec5d2 454 printf("Hello i2s has started!");
roryhand 30:4a8e80b243c4 455 i2s.start();
roryhand 0:e89d7a0bfa3b 456 sampletick.attach(&isr,1.0/sampling_freq); //1/16000
roryhand 67:043fe0b81343 457
roryhand 67:043fe0b81343 458
roryhand 67:043fe0b81343 459 NotchFadeIn.LengthSecs = 2;
roryhand 67:043fe0b81343 460 NotchFadeOut.LengthSecs = 2;
roryhand 67:043fe0b81343 461
roryhand 67:043fe0b81343 462 NotchFadeIn.FadeDataInitialise();
roryhand 67:043fe0b81343 463 NotchFadeOut.FadeDataInitialise();
roryhand 67:043fe0b81343 464
roryhand 58:a174e7a8f5f2 465 classPositionIndicators Positions;
roryhand 61:212d2db45c56 466 slice1 = Positions.notch7_start_pt;
roryhand 60:36df2997de3d 467 FILE* wavfile1 = fopen("/sd/mydir/SoundDecoder_second/All_eight_notches.wav","rb");
roryhand 60:36df2997de3d 468 //FILE* wavfile1 = fopen("/sd/mydir/SoundDecoder_second/01.wav","rb");
roryhand 58:a174e7a8f5f2 469 classSoundFile Sound1;
roryhand 58:a174e7a8f5f2 470 Sound1 = ReadFileInfo(Sound1, wavfile1);
roryhand 61:212d2db45c56 471 fseek(wavfile1,Positions.notch7_start_pt,SEEK_SET);
roryhand 20:9cc7d825c07b 472 printf("about to play wav file\n\r");
roryhand 59:8e7c25a915a0 473 Play_WaveFileLoop(Sound1,wavfile1,Positions);
roryhand 20:9cc7d825c07b 474 printf("finished playing Wav file\n\r");
roryhand 54:606a83fff291 475
roryhand 54:606a83fff291 476
roryhand 31:0f8c3adf09c3 477 timer_open.reset();
roryhand 31:0f8c3adf09c3 478 timer_open.start();
roryhand 45:0e8e1f2ec5d2 479 fclose(wavfile1);
roryhand 31:0f8c3adf09c3 480 timer_open.stop();
roryhand 31:0f8c3adf09c3 481 printf("It took %d useconds to close file\n\r",timer_open.read_us());
roryhand 54:606a83fff291 482
roryhand 54:606a83fff291 483
roryhand 54:606a83fff291 484
roryhand 54:606a83fff291 485
roryhand 9:dd9cae06b202 486 /************************************PLAY WAV FILE LOOP*******************/
roryhand 9:dd9cae06b202 487 /************************************END OF PLAY WAV FILE LOOP*************/
roryhand 13:8e93396a27c5 488
roryhand 13:8e93396a27c5 489
roryhand 1:aac37edee302 490 i2s.stop();
roryhand 0:e89d7a0bfa3b 491
roryhand 0:e89d7a0bfa3b 492 }
roryhand 0:e89d7a0bfa3b 493
roryhand 0:e89d7a0bfa3b 494
roryhand 3:6169aeeaeeb4 495
roryhand 3:6169aeeaeeb4 496
roryhand 3:6169aeeaeeb4 497
roryhand 3:6169aeeaeeb4 498
roryhand 45:0e8e1f2ec5d2 499 classSoundFile ReadFileInfo(classSoundFile Sound, FILE * wav_file)
roryhand 0:e89d7a0bfa3b 500 {
roryhand 0:e89d7a0bfa3b 501 fseek(wav_file,20,SEEK_SET);
roryhand 22:706e86dc0d45 502 printf("We have just seeked through this file\n\r");
roryhand 45:0e8e1f2ec5d2 503 fread(&Sound.FileInfo.FileFormat,sizeof(Sound.FileInfo.FileFormat),1,wav_file);
roryhand 1:aac37edee302 504 //printf("wav_format.sample_rate: %d\n\r",FileInfo.FileFormat.sample_rate);
roryhand 1:aac37edee302 505
roryhand 45:0e8e1f2ec5d2 506 fread(&Sound.FileInfo.FileData,sizeof(Sound.FileInfo.FileData),1,wav_file);
roryhand 55:5a441d3b0d57 507 printf("wav_data.subchunk2_size: %d\n\r",Sound.FileInfo.FileData.subchunk2_size);
roryhand 45:0e8e1f2ec5d2 508 Sound.FileInfo.slice_buf = ( char *)malloc(Sound.FileInfo.FileFormat.block_align);
roryhand 59:8e7c25a915a0 509 printf("Wav File Block Align (number of bytes per sample!!: %d\n\r", Sound.FileInfo.FileFormat.block_align);
roryhand 45:0e8e1f2ec5d2 510 fread(Sound.FileInfo.slice_buf,Sound.FileInfo.FileFormat.block_align,1,wav_file); //This isnt actually required, its just a test
roryhand 45:0e8e1f2ec5d2 511 Sound.FileInfo.num_slices = Sound.FileInfo.FileData.subchunk2_size/Sound.FileInfo.FileFormat.block_align;
roryhand 1:aac37edee302 512 //printf("Number of Slices: %d\n\r",FileInfo.num_slices);
roryhand 45:0e8e1f2ec5d2 513 return Sound;
roryhand 0:e89d7a0bfa3b 514 }
roryhand 1:aac37edee302 515
roryhand 1:aac37edee302 516
roryhand 64:6061ffe25985 517 //Function to initialise Data for classFade objects. Perhaps move this into a constructor for the class? (class still to be added)
roryhand 67:043fe0b81343 518 /*
roryhand 64:6061ffe25985 519 classFade FadeDataInitialise(classFade FadeData)
roryhand 64:6061ffe25985 520 {
roryhand 64:6061ffe25985 521 FadeData.DecayFactor = 1.3;
roryhand 64:6061ffe25985 522 FadeData.FadeIteration = 1;
roryhand 64:6061ffe25985 523 //FadeData.Denom = 11025*FadeData.DecayFactor;
roryhand 64:6061ffe25985 524 FadeData.Denom = 11025*FadeData.DecayFactor;
roryhand 64:6061ffe25985 525 FadeData.Natural_Exp = 2.7183;
roryhand 64:6061ffe25985 526 FadeData.Length = 11025*FadeData.LengthSecs;
roryhand 64:6061ffe25985 527 //FadeData.Natural_Exp = 2.7;
roryhand 64:6061ffe25985 528 return FadeData;
roryhand 64:6061ffe25985 529 }
roryhand 67:043fe0b81343 530 */
roryhand 1:aac37edee302 531
roryhand 1:aac37edee302 532
roryhand 1:aac37edee302 533
roryhand 0:e89d7a0bfa3b 534
roryhand 0:e89d7a0bfa3b 535 void Play_WaveFile(FILE * my_wav, WAV_FILE_STRUCT FileInfo)
roryhand 0:e89d7a0bfa3b 536 {
roryhand 1:aac37edee302 537 while(slice<FileInfo.num_slices) {
roryhand 1:aac37edee302 538 fread(FileInfo.slice_buf,FileInfo.FileFormat.block_align,1,my_wav);
roryhand 1:aac37edee302 539 data_sptr=(short *)FileInfo.slice_buf; // 16 bit samples
roryhand 1:aac37edee302 540 for (channel=0; channel<FileInfo.FileFormat.num_channels; channel++) {
roryhand 1:aac37edee302 541 if(flag1 == 1) {
roryhand 1:aac37edee302 542 Buffer1[place_hold1] = data_sptr[channel];
roryhand 1:aac37edee302 543 place_hold1 = place_hold1 + 1;
roryhand 1:aac37edee302 544 if(place_hold1 >= BufferLen) {
roryhand 1:aac37edee302 545 while(1) {
roryhand 1:aac37edee302 546 if(flag1 == 0) {
roryhand 0:e89d7a0bfa3b 547
roryhand 1:aac37edee302 548 break;
roryhand 0:e89d7a0bfa3b 549 }
roryhand 1:aac37edee302 550
roryhand 0:e89d7a0bfa3b 551 }
roryhand 1:aac37edee302 552 }
roryhand 1:aac37edee302 553
roryhand 1:aac37edee302 554 } else if(flag2 == 1) {
roryhand 1:aac37edee302 555 Buffer2[place_hold2] = data_sptr[channel];
roryhand 1:aac37edee302 556 place_hold2 = place_hold2 + 1;
roryhand 1:aac37edee302 557 if(place_hold2 >= BufferLen) {
roryhand 1:aac37edee302 558
roryhand 1:aac37edee302 559 while(1) {
roryhand 1:aac37edee302 560 if(flag2 == 0) {
roryhand 1:aac37edee302 561
roryhand 1:aac37edee302 562 break;
roryhand 1:aac37edee302 563 }
roryhand 0:e89d7a0bfa3b 564 }
roryhand 1:aac37edee302 565 }
roryhand 0:e89d7a0bfa3b 566 }
roryhand 1:aac37edee302 567
roryhand 1:aac37edee302 568 }
roryhand 1:aac37edee302 569 slice = slice + 1;
roryhand 0:e89d7a0bfa3b 570 }
roryhand 0:e89d7a0bfa3b 571 }
roryhand 0:e89d7a0bfa3b 572
roryhand 0:e89d7a0bfa3b 573
roryhand 0:e89d7a0bfa3b 574
roryhand 0:e89d7a0bfa3b 575
roryhand 38:3b4c05af5f36 576 //***************************************************************************//
roryhand 38:3b4c05af5f36 577
roryhand 38:3b4c05af5f36 578 //**************************************************************************//
roryhand 38:3b4c05af5f36 579
roryhand 38:3b4c05af5f36 580
roryhand 38:3b4c05af5f36 581
roryhand 38:3b4c05af5f36 582
roryhand 38:3b4c05af5f36 583
roryhand 59:8e7c25a915a0 584
roryhand 44:a9e84d333a6a 585
roryhand 57:0c76b15cabaf 586 void Play_WaveFileLoop(classSoundFile Sound1, FILE *wavfile1, classPositionIndicators Positions)
roryhand 45:0e8e1f2ec5d2 587 {
roryhand 45:0e8e1f2ec5d2 588 while(1) { //might have to change this to a while(1) loop?
roryhand 44:a9e84d333a6a 589
roryhand 2:957d3b2afff4 590
roryhand 44:a9e84d333a6a 591
roryhand 44:a9e84d333a6a 592
roryhand 54:606a83fff291 593
roryhand 44:a9e84d333a6a 594 //Sound1=======================================================================================
roryhand 68:55e26c5e837b 595 if( slice1 == (Positions.notch_start_pts[Notching.Notch + 1]) ) {
roryhand 68:55e26c5e837b 596 slice1 = Positions.notch_start_pts[Notching.Notch];
roryhand 68:55e26c5e837b 597 fseek(wavfile1,Positions.notch_start_pts[Notching.Notch],SEEK_SET);
roryhand 67:043fe0b81343 598 }
roryhand 57:0c76b15cabaf 599
roryhand 67:043fe0b81343 600 fread(Sound1.FileInfo.slice_buf,Sound1.FileInfo.FileFormat.block_align,1,wavfile1);
roryhand 67:043fe0b81343 601 Sound1.data_sptr=(short *)Sound1.FileInfo.slice_buf; // 16 bit samples
roryhand 44:a9e84d333a6a 602 //=============================================================================================
roryhand 44:a9e84d333a6a 603
roryhand 67:043fe0b81343 604
roryhand 1:aac37edee302 605
roryhand 1:aac37edee302 606
roryhand 44:a9e84d333a6a 607
roryhand 45:0e8e1f2ec5d2 608 for (channel=0; channel<Sound1.FileInfo.FileFormat.num_channels; channel++) {
roryhand 45:0e8e1f2ec5d2 609 switch (Sound1.FileInfo.FileFormat.sig_bps) {
roryhand 0:e89d7a0bfa3b 610 case 16:
roryhand 1:aac37edee302 611 if(flag1 == 1) {
roryhand 45:0e8e1f2ec5d2 612 Buffer1[place_hold1] = Sound1.data_sptr[channel];
roryhand 1:aac37edee302 613 place_hold1 = place_hold1 + 1;
roryhand 1:aac37edee302 614 if(place_hold1 >= BufferLen) {
roryhand 1:aac37edee302 615 while(1) {
roryhand 1:aac37edee302 616 if(flag1 == 0) {
roryhand 1:aac37edee302 617 break;
roryhand 1:aac37edee302 618 }//if(flag1 == 0)
roryhand 1:aac37edee302 619
roryhand 1:aac37edee302 620
roryhand 1:aac37edee302 621 }//while(1)
roryhand 1:aac37edee302 622 }//if(place_hold1 > = BufferLen)
roryhand 0:e89d7a0bfa3b 623
roryhand 1:aac37edee302 624 } else if(flag2 == 1) {
roryhand 45:0e8e1f2ec5d2 625 Buffer2[place_hold2] = Sound1.data_sptr[channel];
roryhand 1:aac37edee302 626 place_hold2 = place_hold2 + 1;
roryhand 1:aac37edee302 627 if(place_hold2 >= BufferLen) {
roryhand 1:aac37edee302 628 while(1) {
roryhand 1:aac37edee302 629
roryhand 1:aac37edee302 630 if(flag2 == 0) {
roryhand 1:aac37edee302 631 break;
roryhand 1:aac37edee302 632 }
roryhand 1:aac37edee302 633 }
roryhand 0:e89d7a0bfa3b 634 }
roryhand 1:aac37edee302 635
roryhand 1:aac37edee302 636 }
roryhand 0:e89d7a0bfa3b 637 }
roryhand 0:e89d7a0bfa3b 638 }
roryhand 59:8e7c25a915a0 639 slice1 = slice1 + 2;//increment up by the number of bytes per Audio data sample! Makes sense it is 2 bytes, as this is 16-bit data.
roryhand 45:0e8e1f2ec5d2 640 slice2 = slice2 + 1;
roryhand 45:0e8e1f2ec5d2 641 slice3 = slice3 + 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 0:e89d7a0bfa3b 648
roryhand 0:e89d7a0bfa3b 649
roryhand 59:8e7c25a915a0 650
roryhand 59:8e7c25a915a0 651