/* randomly select a move among (nocells * (noparts - 1)) move directions */ int select_cell(int nocells, int noparts, selected_cell_t scell[], allele tchrom[], cells_t cells[], parts_t parts[], cells_info_t cells_info[]) { int cell_no = irandom(0, nocells - 1); int selected = False; /* true if a cell is selected */ int nochecks = 0; /* number of cells tried */ while ((! selected) && (nochecks < nocells)) { nochecks++; int from = tchrom[cell_no]; int to = find_to_part(noparts, cell_no, from, cells, parts); /* no directions for this cell are feasible, so try another */ if ((to < 0) || (parts[from].pmax_cells < 1)) { cell_no = (cell_no + 1) % nocells; } else { selected = True; scell[0].mov_cell_no = cell_no; scell[0].from_part = from; scell[0].to_part = to; scell[0].mov_gain = calculate_gain(scell[0].mov_cell_no, scell[0].from_part, scell[0].to_part, cells_info); } /* else */ } /* while */ if (nochecks < nocells) { return (True); /* a move is found */ } else { return (False); } } /* select_cell */
/** * Main decompose driver. */ static instruction *decompose_mul(mul_env *env, unsigned char *R, int r, ir_tarval *N) { if (r <= 2) return decompose_simple_cases(env, R, r); if (env->params->also_use_subs) { int gain = calculate_gain(R, r); if (gain > 0) { int r1; unsigned char *R1 = complement_condensed(env, R, gain, &r1); int r2 = r - gain + 1; unsigned char *R2 = OALLOCN(&env->obst, unsigned char, r2); int k = 1; for (int i = 0; i < gain; ++i) { k += R[i]; } R2[0] = k; R2[1] = R[gain] - 1; int j = 2; if (R2[1] == 0) { /* Two identical bits: normalize */ ++R2[0]; --j; --r2; } for (int i = gain + 1; i < r; ++i) { R2[j++] = R[i]; } instruction *instr1 = decompose_mul(env, R1, r1, NULL); instruction *instr2 = decompose_mul(env, R2, r2, NULL); return emit_SUB(env, instr2, instr1); }
bool attach(const char *filename) { ctx = WavpackOpenFileInput(filename, error_buff, OPEN_TAGS | OPEN_WVC | OPEN_NORMALIZE, 0); if (ctx == NULL) { return false; } sample_rate = WavpackGetSampleRate(ctx); num_channels = WavpackGetNumChannels(ctx); bytes_per_sample = WavpackGetBytesPerSample(ctx); input = (int32_t *)calloc(BUFFER_SIZE, num_channels * sizeof(int32_t)); output = (int16_t *)calloc(BUFFER_SIZE, num_channels * sizeof(int16_t)); memset (shaping_error, 0, sizeof (shaping_error)); mod->set_info(generate_title(filename, ctx), (int) (WavpackGetNumSamples(ctx) / sample_rate) * 1000, (int) WavpackGetAverageBitrate(ctx, true), (int) sample_rate, num_channels); play_gain = calculate_gain (ctx); DBG("gain value = %g\n", play_gain); return true; }
void hf_adjustment(sbr_info *sbr, qmf_t Xsbr[MAX_NTSRHFG][64] #ifdef SBR_LOW_POWER ,real_t *deg /* aliasing degree */ #endif ,uint8_t ch) { ALIGN sbr_hfadj_info adj = {{{0}}}; map_noise_data(sbr, &adj, ch); map_sinusoids(sbr, &adj, ch); estimate_current_envelope(sbr, &adj, Xsbr, ch); calculate_gain(sbr, &adj, ch); #ifdef SBR_LOW_POWER calc_gain_groups(sbr, &adj, deg, ch); aliasing_reduction(sbr, &adj, deg, ch); #endif hf_assembly(sbr, &adj, Xsbr, ch); }
int CompressorEffect::process_buffer(int64_t size, Samples **buffer, int64_t start_position, int sample_rate) { load_configuration(); // Calculate linear transfer from db levels.remove_all(); for(int i = 0; i < config.levels.total; i++) { levels.append(); levels.values[i].x = DB::fromdb(config.levels.values[i].x); levels.values[i].y = DB::fromdb(config.levels.values[i].y); } min_x = DB::fromdb(config.min_db); min_y = DB::fromdb(config.min_db); max_x = 1.0; max_y = 1.0; int reaction_samples = (int)(config.reaction_len * sample_rate + 0.5); int decay_samples = (int)(config.decay_len * sample_rate + 0.5); int trigger = CLIP(config.trigger, 0, PluginAClient::total_in_buffers - 1); CLAMP(reaction_samples, -1000000, 1000000); CLAMP(decay_samples, reaction_samples, 1000000); CLAMP(decay_samples, 1, 1000000); if(labs(reaction_samples) < 1) reaction_samples = 1; if(labs(decay_samples) < 1) decay_samples = 1; int total_buffers = get_total_buffers(); if(reaction_samples >= 0) { if(target_current_sample < 0) target_current_sample = reaction_samples; for(int i = 0; i < total_buffers; i++) { read_samples(buffer[i], i, sample_rate, start_position, size); } double current_slope = (next_target - previous_target) / reaction_samples; double *trigger_buffer = buffer[trigger]->get_data(); for(int i = 0; i < size; i++) { // Get slope required to reach current sample from smoothed sample over reaction // length. double sample; switch(config.input) { case CompressorConfig::MAX: { double max = 0; for(int j = 0; j < total_buffers; j++) { sample = fabs(buffer[j]->get_data()[i]); if(sample > max) max = sample; } sample = max; break; } case CompressorConfig::TRIGGER: sample = fabs(trigger_buffer[i]); break; case CompressorConfig::SUM: { double max = 0; for(int j = 0; j < total_buffers; j++) { sample = fabs(buffer[j]->get_data()[i]); max += sample; } sample = max; break; } } double new_slope = (sample - current_value) / reaction_samples; // Slope greater than current slope if(new_slope >= current_slope && (current_slope >= 0 || new_slope >= 0)) { next_target = sample; previous_target = current_value; target_current_sample = 0; target_samples = reaction_samples; current_slope = new_slope; } else if(sample > next_target && current_slope < 0) { next_target = sample; previous_target = current_value; target_current_sample = 0; target_samples = decay_samples; current_slope = (sample - current_value) / decay_samples; } // Current smoothed sample came up without finding higher slope if(target_current_sample >= target_samples) { next_target = sample; previous_target = current_value; target_current_sample = 0; target_samples = decay_samples; current_slope = (sample - current_value) / decay_samples; } // Update current value and store gain current_value = (next_target * target_current_sample + previous_target * (target_samples - target_current_sample)) / target_samples; target_current_sample++; if(config.smoothing_only) { for(int j = 0; j < total_buffers; j++) buffer[j]->get_data()[i] = current_value; } else { double gain = calculate_gain(current_value); for(int j = 0; j < total_buffers; j++) { buffer[j]->get_data()[i] *= gain; } } } } else { if(target_current_sample < 0) target_current_sample = target_samples; int64_t preview_samples = -reaction_samples; // Start of new buffer is outside the current buffer. Start buffer over. if(start_position < input_start || start_position >= input_start + input_size) { input_size = 0; input_start = start_position; } else // Shift current buffer so the buffer starts on start_position if(start_position > input_start && start_position < input_start + input_size) { if(input_buffer) { int len = input_start + input_size - start_position; for(int i = 0; i < total_buffers; i++) { memcpy(input_buffer[i]->get_data(), input_buffer[i]->get_data() + (start_position - input_start), len * sizeof(double)); } input_size = len; input_start = start_position; } } // Expand buffer to handle preview size if(size + preview_samples > input_allocated) { Samples **new_input_buffer = new Samples*[total_buffers]; for(int i = 0; i < total_buffers; i++) { new_input_buffer[i] = new Samples(size + preview_samples); if(input_buffer) { memcpy(new_input_buffer[i]->get_data(), input_buffer[i]->get_data(), input_size * sizeof(double)); delete input_buffer[i]; } } if(input_buffer) delete [] input_buffer; input_allocated = size + preview_samples; input_buffer = new_input_buffer; } // Append data to input buffer to construct readahead area. #define MAX_FRAGMENT_SIZE 131072 while(input_size < size + preview_samples) { int fragment_size = MAX_FRAGMENT_SIZE; if(fragment_size + input_size > size + preview_samples) fragment_size = size + preview_samples - input_size; for(int i = 0; i < total_buffers; i++) { input_buffer[i]->set_offset(input_size); //printf("CompressorEffect::process_buffer %d %p %d\n", __LINE__, input_buffer[i], input_size); read_samples(input_buffer[i], i, sample_rate, input_start + input_size, fragment_size); input_buffer[i]->set_offset(0); } input_size += fragment_size; } double current_slope = (next_target - previous_target) / target_samples; double *trigger_buffer = input_buffer[trigger]->get_data(); for(int i = 0; i < size; i++) { // Get slope from current sample to every sample in preview_samples. // Take highest one or first one after target_samples are up. // For optimization, calculate the first slope we really need. // Assume every slope up to the end of preview_samples has been calculated and // found <= to current slope. int first_slope = preview_samples - 1; // Need new slope immediately if(target_current_sample >= target_samples) first_slope = 1; for(int j = first_slope; j < preview_samples; j++) { double sample; switch(config.input) { case CompressorConfig::MAX: { double max = 0; for(int k = 0; k < total_buffers; k++) { sample = fabs(input_buffer[k]->get_data()[i + j]); if(sample > max) max = sample; } sample = max; break; } case CompressorConfig::TRIGGER: sample = fabs(trigger_buffer[i + j]); break; case CompressorConfig::SUM: { double max = 0; for(int k = 0; k < total_buffers; k++) { sample = fabs(input_buffer[k]->get_data()[i + j]); max += sample; } sample = max; break; } } double new_slope = (sample - current_value) / j; // Got equal or higher slope if(new_slope >= current_slope && (current_slope >= 0 || new_slope >= 0)) { target_current_sample = 0; target_samples = j; current_slope = new_slope; next_target = sample; previous_target = current_value; } else if(sample > next_target && current_slope < 0) { target_current_sample = 0; target_samples = decay_samples; current_slope = (sample - current_value) / decay_samples; next_target = sample; previous_target = current_value; } // Hit end of current slope range without finding higher slope if(target_current_sample >= target_samples) { target_current_sample = 0; target_samples = decay_samples; current_slope = (sample - current_value) / decay_samples; next_target = sample; previous_target = current_value; } } // Update current value and multiply gain current_value = (next_target * target_current_sample + previous_target * (target_samples - target_current_sample)) / target_samples; //buffer[0][i] = current_value; target_current_sample++; if(config.smoothing_only) { for(int j = 0; j < total_buffers; j++) { buffer[j]->get_data()[i] = current_value; } } else { double gain = calculate_gain(current_value); for(int j = 0; j < total_buffers; j++) { buffer[j]->get_data()[i] = input_buffer[j]->get_data()[i] * gain; } } } } return 0; }