]> git.sesse.net Git - nageru/blobdiff - nageru/audio_mixer.cpp
Make number of cards flexible at runtime.
[nageru] / nageru / audio_mixer.cpp
index 9e7dd59a0dbc64ab6398e30824c8dae6d676b912..402131f558aa630de5eece413a2c0727dcd7df08 100644 (file)
 #include <limits>
 #include <utility>
 
-#include "db.h"
+#include "decibel.h"
 #include "flags.h"
-#include "metrics.h"
+#include "shared/metrics.h"
 #include "state.pb.h"
-#include "timebase.h"
+#include "shared/timebase.h"
 
 using namespace bmusb;
 using namespace std;
@@ -52,6 +52,26 @@ void convert_fixed16_to_fp32(float *dst, size_t out_channel, size_t out_num_chan
        }
 }
 
+void convert_fixed16_to_fixed32(int32_t *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 * 2;
+       dst += out_channel;
+
+       for (size_t i = 0; i < num_samples; ++i) {
+               uint32_t s = uint32_t(uint16_t(le16toh(*(int16_t *)src))) << 16;
+
+               // Keep the sign bit in place, repeat the other 15 bits as far as they go.
+               *dst = s | ((s & 0x7fffffff) >> 15) | ((s & 0x7fffffff) >> 30);
+
+               src += 2 * in_num_channels;
+               dst += out_num_channels;
+       }
+}
+
 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)
@@ -65,8 +85,31 @@ void convert_fixed24_to_fp32(float *dst, size_t out_channel, size_t out_num_chan
                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);
+               uint32_t s = (s1 << 8) | (s2 << 16) | (s3 << 24);  // Note: The bottom eight bits are zero; s3 includes the sign bit.
+               *dst = int(s) * (1.0f / (256.0f * 8388608.0f));  // 256 for signed down-shift by 8, then 2^23 for the actual conversion.
+
+               src += 3 * in_num_channels;
+               dst += out_num_channels;
+       }
+}
+
+void convert_fixed24_to_fixed32(int32_t *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 * 3;
+       dst += out_channel;
+
+       for (size_t i = 0; i < num_samples; ++i) {
+               uint32_t s1 = src[0];
+               uint32_t s2 = src[1];
+               uint32_t s3 = src[2];
+               uint32_t s = (s1 << 8) | (s2 << 16) | (s3 << 24);
+
+               // Keep the sign bit in place, repeat the other 23 bits as far as they go.
+               *dst = s | ((s & 0x7fffffff) >> 23);
 
                src += 3 * in_num_channels;
                dst += out_num_channels;
@@ -91,6 +134,25 @@ void convert_fixed32_to_fp32(float *dst, size_t out_channel, size_t out_num_chan
        }
 }
 
+// Basically just a reinterleave.
+void convert_fixed32_to_fixed32(int32_t *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;
+
+               src += 4 * in_num_channels;
+               dst += out_num_channels;
+       }
+}
+
 float find_peak_plain(const float *samples, size_t num_samples) __attribute__((unused));
 
 float find_peak_plain(const float *samples, size_t num_samples)
@@ -167,11 +229,8 @@ void deinterleave_samples(const vector<float> &in, vector<float> *out_l, vector<
 
 }  // namespace
 
-AudioMixer::AudioMixer(unsigned num_capture_cards, unsigned num_ffmpeg_inputs)
-       : num_capture_cards(num_capture_cards),
-         num_ffmpeg_inputs(num_ffmpeg_inputs),
-         ffmpeg_inputs(new AudioDevice[num_ffmpeg_inputs]),
-         limiter(OUTPUT_FREQUENCY),
+AudioMixer::AudioMixer()
+       : limiter(OUTPUT_FREQUENCY),
          correlation(OUTPUT_FREQUENCY)
 {
        for (unsigned bus_index = 0; bus_index < MAX_BUSES; ++bus_index) {
@@ -206,7 +265,7 @@ AudioMixer::AudioMixer(unsigned num_capture_cards, unsigned num_ffmpeg_inputs)
                                                  &new_input_mapping)) {
                        fprintf(stderr, "Failed to load input mapping from '%s', exiting.\n",
                                global_flags.input_mapping_filename.c_str());
-                       exit(1);
+                       abort();
                }
                set_input_mapping(new_input_mapping);
        } else {
@@ -239,12 +298,12 @@ void AudioMixer::reset_resampler_mutex_held(DeviceSpec device_spec)
                device->resampling_queue.reset();
        } else {
                device->resampling_queue.reset(new ResamplingQueue(
-                       device_spec, device->capture_frequency, OUTPUT_FREQUENCY, device->interesting_channels.size(),
+                       spec_to_string(device_spec), device->capture_frequency, OUTPUT_FREQUENCY, device->interesting_channels.size(),
                        global_flags.audio_queue_length_ms * 0.001));
        }
 }
 
-bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, AudioFormat audio_format, int64_t frame_length, steady_clock::time_point frame_time)
+bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, AudioFormat audio_format, steady_clock::time_point frame_time)
 {
        AudioDevice *device = find_audio_device(device_spec);
 
@@ -258,7 +317,12 @@ bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned
        }
 
        unsigned num_channels = device->interesting_channels.size();
-       assert(num_channels > 0);
+       if (num_channels == 0) {
+               // No buses use this device; throw it away. (Normally, we should not
+               // be here, but probably, we are in the process of changing a mapping,
+               // and the queue just isn't gone yet. In any case, returning is harmless.)
+               return true;
+       }
 
        // Convert the audio to fp32.
        unique_ptr<float[]> audio(new float[num_samples * num_channels]);
@@ -294,7 +358,39 @@ bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned
        return true;
 }
 
-bool AudioMixer::add_silence(DeviceSpec device_spec, unsigned samples_per_frame, unsigned num_frames, int64_t frame_length)
+vector<int32_t> convert_audio_to_fixed32(const uint8_t *data, unsigned num_samples, bmusb::AudioFormat audio_format, unsigned num_channels)
+{
+       vector<int32_t> audio;
+
+       if (num_channels > audio_format.num_channels) {
+               audio.resize(num_samples * num_channels, 0);
+       } else {
+               audio.resize(num_samples * num_channels);
+       }
+       for (unsigned channel_index = 0; channel_index < num_channels && channel_index < audio_format.num_channels; ++channel_index) {
+               switch (audio_format.bits_per_sample) {
+               case 0:
+                       assert(num_samples == 0);
+                       break;
+               case 16:
+                       convert_fixed16_to_fixed32(&audio[0], channel_index, num_channels, data, channel_index, audio_format.num_channels, num_samples);
+                       break;
+               case 24:
+                       convert_fixed24_to_fixed32(&audio[0], channel_index, num_channels, data, channel_index, audio_format.num_channels, num_samples);
+                       break;
+               case 32:
+                       convert_fixed32_to_fixed32(&audio[0], channel_index, num_channels, data, channel_index, 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);
+               }
+       }
+
+       return audio;
+}
+
+bool AudioMixer::add_silence(DeviceSpec device_spec, unsigned samples_per_frame, unsigned num_frames)
 {
        AudioDevice *device = find_audio_device(device_spec);
 
@@ -392,8 +488,6 @@ AudioMixer::AudioDevice *AudioMixer::find_audio_device(DeviceSpec device)
                return &video_cards[device.index];
        case InputSourceType::ALSA_INPUT:
                return &alsa_inputs[device.index];
-       case InputSourceType::FFMPEG_VIDEO_INPUT:
-               return &ffmpeg_inputs[device.index];
        case InputSourceType::SILENCE:
        default:
                assert(false);
@@ -432,8 +526,7 @@ void AudioMixer::fill_audio_bus(const map<DeviceSpec, vector<float>> &samples_ca
                memset(output, 0, num_samples * 2 * sizeof(*output));
        } else {
                assert(bus.device.type == InputSourceType::CAPTURE_CARD ||
-                      bus.device.type == InputSourceType::ALSA_INPUT ||
-                      bus.device.type == InputSourceType::FFMPEG_VIDEO_INPUT);
+                      bus.device.type == InputSourceType::ALSA_INPUT);
                const float *lsrc, *rsrc;
                unsigned lstride, rstride;
                float *dptr = output;
@@ -499,12 +592,6 @@ vector<DeviceSpec> AudioMixer::get_active_devices() const
                        ret.push_back(device_spec);
                }
        }
-       for (unsigned card_index = 0; card_index < num_ffmpeg_inputs; ++card_index) {
-               const DeviceSpec device_spec{InputSourceType::FFMPEG_VIDEO_INPUT, card_index};
-               if (!find_audio_device(device_spec)->interesting_channels.empty()) {
-                       ret.push_back(device_spec);
-               }
-       }
        return ret;
 }
 
@@ -928,12 +1015,12 @@ map<DeviceSpec, DeviceInfo> AudioMixer::get_devices()
        lock_guard<timed_mutex> lock(audio_mutex);
 
        map<DeviceSpec, DeviceInfo> devices;
-       for (unsigned card_index = 0; card_index < num_capture_cards; ++card_index) {
+       for (unsigned card_index = 0; card_index < MAX_VIDEO_CARDS; ++card_index) {
                const DeviceSpec spec{ InputSourceType::CAPTURE_CARD, card_index };
                const AudioDevice *device = &video_cards[card_index];
                DeviceInfo info;
                info.display_name = device->display_name;
-               info.num_channels = 8;
+               info.num_channels = device->num_channels;
                devices.insert(make_pair(spec, info));
        }
        vector<ALSAPool::Device> available_alsa_devices = alsa_pool.get_devices();
@@ -948,23 +1035,27 @@ map<DeviceSpec, DeviceInfo> AudioMixer::get_devices()
                info.alsa_address = device.address;
                devices.insert(make_pair(spec, info));
        }
-       for (unsigned card_index = 0; card_index < num_ffmpeg_inputs; ++card_index) {
-               const DeviceSpec spec{ InputSourceType::FFMPEG_VIDEO_INPUT, card_index };
-               const AudioDevice *device = &ffmpeg_inputs[card_index];
-               DeviceInfo info;
-               info.display_name = device->display_name;
-               info.num_channels = 2;
-               devices.insert(make_pair(spec, info));
-       }
        return devices;
 }
 
-void AudioMixer::set_display_name(DeviceSpec device_spec, const string &name)
+void AudioMixer::set_device_parameters(DeviceSpec device_spec, const std::string &display_name, CardType card_type, unsigned num_channels, bool active)
+{
+       AudioDevice *device = find_audio_device(device_spec);
+
+       lock_guard<timed_mutex> lock(audio_mutex);
+       if (active || device->display_name.empty()) {
+               device->display_name = display_name;
+       }
+       device->card_type = card_type;
+       device->active = active;
+}
+
+bool AudioMixer::get_active(DeviceSpec device_spec)
 {
        AudioDevice *device = find_audio_device(device_spec);
 
        lock_guard<timed_mutex> lock(audio_mutex);
-       device->display_name = name;
+       return device->active;
 }
 
 void AudioMixer::serialize_device(DeviceSpec device_spec, DeviceSpecProto *device_spec_proto)
@@ -982,25 +1073,16 @@ void AudioMixer::serialize_device(DeviceSpec device_spec, DeviceSpecProto *devic
                case InputSourceType::ALSA_INPUT:
                        alsa_pool.serialize_device(device_spec.index, device_spec_proto);
                        break;
-               case InputSourceType::FFMPEG_VIDEO_INPUT:
-                       device_spec_proto->set_type(DeviceSpecProto::FFMPEG_VIDEO_INPUT);
-                       device_spec_proto->set_index(device_spec.index);
-                       device_spec_proto->set_display_name(ffmpeg_inputs[device_spec.index].display_name);
-                       break;
        }
 }
 
 void AudioMixer::set_simple_input(unsigned card_index)
 {
-       assert(card_index < num_capture_cards + num_ffmpeg_inputs);
+       assert(card_index < MAX_VIDEO_CARDS);
        InputMapping new_input_mapping;
        InputMapping::Bus input;
        input.name = "Main";
-       if (card_index >= num_capture_cards) {
-               input.device = DeviceSpec{InputSourceType::FFMPEG_VIDEO_INPUT, card_index - num_capture_cards};
-       } else {
-               input.device = DeviceSpec{InputSourceType::CAPTURE_CARD, card_index};
-       }
+       input.device = DeviceSpec{InputSourceType::CAPTURE_CARD, card_index};
        input.source_channel[0] = 0;
        input.source_channel[1] = 1;
 
@@ -1020,11 +1102,6 @@ unsigned AudioMixer::get_simple_input() const
            input_mapping.buses[0].source_channel[0] == 0 &&
            input_mapping.buses[0].source_channel[1] == 1) {
                return input_mapping.buses[0].device.index;
-       } else if (input_mapping.buses.size() == 1 &&
-                  input_mapping.buses[0].device.type == InputSourceType::FFMPEG_VIDEO_INPUT &&
-                  input_mapping.buses[0].source_channel[0] == 0 &&
-                  input_mapping.buses[0].source_channel[1] == 1) {
-               return input_mapping.buses[0].device.index + num_capture_cards;
        } else {
                return numeric_limits<unsigned>::max();
        }
@@ -1048,8 +1125,7 @@ void AudioMixer::set_input_mapping_lock_held(const InputMapping &new_input_mappi
        map<DeviceSpec, set<unsigned>> interesting_channels;
        for (const InputMapping::Bus &bus : new_input_mapping.buses) {
                if (bus.device.type == InputSourceType::CAPTURE_CARD ||
-                   bus.device.type == InputSourceType::ALSA_INPUT ||
-                   bus.device.type == InputSourceType::FFMPEG_VIDEO_INPUT) {
+                   bus.device.type == InputSourceType::ALSA_INPUT) {
                        for (unsigned channel = 0; channel < 2; ++channel) {
                                if (bus.source_channel[channel] != -1) {
                                        interesting_channels[bus.device].insert(bus.source_channel[channel]);
@@ -1093,11 +1169,14 @@ void AudioMixer::set_input_mapping_lock_held(const InputMapping &new_input_mappi
                if (bus.device.type == InputSourceType::SILENCE) {
                        metrics.labels.emplace_back("source_type", "silence");
                } else if (bus.device.type == InputSourceType::CAPTURE_CARD) {
-                       metrics.labels.emplace_back("source_type", "capture_card");
+                       AudioDevice *device = find_audio_device(bus.device);
+                       if (device->card_type == CardType::FFMPEG_INPUT) {
+                               metrics.labels.emplace_back("source_type", "ffmpeg_video_input");
+                       } else {
+                               metrics.labels.emplace_back("source_type", "capture_card");
+                       }
                } else if (bus.device.type == InputSourceType::ALSA_INPUT) {
                        metrics.labels.emplace_back("source_type", "alsa_input");
-               } else if (bus.device.type == InputSourceType::FFMPEG_VIDEO_INPUT) {
-                       metrics.labels.emplace_back("source_type", "ffmpeg_video_input");
                } else {
                        assert(false);
                }
@@ -1141,14 +1220,6 @@ void AudioMixer::set_input_mapping_lock_held(const InputMapping &new_input_mappi
                        reset_resampler_mutex_held(device_spec);
                }
        }
-       for (unsigned card_index = 0; card_index < num_ffmpeg_inputs; ++card_index) {
-               const DeviceSpec device_spec{InputSourceType::FFMPEG_VIDEO_INPUT, card_index};
-               AudioDevice *device = find_audio_device(device_spec);
-               if (device->interesting_channels != interesting_channels[device_spec]) {
-                       device->interesting_channels = interesting_channels[device_spec];
-                       reset_resampler_mutex_held(device_spec);
-               }
-       }
 
        input_mapping = new_input_mapping;
 }
@@ -1186,10 +1257,37 @@ bool AudioMixer::is_mono(unsigned bus_index)
                return true;
        } else {
                assert(bus.device.type == InputSourceType::CAPTURE_CARD ||
-                      bus.device.type == InputSourceType::ALSA_INPUT ||
-                      bus.device.type == InputSourceType::FFMPEG_VIDEO_INPUT);
+                      bus.device.type == InputSourceType::ALSA_INPUT);
                return bus.source_channel[0] == bus.source_channel[1];
        }
 }
 
+// This is perhaps not the most user-friendly output, but it's at least better
+// than the raw index. It would be nice to have it identical to
+// Mixer::description_for_card for capture cards, though.
+string AudioMixer::spec_to_string(DeviceSpec device_spec) const
+{
+       char buf[256];
+
+       switch (device_spec.type) {
+               case InputSourceType::SILENCE:
+                       return "<silence>";
+               case InputSourceType::CAPTURE_CARD: {
+                       const AudioDevice *device = find_audio_device(device_spec);
+                       if (device->card_type == CardType::FFMPEG_INPUT) {
+                               snprintf(buf, sizeof(buf), "Virtual capture card %u (%s)", device_spec.index, device->display_name.c_str());
+                       } else {
+                               snprintf(buf, sizeof(buf), "Capture card %u (%s)", device_spec.index, device->display_name.c_str());
+                       }
+                       return buf;
+               }
+               case InputSourceType::ALSA_INPUT:
+                       snprintf(buf, sizeof(buf), "ALSA input %u", device_spec.index);
+                       return buf;
+               default:
+                       assert(false);
+       }
+}
+
+
 AudioMixer *global_audio_mixer = nullptr;