inline void operator() (const fix15_t src_r, const fix15_t src_g, const fix15_t src_b, fix15_t &dst_r, fix15_t &dst_g, fix15_t &dst_b) const { process_channel(src_r, dst_r); process_channel(src_g, dst_g); process_channel(src_b, dst_b); }
void process_frame(const fft_channel *const infos[]) { for (int c = 0; number_channels != c; ++c) process_channel(values[c], infos[c]); }
static void run(LV2_Handle instance, uint32_t n_samples) { uint32_t i,c; BalanceControl* self = (BalanceControl*)instance; const float balance = *self->balance; const float trim = db_to_gain(*self->trim); float gain_left = 1.0; float gain_right = 1.0; const int ascnt = self->samplerate / UPDATE_FREQ; const uint32_t capacity = self->notify->atom.size; lv2_atom_forge_set_buffer(&self->forge, (uint8_t*)self->notify, capacity); lv2_atom_forge_sequence_head(&self->forge, &self->frame, 0); /* reset after state restore */ if (self->queue_stateswitch) { self->queue_stateswitch = 0; self->peak_integrate_pref = self->state[0] * self->samplerate; self->meter_falloff = self->state[1] / UPDATE_FREQ; self->peak_hold = self->state[2] * UPDATE_FREQ; self->peak_integrate_pref = MAX(0, self->peak_integrate_pref); self->peak_integrate_pref = MIN(self->peak_integrate_pref, self->peak_integrate_max); self->meter_falloff = MAX(0, self->meter_falloff); self->meter_falloff = MIN(self->meter_falloff, 1000); self->peak_hold = MAX(0, self->peak_hold); self->peak_hold = MIN(self->peak_hold, 60 * UPDATE_FREQ); reset_uicom(self); send_cfg_to_ui(self); } /* Process incoming events from GUI */ if (self->control) { LV2_Atom_Event* ev = lv2_atom_sequence_begin(&(self->control)->body); while(!lv2_atom_sequence_is_end(&(self->control)->body, (self->control)->atom.size, ev)) { if (ev->body.type == self->uris.atom_Blank || ev->body.type == self->uris.atom_Object) { const LV2_Atom_Object* obj = (LV2_Atom_Object*)&ev->body; if (obj->body.otype == self->uris.blc_meters_on) { if (self->uicom_active == 0) { reset_uicom(self); send_cfg_to_ui(self); self->uicom_active = 1; } } if (obj->body.otype == self->uris.blc_meters_off) { self->uicom_active = 0; } if (obj->body.otype == self->uris.blc_meters_cfg) { const LV2_Atom* key = NULL; const LV2_Atom* value = NULL; lv2_atom_object_get(obj, self->uris.blc_cckey, &key, self->uris.blc_ccval, &value, 0); if (value && key) { update_meter_cfg(self, ((LV2_Atom_Int*)key)->body, ((LV2_Atom_Float*)value)->body); } } } ev = lv2_atom_sequence_next(ev); } } /* pre-calculate parameters */ if (balance < 0) { gain_right = 1.0 + RAIL(balance, -1.0, 0.0); } else if (balance > 0) { gain_left = 1.0 - RAIL(balance, 0.0, 1.0); } switch ((int) *self->unitygain) { case 1: { /* maintain amplitude sum */ const double gaindiff = (gain_left - gain_right); gain_left = 1.0 + gaindiff; gain_right = 1.0 - gaindiff; } break; case 2: { /* equal power*/ if (balance < 0) { gain_right = MAX(.5, gain_right); gain_left = db_to_gain(-gain_to_db(gain_right)); } else { gain_left = MAX(.5, gain_left); gain_right = db_to_gain(-gain_to_db(gain_left)); } } case 0: /* 'tradidional' balance */ break; } if (*(self->phase[C_LEFT])) gain_left *=-1; if (*(self->phase[C_RIGHT])) gain_right *=-1; /* keep track of input levels -- only if GUI is visiable */ if (self->uicom_active) { for (c=0; c < CHANNELS; ++c) { for (i=0; i < n_samples; ++i) { /* input peak meter */ const float ps = fabsf(self->input[c][i]); if (ps > self->p_peak_in[c]) self->p_peak_in[c] = ps; if (self->peak_integrate_pref < 1) { const float psm = ps * ps; if (psm > self->p_peak_inM[c]) self->p_peak_inM[c] = psm; continue; } /* integrated level, peak */ const int pip = (self->peak_integrate_pos + i ) % self->peak_integrate_pref; const double p_sig = SQUARE(self->input[c][i]); self->p_peak_inP[c] += p_sig - self->p_peak_inPi[c][pip]; self->p_peak_inPi[c][pip] = p_sig; /* peak of integrated signal */ const float psm = self->p_peak_inP[c] / (double) self->peak_integrate_pref; if (psm > self->p_peak_inM[c]) self->p_peak_inM[c] = psm; } } } /* process audio -- delayline + balance & gain */ process_channel(self, gain_left * trim, C_LEFT, n_samples); process_channel(self, gain_right * trim, C_RIGHT, n_samples); /* swap/assign channels */ uint32_t pos = 0; if (self->c_monomode != (int) *self->monomode) { /* smooth change */ const uint32_t fade_len = (n_samples >= FADE_LEN) ? FADE_LEN : n_samples; for (; pos < fade_len; pos++) { const float gain = (float)pos / (float)fade_len; float x1[CHANNELS], x2[CHANNELS]; channel_map_change(self, self->c_monomode, pos, x1); channel_map_change(self, (int) *self->monomode, pos, x2); self->output[C_LEFT][pos] = x1[C_LEFT] * (1.0 - gain) + x2[C_LEFT] * gain; self->output[C_RIGHT][pos] = x1[C_RIGHT] * (1.0 - gain) + x2[C_RIGHT] * gain; } } channel_map(self, (int) *self->monomode, pos, n_samples); self->c_monomode = (int) *self->monomode; /* audio processing done */ if (!self->uicom_active) { return; } /* output peak meter */ for (c=0; c < CHANNELS; ++c) { for (i=0; i < n_samples; ++i) { /* peak */ const float ps = fabsf(self->output[c][i]); if (ps > self->p_peak_out[c]) self->p_peak_out[c] = ps; if (self->peak_integrate_pref < 1) { const float psm = ps * ps; if (psm > self->p_peak_outM[c]) self->p_peak_outM[c] = psm; continue; } /* integrated level, peak */ const int pip = (self->peak_integrate_pos + i ) % self->peak_integrate_pref; const double p_sig = SQUARE(self->output[c][i]); self->p_peak_outP[c] += p_sig - self->p_peak_outPi[c][pip]; self->p_peak_outPi[c][pip] = p_sig; /* peak of integrated signal */ const float psm = self->p_peak_outP[c] / (double) self->peak_integrate_pref; if (psm > self->p_peak_outM[c]) self->p_peak_outM[c] = psm; } } if (self->peak_integrate_pref > 0) { self->peak_integrate_pos = (self->peak_integrate_pos + n_samples ) % self->peak_integrate_pref; } /* simple output phase correlation */ for (i=0; i < n_samples; ++i) { const double p_pos = SQUARE(self->output[C_LEFT][i] + self->output[C_RIGHT][i]); const double p_neg = SQUARE(self->output[C_LEFT][i] - self->output[C_RIGHT][i]); /* integrate over 500ms */ self->p_phase_outP += p_pos - self->p_phase_outPi[self->phase_integrate_pos]; self->p_phase_outN += p_neg - self->p_phase_outNi[self->phase_integrate_pos]; self->p_phase_outPi[self->phase_integrate_pos] = p_pos; self->p_phase_outNi[self->phase_integrate_pos] = p_neg; self->phase_integrate_pos = (self->phase_integrate_pos + 1) % self->phase_integrate_max; } /* abs peak hold */ #define PKM(A,CHN,ID) \ { \ const float peak = VALTODB(self->p_peak_##A[CHN]); \ if (peak > self->p_max_##A[CHN]) { \ self->p_max_##A[CHN] = peak; \ self->p_tme_##A[CHN] = 0; \ forge_kvcontrolmessage(&self->forge, &self->uris, ID, self->p_max_##A[CHN]); \ } else if (self->peak_hold <= 0) { \ (self->p_tme_##A[CHN])=0; /* infinite hold */ \ } else if (self->p_tme_##A[CHN] <= self->peak_hold) { \ (self->p_tme_##A[CHN])++; \ } else if (self->meter_falloff == 0) { \ self->p_max_##A[CHN] = peak; \ forge_kvcontrolmessage(&self->forge, &self->uris, ID, self->p_max_##A[CHN]); \ } else { \ self->p_max_##A[CHN] -= self->meter_falloff; \ self->p_max_##A[CHN] = MAX(peak, self->p_max_##A[CHN]); \ forge_kvcontrolmessage(&self->forge, &self->uris, ID, self->p_max_##A[CHN]); \ } \ } /* RMS meter */ #define PKF(A,CHN,ID) \ { \ float dbp = VALTODB(sqrt(2.0 * self->p_peak_##A##M[CHN])); \ if (dbp > self->p_vpeak_##A[CHN]) { \ self->p_vpeak_##A[CHN] = dbp; \ } else if (self->meter_falloff == 0) { \ self->p_vpeak_##A[CHN] = dbp; \ } else { \ self->p_vpeak_##A[CHN] -= self->meter_falloff; \ self->p_vpeak_##A[CHN] = MAX(dbp, self->p_vpeak_##A[CHN]); \ } \ forge_kvcontrolmessage(&self->forge, &self->uris, ID, (self->p_vpeak_##A [CHN])); \ } /* report peaks to UI */ self->p_peakcnt += n_samples; if (self->p_peakcnt > ascnt) { PKF(in, C_LEFT, METER_IN_LEFT) PKF(in, C_RIGHT, METER_IN_RIGHT); PKF(out, C_LEFT, METER_OUT_LEFT); PKF(out, C_RIGHT, METER_OUT_RIGHT); PKM(in, C_LEFT, PEAK_IN_LEFT); PKM(in, C_RIGHT, PEAK_IN_RIGHT); PKM(out, C_LEFT, PEAK_OUT_LEFT); PKM(out, C_RIGHT, PEAK_OUT_RIGHT); #define RMSF(A) sqrt( ( (A) / (double)self->phase_integrate_max ) + 1.0e-12 ) double phase = 0.0; const double phasdiv = self->p_phase_outP + self->p_phase_outN; if (phasdiv >= 1.0e-6) { phase = (RMSF(self->p_phase_outP) - RMSF(self->p_phase_outN)) / RMSF(phasdiv); } else if (self->p_phase_outP > .001 && self->p_phase_outN > .001) { phase = 1.0; } forge_kvcontrolmessage(&self->forge, &self->uris, PHASE_OUT, phase); self->p_peakcnt -= ascnt; for (c=0; c < CHANNELS; ++c) { self->p_peak_in[c] = -INFINITY; self->p_peak_out[c] = -INFINITY; self->p_peak_inM[c] = -INFINITY; self->p_peak_outM[c] = -INFINITY; } } /* report values to UI - if changed*/ float bal = gain_to_db(fabsf(gain_left)); if (bal != self->p_bal[C_LEFT]) { forge_kvcontrolmessage(&self->forge, &self->uris, GAIN_LEFT, bal); } self->p_bal[C_LEFT] = bal; bal = gain_to_db(fabsf(gain_right)); if (bal != self->p_bal[C_RIGHT]) { forge_kvcontrolmessage(&self->forge, &self->uris, GAIN_RIGHT, bal); } self->p_bal[C_RIGHT] = bal; if (self->p_dly[C_LEFT] != self->c_dly[C_LEFT]) { forge_kvcontrolmessage(&self->forge, &self->uris, DELAY_LEFT, (float) self->c_dly[C_LEFT] / self->samplerate); } self->p_dly[C_LEFT] = self->c_dly[C_LEFT]; if (self->p_dly[C_RIGHT] != self->c_dly[C_RIGHT]) { forge_kvcontrolmessage(&self->forge, &self->uris, DELAY_RIGHT, (float) self->c_dly[C_RIGHT] / self->samplerate); } self->p_dly[C_RIGHT] = self->c_dly[C_RIGHT]; }