1 #include "audio_mixer.h"
5 #include <bmusb/bmusb.h>
10 #include <immintrin.h>
17 using namespace bmusb;
19 using namespace std::placeholders;
23 // TODO: If these prove to be a bottleneck, they can be SSSE3-optimized
24 // (usually including multiple channels at a time).
26 void convert_fixed16_to_fp32(float *dst, size_t out_channel, size_t out_num_channels,
27 const uint8_t *src, size_t in_channel, size_t in_num_channels,
30 assert(in_channel < in_num_channels);
31 assert(out_channel < out_num_channels);
32 src += in_channel * 2;
35 for (size_t i = 0; i < num_samples; ++i) {
36 int16_t s = le16toh(*(int16_t *)src);
37 *dst = s * (1.0f / 32768.0f);
39 src += 2 * in_num_channels;
40 dst += out_num_channels;
44 void convert_fixed24_to_fp32(float *dst, size_t out_channel, size_t out_num_channels,
45 const uint8_t *src, size_t in_channel, size_t in_num_channels,
48 assert(in_channel < in_num_channels);
49 assert(out_channel < out_num_channels);
50 src += in_channel * 3;
53 for (size_t i = 0; i < num_samples; ++i) {
57 uint32_t s = s1 | (s1 << 8) | (s2 << 16) | (s3 << 24);
58 *dst = int(s) * (1.0f / 2147483648.0f);
60 src += 3 * in_num_channels;
61 dst += out_num_channels;
65 void convert_fixed32_to_fp32(float *dst, size_t out_channel, size_t out_num_channels,
66 const uint8_t *src, size_t in_channel, size_t in_num_channels,
69 assert(in_channel < in_num_channels);
70 assert(out_channel < out_num_channels);
71 src += in_channel * 4;
74 for (size_t i = 0; i < num_samples; ++i) {
75 int32_t s = le32toh(*(int32_t *)src);
76 *dst = s * (1.0f / 2147483648.0f);
78 src += 4 * in_num_channels;
79 dst += out_num_channels;
83 float find_peak_plain(const float *samples, size_t num_samples) __attribute__((unused));
85 float find_peak_plain(const float *samples, size_t num_samples)
87 float m = fabs(samples[0]);
88 for (size_t i = 1; i < num_samples; ++i) {
89 m = max(m, fabs(samples[i]));
95 static inline float horizontal_max(__m128 m)
97 __m128 tmp = _mm_shuffle_ps(m, m, _MM_SHUFFLE(1, 0, 3, 2));
98 m = _mm_max_ps(m, tmp);
99 tmp = _mm_shuffle_ps(m, m, _MM_SHUFFLE(2, 3, 0, 1));
100 m = _mm_max_ps(m, tmp);
101 return _mm_cvtss_f32(m);
104 float find_peak(const float *samples, size_t num_samples)
106 const __m128 abs_mask = _mm_castsi128_ps(_mm_set1_epi32(0x7fffffffu));
107 __m128 m = _mm_setzero_ps();
108 for (size_t i = 0; i < (num_samples & ~3); i += 4) {
109 __m128 x = _mm_loadu_ps(samples + i);
110 x = _mm_and_ps(x, abs_mask);
111 m = _mm_max_ps(m, x);
113 float result = horizontal_max(m);
115 for (size_t i = (num_samples & ~3); i < num_samples; ++i) {
116 result = max(result, fabs(samples[i]));
120 // Self-test. We should be bit-exact the same.
121 float reference_result = find_peak_plain(samples, num_samples);
122 if (result != reference_result) {
123 fprintf(stderr, "Error: Peak is %f [%f %f %f %f]; should be %f.\n",
125 _mm_cvtss_f32(_mm_shuffle_ps(m, m, _MM_SHUFFLE(0, 0, 0, 0))),
126 _mm_cvtss_f32(_mm_shuffle_ps(m, m, _MM_SHUFFLE(1, 1, 1, 1))),
127 _mm_cvtss_f32(_mm_shuffle_ps(m, m, _MM_SHUFFLE(2, 2, 2, 2))),
128 _mm_cvtss_f32(_mm_shuffle_ps(m, m, _MM_SHUFFLE(3, 3, 3, 3))),
136 float find_peak(const float *samples, size_t num_samples)
138 return find_peak_plain(samples, num_samples);
142 void deinterleave_samples(const vector<float> &in, vector<float> *out_l, vector<float> *out_r)
144 size_t num_samples = in.size() / 2;
145 out_l->resize(num_samples);
146 out_r->resize(num_samples);
148 const float *inptr = in.data();
149 float *lptr = &(*out_l)[0];
150 float *rptr = &(*out_r)[0];
151 for (size_t i = 0; i < num_samples; ++i) {
159 AudioMixer::AudioMixer(unsigned num_cards)
160 : num_cards(num_cards),
161 limiter(OUTPUT_FREQUENCY),
162 correlation(OUTPUT_FREQUENCY)
164 for (unsigned bus_index = 0; bus_index < MAX_BUSES; ++bus_index) {
165 locut[bus_index].init(FILTER_HPF, 2);
166 locut_enabled[bus_index] = global_flags.locut_enabled;
167 gain_staging_db[bus_index] = global_flags.initial_gain_staging_db;
168 compressor[bus_index].reset(new StereoCompressor(OUTPUT_FREQUENCY));
169 compressor_threshold_dbfs[bus_index] = ref_level_dbfs - 12.0f; // -12 dB.
170 compressor_enabled[bus_index] = global_flags.compressor_enabled;
171 level_compressor[bus_index].reset(new StereoCompressor(OUTPUT_FREQUENCY));
172 level_compressor_enabled[bus_index] = global_flags.gain_staging_auto;
174 set_limiter_enabled(global_flags.limiter_enabled);
175 set_final_makeup_gain_auto(global_flags.final_makeup_gain_auto);
177 // Generate a very simple, default input mapping.
178 InputMapping::Bus input;
180 input.device.type = InputSourceType::CAPTURE_CARD;
181 input.device.index = 0;
182 input.source_channel[0] = 0;
183 input.source_channel[1] = 1;
185 InputMapping new_input_mapping;
186 new_input_mapping.buses.push_back(input);
187 set_input_mapping(new_input_mapping);
189 // Look for ALSA cards.
190 available_alsa_cards = ALSAInput::enumerate_devices();
192 r128.init(2, OUTPUT_FREQUENCY);
195 // hlen=16 is pretty low quality, but we use quite a bit of CPU otherwise,
196 // and there's a limit to how important the peak meter is.
197 peak_resampler.setup(OUTPUT_FREQUENCY, OUTPUT_FREQUENCY * 4, /*num_channels=*/2, /*hlen=*/16, /*frel=*/1.0);
200 AudioMixer::~AudioMixer()
202 for (unsigned card_index = 0; card_index < available_alsa_cards.size(); ++card_index) {
203 const AudioDevice &device = alsa_inputs[card_index];
204 if (device.alsa_device != nullptr) {
205 device.alsa_device->stop_capture_thread();
211 void AudioMixer::reset_resampler(DeviceSpec device_spec)
213 lock_guard<timed_mutex> lock(audio_mutex);
214 reset_resampler_mutex_held(device_spec);
217 void AudioMixer::reset_resampler_mutex_held(DeviceSpec device_spec)
219 AudioDevice *device = find_audio_device(device_spec);
221 if (device->interesting_channels.empty()) {
222 device->resampling_queue.reset();
224 // TODO: ResamplingQueue should probably take the full device spec.
225 // (It's only used for console output, though.)
226 device->resampling_queue.reset(new ResamplingQueue(device_spec.index, device->capture_frequency, OUTPUT_FREQUENCY, device->interesting_channels.size()));
228 device->next_local_pts = 0;
231 void AudioMixer::reset_alsa_mutex_held(DeviceSpec device_spec)
233 assert(device_spec.type == InputSourceType::ALSA_INPUT);
234 unsigned card_index = device_spec.index;
235 AudioDevice *device = find_audio_device(device_spec);
237 if (device->alsa_device != nullptr) {
238 device->alsa_device->stop_capture_thread();
240 if (device->interesting_channels.empty()) {
241 device->alsa_device.reset();
243 const ALSAInput::Device &alsa_dev = available_alsa_cards[card_index];
244 device->alsa_device.reset(new ALSAInput(alsa_dev.address.c_str(), OUTPUT_FREQUENCY, alsa_dev.num_channels, bind(&AudioMixer::add_audio, this, device_spec, _1, _2, _3, _4)));
245 device->capture_frequency = device->alsa_device->get_sample_rate();
246 device->alsa_device->start_capture_thread();
250 bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, AudioFormat audio_format, int64_t frame_length)
252 AudioDevice *device = find_audio_device(device_spec);
254 unique_lock<timed_mutex> lock(audio_mutex, defer_lock);
255 if (!lock.try_lock_for(chrono::milliseconds(10))) {
258 if (device->resampling_queue == nullptr) {
259 // No buses use this device; throw it away.
263 unsigned num_channels = device->interesting_channels.size();
264 assert(num_channels > 0);
266 // Convert the audio to fp32.
267 unique_ptr<float[]> audio(new float[num_samples * num_channels]);
268 unsigned channel_index = 0;
269 for (auto channel_it = device->interesting_channels.cbegin(); channel_it != device->interesting_channels.end(); ++channel_it, ++channel_index) {
270 switch (audio_format.bits_per_sample) {
272 assert(num_samples == 0);
275 convert_fixed16_to_fp32(audio.get(), channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples);
278 convert_fixed24_to_fp32(audio.get(), channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples);
281 convert_fixed32_to_fp32(audio.get(), channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples);
284 fprintf(stderr, "Cannot handle audio with %u bits per sample\n", audio_format.bits_per_sample);
290 int64_t local_pts = device->next_local_pts;
291 device->resampling_queue->add_input_samples(local_pts / double(TIMEBASE), audio.get(), num_samples);
292 device->next_local_pts = local_pts + frame_length;
296 bool AudioMixer::add_silence(DeviceSpec device_spec, unsigned samples_per_frame, unsigned num_frames, int64_t frame_length)
298 AudioDevice *device = find_audio_device(device_spec);
300 unique_lock<timed_mutex> lock(audio_mutex, defer_lock);
301 if (!lock.try_lock_for(chrono::milliseconds(10))) {
304 if (device->resampling_queue == nullptr) {
305 // No buses use this device; throw it away.
309 unsigned num_channels = device->interesting_channels.size();
310 assert(num_channels > 0);
312 vector<float> silence(samples_per_frame * num_channels, 0.0f);
313 for (unsigned i = 0; i < num_frames; ++i) {
314 device->resampling_queue->add_input_samples(device->next_local_pts / double(TIMEBASE), silence.data(), samples_per_frame);
315 // Note that if the format changed in the meantime, we have
316 // no way of detecting that; we just have to assume the frame length
317 // is always the same.
318 device->next_local_pts += frame_length;
323 AudioMixer::AudioDevice *AudioMixer::find_audio_device(DeviceSpec device)
325 switch (device.type) {
326 case InputSourceType::CAPTURE_CARD:
327 return &video_cards[device.index];
328 case InputSourceType::ALSA_INPUT:
329 return &alsa_inputs[device.index];
330 case InputSourceType::SILENCE:
337 // Get a pointer to the given channel from the given device.
338 // The channel must be picked out earlier and resampled.
339 void AudioMixer::find_sample_src_from_device(const map<DeviceSpec, vector<float>> &samples_card, DeviceSpec device_spec, int source_channel, const float **srcptr, unsigned *stride)
341 static float zero = 0.0f;
342 if (source_channel == -1 || device_spec.type == InputSourceType::SILENCE) {
347 AudioDevice *device = find_audio_device(device_spec);
348 assert(device->interesting_channels.count(source_channel) != 0);
349 unsigned channel_index = 0;
350 for (int channel : device->interesting_channels) {
351 if (channel == source_channel) break;
354 assert(channel_index < device->interesting_channels.size());
355 const auto it = samples_card.find(device_spec);
356 assert(it != samples_card.end());
357 *srcptr = &(it->second)[channel_index];
358 *stride = device->interesting_channels.size();
361 // TODO: Can be SSSE3-optimized if need be.
362 void AudioMixer::fill_audio_bus(const map<DeviceSpec, vector<float>> &samples_card, const InputMapping::Bus &bus, unsigned num_samples, float *output)
364 if (bus.device.type == InputSourceType::SILENCE) {
365 memset(output, 0, num_samples * sizeof(*output));
367 assert(bus.device.type == InputSourceType::CAPTURE_CARD ||
368 bus.device.type == InputSourceType::ALSA_INPUT);
369 const float *lsrc, *rsrc;
370 unsigned lstride, rstride;
371 float *dptr = output;
372 find_sample_src_from_device(samples_card, bus.device, bus.source_channel[0], &lsrc, &lstride);
373 find_sample_src_from_device(samples_card, bus.device, bus.source_channel[1], &rsrc, &rstride);
374 for (unsigned i = 0; i < num_samples; ++i) {
383 vector<float> AudioMixer::get_output(double pts, unsigned num_samples, ResamplingQueue::RateAdjustmentPolicy rate_adjustment_policy)
385 map<DeviceSpec, vector<float>> samples_card;
386 vector<float> samples_bus;
388 lock_guard<timed_mutex> lock(audio_mutex);
390 // Pick out all the interesting channels from all the cards.
391 // TODO: If the card has been hotswapped, the number of channels
392 // might have changed; if so, we need to do some sort of remapping
394 for (const auto &spec_and_info : get_devices_mutex_held()) {
395 const DeviceSpec &device_spec = spec_and_info.first;
396 AudioDevice *device = find_audio_device(device_spec);
397 if (!device->interesting_channels.empty()) {
398 samples_card[device_spec].resize(num_samples * device->interesting_channels.size());
399 device->resampling_queue->get_output_samples(
401 &samples_card[device_spec][0],
403 rate_adjustment_policy);
407 vector<float> samples_out, left, right;
408 samples_out.resize(num_samples * 2);
409 samples_bus.resize(num_samples * 2);
410 for (unsigned bus_index = 0; bus_index < input_mapping.buses.size(); ++bus_index) {
411 fill_audio_bus(samples_card, input_mapping.buses[bus_index], num_samples, &samples_bus[0]);
413 // Cut away everything under 120 Hz (or whatever the cutoff is);
414 // we don't need it for voice, and it will reduce headroom
415 // and confuse the compressor. (In particular, any hums at 50 or 60 Hz
416 // should be dampened.)
417 if (locut_enabled[bus_index]) {
418 locut[bus_index].render(samples_bus.data(), samples_bus.size() / 2, locut_cutoff_hz * 2.0 * M_PI / OUTPUT_FREQUENCY, 0.5f);
422 lock_guard<mutex> lock(compressor_mutex);
424 // Apply a level compressor to get the general level right.
425 // Basically, if it's over about -40 dBFS, we squeeze it down to that level
426 // (or more precisely, near it, since we don't use infinite ratio),
427 // then apply a makeup gain to get it to -14 dBFS. -14 dBFS is, of course,
428 // entirely arbitrary, but from practical tests with speech, it seems to
429 // put ut around -23 LUFS, so it's a reasonable starting point for later use.
430 if (level_compressor_enabled[bus_index]) {
431 float threshold = 0.01f; // -40 dBFS.
433 float attack_time = 0.5f;
434 float release_time = 20.0f;
435 float makeup_gain = from_db(ref_level_dbfs - (-40.0f)); // +26 dB.
436 level_compressor[bus_index]->process(samples_bus.data(), samples_bus.size() / 2, threshold, ratio, attack_time, release_time, makeup_gain);
437 gain_staging_db[bus_index] = to_db(level_compressor[bus_index]->get_attenuation() * makeup_gain);
439 // Just apply the gain we already had.
440 float g = from_db(gain_staging_db[bus_index]);
441 for (size_t i = 0; i < samples_bus.size(); ++i) {
447 printf("level=%f (%+5.2f dBFS) attenuation=%f (%+5.2f dB) end_result=%+5.2f dB\n",
448 level_compressor.get_level(), to_db(level_compressor.get_level()),
449 level_compressor.get_attenuation(), to_db(level_compressor.get_attenuation()),
450 to_db(level_compressor.get_level() * level_compressor.get_attenuation() * makeup_gain));
453 // The real compressor.
454 if (compressor_enabled[bus_index]) {
455 float threshold = from_db(compressor_threshold_dbfs[bus_index]);
457 float attack_time = 0.005f;
458 float release_time = 0.040f;
459 float makeup_gain = 2.0f; // +6 dB.
460 compressor[bus_index]->process(samples_bus.data(), samples_bus.size() / 2, threshold, ratio, attack_time, release_time, makeup_gain);
461 // compressor_att = compressor.get_attenuation();
465 // TODO: We should measure post-fader.
466 deinterleave_samples(samples_bus, &left, &right);
467 measure_bus_levels(bus_index, left, right);
469 float volume = from_db(fader_volume_db[bus_index]);
470 if (bus_index == 0) {
471 for (unsigned i = 0; i < num_samples * 2; ++i) {
472 samples_out[i] = samples_bus[i] * volume;
475 for (unsigned i = 0; i < num_samples * 2; ++i) {
476 samples_out[i] += samples_bus[i] * volume;
482 lock_guard<mutex> lock(compressor_mutex);
484 // Finally a limiter at -4 dB (so, -10 dBFS) to take out the worst peaks only.
485 // Note that since ratio is not infinite, we could go slightly higher than this.
486 if (limiter_enabled) {
487 float threshold = from_db(limiter_threshold_dbfs);
489 float attack_time = 0.0f; // Instant.
490 float release_time = 0.020f;
491 float makeup_gain = 1.0f; // 0 dB.
492 limiter.process(samples_out.data(), samples_out.size() / 2, threshold, ratio, attack_time, release_time, makeup_gain);
493 // limiter_att = limiter.get_attenuation();
496 // printf("limiter=%+5.1f compressor=%+5.1f\n", to_db(limiter_att), to_db(compressor_att));
499 // At this point, we are most likely close to +0 LU (at least if the
500 // faders sum to 0 dB and the compressors are on), but all of our
501 // measurements have been on raw sample values, not R128 values.
502 // So we have a final makeup gain to get us to +0 LU; the gain
503 // adjustments required should be relatively small, and also, the
504 // offset shouldn't change much (only if the type of audio changes
505 // significantly). Thus, we shoot for updating this value basically
506 // “whenever we process buffers”, since the R128 calculation isn't exactly
507 // something we get out per-sample.
509 // Note that there's a feedback loop here, so we choose a very slow filter
510 // (half-time of 30 seconds).
511 double target_loudness_factor, alpha;
512 double loudness_lu = r128.loudness_M() - ref_level_lufs;
513 double current_makeup_lu = to_db(final_makeup_gain);
514 target_loudness_factor = final_makeup_gain * from_db(-loudness_lu);
516 // If we're outside +/- 5 LU uncorrected, we don't count it as
517 // a normal signal (probably silence) and don't change the
518 // correction factor; just apply what we already have.
519 if (fabs(loudness_lu - current_makeup_lu) >= 5.0 || !final_makeup_gain_auto) {
522 // Formula adapted from
523 // https://en.wikipedia.org/wiki/Low-pass_filter#Simple_infinite_impulse_response_filter.
524 const double half_time_s = 30.0;
525 const double fc_mul_2pi_delta_t = 1.0 / (half_time_s * OUTPUT_FREQUENCY);
526 alpha = fc_mul_2pi_delta_t / (fc_mul_2pi_delta_t + 1.0);
530 lock_guard<mutex> lock(compressor_mutex);
531 double m = final_makeup_gain;
532 for (size_t i = 0; i < samples_out.size(); i += 2) {
533 samples_out[i + 0] *= m;
534 samples_out[i + 1] *= m;
535 m += (target_loudness_factor - m) * alpha;
537 final_makeup_gain = m;
540 update_meters(samples_out);
545 void AudioMixer::measure_bus_levels(unsigned bus_index, const vector<float> &left, const vector<float> &right)
547 const float *ptrs[] = { left.data(), right.data() };
549 lock_guard<mutex> lock(audio_measure_mutex);
550 bus_r128[bus_index]->process(left.size(), const_cast<float **>(ptrs));
554 void AudioMixer::update_meters(const vector<float> &samples)
556 // Upsample 4x to find interpolated peak.
557 peak_resampler.inp_data = const_cast<float *>(samples.data());
558 peak_resampler.inp_count = samples.size() / 2;
560 vector<float> interpolated_samples;
561 interpolated_samples.resize(samples.size());
563 lock_guard<mutex> lock(audio_measure_mutex);
565 while (peak_resampler.inp_count > 0) { // About four iterations.
566 peak_resampler.out_data = &interpolated_samples[0];
567 peak_resampler.out_count = interpolated_samples.size() / 2;
568 peak_resampler.process();
569 size_t out_stereo_samples = interpolated_samples.size() / 2 - peak_resampler.out_count;
570 peak = max<float>(peak, find_peak(interpolated_samples.data(), out_stereo_samples * 2));
571 peak_resampler.out_data = nullptr;
575 // Find R128 levels and L/R correlation.
576 vector<float> left, right;
577 deinterleave_samples(samples, &left, &right);
578 float *ptrs[] = { left.data(), right.data() };
580 lock_guard<mutex> lock(audio_measure_mutex);
581 r128.process(left.size(), ptrs);
582 correlation.process_samples(samples);
585 send_audio_level_callback();
588 void AudioMixer::reset_meters()
590 lock_guard<mutex> lock(audio_measure_mutex);
591 peak_resampler.reset();
598 void AudioMixer::send_audio_level_callback()
600 if (audio_level_callback == nullptr) {
604 lock_guard<mutex> lock(audio_measure_mutex);
605 double loudness_s = r128.loudness_S();
606 double loudness_i = r128.integrated();
607 double loudness_range_low = r128.range_min();
608 double loudness_range_high = r128.range_max();
610 vector<BusLevel> bus_levels;
611 bus_levels.resize(input_mapping.buses.size());
613 lock_guard<mutex> lock(compressor_mutex);
614 for (unsigned bus_index = 0; bus_index < bus_r128.size(); ++bus_index) {
615 bus_levels[bus_index].loudness_lufs = bus_r128[bus_index]->loudness_S();
616 bus_levels[bus_index].gain_staging_db = gain_staging_db[bus_index];
617 if (compressor_enabled[bus_index]) {
618 bus_levels[bus_index].compressor_attenuation_db = -to_db(compressor[bus_index]->get_attenuation());
620 bus_levels[bus_index].compressor_attenuation_db = 0.0;
625 audio_level_callback(loudness_s, to_db(peak), bus_levels,
626 loudness_i, loudness_range_low, loudness_range_high,
627 to_db(final_makeup_gain),
628 correlation.get_correlation());
631 map<DeviceSpec, DeviceInfo> AudioMixer::get_devices() const
633 lock_guard<timed_mutex> lock(audio_mutex);
634 return get_devices_mutex_held();
637 map<DeviceSpec, DeviceInfo> AudioMixer::get_devices_mutex_held() const
639 map<DeviceSpec, DeviceInfo> devices;
640 for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
641 const DeviceSpec spec{ InputSourceType::CAPTURE_CARD, card_index };
642 const AudioDevice *device = &video_cards[card_index];
644 info.name = device->name;
645 info.num_channels = 8; // FIXME: This is wrong for fake cards.
646 devices.insert(make_pair(spec, info));
648 for (unsigned card_index = 0; card_index < available_alsa_cards.size(); ++card_index) {
649 const DeviceSpec spec{ InputSourceType::ALSA_INPUT, card_index };
650 const ALSAInput::Device &device = available_alsa_cards[card_index];
652 info.name = device.name + " (" + device.info + ")";
653 info.num_channels = device.num_channels;
654 devices.insert(make_pair(spec, info));
659 void AudioMixer::set_name(DeviceSpec device_spec, const string &name)
661 AudioDevice *device = find_audio_device(device_spec);
663 lock_guard<timed_mutex> lock(audio_mutex);
667 void AudioMixer::set_input_mapping(const InputMapping &new_input_mapping)
669 lock_guard<timed_mutex> lock(audio_mutex);
671 map<DeviceSpec, set<unsigned>> interesting_channels;
672 for (const InputMapping::Bus &bus : new_input_mapping.buses) {
673 if (bus.device.type == InputSourceType::CAPTURE_CARD ||
674 bus.device.type == InputSourceType::ALSA_INPUT) {
675 for (unsigned channel = 0; channel < 2; ++channel) {
676 if (bus.source_channel[channel] != -1) {
677 interesting_channels[bus.device].insert(bus.source_channel[channel]);
683 // Reset resamplers for all cards that don't have the exact same state as before.
684 for (const auto &spec_and_info : get_devices_mutex_held()) {
685 const DeviceSpec &device_spec = spec_and_info.first;
686 AudioDevice *device = find_audio_device(device_spec);
687 if (device->interesting_channels != interesting_channels[device_spec]) {
688 device->interesting_channels = interesting_channels[device_spec];
689 if (device_spec.type == InputSourceType::ALSA_INPUT) {
690 reset_alsa_mutex_held(device_spec);
692 reset_resampler_mutex_held(device_spec);
697 lock_guard<mutex> lock(audio_measure_mutex);
698 bus_r128.resize(new_input_mapping.buses.size());
699 for (unsigned bus_index = 0; bus_index < bus_r128.size(); ++bus_index) {
700 if (bus_r128[bus_index] == nullptr) {
701 bus_r128[bus_index].reset(new Ebu_r128_proc);
703 bus_r128[bus_index]->init(2, OUTPUT_FREQUENCY);
707 input_mapping = new_input_mapping;
710 InputMapping AudioMixer::get_input_mapping() const
712 lock_guard<timed_mutex> lock(audio_mutex);
713 return input_mapping;