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
wolfson_3_wav.cpp@4:1a9482a07ee2, 2018-10-06 (annotated)
- Committer:
- roryhand
- Date:
- Sat Oct 06 11:51:23 2018 +0000
- Revision:
- 4:1a9482a07ee2
- Parent:
- 3:6fba0a044d85
- Child:
- 5:5b120a2d9249
plays a very bad quality sound out through i2s WITHOUT using interrupt routine. not really sure why....;
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:1a9482a07ee2 | 72 | printf("buffer data: %d \n\r",buffer); |
roryhand | 4:1a9482a07ee2 | 73 | printf("data_sptr data: %d \n\r",data_sptr); |
roryhand | 3:6fba0a044d85 | 74 | //t.start(); |
roryhand | 0:004cb0595aa8 | 75 | //buffer[0] = (int*)data_sptr; |
roryhand | 0:004cb0595aa8 | 76 | //buffer[0] = newvar; |
roryhand | 0:004cb0595aa8 | 77 | //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 | 78 | //buffer[0] = *fp; |
roryhand | 0:004cb0595aa8 | 79 | //printf("value: %d",data_sptr); |
roryhand | 4:1a9482a07ee2 | 80 | i2s.write(data_sptr, bufflen);//Send next PWM value to amp |
roryhand | 3:6fba0a044d85 | 81 | //t.stop(); |
roryhand | 3:6fba0a044d85 | 82 | //printf("The time taken was %f seconds\n", t.read()); |
roryhand | 3:6fba0a044d85 | 83 | myled = 0; |
roryhand | 0:004cb0595aa8 | 84 | } |
roryhand | 1:ec4e2020522c | 85 | |
roryhand | 0:004cb0595aa8 | 86 | |
roryhand | 1:ec4e2020522c | 87 | |
roryhand | 0:004cb0595aa8 | 88 | |
roryhand | 0:004cb0595aa8 | 89 | void wm8731_setup(int chip_addr, int addr, int cmd){ |
roryhand | 0:004cb0595aa8 | 90 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 91 | addr = addr|((cmd >> 8 ))& 0x01;//mess around with order of operations |
roryhand | 0:004cb0595aa8 | 92 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 93 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 94 | i2c.write( chip_addr ); |
roryhand | 0:004cb0595aa8 | 95 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 96 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 97 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 98 | } |
roryhand | 0:004cb0595aa8 | 99 | |
roryhand | 0:004cb0595aa8 | 100 | |
roryhand | 0:004cb0595aa8 | 101 | void wm8731_config(void){ |
roryhand | 0:004cb0595aa8 | 102 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_RESET, _WM8731_RESET );//1 |
roryhand | 0:004cb0595aa8 | 103 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_LLINE_IN, _WM8731_LEFT_LINEIN );//2 |
roryhand | 0:004cb0595aa8 | 104 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_RLINE_IN, _WM8731_RIGHT_LINEIN );//3 |
roryhand | 0:004cb0595aa8 | 105 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_LHPHONE_OUT, _WM8731_LEFT_HP );//4 |
roryhand | 0:004cb0595aa8 | 106 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_RHPHONE_OUT, _WM8731_RIGHT_HP );//5 |
roryhand | 0:004cb0595aa8 | 107 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_ANALOG_PATH, _WM8731_ANALOGAUDIO );//6 |
roryhand | 0:004cb0595aa8 | 108 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_DIGITAL_PATH, _WM8731_DIGITALAUDIO );//7 |
roryhand | 0:004cb0595aa8 | 109 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_PDOWN_CTRL, _WM8731_POWER );//8 |
roryhand | 0:004cb0595aa8 | 110 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_DIGITAL_IF, _WM8731_DAIF );//9 |
roryhand | 0:004cb0595aa8 | 111 | wm8731_setup( WM8731_ADDRESS, WM8731_REG_SAMPLING_CTRL, _WM8731_SAMPLING );//10 |
roryhand | 0:004cb0595aa8 | 112 | } |
roryhand | 0:004cb0595aa8 | 113 | |
roryhand | 0:004cb0595aa8 | 114 | void wm8731_configTEST(void){ |
roryhand | 0:004cb0595aa8 | 115 | int addr = WM8731_REG_RESET; |
roryhand | 0:004cb0595aa8 | 116 | int cmd = _WM8731_RESET; |
roryhand | 0:004cb0595aa8 | 117 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 118 | addr = addr|((cmd >> 8 ))& 0x01;//mess around with order of operations |
roryhand | 0:004cb0595aa8 | 119 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 120 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 121 | i2c.write( WM8731_ADDRESS ); |
roryhand | 0:004cb0595aa8 | 122 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 123 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 124 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 125 | |
roryhand | 0:004cb0595aa8 | 126 | addr = WM8731_REG_LLINE_IN; |
roryhand | 0:004cb0595aa8 | 127 | cmd = _WM8731_LEFT_LINEIN; |
roryhand | 0:004cb0595aa8 | 128 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 129 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 130 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 131 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 132 | i2c.write( WM8731_ADDRESS );//take Fergus' suggestion and declare these to known values EARLIER in program |
roryhand | 0:004cb0595aa8 | 133 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 134 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 135 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 136 | |
roryhand | 0:004cb0595aa8 | 137 | |
roryhand | 0:004cb0595aa8 | 138 | addr = WM8731_REG_RLINE_IN; |
roryhand | 0:004cb0595aa8 | 139 | cmd = _WM8731_RIGHT_LINEIN; |
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 );//take Fergus' suggestion and declare these to known values EARLIER in program |
roryhand | 0:004cb0595aa8 | 145 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 146 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 147 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 148 | |
roryhand | 0:004cb0595aa8 | 149 | |
roryhand | 0:004cb0595aa8 | 150 | //4 |
roryhand | 0:004cb0595aa8 | 151 | addr = WM8731_REG_LHPHONE_OUT; |
roryhand | 0:004cb0595aa8 | 152 | cmd = _WM8731_LEFT_HP; |
roryhand | 0:004cb0595aa8 | 153 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 154 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 155 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 156 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 157 | i2c.write( WM8731_ADDRESS ); |
roryhand | 0:004cb0595aa8 | 158 | i2c.write( addr );//WM8731_REG_LHPHONE_OUT, _WM8731_LEFT_HP |
roryhand | 0:004cb0595aa8 | 159 | i2c.write(cmd); |
roryhand | 0:004cb0595aa8 | 160 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 161 | |
roryhand | 0:004cb0595aa8 | 162 | //5 |
roryhand | 0:004cb0595aa8 | 163 | addr = WM8731_REG_RHPHONE_OUT; |
roryhand | 0:004cb0595aa8 | 164 | cmd = _WM8731_RIGHT_HP; |
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_RHPHONE_OUT, _WM8731_RIGHT_HP |
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 | |
roryhand | 0:004cb0595aa8 | 175 | //6 |
roryhand | 0:004cb0595aa8 | 176 | addr = WM8731_REG_ANALOG_PATH; |
roryhand | 0:004cb0595aa8 | 177 | cmd = _WM8731_ANALOGAUDIO; |
roryhand | 0:004cb0595aa8 | 178 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 179 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 180 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 181 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 182 | i2c.write( WM8731_ADDRESS );//WM8731_REG_ANALOG_PATH, _WM8731_ANALOGAUDIO |
roryhand | 0:004cb0595aa8 | 183 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 184 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 185 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 186 | |
roryhand | 0:004cb0595aa8 | 187 | //7 |
roryhand | 0:004cb0595aa8 | 188 | addr = WM8731_REG_DIGITAL_PATH; |
roryhand | 0:004cb0595aa8 | 189 | cmd = _WM8731_DIGITALAUDIO; |
roryhand | 0:004cb0595aa8 | 190 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 191 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 192 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 193 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 194 | i2c.write( WM8731_ADDRESS );//WM8731_REG_DIGITAL_PATH, _WM8731_DIGITALAUDIO |
roryhand | 0:004cb0595aa8 | 195 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 196 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 197 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 198 | |
roryhand | 0:004cb0595aa8 | 199 | //8 |
roryhand | 0:004cb0595aa8 | 200 | addr = WM8731_REG_PDOWN_CTRL; |
roryhand | 0:004cb0595aa8 | 201 | cmd = _WM8731_POWER; |
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 );//WM8731_REG_PDOWN_CTRL, _WM8731_POWER) |
roryhand | 0:004cb0595aa8 | 207 | i2c.write( addr ); |
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 | //9 |
roryhand | 0:004cb0595aa8 | 213 | addr = WM8731_REG_DIGITAL_IF; |
roryhand | 0:004cb0595aa8 | 214 | cmd = _WM8731_DAIF; |
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 ); |
roryhand | 0:004cb0595aa8 | 220 | i2c.write( addr );//WM8731_REG_DIGITAL_IF, _WM8731_DAIF |
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 | //10 |
roryhand | 0:004cb0595aa8 | 226 | addr = WM8731_REG_SAMPLING_CTRL; |
roryhand | 0:004cb0595aa8 | 227 | cmd = _WM8731_SAMPLING; |
roryhand | 0:004cb0595aa8 | 228 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 229 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 230 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 231 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 232 | i2c.write( WM8731_ADDRESS );//WM8731_REG_SAMPLING_CTRL, _WM8731_SAMPLING |
roryhand | 0:004cb0595aa8 | 233 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 234 | i2c.write( cmd ); |
roryhand | 0:004cb0595aa8 | 235 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 236 | |
roryhand | 0:004cb0595aa8 | 237 | |
roryhand | 0:004cb0595aa8 | 238 | |
roryhand | 0:004cb0595aa8 | 239 | |
roryhand | 0:004cb0595aa8 | 240 | //11 |
roryhand | 0:004cb0595aa8 | 241 | addr = WM8731_REG_ACTIVE_CTRL; |
roryhand | 0:004cb0595aa8 | 242 | cmd = _WM8731_ACTIVATE; |
roryhand | 0:004cb0595aa8 | 243 | addr = addr << 1; |
roryhand | 0:004cb0595aa8 | 244 | addr = addr|((cmd >> 8 )) & 0x01; |
roryhand | 0:004cb0595aa8 | 245 | cmd = cmd&0xFF; |
roryhand | 0:004cb0595aa8 | 246 | i2c.start(); |
roryhand | 0:004cb0595aa8 | 247 | i2c.write( WM8731_ADDRESS );//WM8731_REG_SAMPLING_CTRL, _WM8731_SAMPLING |
roryhand | 0:004cb0595aa8 | 248 | i2c.write( addr ); |
roryhand | 0:004cb0595aa8 | 249 | i2c.write( cmd );//there is also a deactive version!! |
roryhand | 0:004cb0595aa8 | 250 | i2c.stop(); |
roryhand | 0:004cb0595aa8 | 251 | |
roryhand | 0:004cb0595aa8 | 252 | |
roryhand | 0:004cb0595aa8 | 253 | |
roryhand | 0:004cb0595aa8 | 254 | } |
roryhand | 0:004cb0595aa8 | 255 | |
roryhand | 0:004cb0595aa8 | 256 | int main() { |
roryhand | 0:004cb0595aa8 | 257 | FMT_STRUCT wav_format; |
roryhand | 0:004cb0595aa8 | 258 | char data[2]; |
roryhand | 0:004cb0595aa8 | 259 | pc.printf("Beginning of program\n"); |
roryhand | 0:004cb0595aa8 | 260 | data[0] = 0x0F; |
roryhand | 0:004cb0595aa8 | 261 | data[1] = 0x00; |
roryhand | 0:004cb0595aa8 | 262 | FILE *my_wav; |
roryhand | 0:004cb0595aa8 | 263 | //my_wav = fopen("/sd/mydir/gp40.wav", "rb");//i think that it is "w" for write, "r" for read. |
roryhand | 0:004cb0595aa8 | 264 | my_wav = fopen("/sd/mydir/gp40_48K.wav", "rb");//i think that it is "w" for write, "r" for read. |
roryhand | 0:004cb0595aa8 | 265 | fseek(my_wav, 20, SEEK_SET); // set pointer to byte 20 |
roryhand | 0:004cb0595aa8 | 266 | fread(&AudioFormat, 2, 1, my_wav); // check file is PCM |
roryhand | 0:004cb0595aa8 | 267 | if (AudioFormat==0x01) { |
roryhand | 0:004cb0595aa8 | 268 | printf("Wav file is PCM data\n\r"); |
roryhand | 0:004cb0595aa8 | 269 | } |
roryhand | 0:004cb0595aa8 | 270 | else { |
roryhand | 0:004cb0595aa8 | 271 | printf("Wav file is not PCM data\n\r"); |
roryhand | 0:004cb0595aa8 | 272 | } |
roryhand | 0:004cb0595aa8 | 273 | |
roryhand | 0:004cb0595aa8 | 274 | fseek (my_wav,20,SEEK_SET); |
roryhand | 0:004cb0595aa8 | 275 | fread(&wav_format,sizeof(wav_format),1,my_wav); |
roryhand | 0:004cb0595aa8 | 276 | printf("%d block align\n",wav_format.block_align); |
roryhand | 0:004cb0595aa8 | 277 | printf(" %d channels\n",wav_format.num_channels); |
roryhand | 0:004cb0595aa8 | 278 | |
roryhand | 0:004cb0595aa8 | 279 | |
roryhand | 0:004cb0595aa8 | 280 | fseek(my_wav, 36, SEEK_SET); |
roryhand | 0:004cb0595aa8 | 281 | fread(&wav_data.subchunk2_ID,4,1,my_wav); |
roryhand | 0:004cb0595aa8 | 282 | fseek(my_wav, 40, SEEK_SET); |
roryhand | 0:004cb0595aa8 | 283 | fread(&wav_data.subchunk2_size,4,1,my_wav); |
roryhand | 0:004cb0595aa8 | 284 | printf("DATA chunck\n"); |
roryhand | 0:004cb0595aa8 | 285 | printf(" %d subchunk 2 ID\n",wav_data.subchunk2_ID); |
roryhand | 0:004cb0595aa8 | 286 | printf(" %d subchunk 2 size\n",wav_data.subchunk2_size); |
roryhand | 0:004cb0595aa8 | 287 | |
roryhand | 0:004cb0595aa8 | 288 | |
roryhand | 0:004cb0595aa8 | 289 | |
roryhand | 0:004cb0595aa8 | 290 | |
roryhand | 0:004cb0595aa8 | 291 | |
roryhand | 0:004cb0595aa8 | 292 | |
roryhand | 0:004cb0595aa8 | 293 | i2c.frequency(100000); |
roryhand | 0:004cb0595aa8 | 294 | long j = 0; |
roryhand | 0:004cb0595aa8 | 295 | |
roryhand | 0:004cb0595aa8 | 296 | printf("set up the codec\n\r"); |
roryhand | 3:6fba0a044d85 | 297 | while(j <1000){ |
roryhand | 0:004cb0595aa8 | 298 | wm8731_configTEST(); |
roryhand | 3:6fba0a044d85 | 299 | j = j + 1; |
roryhand | 3:6fba0a044d85 | 300 | } |
roryhand | 0:004cb0595aa8 | 301 | //i2s audio data transfer code?? |
roryhand | 0:004cb0595aa8 | 302 | i2s.stereomono(I2S_STEREO); |
roryhand | 0:004cb0595aa8 | 303 | i2s.masterslave(I2S_MASTER);//MASTER definitely works! |
roryhand | 0:004cb0595aa8 | 304 | i2s.frequency(sample_freq); |
roryhand | 0:004cb0595aa8 | 305 | int yes = i2s.setup_ok(); |
roryhand | 0:004cb0595aa8 | 306 | pc.printf("Setup went ok?: %d\n", yes);//0 = ack, 1 = Nack*/ |
roryhand | 0:004cb0595aa8 | 307 | printf("can we get to this point? 1\n\r"); |
roryhand | 0:004cb0595aa8 | 308 | led3 = 1; |
roryhand | 0:004cb0595aa8 | 309 | //myled = 1; |
roryhand | 0:004cb0595aa8 | 310 | led2 = 1; |
roryhand | 0:004cb0595aa8 | 311 | printf("can we get to this point? 2\n\r"); |
roryhand | 0:004cb0595aa8 | 312 | |
roryhand | 0:004cb0595aa8 | 313 | myled = 1; |
roryhand | 0:004cb0595aa8 | 314 | i2s.start(); |
roryhand | 0:004cb0595aa8 | 315 | //while (1) { |
roryhand | 0:004cb0595aa8 | 316 | fread(&wav_format,sizeof(wav_format),1,my_wav); |
roryhand | 0:004cb0595aa8 | 317 | printf("wav_format %d\n\r",wav_format); |
roryhand | 1:ec4e2020522c | 318 | |
roryhand | 0:004cb0595aa8 | 319 | |
roryhand | 0:004cb0595aa8 | 320 | fseek(my_wav,20,SEEK_SET); |
roryhand | 0:004cb0595aa8 | 321 | fread(&wav_format,sizeof(wav_format),1,my_wav); |
roryhand | 0:004cb0595aa8 | 322 | fseek(my_wav,36,SEEK_SET); |
roryhand | 0:004cb0595aa8 | 323 | fread(&wav_data,sizeof(wav_data),1,my_wav); |
roryhand | 2:05b426733282 | 324 | int num_slices = wav_data.subchunk2_size/wav_format.block_align; |
roryhand | 0:004cb0595aa8 | 325 | printf("wav_data.subchunk2_size: %d\n\r",wav_data.subchunk2_size); |
roryhand | 0:004cb0595aa8 | 326 | printf("wav_format.block_align: %d\n\r",wav_format.block_align); |
roryhand | 0:004cb0595aa8 | 327 | printf("num_slices: %d\n\r",num_slices); |
roryhand | 0:004cb0595aa8 | 328 | printf("num_slices*wav_format.block_align %d\n\r",num_slices*wav_format.block_align); |
roryhand | 0:004cb0595aa8 | 329 | 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 | 330 | printf("chunk_size - sizeof(wav_format) %d\n\r",wav_data.subchunk2_size-sizeof(wav_format)); |
roryhand | 0:004cb0595aa8 | 331 | printf("sizeof(wav_format): %d\n\r",sizeof(wav_format)); |
roryhand | 3:6fba0a044d85 | 332 | printf("do we egt to this point:\n\r"); |
roryhand | 4:1a9482a07ee2 | 333 | //sampletick.attach(&isr,1/96000); |
roryhand | 0:004cb0595aa8 | 334 | //while(!feof(my_wav)){ |
roryhand | 0:004cb0595aa8 | 335 | //while(1){ |
roryhand | 0:004cb0595aa8 | 336 | //if (wav_data.subchunk2_size > sizeof(wav_format)){ |
roryhand | 0:004cb0595aa8 | 337 | //fseek(my_wav,wav_data.subchunk2_size-sizeof(wav_format),SEEK_CUR); |
roryhand | 0:004cb0595aa8 | 338 | /*if(wav_data.subchunk2_size > (num_slices*wav_format.block_align)){ |
roryhand | 0:004cb0595aa8 | 339 | fseek(my_wav,wav_data.subchunk2_size - (num_slices*wav_format.block_align),SEEK_CUR); |
roryhand | 0:004cb0595aa8 | 340 | printf("chunk_size - sizeof(wav_format) %d\n\r",wav_data.subchunk2_size-sizeof(wav_format)); |
roryhand | 0:004cb0595aa8 | 341 | printf("sizeof(wav_format): %d\n\r",sizeof(wav_format)); |
roryhand | 0:004cb0595aa8 | 342 | } |
roryhand | 0:004cb0595aa8 | 343 | else { |
roryhand | 0:004cb0595aa8 | 344 | |
roryhand | 0:004cb0595aa8 | 345 | break; |
roryhand | 0:004cb0595aa8 | 346 | }*/ |
roryhand | 0:004cb0595aa8 | 347 | |
roryhand | 3:6fba0a044d85 | 348 | slice_buf=(char *)malloc(wav_format.block_align); |
roryhand | 2:05b426733282 | 349 | for (slice=0;slice<num_slices;slice+=1) { |
roryhand | 3:6fba0a044d85 | 350 | //t.start(); |
roryhand | 3:6fba0a044d85 | 351 | 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 | 352 | //I do not understand why he is not "seeking" through the file first though... |
roryhand | 3:6fba0a044d85 | 353 | if (feof(my_wav)){ |
roryhand | 2:05b426733282 | 354 | printf("Oops -- not enough slices in the wave file\n"); |
roryhand | 2:05b426733282 | 355 | exit(1); |
roryhand | 2:05b426733282 | 356 | } |
roryhand | 3:6fba0a044d85 | 357 | data_sptr=(int *)slice_buf; // 16 bit samples |
roryhand | 2:05b426733282 | 358 | data_bptr=(unsigned char *)slice_buf; // 8 bit samples |
roryhand | 2:05b426733282 | 359 | data_wptr=(int *)slice_buf; // 32 bit samples |
roryhand | 2:05b426733282 | 360 | slice_value=0; |
roryhand | 2:05b426733282 | 361 | for (channel=0;channel<wav_format.num_channels;channel++) { |
roryhand | 2:05b426733282 | 362 | switch (wav_format.sig_bps) { |
roryhand | 2:05b426733282 | 363 | case 16: |
roryhand | 2:05b426733282 | 364 | if (verbosity) |
roryhand | 2:05b426733282 | 365 | printf("16 bit channel %d data=%d ",channel,data_sptr[channel]); |
roryhand | 2:05b426733282 | 366 | slice_value+=data_sptr[channel]; |
roryhand | 2:05b426733282 | 367 | break; |
roryhand | 2:05b426733282 | 368 | case 32: |
roryhand | 2:05b426733282 | 369 | if (verbosity) |
roryhand | 2:05b426733282 | 370 | printf("32 bit channel %d data=%d ",channel,data_wptr[channel]); |
roryhand | 2:05b426733282 | 371 | slice_value+=data_wptr[channel]; |
roryhand | 2:05b426733282 | 372 | break; |
roryhand | 2:05b426733282 | 373 | case 8: |
roryhand | 2:05b426733282 | 374 | if (verbosity) |
roryhand | 2:05b426733282 | 375 | printf("8 bit channel %d data=%d ",channel,(int)data_bptr[channel]); |
roryhand | 2:05b426733282 | 376 | slice_value+=data_bptr[channel]; |
roryhand | 2:05b426733282 | 377 | break; |
roryhand | 2:05b426733282 | 378 | } |
roryhand | 2:05b426733282 | 379 | } |
roryhand | 2:05b426733282 | 380 | slice_value/=wav_format.num_channels; |
roryhand | 2:05b426733282 | 381 | |
roryhand | 2:05b426733282 | 382 | // slice_value is now averaged. Next it needs to be scaled to an unsigned 16 bit value |
roryhand | 2:05b426733282 | 383 | // with DC offset so it can be written to the DAC. |
roryhand | 2:05b426733282 | 384 | switch (wav_format.sig_bps) { |
roryhand | 2:05b426733282 | 385 | case 8: slice_value<<=8; |
roryhand | 2:05b426733282 | 386 | break; |
roryhand | 2:05b426733282 | 387 | case 16: slice_value+=32768; |
roryhand | 2:05b426733282 | 388 | break; |
roryhand | 2:05b426733282 | 389 | case 32: slice_value>>=16; |
roryhand | 2:05b426733282 | 390 | slice_value+=32768; |
roryhand | 2:05b426733282 | 391 | break; |
roryhand | 2:05b426733282 | 392 | } |
roryhand | 4:1a9482a07ee2 | 393 | i2s.write(data_sptr,2); |
roryhand | 3:6fba0a044d85 | 394 | //t.stop(); |
roryhand | 3:6fba0a044d85 | 395 | //printf("The time taken was %f seconds\n", t.read()); |
roryhand | 3:6fba0a044d85 | 396 | } |
roryhand | 3:6fba0a044d85 | 397 | /* dac_data=(short unsigned)slice_value; |
roryhand | 2:05b426733282 | 398 | if (verbosity) |
roryhand | 2:05b426733282 | 399 | printf("sample %d wptr %d slice_value %d dac_data %u\n",slice,DAC_wptr,(int)slice_value,dac_data); |
roryhand | 2:05b426733282 | 400 | DAC_fifo[DAC_wptr]=dac_data; |
roryhand | 2:05b426733282 | 401 | DAC_wptr=(DAC_wptr+1) & 0xff; |
roryhand | 2:05b426733282 | 402 | while (DAC_wptr==DAC_rptr) { |
roryhand | 2:05b426733282 | 403 | } |
roryhand | 3:6fba0a044d85 | 404 | }*/ |
roryhand | 3:6fba0a044d85 | 405 | |
roryhand | 2:05b426733282 | 406 | |
roryhand | 1:ec4e2020522c | 407 | |
roryhand | 0:004cb0595aa8 | 408 | sampletick.detach(); |
roryhand | 0:004cb0595aa8 | 409 | i2s.stop(); |
roryhand | 0:004cb0595aa8 | 410 | |
roryhand | 0:004cb0595aa8 | 411 | fclose(my_wav); |
roryhand | 0:004cb0595aa8 | 412 | printf("File is now closed."); |
roryhand | 0:004cb0595aa8 | 413 | return 0; |
roryhand | 0:004cb0595aa8 | 414 | } |
roryhand | 0:004cb0595aa8 | 415 |