]> git.sesse.net Git - nageru/blob - audio_mixer.cpp
Move the R128 and correlation measurements into AudioMixer.
[nageru] / audio_mixer.cpp
1 #include "audio_mixer.h"
2
3 #include <assert.h>
4 #include <endian.h>
5 #include <bmusb/bmusb.h>
6 #include <stdio.h>
7 #include <endian.h>
8 #include <cmath>
9
10 #include "db.h"
11 #include "flags.h"
12 #include "timebase.h"
13
14 using namespace bmusb;
15 using namespace std;
16 using namespace std::placeholders;
17
18 namespace {
19
20 // TODO: If these prove to be a bottleneck, they can be SSSE3-optimized
21 // (usually including multiple channels at a time).
22
23 void convert_fixed16_to_fp32(float *dst, size_t out_channel, size_t out_num_channels,
24                              const uint8_t *src, size_t in_channel, size_t in_num_channels,
25                              size_t num_samples)
26 {
27         assert(in_channel < in_num_channels);
28         assert(out_channel < out_num_channels);
29         src += in_channel * 2;
30         dst += out_channel;
31
32         for (size_t i = 0; i < num_samples; ++i) {
33                 int16_t s = le16toh(*(int16_t *)src);
34                 *dst = s * (1.0f / 32768.0f);
35
36                 src += 2 * in_num_channels;
37                 dst += out_num_channels;
38         }
39 }
40
41 void convert_fixed24_to_fp32(float *dst, size_t out_channel, size_t out_num_channels,
42                              const uint8_t *src, size_t in_channel, size_t in_num_channels,
43                              size_t num_samples)
44 {
45         assert(in_channel < in_num_channels);
46         assert(out_channel < out_num_channels);
47         src += in_channel * 3;
48         dst += out_channel;
49
50         for (size_t i = 0; i < num_samples; ++i) {
51                 uint32_t s1 = src[0];
52                 uint32_t s2 = src[1];
53                 uint32_t s3 = src[2];
54                 uint32_t s = s1 | (s1 << 8) | (s2 << 16) | (s3 << 24);
55                 *dst = int(s) * (1.0f / 2147483648.0f);
56
57                 src += 3 * in_num_channels;
58                 dst += out_num_channels;
59         }
60 }
61
62 void convert_fixed32_to_fp32(float *dst, size_t out_channel, size_t out_num_channels,
63                              const uint8_t *src, size_t in_channel, size_t in_num_channels,
64                              size_t num_samples)
65 {
66         assert(in_channel < in_num_channels);
67         assert(out_channel < out_num_channels);
68         src += in_channel * 4;
69         dst += out_channel;
70
71         for (size_t i = 0; i < num_samples; ++i) {
72                 int32_t s = le32toh(*(int32_t *)src);
73                 *dst = s * (1.0f / 2147483648.0f);
74
75                 src += 4 * in_num_channels;
76                 dst += out_num_channels;
77         }
78 }
79
80 float find_peak(const float *samples, size_t num_samples)
81 {
82         float m = fabs(samples[0]);
83         for (size_t i = 1; i < num_samples; ++i) {
84                 m = max(m, fabs(samples[i]));
85         }
86         return m;
87 }
88
89 void deinterleave_samples(const vector<float> &in, vector<float> *out_l, vector<float> *out_r)
90 {
91         size_t num_samples = in.size() / 2;
92         out_l->resize(num_samples);
93         out_r->resize(num_samples);
94
95         const float *inptr = in.data();
96         float *lptr = &(*out_l)[0];
97         float *rptr = &(*out_r)[0];
98         for (size_t i = 0; i < num_samples; ++i) {
99                 *lptr++ = *inptr++;
100                 *rptr++ = *inptr++;
101         }
102 }
103
104 }  // namespace
105
106 AudioMixer::AudioMixer(unsigned num_cards)
107         : num_cards(num_cards),
108           level_compressor(OUTPUT_FREQUENCY),
109           limiter(OUTPUT_FREQUENCY),
110           compressor(OUTPUT_FREQUENCY),
111           correlation(OUTPUT_FREQUENCY)
112 {
113         locut.init(FILTER_HPF, 2);
114
115         set_locut_enabled(global_flags.locut_enabled);
116         set_gain_staging_db(global_flags.initial_gain_staging_db);
117         set_gain_staging_auto(global_flags.gain_staging_auto);
118         set_compressor_enabled(global_flags.compressor_enabled);
119         set_limiter_enabled(global_flags.limiter_enabled);
120         set_final_makeup_gain_auto(global_flags.final_makeup_gain_auto);
121
122         // Generate a very simple, default input mapping.
123         InputMapping::Bus input;
124         input.name = "Main";
125         input.device.type = InputSourceType::CAPTURE_CARD;
126         input.device.index = 0;
127         input.source_channel[0] = 0;
128         input.source_channel[1] = 1;
129
130         InputMapping new_input_mapping;
131         new_input_mapping.buses.push_back(input);
132         set_input_mapping(new_input_mapping);
133
134         // Look for ALSA cards.
135         available_alsa_cards = ALSAInput::enumerate_devices();
136
137         r128.init(2, OUTPUT_FREQUENCY);
138         r128.integr_start();
139
140         // hlen=16 is pretty low quality, but we use quite a bit of CPU otherwise,
141         // and there's a limit to how important the peak meter is.
142         peak_resampler.setup(OUTPUT_FREQUENCY, OUTPUT_FREQUENCY * 4, /*num_channels=*/2, /*hlen=*/16, /*frel=*/1.0);
143 }
144
145 AudioMixer::~AudioMixer()
146 {
147         for (unsigned card_index = 0; card_index < available_alsa_cards.size(); ++card_index) {
148                 const AudioDevice &device = alsa_inputs[card_index];
149                 if (device.alsa_device != nullptr) {
150                         device.alsa_device->stop_capture_thread();
151                 }
152         }
153 }
154
155
156 void AudioMixer::reset_resampler(DeviceSpec device_spec)
157 {
158         lock_guard<timed_mutex> lock(audio_mutex);
159         reset_resampler_mutex_held(device_spec);
160 }
161
162 void AudioMixer::reset_resampler_mutex_held(DeviceSpec device_spec)
163 {
164         AudioDevice *device = find_audio_device(device_spec);
165
166         if (device->interesting_channels.empty()) {
167                 device->resampling_queue.reset();
168         } else {
169                 // TODO: ResamplingQueue should probably take the full device spec.
170                 // (It's only used for console output, though.)
171                 device->resampling_queue.reset(new ResamplingQueue(device_spec.index, device->capture_frequency, OUTPUT_FREQUENCY, device->interesting_channels.size()));
172         }
173         device->next_local_pts = 0;
174 }
175
176 void AudioMixer::reset_alsa_mutex_held(DeviceSpec device_spec)
177 {
178         assert(device_spec.type == InputSourceType::ALSA_INPUT);
179         unsigned card_index = device_spec.index;
180         AudioDevice *device = find_audio_device(device_spec);
181
182         if (device->alsa_device != nullptr) {
183                 device->alsa_device->stop_capture_thread();
184         }
185         if (device->interesting_channels.empty()) {
186                 device->alsa_device.reset();
187         } else {
188                 device->alsa_device.reset(new ALSAInput(available_alsa_cards[card_index].address.c_str(), OUTPUT_FREQUENCY, 2, bind(&AudioMixer::add_audio, this, device_spec, _1, _2, _3, _4)));
189                 device->capture_frequency = device->alsa_device->get_sample_rate();
190                 device->alsa_device->start_capture_thread();
191         }
192 }
193
194 bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, AudioFormat audio_format, int64_t frame_length)
195 {
196         AudioDevice *device = find_audio_device(device_spec);
197
198         unique_lock<timed_mutex> lock(audio_mutex, defer_lock);
199         if (!lock.try_lock_for(chrono::milliseconds(10))) {
200                 return false;
201         }
202         if (device->resampling_queue == nullptr) {
203                 // No buses use this device; throw it away.
204                 return true;
205         }
206
207         unsigned num_channels = device->interesting_channels.size();
208         assert(num_channels > 0);
209
210         // Convert the audio to fp32.
211         vector<float> audio;
212         audio.resize(num_samples * num_channels);
213         unsigned channel_index = 0;
214         for (auto channel_it = device->interesting_channels.cbegin(); channel_it != device->interesting_channels.end(); ++channel_it, ++channel_index) {
215                 switch (audio_format.bits_per_sample) {
216                 case 0:
217                         assert(num_samples == 0);
218                         break;
219                 case 16:
220                         convert_fixed16_to_fp32(&audio[0], channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples);
221                         break;
222                 case 24:
223                         convert_fixed24_to_fp32(&audio[0], channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples);
224                         break;
225                 case 32:
226                         convert_fixed32_to_fp32(&audio[0], channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples);
227                         break;
228                 default:
229                         fprintf(stderr, "Cannot handle audio with %u bits per sample\n", audio_format.bits_per_sample);
230                         assert(false);
231                 }
232         }
233
234         // Now add it.
235         int64_t local_pts = device->next_local_pts;
236         device->resampling_queue->add_input_samples(local_pts / double(TIMEBASE), audio.data(), num_samples);
237         device->next_local_pts = local_pts + frame_length;
238         return true;
239 }
240
241 bool AudioMixer::add_silence(DeviceSpec device_spec, unsigned samples_per_frame, unsigned num_frames, int64_t frame_length)
242 {
243         AudioDevice *device = find_audio_device(device_spec);
244
245         unique_lock<timed_mutex> lock(audio_mutex, defer_lock);
246         if (!lock.try_lock_for(chrono::milliseconds(10))) {
247                 return false;
248         }
249         if (device->resampling_queue == nullptr) {
250                 // No buses use this device; throw it away.
251                 return true;
252         }
253
254         unsigned num_channels = device->interesting_channels.size();
255         assert(num_channels > 0);
256
257         vector<float> silence(samples_per_frame * num_channels, 0.0f);
258         for (unsigned i = 0; i < num_frames; ++i) {
259                 device->resampling_queue->add_input_samples(device->next_local_pts / double(TIMEBASE), silence.data(), samples_per_frame);
260                 // Note that if the format changed in the meantime, we have
261                 // no way of detecting that; we just have to assume the frame length
262                 // is always the same.
263                 device->next_local_pts += frame_length;
264         }
265         return true;
266 }
267
268 AudioMixer::AudioDevice *AudioMixer::find_audio_device(DeviceSpec device)
269 {
270         switch (device.type) {
271         case InputSourceType::CAPTURE_CARD:
272                 return &video_cards[device.index];
273         case InputSourceType::ALSA_INPUT:
274                 return &alsa_inputs[device.index];
275         case InputSourceType::SILENCE:
276         default:
277                 assert(false);
278         }
279         return nullptr;
280 }
281
282 // Get a pointer to the given channel from the given device.
283 // The channel must be picked out earlier and resampled.
284 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)
285 {
286         static float zero = 0.0f;
287         if (source_channel == -1 || device_spec.type == InputSourceType::SILENCE) {
288                 *srcptr = &zero;
289                 *stride = 0;
290                 return;
291         }
292         AudioDevice *device = find_audio_device(device_spec);
293         assert(device->interesting_channels.count(source_channel) != 0);
294         unsigned channel_index = 0;
295         for (int channel : device->interesting_channels) {
296                 if (channel == source_channel) break;
297                 ++channel_index;
298         }
299         assert(channel_index < device->interesting_channels.size());
300         const auto it = samples_card.find(device_spec);
301         assert(it != samples_card.end());
302         *srcptr = &(it->second)[channel_index];
303         *stride = device->interesting_channels.size();
304 }
305
306 // TODO: Can be SSSE3-optimized if need be.
307 void AudioMixer::fill_audio_bus(const map<DeviceSpec, vector<float>> &samples_card, const InputMapping::Bus &bus, unsigned num_samples, float *output)
308 {
309         if (bus.device.type == InputSourceType::SILENCE) {
310                 memset(output, 0, num_samples * sizeof(*output));
311         } else {
312                 assert(bus.device.type == InputSourceType::CAPTURE_CARD ||
313                        bus.device.type == InputSourceType::ALSA_INPUT);
314                 const float *lsrc, *rsrc;
315                 unsigned lstride, rstride;
316                 float *dptr = output;
317                 find_sample_src_from_device(samples_card, bus.device, bus.source_channel[0], &lsrc, &lstride);
318                 find_sample_src_from_device(samples_card, bus.device, bus.source_channel[1], &rsrc, &rstride);
319                 for (unsigned i = 0; i < num_samples; ++i) {
320                         *dptr++ = *lsrc;
321                         *dptr++ = *rsrc;
322                         lsrc += lstride;
323                         rsrc += rstride;
324                 }
325         }
326 }
327
328 vector<float> AudioMixer::get_output(double pts, unsigned num_samples, ResamplingQueue::RateAdjustmentPolicy rate_adjustment_policy)
329 {
330         map<DeviceSpec, vector<float>> samples_card;
331         vector<float> samples_bus;
332
333         lock_guard<timed_mutex> lock(audio_mutex);
334
335         // Pick out all the interesting channels from all the cards.
336         // TODO: If the card has been hotswapped, the number of channels
337         // might have changed; if so, we need to do some sort of remapping
338         // to silence.
339         for (const auto &spec_and_info : get_devices_mutex_held()) {
340                 const DeviceSpec &device_spec = spec_and_info.first;
341                 AudioDevice *device = find_audio_device(device_spec);
342                 if (!device->interesting_channels.empty()) {
343                         samples_card[device_spec].resize(num_samples * device->interesting_channels.size());
344                         device->resampling_queue->get_output_samples(
345                                 pts,
346                                 &samples_card[device_spec][0],
347                                 num_samples,
348                                 rate_adjustment_policy);
349                 }
350         }
351
352         // TODO: Move lo-cut etc. into each bus.
353         vector<float> samples_out;
354         samples_out.resize(num_samples * 2);
355         samples_bus.resize(num_samples * 2);
356         for (unsigned bus_index = 0; bus_index < input_mapping.buses.size(); ++bus_index) {
357                 fill_audio_bus(samples_card, input_mapping.buses[bus_index], num_samples, &samples_bus[0]);
358
359                 float volume = from_db(fader_volume_db[bus_index]);
360                 if (bus_index == 0) {
361                         for (unsigned i = 0; i < num_samples * 2; ++i) {
362                                 samples_out[i] = samples_bus[i] * volume;
363                         }
364                 } else {
365                         for (unsigned i = 0; i < num_samples * 2; ++i) {
366                                 samples_out[i] += samples_bus[i] * volume;
367                         }
368                 }
369         }
370
371         // Cut away everything under 120 Hz (or whatever the cutoff is);
372         // we don't need it for voice, and it will reduce headroom
373         // and confuse the compressor. (In particular, any hums at 50 or 60 Hz
374         // should be dampened.)
375         if (locut_enabled) {
376                 locut.render(samples_out.data(), samples_out.size() / 2, locut_cutoff_hz * 2.0 * M_PI / OUTPUT_FREQUENCY, 0.5f);
377         }
378
379         {
380                 lock_guard<mutex> lock(compressor_mutex);
381
382                 // Apply a level compressor to get the general level right.
383                 // Basically, if it's over about -40 dBFS, we squeeze it down to that level
384                 // (or more precisely, near it, since we don't use infinite ratio),
385                 // then apply a makeup gain to get it to -14 dBFS. -14 dBFS is, of course,
386                 // entirely arbitrary, but from practical tests with speech, it seems to
387                 // put ut around -23 LUFS, so it's a reasonable starting point for later use.
388                 {
389                         if (level_compressor_enabled) {
390                                 float threshold = 0.01f;   // -40 dBFS.
391                                 float ratio = 20.0f;
392                                 float attack_time = 0.5f;
393                                 float release_time = 20.0f;
394                                 float makeup_gain = from_db(ref_level_dbfs - (-40.0f));  // +26 dB.
395                                 level_compressor.process(samples_out.data(), samples_out.size() / 2, threshold, ratio, attack_time, release_time, makeup_gain);
396                                 gain_staging_db = to_db(level_compressor.get_attenuation() * makeup_gain);
397                         } else {
398                                 // Just apply the gain we already had.
399                                 float g = from_db(gain_staging_db);
400                                 for (size_t i = 0; i < samples_out.size(); ++i) {
401                                         samples_out[i] *= g;
402                                 }
403                         }
404                 }
405
406         #if 0
407                 printf("level=%f (%+5.2f dBFS) attenuation=%f (%+5.2f dB) end_result=%+5.2f dB\n",
408                         level_compressor.get_level(), to_db(level_compressor.get_level()),
409                         level_compressor.get_attenuation(), to_db(level_compressor.get_attenuation()),
410                         to_db(level_compressor.get_level() * level_compressor.get_attenuation() * makeup_gain));
411         #endif
412
413         //      float limiter_att, compressor_att;
414
415                 // The real compressor.
416                 if (compressor_enabled) {
417                         float threshold = from_db(compressor_threshold_dbfs);
418                         float ratio = 20.0f;
419                         float attack_time = 0.005f;
420                         float release_time = 0.040f;
421                         float makeup_gain = 2.0f;  // +6 dB.
422                         compressor.process(samples_out.data(), samples_out.size() / 2, threshold, ratio, attack_time, release_time, makeup_gain);
423         //              compressor_att = compressor.get_attenuation();
424                 }
425
426                 // Finally a limiter at -4 dB (so, -10 dBFS) to take out the worst peaks only.
427                 // Note that since ratio is not infinite, we could go slightly higher than this.
428                 if (limiter_enabled) {
429                         float threshold = from_db(limiter_threshold_dbfs);
430                         float ratio = 30.0f;
431                         float attack_time = 0.0f;  // Instant.
432                         float release_time = 0.020f;
433                         float makeup_gain = 1.0f;  // 0 dB.
434                         limiter.process(samples_out.data(), samples_out.size() / 2, threshold, ratio, attack_time, release_time, makeup_gain);
435         //              limiter_att = limiter.get_attenuation();
436                 }
437
438         //      printf("limiter=%+5.1f  compressor=%+5.1f\n", to_db(limiter_att), to_db(compressor_att));
439         }
440
441         // At this point, we are most likely close to +0 LU, but all of our
442         // measurements have been on raw sample values, not R128 values.
443         // So we have a final makeup gain to get us to +0 LU; the gain
444         // adjustments required should be relatively small, and also, the
445         // offset shouldn't change much (only if the type of audio changes
446         // significantly). Thus, we shoot for updating this value basically
447         // “whenever we process buffers”, since the R128 calculation isn't exactly
448         // something we get out per-sample.
449         //
450         // Note that there's a feedback loop here, so we choose a very slow filter
451         // (half-time of 30 seconds).
452         double target_loudness_factor, alpha;
453         double loudness_lu = r128.loudness_M() - ref_level_lufs;
454         double current_makeup_lu = to_db(final_makeup_gain);
455         target_loudness_factor = final_makeup_gain * from_db(-loudness_lu);
456
457         // If we're outside +/- 5 LU uncorrected, we don't count it as
458         // a normal signal (probably silence) and don't change the
459         // correction factor; just apply what we already have.
460         if (fabs(loudness_lu - current_makeup_lu) >= 5.0 || !final_makeup_gain_auto) {
461                 alpha = 0.0;
462         } else {
463                 // Formula adapted from
464                 // https://en.wikipedia.org/wiki/Low-pass_filter#Simple_infinite_impulse_response_filter.
465                 const double half_time_s = 30.0;
466                 const double fc_mul_2pi_delta_t = 1.0 / (half_time_s * OUTPUT_FREQUENCY);
467                 alpha = fc_mul_2pi_delta_t / (fc_mul_2pi_delta_t + 1.0);
468         }
469
470         {
471                 lock_guard<mutex> lock(compressor_mutex);
472                 double m = final_makeup_gain;
473                 for (size_t i = 0; i < samples_out.size(); i += 2) {
474                         samples_out[i + 0] *= m;
475                         samples_out[i + 1] *= m;
476                         m += (target_loudness_factor - m) * alpha;
477                 }
478                 final_makeup_gain = m;
479         }
480
481         update_meters(samples_out);
482
483         return samples_out;
484 }
485
486 void AudioMixer::update_meters(const vector<float> &samples)
487 {
488         // Upsample 4x to find interpolated peak.
489         peak_resampler.inp_data = const_cast<float *>(samples.data());
490         peak_resampler.inp_count = samples.size() / 2;
491
492         vector<float> interpolated_samples;
493         interpolated_samples.resize(samples.size());
494         {
495                 unique_lock<mutex> lock(audio_measure_mutex);
496
497                 while (peak_resampler.inp_count > 0) {  // About four iterations.
498                         peak_resampler.out_data = &interpolated_samples[0];
499                         peak_resampler.out_count = interpolated_samples.size() / 2;
500                         peak_resampler.process();
501                         size_t out_stereo_samples = interpolated_samples.size() / 2 - peak_resampler.out_count;
502                         peak = max<float>(peak, find_peak(interpolated_samples.data(), out_stereo_samples * 2));
503                         peak_resampler.out_data = nullptr;
504                 }
505         }
506
507         // Find R128 levels and L/R correlation.
508         vector<float> left, right;
509         deinterleave_samples(samples, &left, &right);
510         float *ptrs[] = { left.data(), right.data() };
511         {
512                 unique_lock<mutex> lock(audio_measure_mutex);
513                 r128.process(left.size(), ptrs);
514                 correlation.process_samples(samples);
515         }
516
517         send_audio_level_callback();
518 }
519
520 void AudioMixer::reset_meters()
521 {
522         unique_lock<mutex> lock(audio_measure_mutex);
523         peak_resampler.reset();
524         peak = 0.0f;
525         r128.reset();
526         r128.integr_start();
527         correlation.reset();
528 }
529
530 void AudioMixer::send_audio_level_callback()
531 {
532         if (audio_level_callback == nullptr) {
533                 return;
534         }
535
536         unique_lock<mutex> lock(audio_measure_mutex);
537         double loudness_s = r128.loudness_S();
538         double loudness_i = r128.integrated();
539         double loudness_range_low = r128.range_min();
540         double loudness_range_high = r128.range_max();
541
542         audio_level_callback(loudness_s, to_db(peak),
543                 loudness_i, loudness_range_low, loudness_range_high,
544                 gain_staging_db,
545                 to_db(final_makeup_gain),
546                 correlation.get_correlation());
547 }
548
549 map<DeviceSpec, DeviceInfo> AudioMixer::get_devices() const
550 {
551         lock_guard<timed_mutex> lock(audio_mutex);
552         return get_devices_mutex_held();
553 }
554
555 map<DeviceSpec, DeviceInfo> AudioMixer::get_devices_mutex_held() const
556 {
557         map<DeviceSpec, DeviceInfo> devices;
558         for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
559                 const DeviceSpec spec{ InputSourceType::CAPTURE_CARD, card_index };
560                 const AudioDevice *device = &video_cards[card_index];
561                 DeviceInfo info;
562                 info.name = device->name;
563                 info.num_channels = 8;  // FIXME: This is wrong for fake cards.
564                 devices.insert(make_pair(spec, info));
565         }
566         for (unsigned card_index = 0; card_index < available_alsa_cards.size(); ++card_index) {
567                 const DeviceSpec spec{ InputSourceType::ALSA_INPUT, card_index };
568                 const ALSAInput::Device &device = available_alsa_cards[card_index];
569                 DeviceInfo info;
570                 info.name = device.name + " (" + device.info + ")";
571                 info.num_channels = device.num_channels;
572                 devices.insert(make_pair(spec, info));
573         }
574         return devices;
575 }
576
577 void AudioMixer::set_name(DeviceSpec device_spec, const string &name)
578 {
579         AudioDevice *device = find_audio_device(device_spec);
580
581         lock_guard<timed_mutex> lock(audio_mutex);
582         device->name = name;
583 }
584
585 void AudioMixer::set_input_mapping(const InputMapping &new_input_mapping)
586 {
587         lock_guard<timed_mutex> lock(audio_mutex);
588
589         map<DeviceSpec, set<unsigned>> interesting_channels;
590         for (const InputMapping::Bus &bus : new_input_mapping.buses) {
591                 if (bus.device.type == InputSourceType::CAPTURE_CARD ||
592                     bus.device.type == InputSourceType::ALSA_INPUT) {
593                         for (unsigned channel = 0; channel < 2; ++channel) {
594                                 if (bus.source_channel[channel] != -1) {
595                                         interesting_channels[bus.device].insert(bus.source_channel[channel]);
596                                 }
597                         }
598                 }
599         }
600
601         // Reset resamplers for all cards that don't have the exact same state as before.
602         for (const auto &spec_and_info : get_devices_mutex_held()) {
603                 const DeviceSpec &device_spec = spec_and_info.first;
604                 AudioDevice *device = find_audio_device(device_spec);
605                 if (device->interesting_channels != interesting_channels[device_spec]) {
606                         device->interesting_channels = interesting_channels[device_spec];
607                         if (device_spec.type == InputSourceType::ALSA_INPUT) {
608                                 reset_alsa_mutex_held(device_spec);
609                         }
610                         reset_resampler_mutex_held(device_spec);
611                 }
612         }
613
614         input_mapping = new_input_mapping;
615 }
616
617 InputMapping AudioMixer::get_input_mapping() const
618 {
619         lock_guard<timed_mutex> lock(audio_mutex);
620         return input_mapping;
621 }