]> git.sesse.net Git - nageru/blobdiff - mixer.cpp
Add a compressor (kindly relicensed by Rune Holm). Fixed for now, and only to get...
[nageru] / mixer.cpp
index 5eca941860f73de3dec58ac5246c07dab9c04467..43e39b6c18e8518b8b0ecee7df91ebf0d2a0b269 100644 (file)
--- a/mixer.cpp
+++ b/mixer.cpp
@@ -68,7 +68,8 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
        : httpd("test.ts", WIDTH, HEIGHT),
          num_cards(num_cards),
          mixer_surface(create_surface(format)),
-         h264_encoder_surface(create_surface(format))
+         h264_encoder_surface(create_surface(format)),
+         compressor(48000.0f)
 {
        httpd.start(9095);
 
@@ -520,25 +521,58 @@ void Mixer::thread_func()
 
 void Mixer::process_audio_one_frame()
 {
-       // TODO: Allow using audio from the other card(s) as well.
+       vector<float> samples_card;
        vector<float> samples_out;
        for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
-               samples_out.resize((48000 / 60) * 2);
+               samples_card.resize((48000 / 60) * 2);
                {
                        unique_lock<mutex> lock(cards[card_index].audio_mutex);
-                       if (!cards[card_index].resampler->get_output_samples(pts(), &samples_out[0], 48000 / 60)) {
+                       if (!cards[card_index].resampler->get_output_samples(pts(), &samples_card[0], 48000 / 60)) {
                                printf("Card %d reported previous underrun.\n", card_index);
                        }
                }
+               // TODO: Allow using audio from the other card(s) as well.
                if (card_index == 0) {
-                       vector<float> left, right;
-                       peak = std::max(peak, find_peak(samples_out));
-                       deinterleave_samples(samples_out, &left, &right);
-                       float *ptrs[] = { left.data(), right.data() };
-                       r128.process(left.size(), ptrs);
-                       h264_encoder->add_audio(pts_int, move(samples_out));
+                       samples_out = move(samples_card);
                }
        }
+
+       // 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 -12 dBFS. -12 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.
+       //
+       // TODO: Hook this up to a UI, so we can see the effects, and/or turn it off
+       // to control the gain manually instead. For now, there's only the #if-ed out
+       // code below.
+       //
+       // TODO: Add the actual compressors/limiters (for taking care of transients)
+       // later in the chain.
+       float threshold = 0.01f;   // -40 dBFS.
+       float ratio = 20.0f;
+       float attack_time = 0.1f;
+       float release_time = 10.0f;
+       float makeup_gain = pow(10.0f, 28.0f / 20.0f);  // +28 dB takes us to -12 dBFS.
+       compressor.process(samples_out.data(), samples_out.size() / 2, threshold, ratio, attack_time, release_time, makeup_gain);
+
+#if 0
+       printf("level=%f (%+5.2f dBFS) attenuation=%f (%+5.2f dB) end_result=%+5.2f dB\n",
+               compressor.get_level(), 20.0 * log10(compressor.get_level()),
+               compressor.get_attenuation(), 20.0 * log10(compressor.get_attenuation()),
+               20.0 * log10(compressor.get_level() * compressor.get_attenuation() * makeup_gain));
+#endif
+
+       // Find peak and R128 levels.
+       peak = std::max(peak, find_peak(samples_out));
+       vector<float> left, right;
+       deinterleave_samples(samples_out, &left, &right);
+       float *ptrs[] = { left.data(), right.data() };
+       r128.process(left.size(), ptrs);
+
+       // Actually add the samples to the output.
+       h264_encoder->add_audio(pts_int, move(samples_out));
 }
 
 void Mixer::subsample_chroma(GLuint src_tex, GLuint dst_tex)