static bool Delay_pstate_set_audio_rate(Device_state* dstate, int32_t audio_rate) { assert(dstate != NULL); assert(audio_rate > 0); Delay_pstate* dpstate = (Delay_pstate*)dstate; const Proc_delay* delay = (const Proc_delay*)dstate->device->dimpl; const int32_t delay_buf_size = delay->max_delay * audio_rate + 1; for (int i = 0; i < KQT_BUFFERS_MAX; ++i) { Work_buffer* buf = dpstate->bufs[i]; if (!Work_buffer_resize(buf, delay_buf_size)) return false; Work_buffer_clear(buf, 0, Work_buffer_get_size(buf)); } dpstate->buf_pos = 0; return true; }
bool Delay_pstate_set_max_delay( Device_state* dstate, const Key_indices indices, double value) { assert(dstate != NULL); ignore(indices); ignore(value); Delay_pstate* dpstate = (Delay_pstate*)dstate; const Proc_delay* delay = (const Proc_delay*)dstate->device->dimpl; const int32_t delay_buf_size = delay->max_delay * dstate->audio_rate + 1; for (int i = 0; i < KQT_BUFFERS_MAX; ++i) { Work_buffer* buf = dpstate->bufs[i]; if (!Work_buffer_resize(buf, delay_buf_size)) return false; Work_buffer_clear(buf, 0, Work_buffer_get_size(buf)); } dpstate->buf_pos = 0; return true; }
static void Device_thread_state_clear_buffers( Device_thread_state* ts, Device_buffer_type buf_type, int32_t buf_start, int32_t buf_stop) { rassert(ts != NULL); rassert(buf_type < DEVICE_BUFFER_TYPES); rassert(buf_start >= 0); rassert(buf_stop >= buf_start); for (Device_port_type port_type = DEVICE_PORT_TYPE_RECV; port_type < DEVICE_PORT_TYPES; ++port_type) { Etable* bufs = ts->buffers[buf_type][port_type]; const int cap = Etable_get_capacity(bufs); for (int port = 0; port < cap; ++port) { Work_buffer* buffer = Etable_get(bufs, port); if (buffer != NULL) Work_buffer_clear(buffer, buf_start, buf_stop); } } Bit_array_clear(ts->in_connected); return; }
static void Delay_pstate_reset(Device_state* dstate) { assert(dstate != NULL); Delay_pstate* dpstate = (Delay_pstate*)dstate; for (int i = 0; i < KQT_BUFFERS_MAX; ++i) { Work_buffer* buf = dpstate->bufs[i]; Work_buffer_clear(buf, 0, Work_buffer_get_size(buf)); } dpstate->buf_pos = 0; return; }
static void Delay_pstate_clear_history(Proc_state* proc_state) { assert(proc_state != NULL); Delay_pstate* dpstate = (Delay_pstate*)proc_state; for (int i = 0; i < KQT_BUFFERS_MAX; ++i) { Work_buffer* buf = dpstate->bufs[i]; Work_buffer_clear(buf, 0, Work_buffer_get_size(buf)); } dpstate->buf_pos = 0; return; }
static int32_t Add_vstate_render_voice( Voice_state* vstate, Proc_state* proc_state, const Device_thread_state* proc_ts, const Au_state* au_state, const Work_buffers* wbs, int32_t buf_start, int32_t buf_stop, double tempo) { rassert(vstate != NULL); rassert(proc_state != NULL); rassert(proc_ts != NULL); rassert(au_state != NULL); rassert(wbs != NULL); rassert(tempo > 0); const Device_state* dstate = &proc_state->parent; const Proc_add* add = (Proc_add*)proc_state->parent.device->dimpl; Add_vstate* add_state = (Add_vstate*)vstate; rassert(is_p2(ADD_BASE_FUNC_SIZE)); // Get frequencies Work_buffer* freqs_wb = Device_thread_state_get_voice_buffer( proc_ts, DEVICE_PORT_TYPE_RECV, PORT_IN_PITCH); Work_buffer* pitches_wb = freqs_wb; if (freqs_wb == NULL) freqs_wb = Work_buffers_get_buffer_mut(wbs, ADD_WORK_BUFFER_FIXED_PITCH); Proc_fill_freq_buffer(freqs_wb, pitches_wb, buf_start, buf_stop); const float* freqs = Work_buffer_get_contents(freqs_wb); // Get volume scales Work_buffer* scales_wb = Device_thread_state_get_voice_buffer( proc_ts, DEVICE_PORT_TYPE_RECV, PORT_IN_FORCE); Work_buffer* dBs_wb = scales_wb; if (scales_wb == NULL) scales_wb = Work_buffers_get_buffer_mut(wbs, ADD_WORK_BUFFER_FIXED_FORCE); Proc_fill_scale_buffer(scales_wb, dBs_wb, buf_start, buf_stop); const float* scales = Work_buffer_get_contents(scales_wb); // Get output buffer for writing float* out_bufs[2] = { NULL }; Proc_state_get_voice_audio_out_buffers( proc_ts, PORT_OUT_AUDIO_L, PORT_OUT_COUNT, out_bufs); // Get phase modulation signal const Work_buffer* mod_wbs[] = { Device_thread_state_get_voice_buffer( proc_ts, DEVICE_PORT_TYPE_RECV, PORT_IN_PHASE_MOD_L), Device_thread_state_get_voice_buffer( proc_ts, DEVICE_PORT_TYPE_RECV, PORT_IN_PHASE_MOD_R), }; for (int ch = 0; ch < 2; ++ch) { if (mod_wbs[ch] == NULL) { Work_buffer* zero_buf = Work_buffers_get_buffer_mut( wbs, (Work_buffer_type)(ADD_WORK_BUFFER_MOD_L + ch)); Work_buffer_clear(zero_buf, buf_start, buf_stop); mod_wbs[ch] = zero_buf; } } // Add base waveform tones const double inv_audio_rate = 1.0 / dstate->audio_rate; const float* base = Sample_get_buffer(add->base, 0); for (int h = 0; h < add_state->tone_limit; ++h) { const Add_tone* tone = &add->tones[h]; const double pitch_factor = tone->pitch_factor; const double volume_factor = tone->volume_factor; if ((pitch_factor <= 0) || (volume_factor <= 0)) continue; const double pannings[] = { -tone->panning, tone->panning, }; const double pitch_factor_inv_audio_rate = pitch_factor * inv_audio_rate; Add_tone_state* tone_state = &add_state->tones[h]; for (int32_t ch = 0; ch < 2; ++ch) { float* out_buf_ch = out_bufs[ch]; if (out_buf_ch == NULL) continue; const double panning_factor = 1 + pannings[ch]; const float* mod_values_ch = Work_buffer_get_contents(mod_wbs[ch]); double phase = tone_state->phase[ch]; int32_t res_slice_start = buf_start; while (res_slice_start < buf_stop) { int32_t res_slice_stop = buf_stop; // Get current pitch range const float first_mod_shift = mod_values_ch[res_slice_start] - add_state->prev_mod[ch]; const float first_phase_shift_abs = (float)fabs( first_mod_shift + (freqs[res_slice_start] * pitch_factor_inv_audio_rate)); int shift_exp = 0; const float shift_norm = frexpf(first_phase_shift_abs, &shift_exp); const float min_phase_shift_abs = ldexpf(0.5f, shift_exp); const float max_phase_shift_abs = min_phase_shift_abs * 2.0f; // Choose appropriate waveform resolution for current pitch range int32_t cur_size = ADD_BASE_FUNC_SIZE; if (isfinite(shift_norm) && (shift_norm > 0.0f)) { cur_size = (int32_t)ipowi(2, max(-shift_exp + 1, 3)); cur_size = min(cur_size, ADD_BASE_FUNC_SIZE * 2); rassert(is_p2(cur_size)); } const uint32_t cur_size_mask = (uint32_t)cur_size - 1; const int base_offset = (ADD_BASE_FUNC_SIZE * 4 - cur_size * 2); rassert(base_offset >= 0); rassert(base_offset < (ADD_BASE_FUNC_SIZE * 4) - 1); const float* cur_base = base + base_offset; // Get length of input compatible with current waveform resolution const int32_t res_check_stop = min(res_slice_stop, max(Work_buffer_get_const_start(freqs_wb), Work_buffer_get_const_start(mod_wbs[ch])) + 1); for (int32_t i = res_slice_start + 1; i < res_check_stop; ++i) { const float cur_mod_shift = mod_values_ch[i] - mod_values_ch[i - 1]; const float cur_phase_shift_abs = (float)fabs( cur_mod_shift + (freqs[i] * pitch_factor_inv_audio_rate)); if (cur_phase_shift_abs < min_phase_shift_abs || cur_phase_shift_abs > max_phase_shift_abs) { res_slice_stop = i; break; } } for (int32_t i = res_slice_start; i < res_slice_stop; ++i) { const float freq = freqs[i]; const float vol_scale = scales[i]; const float mod_val = mod_values_ch[i]; // Note: + mod_val is specific to phase modulation const double actual_phase = phase + mod_val; const double pos = actual_phase * cur_size; // Note: direct cast of negative doubles to uint32_t is undefined const uint32_t pos1 = (uint32_t)(int32_t)floor(pos) & cur_size_mask; const uint32_t pos2 = (pos1 + 1) & cur_size_mask; const float item1 = cur_base[pos1]; const float item_diff = cur_base[pos2] - item1; const double lerp_val = pos - floor(pos); const double value = (item1 + (lerp_val * item_diff)) * volume_factor * panning_factor; out_buf_ch[i] += (float)value * vol_scale; phase += freq * pitch_factor_inv_audio_rate; // Normalise to range [0, 1) if (phase >= 1) { phase -= 1; // Don't bother updating the phase if our frequency is too high if (phase >= 1) phase = tone_state->phase[ch]; } } rassert(res_slice_start < res_slice_stop); add_state->prev_mod[ch] = mod_values_ch[res_slice_stop - 1]; res_slice_start = res_slice_stop; } tone_state->phase[ch] = phase; } } if (add->is_ramp_attack_enabled) Proc_ramp_attack(vstate, 2, out_bufs, buf_start, buf_stop, dstate->audio_rate); return buf_stop; }
static void Compress_states_update( Compress_state cstates[2], const Proc_compress* compress, Work_buffer* gain_wb, Work_buffer* level_wbs[2], const Work_buffer* in_wbs[2], int32_t buf_start, int32_t buf_stop, int32_t audio_rate) { rassert(cstates != NULL); rassert(compress != NULL); rassert(gain_wb != NULL); rassert(level_wbs != NULL); rassert(level_wbs[0] != NULL); rassert(level_wbs[1] != NULL); rassert(in_wbs != NULL); rassert(buf_start >= 0); rassert(buf_stop > buf_start); // Get levels const float attack_mul = (float)dB_to_scale(6 / (compress->attack * 0.001 * audio_rate)); const float release_mul = (float)dB_to_scale(-6 / (compress->release * 0.001 * audio_rate)); for (int ch = 0; ch < 2; ++ch) { Compress_state* cstate = &cstates[ch]; rassert(cstate != NULL); if (in_wbs[ch] == NULL) continue; float level = cstate->level; float* levels = Work_buffer_get_contents_mut(level_wbs[ch]); const float* in = Work_buffer_get_contents(in_wbs[ch]); for (int32_t i = buf_start; i < buf_stop; ++i) { const float in_abs = fabsf(in[i]); if (in_abs > level) { level *= attack_mul; level = min(level, in_abs); } else { level *= release_mul; level = max(level, max((float)MIN_LEVEL, in_abs)); } levels[i] = level; } cstate->level = level; } if ((in_wbs[0] != NULL) && (in_wbs[1] != NULL)) { // Get maximum levels for (int32_t i = buf_start; i < buf_stop; ++i) level_wbs[0] = max(level_wbs[0], level_wbs[1]); } const Work_buffer* applied_levels_wb = (in_wbs[0] == NULL) ? level_wbs[1] : level_wbs[0]; const float* applied_levels = Work_buffer_get_contents(applied_levels_wb); Work_buffer_clear(gain_wb, buf_start, buf_stop); float* gains = Work_buffer_get_contents_mut(gain_wb); for (int32_t i = buf_start; i < buf_stop; ++i) gains[i] = 1.0f; if (compress->upward_enabled) { // Apply upward compression const double upward_threshold_dB = compress->downward_enabled ? min(compress->upward_threshold, compress->downward_threshold) : compress->upward_threshold; const float threshold = (float)dB_to_scale(upward_threshold_dB); const float inv_ratio = (float)(1.0 / compress->upward_ratio); const float max_gain = (float)dB_to_scale(compress->upward_range); for (int32_t i = buf_start; i < buf_stop; ++i) { const float level = applied_levels[i]; if (level < threshold) { const float diff = threshold / level; const float gain = threshold / (powf(diff, inv_ratio) * level); gains[i] = min(gain, max_gain); } } } if (compress->downward_enabled) { // Apply downward compression const float threshold = (float)dB_to_scale(compress->downward_threshold); const float inv_ratio = (float)(1.0 / compress->downward_ratio); const float min_gain = (float)dB_to_scale(-compress->downward_range); for (int32_t i = buf_start; i < buf_stop; ++i) { const float level = applied_levels[i]; if (level > threshold) { const float diff = level / threshold; const float gain = (threshold * powf(diff, inv_ratio)) / level; gains[i] = max(gain, min_gain); } } } return; }
static void Freeverb_pstate_render_mixed( Device_state* dstate, const Work_buffers* wbs, int32_t buf_start, int32_t buf_stop, double tempo) { assert(dstate != NULL); assert(wbs != NULL); assert(buf_start >= 0); assert(tempo > 0); Freeverb_pstate* fstate = (Freeverb_pstate*)dstate; Proc_freeverb* freeverb = (Proc_freeverb*)dstate->device->dimpl; // Get reflectivity parameter stream float* refls = Device_state_get_audio_buffer_contents_mut( dstate, DEVICE_PORT_TYPE_RECEIVE, PORT_IN_REFL); if (refls == NULL) { refls = Work_buffers_get_buffer_contents_mut(wbs, FREEVERB_WB_FIXED_REFL); const float fixed_refl = exp2(-5 / freeverb->reflect_setting); for (int32_t i = buf_start; i < buf_stop; ++i) refls[i] = fixed_refl; } else { // Convert reflectivity to the domain of our algorithm static const float max_param_inv = -5.0 / 200.0; static const float min_param_inv = -5.0 / 0.001; for (int32_t i = buf_start; i < buf_stop; ++i) { const double orig_refl = refls[i]; const double param_inv = -5.0 / max(0, orig_refl); const float refl = fast_exp2(clamp(param_inv, min_param_inv, max_param_inv)); refls[i] = refl; } } // Get damp parameter stream float* damps = Device_state_get_audio_buffer_contents_mut( dstate, DEVICE_PORT_TYPE_RECEIVE, PORT_IN_DAMP); if (damps == NULL) { damps = Work_buffers_get_buffer_contents_mut(wbs, FREEVERB_WB_FIXED_DAMP); const float fixed_damp = freeverb->damp_setting * 0.01; for (int32_t i = buf_start; i < buf_stop; ++i) damps[i] = fixed_damp; } else { for (int32_t i = buf_start; i < buf_stop; ++i) { const float scaled_damp = damps[i] * 0.01f; damps[i] = clamp(scaled_damp, 0, 1); } } Work_buffer* in_wbs[] = { Device_state_get_audio_buffer(dstate, DEVICE_PORT_TYPE_RECEIVE, PORT_IN_AUDIO_L), Device_state_get_audio_buffer(dstate, DEVICE_PORT_TYPE_RECEIVE, PORT_IN_AUDIO_R), }; Work_buffer* out_wbs[] = { Device_state_get_audio_buffer(dstate, DEVICE_PORT_TYPE_SEND, PORT_OUT_AUDIO_L), Device_state_get_audio_buffer(dstate, DEVICE_PORT_TYPE_SEND, PORT_OUT_AUDIO_R), }; // TODO: figure out a cleaner way of dealing with the buffers Work_buffer* workspace[] = { Work_buffers_get_buffer_mut(wbs, FREEVERB_WB_LEFT), Work_buffers_get_buffer_mut(wbs, FREEVERB_WB_RIGHT), }; // Get input data if ((in_wbs[0] != NULL) && (in_wbs[1] != NULL)) { Work_buffer_copy(workspace[0], in_wbs[0], buf_start, buf_stop); Work_buffer_copy(workspace[1], in_wbs[1], buf_start, buf_stop); } else if ((in_wbs[0] == NULL) != (in_wbs[1] == NULL)) { const Work_buffer* existing = (in_wbs[0] != NULL) ? in_wbs[0] : in_wbs[1]; Work_buffer_copy(workspace[0], existing, buf_start, buf_stop); Work_buffer_copy(workspace[1], existing, buf_start, buf_stop); } else { Work_buffer_clear(workspace[0], buf_start, buf_stop); Work_buffer_clear(workspace[1], buf_start, buf_stop); } float* ws[] = { Work_buffer_get_contents_mut(workspace[0]), Work_buffer_get_contents_mut(workspace[1]), }; // Apply reverb { float* comb_input = Work_buffers_get_buffer_contents_mut(wbs, FREEVERB_WB_COMB_INPUT); for (int32_t i = buf_start; i < buf_stop; ++i) comb_input[i] = (ws[0][i] + ws[1][i]) * freeverb->gain; for (int ch = 0; ch < 2; ++ch) { float* ws_buf = ws[ch]; for (int32_t i = buf_start; i < buf_stop; ++i) ws_buf[i] = 0; for (int comb = 0; comb < FREEVERB_COMBS; ++comb) Freeverb_comb_process( fstate->combs[ch][comb], ws_buf, comb_input, refls, damps, buf_start, buf_stop); for (int allpass = 0; allpass < FREEVERB_ALLPASSES; ++allpass) Freeverb_allpass_process( fstate->allpasses[ch][allpass], ws_buf, buf_start, buf_stop); } for (int32_t i = buf_start; i < buf_stop; ++i) { ws[0][i] = ws[0][i] * freeverb->wet1 + ws[1][i] * freeverb->wet2; ws[1][i] = ws[1][i] * freeverb->wet1 + ws[0][i] * freeverb->wet2; } } // Copy results to outputs that exist for (int ch = 0; ch < 2; ++ch) { if (out_wbs[ch] != NULL) Work_buffer_copy(out_wbs[ch], workspace[ch], buf_start, buf_stop); } return; }