X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=audio_mixer.h;h=31611af095151b4f65b2cc40f7836ed39b06887d;hb=4c879d05be9112b33245de1a694713dc59ced565;hp=6f7dbe5d0b45850fed81f743b6674eeb176c6c7b;hpb=4bbcd111d04f36a42cf3d40f18fcee5a91c6322a;p=nageru diff --git a/audio_mixer.h b/audio_mixer.h index 6f7dbe5..31611af 100644 --- a/audio_mixer.h +++ b/audio_mixer.h @@ -7,20 +7,23 @@ // all together into one final audio signal. // // All operations on AudioMixer (except destruction) are thread-safe. -// -// TODO: There might be more audio stuff that should be moved here -// from Mixer. #include #include #include +#include #include #include +#include #include +#include +#include "alsa_input.h" #include "bmusb/bmusb.h" +#include "correlation_measurer.h" #include "db.h" #include "defs.h" +#include "ebu_r128_proc.h" #include "filter.h" #include "resampling_queue.h" #include "stereocompressor.h" @@ -29,35 +32,66 @@ namespace bmusb { struct AudioFormat; } // namespace bmusb -enum class InputSourceType { SILENCE, CAPTURE_CARD }; +enum class InputSourceType { SILENCE, CAPTURE_CARD, ALSA_INPUT }; +struct DeviceSpec { + InputSourceType type; + unsigned index; + + bool operator== (const DeviceSpec &other) const { + return type == other.type && index == other.index; + } + + bool operator< (const DeviceSpec &other) const { + if (type != other.type) + return type < other.type; + return index < other.index; + } +}; +struct DeviceInfo { + std::string name; + unsigned num_channels; +}; + +static inline uint64_t DeviceSpec_to_key(const DeviceSpec &device_spec) +{ + return (uint64_t(device_spec.type) << 32) | device_spec.index; +} + +static inline DeviceSpec key_to_DeviceSpec(uint64_t key) +{ + return DeviceSpec{ InputSourceType(key >> 32), unsigned(key & 0xffffffff) }; +} struct InputMapping { - struct Input { + struct Bus { std::string name; - InputSourceType input_source_type; - unsigned input_source_index; + DeviceSpec device; int source_channel[2] { -1, -1 }; // Left and right. -1 = none. }; - std::vector inputs; + std::vector buses; }; class AudioMixer { public: AudioMixer(unsigned num_cards); - void reset_card(unsigned card_index); + ~AudioMixer(); + void reset_resampler(DeviceSpec device_spec); + void reset_meters(); + + // Add audio (or silence) to the given device's queue. Can return false if + // the lock wasn't successfully taken; if so, you should simply try again. + // (This is to avoid a deadlock where a card hangs on the mutex in add_audio() + // while we are trying to shut it down from another thread that also holds + // the mutex.) frame_length is in TIMEBASE units. + bool add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, bmusb::AudioFormat audio_format, int64_t frame_length); + bool add_silence(DeviceSpec device_spec, unsigned samples_per_frame, unsigned num_frames, int64_t frame_length); - // frame_length is in TIMEBASE units. - void add_audio(unsigned card_index, const uint8_t *data, unsigned num_samples, bmusb::AudioFormat audio_format, int64_t frame_length); - void add_silence(unsigned card_index, unsigned samples_per_frame, unsigned num_frames, int64_t frame_length); std::vector get_output(double pts, unsigned num_samples, ResamplingQueue::RateAdjustmentPolicy rate_adjustment_policy); - // See comments inside get_output(). - void set_current_loudness(double level_lufs) { loudness_lufs = level_lufs; } - - void set_fader_volume(unsigned card_index, float level_db) { cards[card_index].fader_volume_db = level_db; } - std::vector get_names() const; - void set_name(unsigned card_index, const std::string &name); + void set_fader_volume(unsigned bus_index, float level_db) { fader_volume_db[bus_index] = level_db; } + std::map get_devices() const; + void set_name(DeviceSpec device_spec, const std::string &name); void set_input_mapping(const InputMapping &input_mapping); InputMapping get_input_mapping() const; @@ -167,19 +201,47 @@ public: return final_makeup_gain_auto; } -private: - unsigned num_cards; - - struct CaptureCard { - std::atomic fader_volume_db{0.0f}; + typedef std::function bus_level_lufs, + float global_level_lufs, float range_low_lufs, float range_high_lufs, + float gain_staging_db, float final_makeup_gain_db, + float correlation)> audio_level_callback_t; + void set_audio_level_callback(audio_level_callback_t callback) + { + audio_level_callback = callback; + } - // Everything below audio_mutex is protected by it. - mutable std::mutex audio_mutex; +private: + struct AudioDevice { std::unique_ptr resampling_queue; int64_t next_local_pts = 0; std::string name; + unsigned capture_frequency = OUTPUT_FREQUENCY; + // Which channels we consider interesting (ie., are part of some input_mapping). + std::set interesting_channels; + // Only used for ALSA cards, obviously. + std::unique_ptr alsa_device; }; - CaptureCard cards[MAX_CARDS]; + AudioDevice *find_audio_device(DeviceSpec device_spec); + + void find_sample_src_from_device(const std::map> &samples_card, DeviceSpec device_spec, int source_channel, const float **srcptr, unsigned *stride); + void fill_audio_bus(const std::map> &samples_card, const InputMapping::Bus &bus, unsigned num_samples, float *output); + void reset_resampler_mutex_held(DeviceSpec device_spec); + void reset_alsa_mutex_held(DeviceSpec device_spec); + std::map get_devices_mutex_held() const; + void update_meters(const std::vector &samples); + void measure_bus_levels(unsigned bus_index, const std::vector &left, const std::vector &right); + void send_audio_level_callback(); + + unsigned num_cards; + + mutable std::timed_mutex audio_mutex; + + AudioDevice video_cards[MAX_VIDEO_CARDS]; // Under audio_mutex. + + // TODO: Figure out a better way to unify these two, as they are sharing indexing. + AudioDevice alsa_inputs[MAX_ALSA_CARDS]; // Under audio_mutex. + std::vector available_alsa_cards; StereoFilter locut; // Default cutoff 120 Hz, 24 dB/oct. std::atomic locut_cutoff_hz; @@ -194,8 +256,6 @@ private: static constexpr float ref_level_dbfs = -14.0f; // Chosen so that we end up around 0 LU in practice. static constexpr float ref_level_lufs = -23.0f; // 0 LU, more or less by definition. - std::atomic loudness_lufs{ref_level_lufs}; - StereoCompressor limiter; std::atomic limiter_threshold_dbfs{ref_level_dbfs + 4.0f}; // 4 dB. std::atomic limiter_enabled{true}; @@ -206,8 +266,21 @@ private: double final_makeup_gain = 1.0; // Under compressor_mutex. Read/write by the user. Note: Not in dB, we want the numeric precision so that we can change it slowly. bool final_makeup_gain_auto = true; // Under compressor_mutex. - mutable std::mutex mapping_mutex; - InputMapping input_mapping; // Under mapping_mutex. + InputMapping input_mapping; // Under audio_mutex. + std::atomic fader_volume_db[MAX_BUSES] {{ 0.0f }}; + + audio_level_callback_t audio_level_callback = nullptr; + mutable std::mutex audio_measure_mutex; + Ebu_r128_proc r128; // Under audio_measure_mutex. + CorrelationMeasurer correlation; // Under audio_measure_mutex. + Resampler peak_resampler; // Under audio_measure_mutex. + std::atomic peak{0.0f}; + + // Under audio_measure_mutex. Note that Ebu_r128_proc has a broken + // copy constructor (it uses the default, but holds arrays), + // so we can't just use raw Ebu_r128_proc elements, but need to use + // unique_ptrs. + std::vector> bus_r128; }; #endif // !defined(_AUDIO_MIXER_H)