does not work yet. Sound starts but then stops after a few seconds, whole thing hangs. Published so as I can import through mbed CLI.

Dependencies:   mbed sinelookup SDFileSystem_Copy_of_mbed_version I2S

Committer:
roryhand
Date:
Sat Oct 06 11:34:37 2018 +0000
Revision:
3:6fba0a044d85
Parent:
2:05b426733282
Child:
4:1a9482a07ee2
version that makes a horrid buzzing noise from codec on setup.  then a different buzzing noise when playing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
roryhand 0:004cb0595aa8 1
roryhand 0:004cb0595aa8 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:004cb0595aa8 3 //however if i use the write function with 4 arguments (as opposed to just 1 argument) then it doesnt work
roryhand 0:004cb0595aa8 4 //only works with the 1 argument version!!!
roryhand 0:004cb0595aa8 5
roryhand 0:004cb0595aa8 6
roryhand 0:004cb0595aa8 7 //THIS VERSION WORKED, CHANGED SOME THINGS, THEN CHANGED THEM BACK. NOW IT NO LONGER WORKS!!!!
roryhand 0:004cb0595aa8 8 #include "mbed.h"
roryhand 0:004cb0595aa8 9 #include "sinelookup.h"
roryhand 0:004cb0595aa8 10 #include "I2S.h"
roryhand 0:004cb0595aa8 11 #define sample_freq 48000
roryhand 0:004cb0595aa8 12 DigitalOut myled(LED1);
roryhand 0:004cb0595aa8 13 DigitalOut led2(LED2);
roryhand 0:004cb0595aa8 14 DigitalOut led3(LED3);
roryhand 0:004cb0595aa8 15 Ticker sampletick;
roryhand 0:004cb0595aa8 16 Timer t;
roryhand 0:004cb0595aa8 17 #include "SDFileSystem.h"
roryhand 0:004cb0595aa8 18 #include "WOLFSON_config.h"
roryhand 0:004cb0595aa8 19 Serial pc(USBTX, USBRX); // tx, rx //FOR DEBUGGING PROGRAM USING GNU SCREEN
roryhand 0:004cb0595aa8 20 DigitalOut cs(p8);
roryhand 0:004cb0595aa8 21 I2S i2s(I2S_TRANSMIT,p5,p6,p7);
roryhand 0:004cb0595aa8 22 I2C i2c(p9, p10);
roryhand 0:004cb0595aa8 23
roryhand 0:004cb0595aa8 24 SDFileSystem sd(p11, p12, p13, p8, "sd"); // the new pinout that i am using
roryhand 0:004cb0595aa8 25
roryhand 0:004cb0595aa8 26 typedef struct uFMT_STRUCT{
roryhand 0:004cb0595aa8 27 short comp_code;
roryhand 0:004cb0595aa8 28 short num_channels;
roryhand 0:004cb0595aa8 29 unsigned sample_rate;
roryhand 0:004cb0595aa8 30 unsigned avg_Bps;
roryhand 0:004cb0595aa8 31 short block_align;
roryhand 0:004cb0595aa8 32 short sig_bps;
roryhand 0:004cb0595aa8 33 } FMT_STRUCT;
roryhand 0:004cb0595aa8 34
roryhand 0:004cb0595aa8 35 typedef struct uDATA_STRUCT {
roryhand 0:004cb0595aa8 36 unsigned subchunk2_ID;
roryhand 0:004cb0595aa8 37 unsigned subchunk2_size;
roryhand 0:004cb0595aa8 38 char * data_buf;
roryhand 0:004cb0595aa8 39 }DATA_STRUCT;
roryhand 0:004cb0595aa8 40 short hello;
roryhand 0:004cb0595aa8 41 int i = 0;
roryhand 0:004cb0595aa8 42 int h = 0;
roryhand 0:004cb0595aa8 43 int bufflen = 1;
roryhand 0:004cb0595aa8 44 int buffer[1];
roryhand 0:004cb0595aa8 45 int AudioFormat, NumChannels, SampleRate, BitsPerSample ;
roryhand 0:004cb0595aa8 46 char *slice_buf;
roryhand 0:004cb0595aa8 47 int *data_sptr;
roryhand 3:6fba0a044d85 48 unsigned char *data_bptr;
roryhand 3:6fba0a044d85 49 int *data_wptr;
roryhand 3:6fba0a044d85 50 unsigned channel;
roryhand 0:004cb0595aa8 51 int newvar;
roryhand 3:6fba0a044d85 52 long slice, num_slices;
roryhand 3:6fba0a044d85 53 int verbosity = 0;
roryhand 0:004cb0595aa8 54 //long long slice_value;
roryhand 0:004cb0595aa8 55 int slice_value;
roryhand 0:004cb0595aa8 56 FILE *my_wav;
roryhand 0:004cb0595aa8 57 FMT_STRUCT wav_format;
roryhand 0:004cb0595aa8 58 DATA_STRUCT wav_data;
roryhand 0:004cb0595aa8 59
roryhand 0:004cb0595aa8 60 Ticker flipper;
roryhand 0:004cb0595aa8 61 //test
roryhand 0:004cb0595aa8 62 void flip() {
roryhand 0:004cb0595aa8 63 led2 = !led2;
roryhand 0:004cb0595aa8 64 }
roryhand 0:004cb0595aa8 65
roryhand 0:004cb0595aa8 66
roryhand 0:004cb0595aa8 67 void isr()
roryhand 0:004cb0595aa8 68 {
roryhand 3:6fba0a044d85 69 buffer[0] = (*data_sptr)>>1;
roryhand 3:6fba0a044d85 70 myled = 1;
roryhand 3:6fba0a044d85 71 int bufflen = sizeof(buffer);
roryhand 3:6fba0a044d85 72 //t.start();
roryhand 0:004cb0595aa8 73 //buffer[0] = (int*)data_sptr;
roryhand 0:004cb0595aa8 74 //buffer[0] = newvar;
roryhand 0:004cb0595aa8 75 //buffer[0] = sine16lookup[i];//>>1;//sine16lookup[i];//scale down volume a bit on amp//how would this scale down the volume a bit??
roryhand 0:004cb0595aa8 76 //buffer[0] = *fp;
roryhand 0:004cb0595aa8 77 //printf("value: %d",data_sptr);
roryhand 3:6fba0a044d85 78 i2s.write(buffer, bufflen);//Send next PWM value to amp
roryhand 3:6fba0a044d85 79 //t.stop();
roryhand 3:6fba0a044d85 80 //printf("The time taken was %f seconds\n", t.read());
roryhand 3:6fba0a044d85 81 myled = 0;
roryhand 0:004cb0595aa8 82 }
roryhand 1:ec4e2020522c 83
roryhand 0:004cb0595aa8 84
roryhand 1:ec4e2020522c 85
roryhand 0:004cb0595aa8 86
roryhand 0:004cb0595aa8 87 void wm8731_setup(int chip_addr, int addr, int cmd){
roryhand 0:004cb0595aa8 88 addr = addr << 1;
roryhand 0:004cb0595aa8 89 addr = addr|((cmd >> 8 ))& 0x01;//mess around with order of operations
roryhand 0:004cb0595aa8 90 cmd = cmd&0xFF;
roryhand 0:004cb0595aa8 91 i2c.start();
roryhand 0:004cb0595aa8 92 i2c.write( chip_addr );
roryhand 0:004cb0595aa8 93 i2c.write( addr );
roryhand 0:004cb0595aa8 94 i2c.write( cmd );
roryhand 0:004cb0595aa8 95 i2c.stop();
roryhand 0:004cb0595aa8 96 }
roryhand 0:004cb0595aa8 97
roryhand 0:004cb0595aa8 98
roryhand 0:004cb0595aa8 99 void wm8731_config(void){
roryhand 0:004cb0595aa8 100 wm8731_setup( WM8731_ADDRESS, WM8731_REG_RESET, _WM8731_RESET );//1
roryhand 0:004cb0595aa8 101 wm8731_setup( WM8731_ADDRESS, WM8731_REG_LLINE_IN, _WM8731_LEFT_LINEIN );//2
roryhand 0:004cb0595aa8 102 wm8731_setup( WM8731_ADDRESS, WM8731_REG_RLINE_IN, _WM8731_RIGHT_LINEIN );//3
roryhand 0:004cb0595aa8 103 wm8731_setup( WM8731_ADDRESS, WM8731_REG_LHPHONE_OUT, _WM8731_LEFT_HP );//4
roryhand 0:004cb0595aa8 104 wm8731_setup( WM8731_ADDRESS, WM8731_REG_RHPHONE_OUT, _WM8731_RIGHT_HP );//5
roryhand 0:004cb0595aa8 105 wm8731_setup( WM8731_ADDRESS, WM8731_REG_ANALOG_PATH, _WM8731_ANALOGAUDIO );//6
roryhand 0:004cb0595aa8 106 wm8731_setup( WM8731_ADDRESS, WM8731_REG_DIGITAL_PATH, _WM8731_DIGITALAUDIO );//7
roryhand 0:004cb0595aa8 107 wm8731_setup( WM8731_ADDRESS, WM8731_REG_PDOWN_CTRL, _WM8731_POWER );//8
roryhand 0:004cb0595aa8 108 wm8731_setup( WM8731_ADDRESS, WM8731_REG_DIGITAL_IF, _WM8731_DAIF );//9
roryhand 0:004cb0595aa8 109 wm8731_setup( WM8731_ADDRESS, WM8731_REG_SAMPLING_CTRL, _WM8731_SAMPLING );//10
roryhand 0:004cb0595aa8 110 }
roryhand 0:004cb0595aa8 111
roryhand 0:004cb0595aa8 112 void wm8731_configTEST(void){
roryhand 0:004cb0595aa8 113 int addr = WM8731_REG_RESET;
roryhand 0:004cb0595aa8 114 int cmd = _WM8731_RESET;
roryhand 0:004cb0595aa8 115 addr = addr << 1;
roryhand 0:004cb0595aa8 116 addr = addr|((cmd >> 8 ))& 0x01;//mess around with order of operations
roryhand 0:004cb0595aa8 117 cmd = cmd&0xFF;
roryhand 0:004cb0595aa8 118 i2c.start();
roryhand 0:004cb0595aa8 119 i2c.write( WM8731_ADDRESS );
roryhand 0:004cb0595aa8 120 i2c.write( addr );
roryhand 0:004cb0595aa8 121 i2c.write( cmd );
roryhand 0:004cb0595aa8 122 i2c.stop();
roryhand 0:004cb0595aa8 123
roryhand 0:004cb0595aa8 124 addr = WM8731_REG_LLINE_IN;
roryhand 0:004cb0595aa8 125 cmd = _WM8731_LEFT_LINEIN;
roryhand 0:004cb0595aa8 126 addr = addr << 1;
roryhand 0:004cb0595aa8 127 addr = addr|((cmd >> 8 )) & 0x01;
roryhand 0:004cb0595aa8 128 cmd = cmd&0xFF;
roryhand 0:004cb0595aa8 129 i2c.start();
roryhand 0:004cb0595aa8 130 i2c.write( WM8731_ADDRESS );//take Fergus' suggestion and declare these to known values EARLIER in program
roryhand 0:004cb0595aa8 131 i2c.write( addr );
roryhand 0:004cb0595aa8 132 i2c.write( cmd );
roryhand 0:004cb0595aa8 133 i2c.stop();
roryhand 0:004cb0595aa8 134
roryhand 0:004cb0595aa8 135
roryhand 0:004cb0595aa8 136 addr = WM8731_REG_RLINE_IN;
roryhand 0:004cb0595aa8 137 cmd = _WM8731_RIGHT_LINEIN;
roryhand 0:004cb0595aa8 138 addr = addr << 1;
roryhand 0:004cb0595aa8 139 addr = addr|((cmd >> 8 )) & 0x01;
roryhand 0:004cb0595aa8 140 cmd = cmd&0xFF;
roryhand 0:004cb0595aa8 141 i2c.start();
roryhand 0:004cb0595aa8 142 i2c.write( WM8731_ADDRESS );//take Fergus' suggestion and declare these to known values EARLIER in program
roryhand 0:004cb0595aa8 143 i2c.write( addr );
roryhand 0:004cb0595aa8 144 i2c.write( cmd );
roryhand 0:004cb0595aa8 145 i2c.stop();
roryhand 0:004cb0595aa8 146
roryhand 0:004cb0595aa8 147
roryhand 0:004cb0595aa8 148 //4
roryhand 0:004cb0595aa8 149 addr = WM8731_REG_LHPHONE_OUT;
roryhand 0:004cb0595aa8 150 cmd = _WM8731_LEFT_HP;
roryhand 0:004cb0595aa8 151 addr = addr << 1;
roryhand 0:004cb0595aa8 152 addr = addr|((cmd >> 8 )) & 0x01;
roryhand 0:004cb0595aa8 153 cmd = cmd&0xFF;
roryhand 0:004cb0595aa8 154 i2c.start();
roryhand 0:004cb0595aa8 155 i2c.write( WM8731_ADDRESS );
roryhand 0:004cb0595aa8 156 i2c.write( addr );//WM8731_REG_LHPHONE_OUT, _WM8731_LEFT_HP
roryhand 0:004cb0595aa8 157 i2c.write(cmd);
roryhand 0:004cb0595aa8 158 i2c.stop();
roryhand 0:004cb0595aa8 159
roryhand 0:004cb0595aa8 160 //5
roryhand 0:004cb0595aa8 161 addr = WM8731_REG_RHPHONE_OUT;
roryhand 0:004cb0595aa8 162 cmd = _WM8731_RIGHT_HP;
roryhand 0:004cb0595aa8 163 addr = addr << 1;
roryhand 0:004cb0595aa8 164 addr = addr|((cmd >> 8 )) & 0x01;
roryhand 0:004cb0595aa8 165 cmd = cmd&0xFF;
roryhand 0:004cb0595aa8 166 i2c.start();
roryhand 0:004cb0595aa8 167 i2c.write( WM8731_ADDRESS );//WM8731_REG_RHPHONE_OUT, _WM8731_RIGHT_HP
roryhand 0:004cb0595aa8 168 i2c.write( addr );
roryhand 0:004cb0595aa8 169 i2c.write( cmd );
roryhand 0:004cb0595aa8 170 i2c.stop();
roryhand 0:004cb0595aa8 171
roryhand 0:004cb0595aa8 172
roryhand 0:004cb0595aa8 173 //6
roryhand 0:004cb0595aa8 174 addr = WM8731_REG_ANALOG_PATH;
roryhand 0:004cb0595aa8 175 cmd = _WM8731_ANALOGAUDIO;
roryhand 0:004cb0595aa8 176 addr = addr << 1;
roryhand 0:004cb0595aa8 177 addr = addr|((cmd >> 8 )) & 0x01;
roryhand 0:004cb0595aa8 178 cmd = cmd&0xFF;
roryhand 0:004cb0595aa8 179 i2c.start();
roryhand 0:004cb0595aa8 180 i2c.write( WM8731_ADDRESS );//WM8731_REG_ANALOG_PATH, _WM8731_ANALOGAUDIO
roryhand 0:004cb0595aa8 181 i2c.write( addr );
roryhand 0:004cb0595aa8 182 i2c.write( cmd );
roryhand 0:004cb0595aa8 183 i2c.stop();
roryhand 0:004cb0595aa8 184
roryhand 0:004cb0595aa8 185 //7
roryhand 0:004cb0595aa8 186 addr = WM8731_REG_DIGITAL_PATH;
roryhand 0:004cb0595aa8 187 cmd = _WM8731_DIGITALAUDIO;
roryhand 0:004cb0595aa8 188 addr = addr << 1;
roryhand 0:004cb0595aa8 189 addr = addr|((cmd >> 8 )) & 0x01;
roryhand 0:004cb0595aa8 190 cmd = cmd&0xFF;
roryhand 0:004cb0595aa8 191 i2c.start();
roryhand 0:004cb0595aa8 192 i2c.write( WM8731_ADDRESS );//WM8731_REG_DIGITAL_PATH, _WM8731_DIGITALAUDIO
roryhand 0:004cb0595aa8 193 i2c.write( addr );
roryhand 0:004cb0595aa8 194 i2c.write( cmd );
roryhand 0:004cb0595aa8 195 i2c.stop();
roryhand 0:004cb0595aa8 196
roryhand 0:004cb0595aa8 197 //8
roryhand 0:004cb0595aa8 198 addr = WM8731_REG_PDOWN_CTRL;
roryhand 0:004cb0595aa8 199 cmd = _WM8731_POWER;
roryhand 0:004cb0595aa8 200 addr = addr << 1;
roryhand 0:004cb0595aa8 201 addr = addr|((cmd >> 8 )) & 0x01;
roryhand 0:004cb0595aa8 202 cmd = cmd&0xFF;
roryhand 0:004cb0595aa8 203 i2c.start();
roryhand 0:004cb0595aa8 204 i2c.write( WM8731_ADDRESS );//WM8731_REG_PDOWN_CTRL, _WM8731_POWER)
roryhand 0:004cb0595aa8 205 i2c.write( addr );
roryhand 0:004cb0595aa8 206 i2c.write( cmd );
roryhand 0:004cb0595aa8 207 i2c.stop();
roryhand 0:004cb0595aa8 208
roryhand 0:004cb0595aa8 209
roryhand 0:004cb0595aa8 210 //9
roryhand 0:004cb0595aa8 211 addr = WM8731_REG_DIGITAL_IF;
roryhand 0:004cb0595aa8 212 cmd = _WM8731_DAIF;
roryhand 0:004cb0595aa8 213 addr = addr << 1;
roryhand 0:004cb0595aa8 214 addr = addr|((cmd >> 8 )) & 0x01;
roryhand 0:004cb0595aa8 215 cmd = cmd&0xFF;
roryhand 0:004cb0595aa8 216 i2c.start();
roryhand 0:004cb0595aa8 217 i2c.write( WM8731_ADDRESS );
roryhand 0:004cb0595aa8 218 i2c.write( addr );//WM8731_REG_DIGITAL_IF, _WM8731_DAIF
roryhand 0:004cb0595aa8 219 i2c.write( cmd );
roryhand 0:004cb0595aa8 220 i2c.stop();
roryhand 0:004cb0595aa8 221
roryhand 0:004cb0595aa8 222
roryhand 0:004cb0595aa8 223 //10
roryhand 0:004cb0595aa8 224 addr = WM8731_REG_SAMPLING_CTRL;
roryhand 0:004cb0595aa8 225 cmd = _WM8731_SAMPLING;
roryhand 0:004cb0595aa8 226 addr = addr << 1;
roryhand 0:004cb0595aa8 227 addr = addr|((cmd >> 8 )) & 0x01;
roryhand 0:004cb0595aa8 228 cmd = cmd&0xFF;
roryhand 0:004cb0595aa8 229 i2c.start();
roryhand 0:004cb0595aa8 230 i2c.write( WM8731_ADDRESS );//WM8731_REG_SAMPLING_CTRL, _WM8731_SAMPLING
roryhand 0:004cb0595aa8 231 i2c.write( addr );
roryhand 0:004cb0595aa8 232 i2c.write( cmd );
roryhand 0:004cb0595aa8 233 i2c.stop();
roryhand 0:004cb0595aa8 234
roryhand 0:004cb0595aa8 235
roryhand 0:004cb0595aa8 236
roryhand 0:004cb0595aa8 237
roryhand 0:004cb0595aa8 238 //11
roryhand 0:004cb0595aa8 239 addr = WM8731_REG_ACTIVE_CTRL;
roryhand 0:004cb0595aa8 240 cmd = _WM8731_ACTIVATE;
roryhand 0:004cb0595aa8 241 addr = addr << 1;
roryhand 0:004cb0595aa8 242 addr = addr|((cmd >> 8 )) & 0x01;
roryhand 0:004cb0595aa8 243 cmd = cmd&0xFF;
roryhand 0:004cb0595aa8 244 i2c.start();
roryhand 0:004cb0595aa8 245 i2c.write( WM8731_ADDRESS );//WM8731_REG_SAMPLING_CTRL, _WM8731_SAMPLING
roryhand 0:004cb0595aa8 246 i2c.write( addr );
roryhand 0:004cb0595aa8 247 i2c.write( cmd );//there is also a deactive version!!
roryhand 0:004cb0595aa8 248 i2c.stop();
roryhand 0:004cb0595aa8 249
roryhand 0:004cb0595aa8 250
roryhand 0:004cb0595aa8 251
roryhand 0:004cb0595aa8 252 }
roryhand 0:004cb0595aa8 253
roryhand 0:004cb0595aa8 254 int main() {
roryhand 0:004cb0595aa8 255 FMT_STRUCT wav_format;
roryhand 0:004cb0595aa8 256 char data[2];
roryhand 0:004cb0595aa8 257 pc.printf("Beginning of program\n");
roryhand 0:004cb0595aa8 258 data[0] = 0x0F;
roryhand 0:004cb0595aa8 259 data[1] = 0x00;
roryhand 0:004cb0595aa8 260 FILE *my_wav;
roryhand 0:004cb0595aa8 261 //my_wav = fopen("/sd/mydir/gp40.wav", "rb");//i think that it is "w" for write, "r" for read.
roryhand 0:004cb0595aa8 262 my_wav = fopen("/sd/mydir/gp40_48K.wav", "rb");//i think that it is "w" for write, "r" for read.
roryhand 0:004cb0595aa8 263 fseek(my_wav, 20, SEEK_SET); // set pointer to byte 20
roryhand 0:004cb0595aa8 264 fread(&AudioFormat, 2, 1, my_wav); // check file is PCM
roryhand 0:004cb0595aa8 265 if (AudioFormat==0x01) {
roryhand 0:004cb0595aa8 266 printf("Wav file is PCM data\n\r");
roryhand 0:004cb0595aa8 267 }
roryhand 0:004cb0595aa8 268 else {
roryhand 0:004cb0595aa8 269 printf("Wav file is not PCM data\n\r");
roryhand 0:004cb0595aa8 270 }
roryhand 0:004cb0595aa8 271
roryhand 0:004cb0595aa8 272 fseek (my_wav,20,SEEK_SET);
roryhand 0:004cb0595aa8 273 fread(&wav_format,sizeof(wav_format),1,my_wav);
roryhand 0:004cb0595aa8 274 printf("%d block align\n",wav_format.block_align);
roryhand 0:004cb0595aa8 275 printf(" %d channels\n",wav_format.num_channels);
roryhand 0:004cb0595aa8 276
roryhand 0:004cb0595aa8 277
roryhand 0:004cb0595aa8 278 fseek(my_wav, 36, SEEK_SET);
roryhand 0:004cb0595aa8 279 fread(&wav_data.subchunk2_ID,4,1,my_wav);
roryhand 0:004cb0595aa8 280 fseek(my_wav, 40, SEEK_SET);
roryhand 0:004cb0595aa8 281 fread(&wav_data.subchunk2_size,4,1,my_wav);
roryhand 0:004cb0595aa8 282 printf("DATA chunck\n");
roryhand 0:004cb0595aa8 283 printf(" %d subchunk 2 ID\n",wav_data.subchunk2_ID);
roryhand 0:004cb0595aa8 284 printf(" %d subchunk 2 size\n",wav_data.subchunk2_size);
roryhand 0:004cb0595aa8 285
roryhand 0:004cb0595aa8 286
roryhand 0:004cb0595aa8 287
roryhand 0:004cb0595aa8 288
roryhand 0:004cb0595aa8 289
roryhand 0:004cb0595aa8 290
roryhand 0:004cb0595aa8 291 i2c.frequency(100000);
roryhand 0:004cb0595aa8 292 long j = 0;
roryhand 0:004cb0595aa8 293
roryhand 0:004cb0595aa8 294 printf("set up the codec\n\r");
roryhand 3:6fba0a044d85 295 while(j <1000){
roryhand 0:004cb0595aa8 296 wm8731_configTEST();
roryhand 3:6fba0a044d85 297 j = j + 1;
roryhand 3:6fba0a044d85 298 }
roryhand 0:004cb0595aa8 299 //i2s audio data transfer code??
roryhand 0:004cb0595aa8 300 i2s.stereomono(I2S_STEREO);
roryhand 0:004cb0595aa8 301 i2s.masterslave(I2S_MASTER);//MASTER definitely works!
roryhand 0:004cb0595aa8 302 i2s.frequency(sample_freq);
roryhand 0:004cb0595aa8 303 int yes = i2s.setup_ok();
roryhand 0:004cb0595aa8 304 pc.printf("Setup went ok?: %d\n", yes);//0 = ack, 1 = Nack*/
roryhand 0:004cb0595aa8 305 printf("can we get to this point? 1\n\r");
roryhand 0:004cb0595aa8 306 led3 = 1;
roryhand 0:004cb0595aa8 307 //myled = 1;
roryhand 0:004cb0595aa8 308 led2 = 1;
roryhand 0:004cb0595aa8 309 printf("can we get to this point? 2\n\r");
roryhand 0:004cb0595aa8 310
roryhand 0:004cb0595aa8 311 myled = 1;
roryhand 0:004cb0595aa8 312 i2s.start();
roryhand 0:004cb0595aa8 313 //while (1) {
roryhand 0:004cb0595aa8 314 fread(&wav_format,sizeof(wav_format),1,my_wav);
roryhand 0:004cb0595aa8 315 printf("wav_format %d\n\r",wav_format);
roryhand 1:ec4e2020522c 316
roryhand 0:004cb0595aa8 317
roryhand 0:004cb0595aa8 318 fseek(my_wav,20,SEEK_SET);
roryhand 0:004cb0595aa8 319 fread(&wav_format,sizeof(wav_format),1,my_wav);
roryhand 0:004cb0595aa8 320 fseek(my_wav,36,SEEK_SET);
roryhand 0:004cb0595aa8 321 fread(&wav_data,sizeof(wav_data),1,my_wav);
roryhand 2:05b426733282 322 int num_slices = wav_data.subchunk2_size/wav_format.block_align;
roryhand 0:004cb0595aa8 323 printf("wav_data.subchunk2_size: %d\n\r",wav_data.subchunk2_size);
roryhand 0:004cb0595aa8 324 printf("wav_format.block_align: %d\n\r",wav_format.block_align);
roryhand 0:004cb0595aa8 325 printf("num_slices: %d\n\r",num_slices);
roryhand 0:004cb0595aa8 326 printf("num_slices*wav_format.block_align %d\n\r",num_slices*wav_format.block_align);
roryhand 0:004cb0595aa8 327 printf("wav_format.num_channels*wav_format.sig_bps/8: %d\n\r",wav_format.num_channels*wav_format.sig_bps/8);
roryhand 3:6fba0a044d85 328 printf("chunk_size - sizeof(wav_format) %d\n\r",wav_data.subchunk2_size-sizeof(wav_format));
roryhand 0:004cb0595aa8 329 printf("sizeof(wav_format): %d\n\r",sizeof(wav_format));
roryhand 3:6fba0a044d85 330 printf("do we egt to this point:\n\r");
roryhand 3:6fba0a044d85 331 sampletick.attach(&isr,1/96000);
roryhand 0:004cb0595aa8 332 //while(!feof(my_wav)){
roryhand 0:004cb0595aa8 333 //while(1){
roryhand 0:004cb0595aa8 334 //if (wav_data.subchunk2_size > sizeof(wav_format)){
roryhand 0:004cb0595aa8 335 //fseek(my_wav,wav_data.subchunk2_size-sizeof(wav_format),SEEK_CUR);
roryhand 0:004cb0595aa8 336 /*if(wav_data.subchunk2_size > (num_slices*wav_format.block_align)){
roryhand 0:004cb0595aa8 337 fseek(my_wav,wav_data.subchunk2_size - (num_slices*wav_format.block_align),SEEK_CUR);
roryhand 0:004cb0595aa8 338 printf("chunk_size - sizeof(wav_format) %d\n\r",wav_data.subchunk2_size-sizeof(wav_format));
roryhand 0:004cb0595aa8 339 printf("sizeof(wav_format): %d\n\r",sizeof(wav_format));
roryhand 0:004cb0595aa8 340 }
roryhand 0:004cb0595aa8 341 else {
roryhand 0:004cb0595aa8 342
roryhand 0:004cb0595aa8 343 break;
roryhand 0:004cb0595aa8 344 }*/
roryhand 0:004cb0595aa8 345
roryhand 3:6fba0a044d85 346 slice_buf=(char *)malloc(wav_format.block_align);
roryhand 2:05b426733282 347 for (slice=0;slice<num_slices;slice+=1) {
roryhand 3:6fba0a044d85 348 //t.start();
roryhand 3:6fba0a044d85 349 fread(slice_buf,wav_format.block_align,1,my_wav);//THIS IS WHERE HE READS IN THE DATA, TO SLICE_BUF. BUT USES WAV_FORMAT.BLOCKALIGN AS HIS SIZE GUIDE
roryhand 2:05b426733282 350 //I do not understand why he is not "seeking" through the file first though...
roryhand 3:6fba0a044d85 351 if (feof(my_wav)){
roryhand 2:05b426733282 352 printf("Oops -- not enough slices in the wave file\n");
roryhand 2:05b426733282 353 exit(1);
roryhand 2:05b426733282 354 }
roryhand 3:6fba0a044d85 355 data_sptr=(int *)slice_buf; // 16 bit samples
roryhand 2:05b426733282 356 data_bptr=(unsigned char *)slice_buf; // 8 bit samples
roryhand 2:05b426733282 357 data_wptr=(int *)slice_buf; // 32 bit samples
roryhand 2:05b426733282 358 slice_value=0;
roryhand 2:05b426733282 359 for (channel=0;channel<wav_format.num_channels;channel++) {
roryhand 2:05b426733282 360 switch (wav_format.sig_bps) {
roryhand 2:05b426733282 361 case 16:
roryhand 2:05b426733282 362 if (verbosity)
roryhand 2:05b426733282 363 printf("16 bit channel %d data=%d ",channel,data_sptr[channel]);
roryhand 2:05b426733282 364 slice_value+=data_sptr[channel];
roryhand 2:05b426733282 365 break;
roryhand 2:05b426733282 366 case 32:
roryhand 2:05b426733282 367 if (verbosity)
roryhand 2:05b426733282 368 printf("32 bit channel %d data=%d ",channel,data_wptr[channel]);
roryhand 2:05b426733282 369 slice_value+=data_wptr[channel];
roryhand 2:05b426733282 370 break;
roryhand 2:05b426733282 371 case 8:
roryhand 2:05b426733282 372 if (verbosity)
roryhand 2:05b426733282 373 printf("8 bit channel %d data=%d ",channel,(int)data_bptr[channel]);
roryhand 2:05b426733282 374 slice_value+=data_bptr[channel];
roryhand 2:05b426733282 375 break;
roryhand 2:05b426733282 376 }
roryhand 2:05b426733282 377 }
roryhand 2:05b426733282 378 slice_value/=wav_format.num_channels;
roryhand 2:05b426733282 379
roryhand 2:05b426733282 380 // slice_value is now averaged. Next it needs to be scaled to an unsigned 16 bit value
roryhand 2:05b426733282 381 // with DC offset so it can be written to the DAC.
roryhand 2:05b426733282 382 switch (wav_format.sig_bps) {
roryhand 2:05b426733282 383 case 8: slice_value<<=8;
roryhand 2:05b426733282 384 break;
roryhand 2:05b426733282 385 case 16: slice_value+=32768;
roryhand 2:05b426733282 386 break;
roryhand 2:05b426733282 387 case 32: slice_value>>=16;
roryhand 2:05b426733282 388 slice_value+=32768;
roryhand 2:05b426733282 389 break;
roryhand 2:05b426733282 390 }
roryhand 3:6fba0a044d85 391 //t.stop();
roryhand 3:6fba0a044d85 392 //printf("The time taken was %f seconds\n", t.read());
roryhand 3:6fba0a044d85 393 }
roryhand 3:6fba0a044d85 394 /* dac_data=(short unsigned)slice_value;
roryhand 2:05b426733282 395 if (verbosity)
roryhand 2:05b426733282 396 printf("sample %d wptr %d slice_value %d dac_data %u\n",slice,DAC_wptr,(int)slice_value,dac_data);
roryhand 2:05b426733282 397 DAC_fifo[DAC_wptr]=dac_data;
roryhand 2:05b426733282 398 DAC_wptr=(DAC_wptr+1) & 0xff;
roryhand 2:05b426733282 399 while (DAC_wptr==DAC_rptr) {
roryhand 2:05b426733282 400 }
roryhand 3:6fba0a044d85 401 }*/
roryhand 3:6fba0a044d85 402
roryhand 2:05b426733282 403
roryhand 1:ec4e2020522c 404
roryhand 0:004cb0595aa8 405 sampletick.detach();
roryhand 0:004cb0595aa8 406 i2s.stop();
roryhand 0:004cb0595aa8 407
roryhand 0:004cb0595aa8 408 fclose(my_wav);
roryhand 0:004cb0595aa8 409 printf("File is now closed.");
roryhand 0:004cb0595aa8 410 return 0;
roryhand 0:004cb0595aa8 411 }
roryhand 0:004cb0595aa8 412