+// Get a pointer to the given channel from the given device.
+// The channel must be picked out earlier and resampled.
+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)
+{
+ static float zero = 0.0f;
+ if (source_channel == -1 || device_spec.type == InputSourceType::SILENCE) {
+ *srcptr = &zero;
+ *stride = 0;
+ return;
+ }
+ AudioDevice *device = find_audio_device(device_spec);
+ assert(device->interesting_channels.count(source_channel) != 0);
+ unsigned channel_index = 0;
+ for (int channel : device->interesting_channels) {
+ if (channel == source_channel) break;
+ ++channel_index;
+ }
+ assert(channel_index < device->interesting_channels.size());
+ const auto it = samples_card.find(device_spec);
+ assert(it != samples_card.end());
+ *srcptr = &(it->second)[channel_index];
+ *stride = device->interesting_channels.size();
+}
+
+// TODO: Can be SSSE3-optimized if need be.
+void AudioMixer::fill_audio_bus(const map<DeviceSpec, vector<float>> &samples_card, const InputMapping::Bus &bus, unsigned num_samples, float *output)
+{
+ if (bus.device.type == InputSourceType::SILENCE) {
+ memset(output, 0, num_samples * sizeof(*output));
+ } else {
+ assert(bus.device.type == InputSourceType::CAPTURE_CARD ||
+ bus.device.type == InputSourceType::ALSA_INPUT);
+ const float *lsrc, *rsrc;
+ unsigned lstride, rstride;
+ float *dptr = output;
+ find_sample_src_from_device(samples_card, bus.device, bus.source_channel[0], &lsrc, &lstride);
+ find_sample_src_from_device(samples_card, bus.device, bus.source_channel[1], &rsrc, &rstride);
+ for (unsigned i = 0; i < num_samples; ++i) {
+ *dptr++ = *lsrc;
+ *dptr++ = *rsrc;
+ lsrc += lstride;
+ rsrc += rstride;
+ }
+ }
+}
+
+vector<float> AudioMixer::get_output(double pts, unsigned num_samples, ResamplingQueue::RateAdjustmentPolicy rate_adjustment_policy)
+{
+ map<DeviceSpec, vector<float>> samples_card;
+ vector<float> samples_bus;
+
+ lock_guard<timed_mutex> lock(audio_mutex);
+
+ // Pick out all the interesting channels from all the cards.
+ // TODO: If the card has been hotswapped, the number of channels
+ // might have changed; if so, we need to do some sort of remapping
+ // to silence.
+ for (const auto &spec_and_info : get_devices_mutex_held()) {
+ const DeviceSpec &device_spec = spec_and_info.first;
+ AudioDevice *device = find_audio_device(device_spec);
+ if (!device->interesting_channels.empty()) {
+ samples_card[device_spec].resize(num_samples * device->interesting_channels.size());
+ device->resampling_queue->get_output_samples(