void processAudio(AudioBuffer& _buf){ uint32_t sample_count = _buf.getSize(); float s_rate = getSampleRate(); delay_depth_avg = getParameterValue(PARAMETER_A)*10 - 0; law_freq = getParameterValue(PARAMETER_B)*7.5 - 0.5; input = _buf.getSamples(0); output = _buf.getSamples(0); RetroFlangePatch* plugin_data = this; long int pos; int law_p = f_trunc(LIMIT(sample_rate / f_clamp(law_freq, 0.0001f, 100.0f), 1, max_law_p)); float increment; float lin_int, lin_inc; int track; int fph; LADSPA_Data out = 0.0f; const float dda_c = f_clamp(delay_depth_avg, 0.0f, 10.0f); int dl_used = (dda_c * sample_rate) / 1000; float inc_base = 1000.0f * (float)BASE_BUFFER; const float delay_depth = 2.0f * dda_c; float n_ph, p_ph, law; for (pos = 0; pos < sample_count; pos++) { // Write into the delay line delay_line[delay_pos] = input[pos]; z0 = delay_line[MOD(delay_pos - dl_used, delay_line_length)] + 0.12919609397f*z1 - 0.31050847f*z2; out = sat(z0*0.20466966f + z1*0.40933933f + z2*0.40933933f, -0.23f, 3.3f); z2 = z1; z1 = z0; delay_pos = (delay_pos + 1) % delay_line_length; if ((count++ % law_p) == 0) { // Value for amplitude of law peak next_law_peak = (float)rand() / (float)RAND_MAX; next_law_pos = count + law_p; } else if (count % law_p == law_p / 2) { // Value for amplitude of law peak prev_law_peak = (float)rand() / (float)RAND_MAX; prev_law_pos = count + law_p; } n_ph = (float)(law_p - abs(next_law_pos - count))/(float)law_p; p_ph = n_ph + 0.5f; if (p_ph > 1.0f) { p_ph -= 1.0f; } law = f_sin_sq(3.1415926f*p_ph)*prev_law_peak + f_sin_sq(3.1415926f*n_ph)*next_law_peak; increment = inc_base / (delay_depth * law + 0.2); fph = f_trunc(phase); last_phase = fph; lin_int = phase - (float)fph; out += LIN_INTERP(lin_int, buffer[(fph+1) % buffer_size], buffer[(fph+2) % buffer_size]); phase += increment; lin_inc = 1.0f / (floor(phase) - last_phase + 1); lin_inc = lin_inc > 1.0f ? 1.0f : lin_inc; lin_int = 0.0f; for (track = last_phase; track < phase; track++) { lin_int += lin_inc; buffer[track % buffer_size] = LIN_INTERP(lin_int, last_in, input[pos]); } last_in = input[pos]; buffer_write(output[pos], out * 0.707f); if (phase >= buffer_size) { phase -= buffer_size; } } // Store current phase in instance plugin_data->phase = phase; plugin_data->prev_law_peak = prev_law_peak; plugin_data->next_law_peak = next_law_peak; plugin_data->prev_law_pos = prev_law_pos; plugin_data->next_law_pos = next_law_pos; plugin_data->last_phase = last_phase; plugin_data->last_in = last_in; plugin_data->count = count; plugin_data->last_law_p = last_law_p; plugin_data->delay_pos = delay_pos; plugin_data->z0 = z0; plugin_data->z1 = z1; plugin_data->z2 = z2; }
static void runAddingRetroFlange(LADSPA_Handle instance, unsigned long sample_count) { RetroFlange *plugin_data = (RetroFlange *)instance; LADSPA_Data run_adding_gain = plugin_data->run_adding_gain; /* Average stall (ms) (float value) */ const LADSPA_Data delay_depth_avg = *(plugin_data->delay_depth_avg); /* Flange frequency (Hz) (float value) */ const LADSPA_Data law_freq = *(plugin_data->law_freq); /* Input (array of floats of length sample_count) */ const LADSPA_Data * const input = plugin_data->input; /* Output (array of floats of length sample_count) */ LADSPA_Data * const output = plugin_data->output; LADSPA_Data * buffer = plugin_data->buffer; long buffer_size = plugin_data->buffer_size; long count = plugin_data->count; LADSPA_Data * delay_line = plugin_data->delay_line; int delay_line_length = plugin_data->delay_line_length; int delay_pos = plugin_data->delay_pos; LADSPA_Data last_in = plugin_data->last_in; int last_law_p = plugin_data->last_law_p; int last_phase = plugin_data->last_phase; int max_law_p = plugin_data->max_law_p; float next_law_peak = plugin_data->next_law_peak; int next_law_pos = plugin_data->next_law_pos; float phase = plugin_data->phase; float prev_law_peak = plugin_data->prev_law_peak; int prev_law_pos = plugin_data->prev_law_pos; long sample_rate = plugin_data->sample_rate; LADSPA_Data z0 = plugin_data->z0; LADSPA_Data z1 = plugin_data->z1; LADSPA_Data z2 = plugin_data->z2; #line 75 "retro_flange_1208.xml" long int pos; int law_p = f_trunc(LIMIT(sample_rate / f_clamp(law_freq, 0.0001f, 100.0f), 1, max_law_p)); float increment; float lin_int, lin_inc; int track; int fph; LADSPA_Data out = 0.0f; const float dda_c = f_clamp(delay_depth_avg, 0.0f, 10.0f); int dl_used = (dda_c * sample_rate) / 1000; float inc_base = 1000.0f * (float)BASE_BUFFER; const float delay_depth = 2.0f * dda_c; float n_ph, p_ph, law; for (pos = 0; pos < sample_count; pos++) { // Write into the delay line delay_line[delay_pos] = input[pos]; z0 = delay_line[MOD(delay_pos - dl_used, delay_line_length)] + 0.12919609397f*z1 - 0.31050847f*z2; out = sat(z0*0.20466966f + z1*0.40933933f + z2*0.40933933f, -0.23f, 3.3f); z2 = z1; z1 = z0; delay_pos = (delay_pos + 1) % delay_line_length; if ((count++ % law_p) == 0) { // Value for amplitude of law peak next_law_peak = (float)rand() / (float)RAND_MAX; next_law_pos = count + law_p; } else if (count % law_p == law_p / 2) { // Value for amplitude of law peak prev_law_peak = (float)rand() / (float)RAND_MAX; prev_law_pos = count + law_p; } n_ph = (float)(law_p - abs(next_law_pos - count))/(float)law_p; p_ph = n_ph + 0.5f; if (p_ph > 1.0f) { p_ph -= 1.0f; } law = f_sin_sq(3.1415926f*p_ph)*prev_law_peak + f_sin_sq(3.1415926f*n_ph)*next_law_peak; increment = inc_base / (delay_depth * law + 0.2); fph = f_trunc(phase); last_phase = fph; lin_int = phase - (float)fph; out += LIN_INTERP(lin_int, buffer[(fph+1) % buffer_size], buffer[(fph+2) % buffer_size]); phase += increment; lin_inc = 1.0f / (floor(phase) - last_phase + 1); lin_inc = lin_inc > 1.0f ? 1.0f : lin_inc; lin_int = 0.0f; for (track = last_phase; track < phase; track++) { lin_int += lin_inc; buffer[track % buffer_size] = LIN_INTERP(lin_int, last_in, input[pos]); } last_in = input[pos]; buffer_write(output[pos], out * 0.707f); if (phase >= buffer_size) { phase -= buffer_size; } } // Store current phase in instance plugin_data->phase = phase; plugin_data->prev_law_peak = prev_law_peak; plugin_data->next_law_peak = next_law_peak; plugin_data->prev_law_pos = prev_law_pos; plugin_data->next_law_pos = next_law_pos; plugin_data->last_phase = last_phase; plugin_data->last_in = last_in; plugin_data->count = count; plugin_data->last_law_p = last_law_p; plugin_data->delay_pos = delay_pos; plugin_data->z0 = z0; plugin_data->z1 = z1; plugin_data->z2 = z2; }
static void runAddingFlanger(LADSPA_Handle instance, unsigned long sample_count) { Flanger *plugin_data = (Flanger *)instance; LADSPA_Data run_adding_gain = plugin_data->run_adding_gain; /* Delay base (ms) (float value) */ const LADSPA_Data delay_base = *(plugin_data->delay_base); /* Max slowdown (ms) (float value) */ const LADSPA_Data detune = *(plugin_data->detune); /* LFO frequency (Hz) (float value) */ const LADSPA_Data law_freq = *(plugin_data->law_freq); /* Feedback (float value) */ const LADSPA_Data feedback = *(plugin_data->feedback); /* Input (array of floats of length sample_count) */ const LADSPA_Data * const input = plugin_data->input; /* Output (array of floats of length sample_count) */ LADSPA_Data * const output = plugin_data->output; long count = plugin_data->count; long delay_pos = plugin_data->delay_pos; long delay_size = plugin_data->delay_size; LADSPA_Data * delay_tbl = plugin_data->delay_tbl; float next_law_peak = plugin_data->next_law_peak; int next_law_pos = plugin_data->next_law_pos; long old_d_base = plugin_data->old_d_base; float prev_law_peak = plugin_data->prev_law_peak; int prev_law_pos = plugin_data->prev_law_pos; long sample_rate = plugin_data->sample_rate; #line 50 "flanger_1191.xml" unsigned long pos; long d_base, new_d_base; LADSPA_Data out; float delay_depth; float dp; // float delay position float dp_frac; // fractional part long dp_idx; // integer delay index long law_p; // period of law float frac = 0.0f, step; // Portion the way through the block float law; /* law amplitude */ float n_ph, p_ph; const float fb = f_clamp(feedback, -0.999f, 0.999f); // Set law params law_p = (float)sample_rate / law_freq; if (law_p < 1) { law_p = 1; } // Calculate base delay size in samples new_d_base = (LIMIT(f_round(delay_base), 0, 25) * sample_rate) / 1000; // Calculate delay depth in samples delay_depth = f_clamp(detune * (float)sample_rate * 0.001f, 0.0f, delay_size - new_d_base - 1.0f); step = 1.0f/sample_count; for (pos = 0; pos < sample_count; pos++) { if (count % law_p == 0) { // Value for amplitude of law peak next_law_peak = (float)rand() / (float)RAND_MAX; next_law_pos = count + law_p; } else if (count % law_p == law_p / 2) { // Value for amplitude of law peak prev_law_peak = (float)rand() / (float)RAND_MAX; prev_law_pos = count + law_p; } // Calculate position in delay table d_base = LIN_INTERP(frac, old_d_base, new_d_base); n_ph = (float)(law_p - abs(next_law_pos - count))/(float)law_p; p_ph = n_ph + 0.5f; while (p_ph > 1.0f) { p_ph -= 1.0f; } law = f_sin_sq(3.1415926f*p_ph)*prev_law_peak + f_sin_sq(3.1415926f*n_ph)*next_law_peak; dp = (float)(delay_pos - d_base) - (delay_depth * law); // Get the integer part dp_idx = f_round(dp - 0.5f); // Get the fractional part dp_frac = dp - dp_idx; // Accumulate into output buffer out = cube_interp(dp_frac, delay_tbl[(dp_idx-1) & (delay_size-1)], delay_tbl[dp_idx & (delay_size-1)], delay_tbl[(dp_idx+1) & (delay_size-1)], delay_tbl[(dp_idx+2) & (delay_size-1)]); // Store new delayed value delay_tbl[delay_pos] = flush_to_zero(input[pos] + (fb * out)); // Sometimes the delay can pick up NaN values, I'm not sure why // and this is easier than fixing it if (isnan(delay_tbl[delay_pos])) { delay_tbl[delay_pos] = 0.0f; } out = f_clamp(delay_tbl[delay_pos] * 0.707f, -1.0, 1.0); buffer_write(output[pos], out); frac += step; delay_pos = (delay_pos + 1) & (delay_size-1); count++; } plugin_data->count = count; plugin_data->prev_law_peak = prev_law_peak; plugin_data->next_law_peak = next_law_peak; plugin_data->prev_law_pos = prev_law_pos; plugin_data->next_law_pos = next_law_pos; plugin_data->delay_pos = delay_pos; plugin_data->old_d_base = new_d_base; }