/* Volume toggler between 0 <--> element default volume. * Now excepts new_vol to be in default hardware range of the sound card in use. */ void toggle_volume( struct sound_profile* sp, long int const new_vol, // If INT_MAX or negative then toggle to sound // profiles default volume enum Volume_type volume_type) { // TODO: USE "avolt" SEMAPHORE long int current_vol; get_vol(sp->volume_cntrl_mixer_element, hardware, ¤t_vol); long int min, _; snd_mixer_selem_get_playback_volume_range(sp->volume_cntrl_mixer_element, &min, &_); // If current volume is lowest possible if (current_vol == min) { if (new_vol > 0 && new_vol != INT_MAX) set_vol(sp->volume_cntrl_mixer_element, volume_type, new_vol, 0); else set_vol(sp->volume_cntrl_mixer_element, sp->volume_type, sp->default_volume, 0); } else { // Else zero current volume set_vol(sp->volume_cntrl_mixer_element, hardware_percentage, 0, 0); } return; }
/* Sets new volume, expects new_vol to be within [0,100] range. */ bool set_new_volume( struct sound_profile* sp, long int new_vol, bool relative_inc, bool set_default_vol, bool toggle_vol, bool use_semaphore, enum Volume_type volume_type) { /* XXX: Checking new_vol limits */ if (relative_inc) { if (new_vol < 0 || new_vol > 100) { fprintf(stderr, "Cannot set volume which is not in range [0,100]: %li\n", new_vol); return false; } } else if ((new_vol < -100 || new_vol > 100) && !set_default_vol && !toggle_vol) { fprintf(stderr, "Cannot set volume which is not in range [-100,100]: %li\n", new_vol); return false; } /* **************************** */ sem_t *sem = NULL; /* Semaphore which is used if USE_SEMAPHORE is true */ if (use_semaphore && !check_semaphore(&sem)) return false; /* Change new volume to native range */ PD_M("set_new_volume: new vol [-100,100], relative or toggling: %li\n", new_vol); if (set_default_vol) { // Set default volume PD_M("set_new_volume: setting default vol..\n"); set_vol(sp->volume_cntrl_mixer_element, sp->volume_type, sp->default_volume, 0); } else if (toggle_vol) { // toggle volume PD_M("set_new_volume: toggling..\n"); toggle_volume(sp, new_vol, volume_type); } else { /* Change absolute and relative volumes */ /* First check if relative volume */ if (relative_inc || new_vol < 0) { if (new_vol != 0) { PD_M("set_new_volume: Relative vol change...\n"); long int current_vol = 0; // Change volume relative to current volume get_vol(sp->volume_cntrl_mixer_element, volume_type, ¤t_vol); // By setting the round direction we always guarantee that // some change happens. int round_direction = new_vol < 0 ? -1 : 1; set_vol(sp->volume_cntrl_mixer_element, volume_type, current_vol + new_vol, round_direction); } } else { // Change absolute volume PD_M("set_new_volume: Changing absolute volume: %li\n", new_vol); set_vol(sp->volume_cntrl_mixer_element, volume_type, new_vol, 0); } } if (use_semaphore && !check_semaphore(&sem)) return false; return true; }
void conv_load(conv_layer_t* l, const char* fn) { int sx, sy, depth, filters; FILE* fin = fopen(fn, "r"); fscanf(fin, "%d %d %d %d", &sx, &sy, &depth, &filters); assert(sx == l->sx); assert(sy == l->sy); assert(depth == l->in_depth); assert(filters == l->out_depth); for(int d = 0; d < l->out_depth; d++) for (int x = 0; x < sx; x++) for (int y = 0; y < sy; y++) for (int z = 0; z < depth; z++) { double val; fscanf(fin, "%lf", &val); set_vol(l->filters[d], x, y, z, val); } for(int d = 0; d < l->out_depth; d++) { double val; fscanf(fin, "%lf", &val); set_vol(l->biases, 0, 0, d, val); } fclose(fin); }
// Load an image from the cifar10 data set. void load_sample(vol_t *v, int sample_num) { fprintf(stderr, "Loading input sample %d...\n", sample_num); int batch = sample_num / 10000; int ix = sample_num % 10000; char fn[1024]; sprintf(fn, "%s/data_batch_%d.bin", DATA_FOLDER, batch+1); FILE* fin = fopen(fn, "rb"); assert(fin != NULL); fseek(fin, ix*3073, SEEK_SET); uint8_t data[3073]; assert(fread(data, 1, 3073, fin) == 3073); int outp = 1; for (int z = 0; z < 3; z++) for (int y = 0; y < 32; y++) for (int x = 0; x < 32; x++) { set_vol(v, x, y, z, ((double)data[outp++])/255.0-0.5); } fclose(fin); }
void pool_forward(pool_layer_t* l, vol_t** in, vol_t** out, int start, int end) { for (int i = start; i <= end; i++) { vol_t* V = in[i]; vol_t* A = out[i]; int n=0; for(int d=0;d<l->out_depth;d++) { int x = -l->pad; int y = -l->pad; for(int ax=0; ax<l->out_sx; x+=l->stride,ax++) { y = -l->pad; for(int ay=0; ay<l->out_sy; y+=l->stride,ay++) { double a = -99999; for(int fx=0;fx<l->sx;fx++) { for(int fy=0;fy<l->sy;fy++) { int oy = y+fy; int ox = x+fx; if(oy>=0 && oy<V->sy && ox>=0 && ox<V->sx) { double v = get_vol(V, ox, oy, d); if(v > a) { a = v; } } } } n++; set_vol(A, ax, ay, d, a); } } } } }
void conv_forward(conv_layer_t* l, vol_t** in, vol_t** out, int start, int end) { for (int i = start; i <= end; i++) { vol_t* V = in[i]; vol_t* A = out[i]; int V_sx = V->sx; int V_sy = V->sy; int xy_stride = l->stride; for(int d = 0; d < l->out_depth; d++) { vol_t* f = l->filters[d]; int x = -l->pad; int y = -l->pad; for(int ay = 0; ay < l->out_sy; y += xy_stride, ay++) { x = -l->pad; for(int ax=0; ax < l->out_sx; x += xy_stride, ax++) { double a = 0.0; for(int fy = 0; fy < f->sy; fy++) { int oy = y + fy; for(int fx = 0; fx < f->sx; fx++) { int ox = x + fx; if(oy >= 0 && oy < V_sy && ox >=0 && ox < V_sx) { for(int fd=0;fd < f->depth; fd++) { a += f->w[((f->sx * fy)+fx)*f->depth+fd] * V->w[((V_sx * oy)+ox)*V->depth+fd]; } } } } a += l->biases->w[d]; set_vol(A, ax, ay, d, a); } } } } }
//for 20 depth void conv_forward_1(conv_layer_t* l, vol_t** in, vol_t** out, int start, int end) { uint64_t tempTime = timestamp_us(); for (int i = start; i <= end; i++) { vol_t* V = in[i]; vol_t* A = out[i]; for(int d = 0; d < 20; d++) { vol_t* f = l->filters[d]; int x = -2; int y = -2; for(int ay = 0; ay < 8; y += 1, ay++) { x = -2; for(int ax=0; ax < 8; x += 1, ax++) { double a = 0.0; __m256d sum = _mm256_setzero_pd(); for(int fy = 0; fy < 5; fy++) { int oy = y + fy; for(int fx = 0; fx < 5; fx++) { int ox = x + fx; if(oy >= 0 && oy < 8 && ox >=0 && ox < 8) { __m256d vector = _mm256_loadu_pd (&(f->w[((5 * fy)+fx)*20])); __m256d vector2 = _mm256_loadu_pd (&(V->w[((8 * oy)+ox)*20])); __m256d vectorMult = _mm256_mul_pd(vector, vector2); sum =_mm256_add_pd (vectorMult, sum); __m256d vector0 = _mm256_loadu_pd (&(f->w[((5 * fy)+fx)*20+4])); __m256d vector9 = _mm256_loadu_pd (&(V->w[((8 * oy)+ox)*20+ 4])); __m256d vectorMult0 = _mm256_mul_pd(vector0, vector9); sum =_mm256_add_pd (vectorMult0, sum); __m256d vector3 = _mm256_loadu_pd (&(f->w[((5 * fy)+fx)*20+8])); __m256d vector4 = _mm256_loadu_pd (&(V->w[((8 * oy)+ox)*20+8])); __m256d vectorMult2 = _mm256_mul_pd(vector3, vector4); sum =_mm256_add_pd (vectorMult2, sum); __m256d vector5 = _mm256_loadu_pd (&(f->w[((5 * fy)+fx)*20+12])); __m256d vector6 = _mm256_loadu_pd (&(V->w[((8 * oy)+ox)*20+12])); __m256d vectorMult3 = _mm256_mul_pd(vector5, vector6); sum =_mm256_add_pd (vectorMult3, sum); __m256d vector7 = _mm256_loadu_pd (&(f->w[((5 * fy)+fx)*20+16])); __m256d vector8 = _mm256_loadu_pd (&(V->w[((8 * oy)+ox)*20+16])); __m256d vectorMult4 = _mm256_mul_pd(vector7, vector8); sum =_mm256_add_pd (vectorMult4, sum); } } } for(int i = 0; i < 4; i++) { a+= sum[i]; } a += l->biases->w[d]; set_vol(A, ax, ay, d, a); } } } } l->myTime += timestamp_us() - tempTime; }
// Load an entire batch of images from the gtsrb data set (which is divided // into 5 batches with 10,000 images each). void load_gtsrb_data(vol_t** data, label_t* label, int size) { fprintf(stderr, "Loading Data \n"); assert(size <= 7830); // the size must be smaller than 7830 FILE *meanf = fopen("data/gtsrb/mean.binaryproto","rb"); float mean_buffer[6912]; FILE *dataf = fopen("data/gtsrb/test.bin","rb"); uint8_t data_buffer[6913]; FILE *aftrf = fopen("data/gtsrb/gtsrb_after_mean.bin","rb"); float aftr_buffer[6912]; FILE *lablf = fopen("data/gtsrb/gtsrb_test_labels.bin","rb"); assert(fread(mean_buffer,1,12,meanf) == 12); assert(fread(mean_buffer,sizeof(float),6912,meanf) == 6912); fclose(meanf); // assert(fread(label,sizeof(uint8_t),size,lablf) == size); for (int i = 0; i < size; i++) { int outp = 0; data[i] = make_vol(48, 48, 3, 0.0); assert(fread(data_buffer,sizeof(uint8_t),6913,dataf) == 6913); label[i] = data_buffer[0]; for (int z = 0; z < 3; z++) for (int y = 0; y < 48; y++) for (int x = 0; x < 48; x++) { set_vol(data[i], y, x, z, (storage_t)data_buffer[outp+1] -(storage_t)mean_buffer[outp]); outp++; } // assert(fread(aftr_buffer,sizeof(float),6912,aftrf) == 6912); // // for (int z = 0; z < 3; z++) // for (int y = 0; y < 48; y++) // for (int x = 0; x < 48; x++) { // set_vol(data[i], y, x, z, (storage_t)aftr_buffer[outp++]); // } } fclose(dataf); fclose(aftrf); fclose(lablf); fprintf(stderr, "input batch loaded successfully \n"); }
static vol_t* make_vol(int sx, int sy, int d, double v) { vol_t* out = (vol_t*)malloc(sizeof(struct vol)); out->w = (double*)malloc(sizeof(double)*(sx*sy*d)); out->sx = sx; out->sy = sy; out->depth = d; for (int x = 0; x < sx; x++) for (int y = 0; y < sy; y++) for (int z = 0; z < d; z++) set_vol(out, x, y, z, v); return out; }
void sound_play_sample(U8 *data, U32 length, U32 freq, int vol) { if (data == (U8 *) 0 || length == 0) return; // Calculate the clock divisor based upon the recorded sample frequency */ if (freq == 0) freq = DEFRATE; if (freq > MAXRATE) freq = MAXRATE; if (freq < MINRATE) freq = MINRATE; U32 cdiv = (OSC/(2*SAMPBITS) + freq/2)/freq; set_vol(vol); // Turn off ints while we update shared values sound_interrupt_disable(); sound_mode = SOUND_MODE_PCM; sample.count = length; sample.ptr = data; sample.len = PDM_BUFFER_LENGTH; sample.clock_div = cdiv; // re-enable and wait for the current sample to complete sound_interrupt_enable(AT91C_SSC_TXBUFE); *AT91C_SSC_PTCR = AT91C_PDC_TXTEN; }
void sound_freq(U32 freq, U32 ms, int vol) { // Set things up ready to go, note we avoid using anything that may // be used by the interupt routine because ints may still be enabled // at this point int len; // we use longer samples for lower frequencies if (freq > 1000) len = 16; else if (freq < 500) len = 64; else len = 32; sound_mode = SOUND_MODE_TONE; // Update the volume lookup table if we need to set_vol(vol); int buf = sample.buf_id^1; create_tone(sine, sizeof(sine), sample.buf[buf], len); // The note gneration takes approx 1ms, to ensure that we do not get gaps // when playing a series of tones we extend the requested period to cover // this 1ms cost. ms += TONE_OVERHEAD; // Turn of ints while we update shared values sound_interrupt_disable(); /* Genrate the pdm wave of the correct amplitude */ sample.clock_div = (OSC/(len*32*2) + freq/2)/freq; // Calculate actual frequency and use this for length calc freq = (OSC/(2*sample.clock_div))/(len*32); if (ms <= TONE_OVERHEAD) sample.count = 0; else sample.count = (freq*ms + 1000-1)/1000; sample.len = len; sample.ptr = (U8 *)sample.buf[buf]; sample.buf_id = buf; *AT91C_SSC_PTCR = AT91C_PDC_TXTEN; sound_mode = SOUND_MODE_TONE; sample.in_index = sample.out_index; sound_interrupt_enable(AT91C_SSC_TXBUFE); }
void pool_forward(pool_layer_t* l, vol_t** in, vol_t** out, int start, int end) { uint64_t tempTime = timestamp_us(); for (int i = start; i <= end; i++) { vol_t* V = in[i]; vol_t* A = out[i]; int n=0; for(int d=0;d<l->out_depth;d++) { int y = 0; int x = 0; int lsx = l->out_sx; int lsy = l->out_sy; int lusx = l->sx; for(int ax=0; ax<lsx; x+=2, ax++) { y = 0; for(int ay=0; ay<lsy; y+=2,ay++) { double a = -99999; for(int fx=0;fx<lusx;fx++) { for(int fy=0;fy<2;fy++) { int oy = y+fy; int ox = x+fx; if(oy>=0 && oy<32 && ox>=0 && ox<32) { double v = get_vol(V, ox, oy, d); if(v > a) { a = v; } } } } n++; set_vol(A, ax, ay, d, a); } } } } l->myTime += timestamp_us() - tempTime; }
void conv_forward(conv_layer_t* l, vol_t** in, vol_t** out, int start, int end) { uint64_t tempTime = timestamp_us(); for (int i = start; i <= end; i++) { vol_t* V = in[i]; vol_t* A = out[i]; for(int d = 0; d < 16; d++) { vol_t* f = l->filters[d]; int x = -2; int y = -2; for(int ay = 0; ay < 32; y += 1, ay++) { x = -2; for(int ax=0; ax < 32; x += 1, ax++) { double a = 0.0; __m256d sum = _mm256_setzero_pd(); for(int fy = 0; fy < 5; fy++) { int oy = y + fy; for(int fx = 0; fx < 5; fx++) { int ox = x + fx; if(oy >= 0 && oy < 32 && ox >=0 && ox < 32) { __m256d vector = _mm256_loadu_pd (&(f->w[((5 * fy)+fx)*3])); __m256d vector2 = _mm256_loadu_pd (&(V->w[((32 * oy)+ox)*3])); __m256d vectorMult = _mm256_mul_pd(vector, vector2); sum =_mm256_add_pd (vectorMult, sum); } } } for(int i = 0; i < 3; i++) { a+= sum[i]; } a += l->biases->w[d]; set_vol(A, ax, ay, d, a); } } } } l->myTime += timestamp_us() - tempTime; }
HarmEnhancer::HarmEnhancer(Parameters *param, float *Rmag, float hfreq, float lfreq, float gain) { this->param = param; inputl = (float *) malloc (sizeof (float) * 44100+100);//param->PERIOD); inputr = (float *) malloc (sizeof (float) * 44100+100);//param->PERIOD); set_vol(0,gain); realvol = gain; itm1l = 0.0f; itm1r = 0.0f; otm1l = 0.0f; otm1r = 0.0f; hpffreq = hfreq; lpffreq = lfreq; hpfl = new AnalogFilter(param,3, hfreq, 1, 0); hpfr = new AnalogFilter(param,3, hfreq, 1, 0); lpfl = new AnalogFilter(param,2, lfreq, 1, 0); lpfr = new AnalogFilter(param,2, lfreq, 1, 0); limiter = new Compressor (param,inputl, inputr); limiter->Compressor_Change_Preset(0,4); calcula_mag(Rmag); }
static vol_t* copy_vol(vol_t* dest, vol_t* src) { for (int x = 0; x < dest->sx; x++) for (int y = 0; y < dest->sy; y++) for (int z = 0; z < dest->depth; z++) set_vol(dest, x, y, z, get_vol(src, x, y, z)); }
static vol_t* copy_vol(vol_t* dest, vol_t* src) { for (int x = 0; x < 32; x++) for (int y = 0; y < 32; y++) for (int z = 0; z < 3; z++) set_vol(dest, x, y, z, get_vol(src, x, y, z)); }