// Resample the audio as needed, including from previously dropped frames.
assert(num_cards > 0);
for (unsigned frame_num = 0; frame_num < dropped_frames + 1; ++frame_num) {
+ const bool dropped_frame = (frame_num != dropped_frames);
{
// Signal to the audio thread to process this frame.
+ // Note that if the frame is a dropped frame, we signal that
+ // we don't want to use this frame as base for adjusting
+ // the resampler rate. The reason for this is that the timing
+ // of these frames is often way too late; they typically don't
+ // “arrive” before we synthesize them. Thus, we could end up
+ // in a situation where we have inserted e.g. five audio frames
+ // into the queue before we then start pulling five of them
+ // back out. This makes ResamplingQueue overestimate the delay,
+ // causing undue resampler changes. (We _do_ use the last,
+ // non-dropped frame; perhaps we should just discard that as well,
+ // since dropped frames are expected to be rare, and it might be
+ // better to just wait until we have a slightly more normal situation).
unique_lock<mutex> lock(audio_mutex);
- audio_task_queue.push(AudioTask{pts_int, num_samples_per_frame});
+ bool adjust_rate = !dropped_frame;
+ audio_task_queue.push(AudioTask{pts_int, num_samples_per_frame, adjust_rate});
audio_task_queue_changed.notify_one();
}
- if (frame_num != dropped_frames) {
+ if (dropped_frame) {
// For dropped frames, increase the pts. Note that if the format changed
// in the meantime, we have no way of detecting that; we just have to
// assume the frame length is always the same.
audio_task_queue.pop();
}
- process_audio_one_frame(task.pts_int, task.num_samples);
+ process_audio_one_frame(task.pts_int, task.num_samples, task.adjust_rate);
}
}
-void Mixer::process_audio_one_frame(int64_t frame_pts_int, int num_samples)
+void Mixer::process_audio_one_frame(int64_t frame_pts_int, int num_samples, bool adjust_rate)
{
vector<float> samples_card;
vector<float> samples_out;
samples_card.resize(num_samples * 2);
{
unique_lock<mutex> lock(cards[card_index].audio_mutex);
- cards[card_index].resampling_queue->get_output_samples(double(frame_pts_int) / TIMEBASE, &samples_card[0], num_samples);
+ ResamplingQueue::RateAdjustmentPolicy rate_adjustment_policy =
+ adjust_rate ? ResamplingQueue::ADJUST_RATE : ResamplingQueue::DO_NOT_ADJUST_RATE;
+ cards[card_index].resampling_queue->get_output_samples(
+ double(frame_pts_int) / TIMEBASE,
+ &samples_card[0],
+ num_samples,
+ rate_adjustment_policy);
}
if (card_index == selected_audio_card) {
samples_out = move(samples_card);
}
}
-bool ResamplingQueue::get_output_samples(double pts, float *samples, ssize_t num_samples)
+bool ResamplingQueue::get_output_samples(double pts, float *samples, ssize_t num_samples, ResamplingQueue::RateAdjustmentPolicy rate_adjustment_policy)
{
assert(num_samples > 0);
if (first_input) {
return true;
}
- double last_output_len;
- if (first_output) {
- // Synthesize a fake length.
- last_output_len = double(num_samples) / freq_out;
- } else {
- last_output_len = pts - last_output_pts;
- }
- last_output_pts = pts;
-
- // Using the time point since just before the last call to add_input_samples() as a base,
- // estimate actual delay based on activity since then, measured in number of input samples:
- double actual_delay = 0.0;
- assert(last_input_len != 0);
- actual_delay += (k_a1 - k_a0) * last_output_len / last_input_len; // Inserted samples since k_a0, rescaled for the different time periods.
- actual_delay += k_a0 - total_consumed_samples; // Samples inserted before k_a0 but not consumed yet.
- actual_delay += vresampler.inpdist(); // Delay in the resampler itself.
- double err = actual_delay - expected_delay;
- if (first_output && err < 0.0) {
- // Before the very first block, insert artificial delay based on our initial estimate,
- // so that we don't need a long period to stabilize at the beginning.
- int delay_samples_to_add = lrintf(-err);
- for (ssize_t i = 0; i < delay_samples_to_add * num_channels; ++i) {
- buffer.push_front(0.0f);
+ double rcorr = -1.0;
+ if (rate_adjustment_policy == ADJUST_RATE) {
+ double last_output_len;
+ if (first_output) {
+ // Synthesize a fake length.
+ last_output_len = double(num_samples) / freq_out;
+ } else {
+ last_output_len = pts - last_output_pts;
}
- total_consumed_samples -= delay_samples_to_add; // Equivalent to increasing k_a0 and k_a1.
- err += delay_samples_to_add;
- }
- first_output = false;
-
- // Compute loop filter coefficients for the two filters. We need to compute them
- // every time, since they depend on the number of samples the user asked for.
- //
- // The loop bandwidth is at 0.02 Hz; we trust the initial estimate quite well,
- // and our jitter is pretty large since none of the threads involved run at
- // real-time priority.
- double loop_bandwidth_hz = 0.02;
-
- // Set filters. The first filter much wider than the first one (20x as wide).
- double w = (2.0 * M_PI) * loop_bandwidth_hz * num_samples / freq_out;
- double w0 = 1.0 - exp(-20.0 * w);
- double w1 = w * 1.5 / num_samples / ratio;
- double w2 = w / 1.5;
-
- // Filter <err> through the loop filter to find the correction ratio.
- z1 += w0 * (w1 * err - z1);
- z2 += w0 * (z1 - z2);
- z3 += w2 * z2;
- double rcorr = 1.0 - z2 - z3;
- if (rcorr > 1.05) rcorr = 1.05;
- if (rcorr < 0.95) rcorr = 0.95;
- assert(!isnan(rcorr));
- vresampler.set_rratio(rcorr);
+ last_output_pts = pts;
+
+ // Using the time point since just before the last call to add_input_samples() as a base,
+ // estimate actual delay based on activity since then, measured in number of input samples:
+ double actual_delay = 0.0;
+ assert(last_input_len != 0);
+ actual_delay += (k_a1 - k_a0) * last_output_len / last_input_len; // Inserted samples since k_a0, rescaled for the different time periods.
+ actual_delay += k_a0 - total_consumed_samples; // Samples inserted before k_a0 but not consumed yet.
+ actual_delay += vresampler.inpdist(); // Delay in the resampler itself.
+ double err = actual_delay - expected_delay;
+ if (first_output && err < 0.0) {
+ // Before the very first block, insert artificial delay based on our initial estimate,
+ // so that we don't need a long period to stabilize at the beginning.
+ int delay_samples_to_add = lrintf(-err);
+ for (ssize_t i = 0; i < delay_samples_to_add * num_channels; ++i) {
+ buffer.push_front(0.0f);
+ }
+ total_consumed_samples -= delay_samples_to_add; // Equivalent to increasing k_a0 and k_a1.
+ err += delay_samples_to_add;
+ }
+ first_output = false;
+
+ // Compute loop filter coefficients for the two filters. We need to compute them
+ // every time, since they depend on the number of samples the user asked for.
+ //
+ // The loop bandwidth is at 0.02 Hz; we trust the initial estimate quite well,
+ // and our jitter is pretty large since none of the threads involved run at
+ // real-time priority.
+ double loop_bandwidth_hz = 0.02;
+
+ // Set filters. The first filter much wider than the first one (20x as wide).
+ double w = (2.0 * M_PI) * loop_bandwidth_hz * num_samples / freq_out;
+ double w0 = 1.0 - exp(-20.0 * w);
+ double w1 = w * 1.5 / num_samples / ratio;
+ double w2 = w / 1.5;
+
+ // Filter <err> through the loop filter to find the correction ratio.
+ z1 += w0 * (w1 * err - z1);
+ z2 += w0 * (z1 - z2);
+ z3 += w2 * z2;
+ rcorr = 1.0 - z2 - z3;
+ if (rcorr > 1.05) rcorr = 1.05;
+ if (rcorr < 0.95) rcorr = 0.95;
+ assert(!isnan(rcorr));
+ vresampler.set_rratio(rcorr);
+ } else {
+ assert(rate_adjustment_policy == DO_NOT_ADJUST_RATE);
+ };
// Finally actually resample, consuming exactly <num_samples> output samples.
vresampler.out_data = samples;
// card_num is for debugging outputs only.
ResamplingQueue(unsigned card_num, unsigned freq_in, unsigned freq_out, unsigned num_channels = 2);
+ // If policy is DO_NOT_ADJUST_RATE, the resampling rate will not be changed.
+ // This is primarily useful if you have an extraordinary situation, such as
+ // dropped frames.
+ enum RateAdjustmentPolicy {
+ DO_NOT_ADJUST_RATE,
+ ADJUST_RATE
+ };
+
// Note: pts is always in seconds.
void add_input_samples(double pts, const float *samples, ssize_t num_samples);
- bool get_output_samples(double pts, float *samples, ssize_t num_samples); // Returns false if underrun.
+ // Returns false if underrun.
+ bool get_output_samples(double pts, float *samples, ssize_t num_samples, RateAdjustmentPolicy rate_adjustment_policy);
private:
void init_loop_filter(double bandwidth_hz);