- const InputMapping::Bus &input = input_mapping.buses[bus_index];
- if (input.input_source_type == InputSourceType::SILENCE) {
- memset(&samples_bus[0], 0, samples_bus.size() * sizeof(samples_bus[0]));
- } else {
- // TODO: Move this into its own function. Can be SSSE3-optimized if need be.
- assert(input.input_source_type == InputSourceType::CAPTURE_CARD);
- const float *lsrc, *rsrc;
- unsigned lstride, rstride;
- float *dptr = &samples_bus[0];
- find_sample_src_from_capture_card(samples_card, input.input_source_index, input.source_channel[0], &lsrc, &lstride);
- find_sample_src_from_capture_card(samples_card, input.input_source_index, input.source_channel[1], &rsrc, &rstride);
- for (unsigned i = 0; i < num_samples; ++i) {
- *dptr++ = *lsrc;
- *dptr++ = *rsrc;
- lsrc += lstride;
- rsrc += rstride;
+ fill_audio_bus(samples_card, input_mapping.buses[bus_index], num_samples, &samples_bus[0]);
+
+ // Cut away everything under 120 Hz (or whatever the cutoff is);
+ // we don't need it for voice, and it will reduce headroom
+ // and confuse the compressor. (In particular, any hums at 50 or 60 Hz
+ // should be dampened.)
+ if (locut_enabled[bus_index]) {
+ locut[bus_index].render(samples_bus.data(), samples_bus.size() / 2, locut_cutoff_hz * 2.0 * M_PI / OUTPUT_FREQUENCY, 0.5f);
+ }
+
+ {
+ lock_guard<mutex> lock(compressor_mutex);
+
+ // Apply a level compressor to get the general level right.
+ // Basically, if it's over about -40 dBFS, we squeeze it down to that level
+ // (or more precisely, near it, since we don't use infinite ratio),
+ // then apply a makeup gain to get it to -14 dBFS. -14 dBFS is, of course,
+ // entirely arbitrary, but from practical tests with speech, it seems to
+ // put ut around -23 LUFS, so it's a reasonable starting point for later use.
+ if (level_compressor_enabled[bus_index]) {
+ float threshold = 0.01f; // -40 dBFS.
+ float ratio = 20.0f;
+ float attack_time = 0.5f;
+ float release_time = 20.0f;
+ float makeup_gain = from_db(ref_level_dbfs - (-40.0f)); // +26 dB.
+ level_compressor[bus_index]->process(samples_bus.data(), samples_bus.size() / 2, threshold, ratio, attack_time, release_time, makeup_gain);
+ gain_staging_db[bus_index] = to_db(level_compressor[bus_index]->get_attenuation() * makeup_gain);
+ } else {
+ // Just apply the gain we already had.
+ float g = from_db(gain_staging_db[bus_index]);
+ for (size_t i = 0; i < samples_bus.size(); ++i) {
+ samples_bus[i] *= g;
+ }
+ }
+
+#if 0
+ printf("level=%f (%+5.2f dBFS) attenuation=%f (%+5.2f dB) end_result=%+5.2f dB\n",
+ level_compressor.get_level(), to_db(level_compressor.get_level()),
+ level_compressor.get_attenuation(), to_db(level_compressor.get_attenuation()),
+ to_db(level_compressor.get_level() * level_compressor.get_attenuation() * makeup_gain));
+#endif
+
+ // The real compressor.
+ if (compressor_enabled[bus_index]) {
+ float threshold = from_db(compressor_threshold_dbfs[bus_index]);
+ float ratio = 20.0f;
+ float attack_time = 0.005f;
+ float release_time = 0.040f;
+ float makeup_gain = 2.0f; // +6 dB.
+ compressor[bus_index]->process(samples_bus.data(), samples_bus.size() / 2, threshold, ratio, attack_time, release_time, makeup_gain);
+ // compressor_att = compressor.get_attenuation();