+ // Convert the audio to stereo fp32 and add it.
+ size_t num_samples = (audio_frame.len - audio_offset) / 8 / 3;
+ vector<float> audio;
+ audio.resize(num_samples * 2);
+ convert_fixed24_to_fp32(&audio[0], 2, audio_frame.data + audio_offset, 8, num_samples);
+
+ int unwrapped_timecode = timecode;
+ int dropped_frames = 0;
+ if (card->last_timecode != -1) {
+ unwrapped_timecode = unwrap_timecode(unwrapped_timecode, card->last_timecode);
+ dropped_frames = unwrapped_timecode - card->last_timecode - 1;
+ }
+ card->last_timecode = unwrapped_timecode;
+
+ // Add the audio.
+ {
+ unique_lock<mutex> lock(card->audio_mutex);
+
+ int unwrapped_timecode = timecode;
+ if (dropped_frames > 60 * 2) {
+ fprintf(stderr, "Card %d lost more than two seconds (or time code jumping around), resetting resampler\n",
+ card_index);
+ card->resampler.reset(new Resampler(48000.0, 48000.0, 2));
+ } else if (dropped_frames > 0) {
+ // Insert silence as needed.
+ fprintf(stderr, "Card %d dropped %d frame(s) (before timecode 0x%04x), inserting silence.\n",
+ card_index, dropped_frames, timecode);
+ vector<float> silence;
+ silence.resize((48000 / 60) * 2);
+ for (int i = 0; i < dropped_frames; ++i) {
+ card->resampler->add_input_samples((unwrapped_timecode - dropped_frames + i) / 60.0, silence.data(), (48000 / 60));
+ }
+ }
+ card->resampler->add_input_samples(unwrapped_timecode / 60.0, audio.data(), num_samples);
+ }
+
+ // Done with the audio, so release it.
+ if (audio_frame.owner) {
+ audio_frame.owner->release_frame(audio_frame);
+ }
+