WavePlayer audio effects

Fork of wave_player by Stuart Kent

Committer:
acorness
Date:
Tue Nov 18 23:18:45 2014 +0000
Revision:
3:06bc8350c139
Parent:
2:32da40a33717
IT LIVES

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