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:
Mon Oct 01 20:33:33 2018 +0000
Revision:
2:05b426733282
Parent:
1:ec4e2020522c
Child:
3:6fba0a044d85
probably doesnt work.  copied the for loop right out of the wavplayer code to see why mine does not work

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