WavePlayer audio effects

Fork of wave_player by Stuart Kent

Committer:
ballaw
Date:
Tue Nov 18 22:26:27 2014 +0000
Revision:
2:32da40a33717
Parent:
0:62c18ade9a60
Child:
3:06bc8350c139
Got it all to compile.  Had to make vars in shared.h extern

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sravet 0:62c18ade9a60 1 //-----------------------------------------------------------------------------
sravet 0:62c18ade9a60 2 // a sample mbed library to play back wave files.
sravet 0:62c18ade9a60 3 //
sravet 0:62c18ade9a60 4 // explanation of wave file format.
sravet 0:62c18ade9a60 5 // https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
sravet 0:62c18ade9a60 6
sravet 0:62c18ade9a60 7 // if VERBOSE is uncommented then the wave player will enter a verbose
sravet 0:62c18ade9a60 8 // mode that displays all data values as it reads them from the file
sravet 0:62c18ade9a60 9 // and writes them to the DAC. Very slow and unusable output on the DAC,
sravet 0:62c18ade9a60 10 // but useful for debugging wave files that don't work.
sravet 0:62c18ade9a60 11 //#define VERBOSE
sravet 0:62c18ade9a60 12
sravet 0:62c18ade9a60 13
sravet 0:62c18ade9a60 14 #include <mbed.h>
ballaw 2:32da40a33717 15 #include <math.h>
sravet 0:62c18ade9a60 16 #include <stdio.h>
sravet 0:62c18ade9a60 17 #include <wave_player.h>
ballaw 2:32da40a33717 18 #include <shared.h>
ballaw 2:32da40a33717 19
ballaw 2:32da40a33717 20 Wiistate wiistate = Nothing;
ballaw 2:32da40a33717 21 float vol = 0; //VOLUME FLOAT
ballaw 2:32da40a33717 22 Mutex key;
sravet 0:62c18ade9a60 23
sravet 0:62c18ade9a60 24
sravet 0:62c18ade9a60 25 //-----------------------------------------------------------------------------
sravet 0:62c18ade9a60 26 // constructor -- accepts an mbed pin to use for AnalogOut. Only p18 will work
sravet 0:62c18ade9a60 27 wave_player::wave_player(AnalogOut *_dac)
sravet 0:62c18ade9a60 28 {
sravet 0:62c18ade9a60 29 wave_DAC=_dac;
sravet 0:62c18ade9a60 30 wave_DAC->write_u16(32768); //DAC is 0-3.3V, so idles at ~1.6V
sravet 0:62c18ade9a60 31 verbosity=0;
sravet 0:62c18ade9a60 32 }
sravet 0:62c18ade9a60 33
sravet 0:62c18ade9a60 34 //-----------------------------------------------------------------------------
sravet 0:62c18ade9a60 35 // if verbosity is set then wave player enters a mode where the wave file
sravet 0:62c18ade9a60 36 // is decoded and displayed to the screen, including sample values put into
sravet 0:62c18ade9a60 37 // the DAC FIFO, and values read out of the DAC FIFO by the ISR. The DAC output
sravet 0:62c18ade9a60 38 // itself is so slow as to be unusable, but this might be handy for debugging
sravet 0:62c18ade9a60 39 // wave files that don't play
sravet 0:62c18ade9a60 40 //-----------------------------------------------------------------------------
sravet 0:62c18ade9a60 41 void wave_player::set_verbosity(int v)
sravet 0:62c18ade9a60 42 {
sravet 0:62c18ade9a60 43 verbosity=v;
sravet 0:62c18ade9a60 44 }
sravet 0:62c18ade9a60 45
ballaw 2:32da40a33717 46 int do_vol = 0;
ballaw 2:32da40a33717 47 int do_flange = 1;
ballaw 2:32da40a33717 48 int do_bit_crush = 0;
ballaw 2:32da40a33717 49 int do_delay = 1;
ballaw 2:32da40a33717 50
ballaw 2:32da40a33717 51 void update_state() {
ballaw 2:32da40a33717 52 Wiistate cur_state;
ballaw 2:32da40a33717 53 key.lock();
ballaw 2:32da40a33717 54 cur_state = wiistate;
ballaw 2:32da40a33717 55 key.unlock();
ballaw 2:32da40a33717 56
ballaw 2:32da40a33717 57 if(cur_state == Left) {
ballaw 2:32da40a33717 58 do_vol = 1;
ballaw 2:32da40a33717 59 do_flange = 1;
ballaw 2:32da40a33717 60 do_bit_crush = 0;
ballaw 2:32da40a33717 61 do_delay = 0;
ballaw 2:32da40a33717 62 } else if(cur_state == Right) {
ballaw 2:32da40a33717 63 do_vol = 1;
ballaw 2:32da40a33717 64 do_flange = 0;
ballaw 2:32da40a33717 65 do_bit_crush = 1;
ballaw 2:32da40a33717 66 do_delay = 0;
ballaw 2:32da40a33717 67 } else if(cur_state == Up) {
ballaw 2:32da40a33717 68 do_vol = 1;
ballaw 2:32da40a33717 69 do_flange = 0;
ballaw 2:32da40a33717 70 do_bit_crush = 0;
ballaw 2:32da40a33717 71 do_delay = 1;
ballaw 2:32da40a33717 72 } else if(cur_state == Down) {
ballaw 2:32da40a33717 73 do_vol = 1;
ballaw 2:32da40a33717 74 do_flange = 1;
ballaw 2:32da40a33717 75 do_bit_crush = 0;
ballaw 2:32da40a33717 76 do_delay = 1;
ballaw 2:32da40a33717 77 } else if(cur_state == Nothing) {
ballaw 2:32da40a33717 78 do_vol = 0;
ballaw 2:32da40a33717 79 do_flange = 0;
ballaw 2:32da40a33717 80 do_bit_crush = 0;
ballaw 2:32da40a33717 81 do_delay = 0;
ballaw 2:32da40a33717 82 //printf("%d %d %d %d\r\n", do_vol, do_flange, do_bit_crush, do_delay);
ballaw 2:32da40a33717 83 //printf("%d %d %d %d %d %d\r\n", cur_state==Nothing, Left, Right,Up,Down, Nothing);
ballaw 2:32da40a33717 84 }
ballaw 2:32da40a33717 85
ballaw 2:32da40a33717 86 //printf("%d %d %d %d\r\n", do_vol, do_flange, do_bit_crush, do_delay);
ballaw 2:32da40a33717 87 }
ballaw 2:32da40a33717 88
sravet 0:62c18ade9a60 89 //-----------------------------------------------------------------------------
sravet 0:62c18ade9a60 90 // player function. Takes a pointer to an opened wave file. The file needs
sravet 0:62c18ade9a60 91 // to be stored in a filesystem with enough bandwidth to feed the wave data.
sravet 0:62c18ade9a60 92 // LocalFileSystem isn't, but the SDcard is, at least for 22kHz files. The
sravet 0:62c18ade9a60 93 // SDcard filesystem can be hotrodded by increasing the SPI frequency it uses
sravet 0:62c18ade9a60 94 // internally.
sravet 0:62c18ade9a60 95 //-----------------------------------------------------------------------------
sravet 0:62c18ade9a60 96 void wave_player::play(FILE *wavefile)
sravet 0:62c18ade9a60 97 {
sravet 0:62c18ade9a60 98 unsigned chunk_id,chunk_size,channel;
sravet 0:62c18ade9a60 99 unsigned data,samp_int,i;
sravet 0:62c18ade9a60 100 short unsigned dac_data;
sravet 0:62c18ade9a60 101 long long slice_value;
sravet 0:62c18ade9a60 102 char *slice_buf;
sravet 0:62c18ade9a60 103 short *data_sptr;
sravet 0:62c18ade9a60 104 unsigned char *data_bptr;
sravet 0:62c18ade9a60 105 int *data_wptr;
sravet 0:62c18ade9a60 106 FMT_STRUCT wav_format;
sravet 0:62c18ade9a60 107 long slice,num_slices;
sravet 0:62c18ade9a60 108 DAC_wptr=0;
sravet 0:62c18ade9a60 109 DAC_rptr=0;
sravet 0:62c18ade9a60 110 for (i=0;i<256;i+=2) {
sravet 0:62c18ade9a60 111 DAC_fifo[i]=0;
sravet 0:62c18ade9a60 112 DAC_fifo[i+1]=3000;
sravet 0:62c18ade9a60 113 }
sravet 0:62c18ade9a60 114 DAC_wptr=4;
sravet 0:62c18ade9a60 115 DAC_on=0;
sravet 0:62c18ade9a60 116
sravet 0:62c18ade9a60 117 fread(&chunk_id,4,1,wavefile);
sravet 0:62c18ade9a60 118 fread(&chunk_size,4,1,wavefile);
ballaw 2:32da40a33717 119
ballaw 2:32da40a33717 120
ballaw 2:32da40a33717 121 ////
ballaw 2:32da40a33717 122 #define SAMPLE_RATE 11025
ballaw 2:32da40a33717 123 #define D_SIZE 8000
ballaw 2:32da40a33717 124 #define M_PI 3.14159265358979323846
ballaw 2:32da40a33717 125 short unsigned delayed[D_SIZE];
ballaw 2:32da40a33717 126 unsigned int d_rptr = 0;
ballaw 2:32da40a33717 127 unsigned int d_wptr = D_SIZE-1000;//((float)D_SIZE)/2.0;
ballaw 2:32da40a33717 128 unsigned int d_wptr_2 = D_SIZE-3000;
ballaw 2:32da40a33717 129 int flange = 0; // CHANG
ballaw 2:32da40a33717 130 int flange_amplitude = D_SIZE/4; // /2
ballaw 2:32da40a33717 131 //int flange_freq = SAMPLE_RATE; // 2*pi*cur_samp_cnt/11025
ballaw 2:32da40a33717 132 float cur_samp_cnt = 0;
ballaw 2:32da40a33717 133 int flange_r_ptr = 0;
ballaw 2:32da40a33717 134 int flange_dir = 1;
ballaw 2:32da40a33717 135 int flip_sec = 0;
ballaw 2:32da40a33717 136
ballaw 2:32da40a33717 137 ////
ballaw 2:32da40a33717 138 for(int di = 0; di < D_SIZE; di++) {
ballaw 2:32da40a33717 139 delayed[di] = 0;
ballaw 2:32da40a33717 140 }
ballaw 2:32da40a33717 141
sravet 0:62c18ade9a60 142 while (!feof(wavefile)) {
sravet 0:62c18ade9a60 143 if (verbosity)
ballaw 2:32da40a33717 144 printf("Read chunk ID 0x%x, size 0x%x\r\n",chunk_id,chunk_size);
sravet 0:62c18ade9a60 145 switch (chunk_id) {
sravet 0:62c18ade9a60 146 case 0x46464952:
sravet 0:62c18ade9a60 147 fread(&data,4,1,wavefile);
sravet 0:62c18ade9a60 148 if (verbosity) {
ballaw 2:32da40a33717 149 printf("RIFF chunk\r\n");
ballaw 2:32da40a33717 150 printf(" chunk size %d (0x%x)\r\n",chunk_size,chunk_size);
ballaw 2:32da40a33717 151 printf(" RIFF type 0x%x\r\n",data);
sravet 0:62c18ade9a60 152 }
sravet 0:62c18ade9a60 153 break;
sravet 0:62c18ade9a60 154 case 0x20746d66:
sravet 0:62c18ade9a60 155 fread(&wav_format,sizeof(wav_format),1,wavefile);
sravet 0:62c18ade9a60 156 if (verbosity) {
ballaw 2:32da40a33717 157 printf("FORMAT chunk\r\n");
ballaw 2:32da40a33717 158 printf(" chunk size %d (0x%x)\r\n",chunk_size,chunk_size);
ballaw 2:32da40a33717 159 printf(" compression code %d\r\n",wav_format.comp_code);
ballaw 2:32da40a33717 160 printf(" %d channels\r\n",wav_format.num_channels);
ballaw 2:32da40a33717 161 printf(" %d samples/sec\r\n",wav_format.sample_rate);
ballaw 2:32da40a33717 162 printf(" %d bytes/sec\r\n",wav_format.avg_Bps);
ballaw 2:32da40a33717 163 printf(" block align %d\r\n",wav_format.block_align);
ballaw 2:32da40a33717 164 printf(" %d bits per sample\r\n",wav_format.sig_bps);
sravet 0:62c18ade9a60 165 }
sravet 0:62c18ade9a60 166 if (chunk_size > sizeof(wav_format))
sravet 0:62c18ade9a60 167 fseek(wavefile,chunk_size-sizeof(wav_format),SEEK_CUR);
sravet 0:62c18ade9a60 168 break;
sravet 0:62c18ade9a60 169 case 0x61746164:
sravet 0:62c18ade9a60 170 // allocate a buffer big enough to hold a slice
sravet 0:62c18ade9a60 171 slice_buf=(char *)malloc(wav_format.block_align);
sravet 0:62c18ade9a60 172 if (!slice_buf) {
sravet 0:62c18ade9a60 173 printf("Unable to malloc slice buffer");
sravet 0:62c18ade9a60 174 exit(1);
sravet 0:62c18ade9a60 175 }
sravet 0:62c18ade9a60 176 num_slices=chunk_size/wav_format.block_align;
sravet 0:62c18ade9a60 177 samp_int=1000000/(wav_format.sample_rate);
sravet 0:62c18ade9a60 178 if (verbosity) {
ballaw 2:32da40a33717 179 printf("DATA chunk\r\n");
ballaw 2:32da40a33717 180 printf(" chunk size %d (0x%x)\r\n",chunk_size,chunk_size);
ballaw 2:32da40a33717 181 printf(" %d slices\r\n",num_slices);
ballaw 2:32da40a33717 182 printf(" Ideal sample interval=%d\r\n",(unsigned)(1000000.0/wav_format.sample_rate));
ballaw 2:32da40a33717 183 printf(" programmed interrupt tick interval=%d\r\n",samp_int);
sravet 0:62c18ade9a60 184 }
sravet 0:62c18ade9a60 185
sravet 0:62c18ade9a60 186 // starting up ticker to write samples out -- no printfs until tick.detach is called
sravet 0:62c18ade9a60 187 if (verbosity)
sravet 0:62c18ade9a60 188 tick.attach_us(this,&wave_player::dac_out, 500000);
sravet 0:62c18ade9a60 189 else
sravet 0:62c18ade9a60 190 tick.attach_us(this,&wave_player::dac_out, samp_int);
sravet 0:62c18ade9a60 191 DAC_on=1;
sravet 0:62c18ade9a60 192
sravet 0:62c18ade9a60 193 // start reading slices, which contain one sample each for however many channels
sravet 0:62c18ade9a60 194 // are in the wave file. one channel=mono, two channels=stereo, etc. Since
sravet 0:62c18ade9a60 195 // mbed only has a single AnalogOut, all of the channels present are averaged
sravet 0:62c18ade9a60 196 // to produce a single sample value. This summing and averaging happens in
sravet 0:62c18ade9a60 197 // a variable of type signed long long, to make sure that the data doesn't
sravet 0:62c18ade9a60 198 // overflow regardless of sample size (8 bits, 16 bits, 32 bits).
sravet 0:62c18ade9a60 199 //
sravet 0:62c18ade9a60 200 // note that from what I can find that 8 bit wave files use unsigned data,
sravet 0:62c18ade9a60 201 // while 16 and 32 bit wave files use signed data
sravet 0:62c18ade9a60 202 //
ballaw 2:32da40a33717 203
ballaw 2:32da40a33717 204
sravet 0:62c18ade9a60 205 for (slice=0;slice<num_slices;slice+=1) {
sravet 0:62c18ade9a60 206 fread(slice_buf,wav_format.block_align,1,wavefile);
sravet 0:62c18ade9a60 207 if (feof(wavefile)) {
ballaw 2:32da40a33717 208 printf("Oops -- not enough slices in the wave file\r\n");
sravet 0:62c18ade9a60 209 exit(1);
sravet 0:62c18ade9a60 210 }
sravet 0:62c18ade9a60 211 data_sptr=(short *)slice_buf; // 16 bit samples
sravet 0:62c18ade9a60 212 data_bptr=(unsigned char *)slice_buf; // 8 bit samples
sravet 0:62c18ade9a60 213 data_wptr=(int *)slice_buf; // 32 bit samples
sravet 0:62c18ade9a60 214 slice_value=0;
sravet 0:62c18ade9a60 215 for (channel=0;channel<wav_format.num_channels;channel++) {
sravet 0:62c18ade9a60 216 switch (wav_format.sig_bps) {
sravet 0:62c18ade9a60 217 case 16:
sravet 0:62c18ade9a60 218 if (verbosity)
sravet 0:62c18ade9a60 219 printf("16 bit channel %d data=%d ",channel,data_sptr[channel]);
sravet 0:62c18ade9a60 220 slice_value+=data_sptr[channel];
sravet 0:62c18ade9a60 221 break;
sravet 0:62c18ade9a60 222 case 32:
sravet 0:62c18ade9a60 223 if (verbosity)
sravet 0:62c18ade9a60 224 printf("32 bit channel %d data=%d ",channel,data_wptr[channel]);
sravet 0:62c18ade9a60 225 slice_value+=data_wptr[channel];
sravet 0:62c18ade9a60 226 break;
sravet 0:62c18ade9a60 227 case 8:
sravet 0:62c18ade9a60 228 if (verbosity)
sravet 0:62c18ade9a60 229 printf("8 bit channel %d data=%d ",channel,(int)data_bptr[channel]);
sravet 0:62c18ade9a60 230 slice_value+=data_bptr[channel];
sravet 0:62c18ade9a60 231 break;
sravet 0:62c18ade9a60 232 }
sravet 0:62c18ade9a60 233 }
sravet 0:62c18ade9a60 234 slice_value/=wav_format.num_channels;
sravet 0:62c18ade9a60 235
sravet 0:62c18ade9a60 236 // slice_value is now averaged. Next it needs to be scaled to an unsigned 16 bit value
sravet 0:62c18ade9a60 237 // with DC offset so it can be written to the DAC.
sravet 0:62c18ade9a60 238 switch (wav_format.sig_bps) {
sravet 0:62c18ade9a60 239 case 8: slice_value<<=8;
sravet 0:62c18ade9a60 240 break;
sravet 0:62c18ade9a60 241 case 16: slice_value+=32768;
sravet 0:62c18ade9a60 242 break;
sravet 0:62c18ade9a60 243 case 32: slice_value>>=16;
sravet 0:62c18ade9a60 244 slice_value+=32768;
sravet 0:62c18ade9a60 245 break;
sravet 0:62c18ade9a60 246 }
sravet 0:62c18ade9a60 247 dac_data=(short unsigned)slice_value;
sravet 0:62c18ade9a60 248 if (verbosity)
ballaw 2:32da40a33717 249 printf("sample %d wptr %d slice_value %d dac_data %u\r\n",slice,DAC_wptr,(int)slice_value,dac_data);
ballaw 2:32da40a33717 250
ballaw 2:32da40a33717 251
ballaw 2:32da40a33717 252
ballaw 2:32da40a33717 253 update_state();
ballaw 2:32da40a33717 254
ballaw 2:32da40a33717 255
ballaw 2:32da40a33717 256 ////
ballaw 2:32da40a33717 257 delayed[d_rptr] = dac_data;
ballaw 2:32da40a33717 258
ballaw 2:32da40a33717 259 d_rptr = (d_rptr == (D_SIZE-1)) ? 0 : (d_rptr + 1);
ballaw 2:32da40a33717 260 d_wptr = (d_wptr == (D_SIZE-1)) ? 0 : (d_wptr + 1);
ballaw 2:32da40a33717 261 d_wptr_2 = (d_wptr_2 == (D_SIZE-1)) ? 0 : (d_wptr_2 + 1);
ballaw 2:32da40a33717 262
ballaw 2:32da40a33717 263 float f_dac_data = (float)dac_data;
ballaw 2:32da40a33717 264 float f_delay_data;
ballaw 2:32da40a33717 265 float f_delay_data_2;
ballaw 2:32da40a33717 266 int flange_dist = 0;
ballaw 2:32da40a33717 267 int flange_dist_check;
ballaw 2:32da40a33717 268 if(flange) {
ballaw 2:32da40a33717 269 flange_dist = flange_amplitude*sin(2*M_PI*cur_samp_cnt/(float)SAMPLE_RATE);
ballaw 2:32da40a33717 270 /*if(cur_samp_cnt != 0) {
ballaw 2:32da40a33717 271 while(cur_samp_cnt < 4000) {
ballaw 2:32da40a33717 272 flange_dist = flange_amplitude*sin(2*M_PI*cur_samp_cnt/(float)SAMPLE_RATE);
ballaw 2:32da40a33717 273 printf("%f %f\r\n", flange_dist, 2*M_PI*cur_samp_cnt/(float)SAMPLE_RATE);
ballaw 2:32da40a33717 274 cur_samp_cnt++;
ballaw 2:32da40a33717 275 }
ballaw 2:32da40a33717 276 return;
ballaw 2:32da40a33717 277 }*/
ballaw 2:32da40a33717 278 //printf("%d\r\n",flange_dist);
ballaw 2:32da40a33717 279 flange_dist_check = d_wptr + flange_dist - D_SIZE;
ballaw 2:32da40a33717 280 //d_wptr += flange_dist_check > 0 ? flange_dist_check : (d_wptr + flange_dist) < 0 ? (D_SIZE + d_wptr + flange_dist) : flange_dist;
ballaw 2:32da40a33717 281 //d_wptr += flange_dist_check < 0 ? flange_dist : ((d_wptr + flange_dist) < 0) ? 0 : flange_dist;
ballaw 2:32da40a33717 282 d_wptr = ((d_wptr + flange_dist) > 0) ? (d_wptr + flange_dist) % D_SIZE : (D_SIZE + flange_dist); //(D_SIZE + flange_dist) WRONG
ballaw 2:32da40a33717 283 }
ballaw 2:32da40a33717 284 //cur_samp_cnt = (cur_samp_cnt+1) > SAMPLE_RATE ? 0 : (cur_samp_cnt+1);
ballaw 2:32da40a33717 285 cur_samp_cnt = (cur_samp_cnt+1) > SAMPLE_RATE ? 0 : (cur_samp_cnt+1);
ballaw 2:32da40a33717 286
ballaw 2:32da40a33717 287 if(cur_samp_cnt == 0) {
ballaw 2:32da40a33717 288 flip_sec = !flip_sec;
ballaw 2:32da40a33717 289 }
ballaw 2:32da40a33717 290
ballaw 2:32da40a33717 291
ballaw 2:32da40a33717 292 #define N_SAMPLES_FLANGE_PERIOD 100//((SAMPLE_RATE) >> 2) // WHY IS MZ 100???
ballaw 2:32da40a33717 293
ballaw 2:32da40a33717 294 if(do_flange) {// && (flip_sec)) {
ballaw 2:32da40a33717 295 if(flange_r_ptr == N_SAMPLES_FLANGE_PERIOD) {
ballaw 2:32da40a33717 296 flange_dir = -1;
ballaw 2:32da40a33717 297 } else if(flange_r_ptr == -N_SAMPLES_FLANGE_PERIOD) {
ballaw 2:32da40a33717 298 flange_dir = 1;
ballaw 2:32da40a33717 299 }
ballaw 2:32da40a33717 300 flange_r_ptr += flange_dir;
ballaw 2:32da40a33717 301 } else {
ballaw 2:32da40a33717 302 flange_r_ptr = 0;
ballaw 2:32da40a33717 303 }
ballaw 2:32da40a33717 304
ballaw 2:32da40a33717 305 int total_ptr = (d_wptr + flange_r_ptr);
ballaw 2:32da40a33717 306 total_ptr = (total_ptr > D_SIZE) ? (total_ptr - D_SIZE) : total_ptr;
ballaw 2:32da40a33717 307 total_ptr = (total_ptr < 0) ? (D_SIZE - total_ptr) : total_ptr;
ballaw 2:32da40a33717 308
ballaw 2:32da40a33717 309 //f_delay_data = (float) delayed[d_wptr];
ballaw 2:32da40a33717 310 f_delay_data = (float) delayed[total_ptr];
ballaw 2:32da40a33717 311 f_delay_data_2 = (float) delayed[d_wptr_2];
ballaw 2:32da40a33717 312 float f_mix;
ballaw 2:32da40a33717 313
ballaw 2:32da40a33717 314
ballaw 2:32da40a33717 315 if(do_delay || do_flange) {
ballaw 2:32da40a33717 316 if(do_flange) {
ballaw 2:32da40a33717 317 f_mix = 0.01*f_dac_data + 0.7*f_delay_data;
ballaw 2:32da40a33717 318 } else {
ballaw 2:32da40a33717 319 f_mix = 0.333*f_dac_data + 0.333*f_delay_data + 0.333*f_delay_data_2;
ballaw 2:32da40a33717 320 }
ballaw 2:32da40a33717 321 //0.01*f_dac_data + 0.7*f_delay_data;
ballaw 2:32da40a33717 322 } else {
ballaw 2:32da40a33717 323 f_mix = f_dac_data;
ballaw 2:32da40a33717 324 }
ballaw 2:32da40a33717 325 ////
ballaw 2:32da40a33717 326
ballaw 2:32da40a33717 327 float time_in_period = (float)cur_samp_cnt/(float)SAMPLE_RATE;
ballaw 2:32da40a33717 328 //float vol = 0.5 + 0.5*sin(2*M_PI*time_in_period); // Full dynamic range
ballaw 2:32da40a33717 329 float my_vol = 0.75 + 0.25*sin(2*M_PI*time_in_period);
ballaw 2:32da40a33717 330
ballaw 2:32da40a33717 331 if(do_vol) f_mix = (my_vol*f_mix);
ballaw 2:32da40a33717 332
ballaw 2:32da40a33717 333 if(do_bit_crush) {
ballaw 2:32da40a33717 334 short unsigned beef = (short unsigned)f_mix;
ballaw 2:32da40a33717 335 //beef = (beef>>7) << 7;
ballaw 2:32da40a33717 336 //printf("beef %x\r\nbeeeef %x\r\n", beef, beef & 0xFFF0);
ballaw 2:32da40a33717 337 DAC_fifo[DAC_wptr]=beef &0xF800;
ballaw 2:32da40a33717 338 } else {
ballaw 2:32da40a33717 339 DAC_fifo[DAC_wptr]=(short unsigned)(f_mix);
ballaw 2:32da40a33717 340 }
sravet 0:62c18ade9a60 341 DAC_wptr=(DAC_wptr+1) & 0xff;
sravet 0:62c18ade9a60 342 while (DAC_wptr==DAC_rptr) {
sravet 0:62c18ade9a60 343 }
sravet 0:62c18ade9a60 344 }
sravet 0:62c18ade9a60 345 DAC_on=0;
sravet 0:62c18ade9a60 346 tick.detach();
sravet 0:62c18ade9a60 347 free(slice_buf);
sravet 0:62c18ade9a60 348 break;
sravet 0:62c18ade9a60 349 case 0x5453494c:
sravet 0:62c18ade9a60 350 if (verbosity)
ballaw 2:32da40a33717 351 printf("INFO chunk, size %d\r\n",chunk_size);
sravet 0:62c18ade9a60 352 fseek(wavefile,chunk_size,SEEK_CUR);
sravet 0:62c18ade9a60 353 break;
sravet 0:62c18ade9a60 354 default:
ballaw 2:32da40a33717 355 printf("unknown chunk type 0x%x, size %d\r\n",chunk_id,chunk_size);
sravet 0:62c18ade9a60 356 data=fseek(wavefile,chunk_size,SEEK_CUR);
sravet 0:62c18ade9a60 357 break;
sravet 0:62c18ade9a60 358 }
sravet 0:62c18ade9a60 359 fread(&chunk_id,4,1,wavefile);
sravet 0:62c18ade9a60 360 fread(&chunk_size,4,1,wavefile);
sravet 0:62c18ade9a60 361 }
sravet 0:62c18ade9a60 362 }
sravet 0:62c18ade9a60 363
sravet 0:62c18ade9a60 364
sravet 0:62c18ade9a60 365 void wave_player::dac_out()
sravet 0:62c18ade9a60 366 {
sravet 0:62c18ade9a60 367 if (DAC_on) {
sravet 0:62c18ade9a60 368 #ifdef VERBOSE
ballaw 2:32da40a33717 369 printf("ISR rdptr %d got %u\r\n",DAC_rptr,DAC_fifo[DAC_rptr]);
sravet 0:62c18ade9a60 370 #endif
sravet 0:62c18ade9a60 371 wave_DAC->write_u16(DAC_fifo[DAC_rptr]);
sravet 0:62c18ade9a60 372 DAC_rptr=(DAC_rptr+1) & 0xff;
sravet 0:62c18ade9a60 373 }
sravet 0:62c18ade9a60 374 }
sravet 0:62c18ade9a60 375