+ // Resample the audio as needed, including from previously dropped frames.
+ vector<float> samples_out;
+ // TODO: Allow using audio from the other card(s) as well.
+ for (unsigned frame_num = 0; frame_num < card_copy[0].dropped_frames + 1; ++frame_num) {
+ for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
+ samples_out.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)) {
+ printf("Card %d reported previous underrun.\n", card_index);
+ }
+ }
+ 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));
+ }
+ }
+ if (frame_num != card_copy[0].dropped_frames) {
+ // For dropped frames, increase the pts.
+ ++dropped_frames;
+ pts_int += TIMEBASE / 60;
+ }
+ }
+
+ if (audio_level_callback != nullptr) {
+ double loudness_s = r128.loudness_S();
+ double loudness_i = r128.integrated();
+ double loudness_range_low = r128.range_min();
+ double loudness_range_high = r128.range_max();
+
+ audio_level_callback(loudness_s, 20.0 * log10(peak),
+ loudness_i, loudness_range_low, loudness_range_high);
+ }
+
+ for (unsigned card_index = 1; card_index < num_cards; ++card_index) {
+ if (card_copy[card_index].new_data_ready && card_copy[card_index].new_frame->len == 0) {
+ ++card_copy[card_index].dropped_frames;
+ }
+ if (card_copy[card_index].dropped_frames > 0) {
+ printf("Card %u dropped %d frames before this\n",
+ card_index, int(card_copy[card_index].dropped_frames));
+ }
+ }
+
+ // If the first card is reporting a corrupted or otherwise dropped frame,
+ // just increase the pts (skipping over this frame) and don't try to compute anything new.
+ if (card_copy[0].new_frame->len == 0) {
+ ++dropped_frames;
+ pts_int += TIMEBASE / 60;
+ continue;
+ }
+
+ for (unsigned card_index = 0; card_index < num_cards; ++card_index) {