X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=audio_mixer.cpp;h=b61a3c300009eda219179863617914325517ba1e;hb=95c6bc9d8e340b02112f713389390102d547cc4c;hp=7a36b0de255e53daeeb96e0fcc2de5521b411364;hpb=a803d2e93f69b6aa968e6b0f4a8498d0ef1b3b70;p=nageru diff --git a/audio_mixer.cpp b/audio_mixer.cpp index 7a36b0d..b61a3c3 100644 --- a/audio_mixer.cpp +++ b/audio_mixer.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "db.h" @@ -15,33 +16,63 @@ using namespace std; namespace { -// TODO: If these prove to be a bottleneck, they can be SSSE3-optimized. +// TODO: If these prove to be a bottleneck, they can be SSSE3-optimized +// (usually including multiple channels at a time). -void convert_fixed24_to_fp32(float *dst, size_t out_channels, const uint8_t *src, size_t in_channels, size_t num_samples) +void convert_fixed16_to_fp32(float *dst, size_t out_channel, size_t out_num_channels, + const uint8_t *src, size_t in_channel, size_t in_num_channels, + size_t num_samples) { - assert(in_channels >= out_channels); + assert(in_channel < in_num_channels); + assert(out_channel < out_num_channels); + src += in_channel * 2; + dst += out_channel; + for (size_t i = 0; i < num_samples; ++i) { - for (size_t j = 0; j < out_channels; ++j) { - uint32_t s1 = *src++; - uint32_t s2 = *src++; - uint32_t s3 = *src++; - uint32_t s = s1 | (s1 << 8) | (s2 << 16) | (s3 << 24); - dst[i * out_channels + j] = int(s) * (1.0f / 2147483648.0f); - } - src += 3 * (in_channels - out_channels); + int16_t s = le16toh(*(int16_t *)src); + *dst = s * (1.0f / 32768.0f); + + src += 2 * in_num_channels; + dst += out_num_channels; } } -void convert_fixed32_to_fp32(float *dst, size_t out_channels, const uint8_t *src, size_t in_channels, size_t num_samples) +void convert_fixed24_to_fp32(float *dst, size_t out_channel, size_t out_num_channels, + const uint8_t *src, size_t in_channel, size_t in_num_channels, + size_t num_samples) { - assert(in_channels >= out_channels); + assert(in_channel < in_num_channels); + assert(out_channel < out_num_channels); + src += in_channel * 3; + dst += out_channel; + for (size_t i = 0; i < num_samples; ++i) { - for (size_t j = 0; j < out_channels; ++j) { - int32_t s = le32toh(*(int32_t *)src); - dst[i * out_channels + j] = s * (1.0f / 2147483648.0f); - src += 4; - } - src += 4 * (in_channels - out_channels); + uint32_t s1 = src[0]; + uint32_t s2 = src[1]; + uint32_t s3 = src[2]; + uint32_t s = s1 | (s1 << 8) | (s2 << 16) | (s3 << 24); + *dst = int(s) * (1.0f / 2147483648.0f); + + src += 3 * in_num_channels; + dst += out_num_channels; + } +} + +void convert_fixed32_to_fp32(float *dst, size_t out_channel, size_t out_num_channels, + const uint8_t *src, size_t in_channel, size_t in_num_channels, + size_t num_samples) +{ + assert(in_channel < in_num_channels); + assert(out_channel < out_num_channels); + src += in_channel * 4; + dst += out_channel; + + for (size_t i = 0; i < num_samples; ++i) { + int32_t s = le32toh(*(int32_t *)src); + *dst = s * (1.0f / 2147483648.0f); + + src += 4 * in_num_channels; + dst += out_num_channels; } } @@ -63,123 +94,172 @@ AudioMixer::AudioMixer(unsigned num_cards) set_final_makeup_gain_auto(global_flags.final_makeup_gain_auto); // Generate a very simple, default input mapping. - InputMapping::Input input; + InputMapping::Bus input; input.name = "Main"; - input.input_source_type = InputSourceType::CAPTURE_CARD; - input.input_source_index = 0; + input.device.type = InputSourceType::CAPTURE_CARD; + input.device.index = 0; input.source_channel[0] = 0; input.source_channel[1] = 1; InputMapping new_input_mapping; - new_input_mapping.inputs.push_back(input); + new_input_mapping.buses.push_back(input); set_input_mapping(new_input_mapping); } -void AudioMixer::reset_card(unsigned card_index) +void AudioMixer::reset_device(DeviceSpec device_spec) { lock_guard lock(audio_mutex); - reset_card_mutex_held(card_index); + reset_device_mutex_held(device_spec); } -void AudioMixer::reset_card_mutex_held(unsigned card_index) +void AudioMixer::reset_device_mutex_held(DeviceSpec device_spec) { - CaptureCard *card = &cards[card_index]; - if (card->interesting_channels.empty()) { - card->resampling_queue.reset(); + AudioDevice *device = find_audio_device(device_spec); + if (device->interesting_channels.empty()) { + device->resampling_queue.reset(); } else { - card->resampling_queue.reset(new ResamplingQueue(card_index, OUTPUT_FREQUENCY, OUTPUT_FREQUENCY, card->interesting_channels.size())); + // TODO: ResamplingQueue should probably take the full device spec. + // (It's only used for console output, though.) + device->resampling_queue.reset(new ResamplingQueue(device_spec.index, device->capture_frequency, OUTPUT_FREQUENCY, device->interesting_channels.size())); } - card->next_local_pts = 0; + device->next_local_pts = 0; } -void AudioMixer::add_audio(unsigned card_index, const uint8_t *data, unsigned num_samples, AudioFormat audio_format, int64_t frame_length) +void AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, AudioFormat audio_format, int64_t frame_length) { - lock_guard lock(audio_mutex); - CaptureCard *card = &cards[card_index]; + AudioDevice *device = find_audio_device(device_spec); - if (card->resampling_queue == nullptr) { - // No inputs use this card; throw it away. + lock_guard lock(audio_mutex); + if (device->resampling_queue == nullptr) { + // No buses use this device; throw it away. return; } - unsigned num_channels = card->interesting_channels.size(); + unsigned num_channels = device->interesting_channels.size(); assert(num_channels > 0); - // Convert the audio to stereo fp32. - // FIXME: Pick out the right channels; this takes the first ones. + // Convert the audio to fp32. vector audio; audio.resize(num_samples * num_channels); - switch (audio_format.bits_per_sample) { - case 0: - assert(num_samples == 0); - break; - case 24: - convert_fixed24_to_fp32(&audio[0], num_channels, data, audio_format.num_channels, num_samples); - break; - case 32: - convert_fixed32_to_fp32(&audio[0], num_channels, data, audio_format.num_channels, num_samples); - break; - default: - fprintf(stderr, "Cannot handle audio with %u bits per sample\n", audio_format.bits_per_sample); - assert(false); + unsigned channel_index = 0; + for (auto channel_it = device->interesting_channels.cbegin(); channel_it != device->interesting_channels.end(); ++channel_it, ++channel_index) { + switch (audio_format.bits_per_sample) { + case 0: + assert(num_samples == 0); + break; + case 16: + convert_fixed16_to_fp32(&audio[0], channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples); + break; + case 24: + convert_fixed24_to_fp32(&audio[0], channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples); + break; + case 32: + convert_fixed32_to_fp32(&audio[0], channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples); + break; + default: + fprintf(stderr, "Cannot handle audio with %u bits per sample\n", audio_format.bits_per_sample); + assert(false); + } } // Now add it. - int64_t local_pts = card->next_local_pts; - card->resampling_queue->add_input_samples(local_pts / double(TIMEBASE), audio.data(), num_samples); - card->next_local_pts = local_pts + frame_length; + int64_t local_pts = device->next_local_pts; + device->resampling_queue->add_input_samples(local_pts / double(TIMEBASE), audio.data(), num_samples); + device->next_local_pts = local_pts + frame_length; } -void AudioMixer::add_silence(unsigned card_index, unsigned samples_per_frame, unsigned num_frames, int64_t frame_length) +void AudioMixer::add_silence(DeviceSpec device_spec, unsigned samples_per_frame, unsigned num_frames, int64_t frame_length) { - CaptureCard *card = &cards[card_index]; - lock_guard lock(audio_mutex); + AudioDevice *device = find_audio_device(device_spec); - if (card->resampling_queue == nullptr) { - // No inputs use this card; throw it away. + lock_guard lock(audio_mutex); + if (device->resampling_queue == nullptr) { + // No buses use this device; throw it away. return; } - unsigned num_channels = card->interesting_channels.size(); + unsigned num_channels = device->interesting_channels.size(); assert(num_channels > 0); vector silence(samples_per_frame * num_channels, 0.0f); for (unsigned i = 0; i < num_frames; ++i) { - card->resampling_queue->add_input_samples(card->next_local_pts / double(TIMEBASE), silence.data(), samples_per_frame); + device->resampling_queue->add_input_samples(device->next_local_pts / double(TIMEBASE), silence.data(), samples_per_frame); // Note that if the format changed in the meantime, we have // no way of detecting that; we just have to assume the frame length // is always the same. - card->next_local_pts += frame_length; + device->next_local_pts += frame_length; + } +} + +AudioMixer::AudioDevice *AudioMixer::find_audio_device(DeviceSpec device) +{ + switch (device.type) { + case InputSourceType::CAPTURE_CARD: + return &cards[device.index]; + break; + case InputSourceType::SILENCE: + default: + assert(false); } + return nullptr; } -void AudioMixer::find_sample_src_from_capture_card(const vector *samples_card, unsigned card_index, int source_channel, const float **srcptr, unsigned *stride) +void AudioMixer::find_sample_src_from_device(const vector *samples_card, DeviceSpec device_spec, int source_channel, const float **srcptr, unsigned *stride) { static float zero = 0.0f; - if (source_channel == -1) { + if (source_channel == -1 || device_spec.type == InputSourceType::SILENCE) { *srcptr = &zero; *stride = 0; return; } - // FIXME: map back through the interesting_channels squeeze map instead of using source_channel - // directly, which will be wrong (and might even overrun). - *srcptr = &samples_card[card_index][source_channel]; - *stride = cards[card_index].interesting_channels.size(); + AudioDevice *device = find_audio_device(device_spec); + unsigned channel_index = 0; + for (int channel : device->interesting_channels) { + if (channel == source_channel) break; + ++channel_index; + } + assert(channel_index < device->interesting_channels.size()); + *srcptr = &samples_card[device_spec.index][channel_index]; + *stride = device->interesting_channels.size(); +} + +// TODO: Can be SSSE3-optimized if need be. +void AudioMixer::fill_audio_bus(const vector *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); + 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 AudioMixer::get_output(double pts, unsigned num_samples, ResamplingQueue::RateAdjustmentPolicy rate_adjustment_policy) { - vector samples_card[MAX_CARDS]; + vector samples_card[MAX_CARDS]; // TODO: Needs room for other kinds of capture cards. vector samples_bus; lock_guard 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 (unsigned card_index = 0; card_index < num_cards; ++card_index) { - CaptureCard *card = &cards[card_index]; - if (!card->interesting_channels.empty()) { - samples_card[card_index].resize(num_samples * card->interesting_channels.size()); - card->resampling_queue->get_output_samples( + AudioDevice *device = &cards[card_index]; + if (!device->interesting_channels.empty()) { + samples_card[card_index].resize(num_samples * device->interesting_channels.size()); + device->resampling_queue->get_output_samples( pts, &samples_card[card_index][0], num_samples, @@ -191,28 +271,11 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin vector samples_out; samples_out.resize(num_samples * 2); samples_bus.resize(num_samples * 2); - for (unsigned input_index = 0; input_index < input_mapping.inputs.size(); ++input_index) { - const InputMapping::Input &input = input_mapping.inputs[input_index]; - if (input.input_source_type == InputSourceType::SILENCE) { - memset(&samples_bus[0], 0, samples_bus.size() * sizeof(samples_bus[0])); - } else { - // TODO: Move this into its own function. Can be SSSE3-optimized if need be. - assert(input.input_source_type == InputSourceType::CAPTURE_CARD); - const float *lsrc, *rsrc; - unsigned lstride, rstride; - float *dptr = &samples_bus[0]; - find_sample_src_from_capture_card(samples_card, input.input_source_index, input.source_channel[0], &lsrc, &lstride); - find_sample_src_from_capture_card(samples_card, input.input_source_index, input.source_channel[1], &rsrc, &rstride); - for (unsigned i = 0; i < num_samples; ++i) { - *dptr++ = *lsrc; - *dptr++ = *rsrc; - lsrc += lstride; - rsrc += rstride; - } - } + for (unsigned bus_index = 0; bus_index < input_mapping.buses.size(); ++bus_index) { + fill_audio_bus(samples_card, input_mapping.buses[bus_index], num_samples, &samples_bus[0]); - float volume = from_db(fader_volume_db[input_index]); - if (input_index == 0) { + float volume = from_db(fader_volume_db[bus_index]); + if (bus_index == 0) { for (unsigned i = 0; i < num_samples * 2; ++i) { samples_out[i] = samples_bus[i] * volume; } @@ -336,34 +399,39 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin return samples_out; } -vector AudioMixer::get_names() const +map AudioMixer::get_devices() const { lock_guard lock(audio_mutex); - vector names; + map devices; for (unsigned card_index = 0; card_index < num_cards; ++card_index) { - const CaptureCard *card = &cards[card_index]; - names.push_back(card->name); + const DeviceSpec spec{ InputSourceType::CAPTURE_CARD, card_index }; + const AudioDevice *device = &cards[card_index]; + DeviceInfo info; + info.name = device->name; + info.num_channels = 8; // FIXME: This is wrong for fake cards. + devices.insert(make_pair(spec, info)); } - return names; + return devices; } -void AudioMixer::set_name(unsigned card_index, const string &name) +void AudioMixer::set_name(DeviceSpec device_spec, const string &name) { + AudioDevice *device = find_audio_device(device_spec); + lock_guard lock(audio_mutex); - CaptureCard *card = &cards[card_index]; - card->name = name; + device->name = name; } void AudioMixer::set_input_mapping(const InputMapping &new_input_mapping) { lock_guard lock(audio_mutex); - map> interesting_channels; - for (const InputMapping::Input &input : new_input_mapping.inputs) { - if (input.input_source_type == InputSourceType::CAPTURE_CARD) { + map> interesting_channels; + for (const InputMapping::Bus &bus : new_input_mapping.buses) { + if (bus.device.type == InputSourceType::CAPTURE_CARD) { for (unsigned channel = 0; channel < 2; ++channel) { - if (input.source_channel[channel] != -1) { - interesting_channels[input.input_source_index].insert(input.source_channel[channel]); + if (bus.source_channel[channel] != -1) { + interesting_channels[bus.device].insert(bus.source_channel[channel]); } } } @@ -371,10 +439,11 @@ void AudioMixer::set_input_mapping(const InputMapping &new_input_mapping) // Reset resamplers for all cards that don't have the exact same state as before. for (unsigned card_index = 0; card_index < num_cards; ++card_index) { - CaptureCard *card = &cards[card_index]; - if (card->interesting_channels != interesting_channels[card_index]) { - card->interesting_channels = interesting_channels[card_index]; - reset_card_mutex_held(card_index); + DeviceSpec device_spec{InputSourceType::CAPTURE_CARD, card_index}; + AudioDevice *device = &cards[card_index]; + if (device->interesting_channels != interesting_channels[device_spec]) { + device->interesting_channels = interesting_channels[device_spec]; + reset_device_mutex_held(DeviceSpec{InputSourceType::CAPTURE_CARD, card_index}); } }