]> git.sesse.net Git - nageru/blobdiff - audio_mixer.h
Make AudioMixer ready for indexing on multiple types of devices.
[nageru] / audio_mixer.h
index ef49120dc2b2047b99ed7ff045ec17ad226a137f..c7841fbdf32125116803af67dfffe4d0f3c66a34 100644 (file)
 #include <math.h>
 #include <stdint.h>
 #include <atomic>
+#include <map>
 #include <memory>
 #include <mutex>
+#include <set>
 #include <vector>
 
 #include "bmusb/bmusb.h"
+#include "db.h"
 #include "defs.h"
 #include "filter.h"
 #include "resampling_queue.h"
@@ -28,19 +31,42 @@ namespace bmusb {
 struct AudioFormat;
 }  // namespace bmusb
 
+enum class InputSourceType { SILENCE, CAPTURE_CARD };
+struct DeviceSpec {
+       InputSourceType type;
+       unsigned index;
+};
+
+struct InputMapping {
+       struct Bus {
+               std::string name;
+               DeviceSpec device;
+               int source_channel[2] { -1, -1 };  // Left and right. -1 = none.
+       };
+
+       std::vector<Bus> buses;
+};
+
 class AudioMixer {
 public:
        AudioMixer(unsigned num_cards);
-       void reset_card(unsigned card_index);
+       void reset_device(DeviceSpec device_spec);
 
        // 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);
+       void add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, bmusb::AudioFormat audio_format, int64_t frame_length);
+       void add_silence(DeviceSpec device_spec, unsigned samples_per_frame, unsigned num_frames, int64_t frame_length);
        std::vector<float> 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 bus_index, float level_db) { fader_volume_db[bus_index] = level_db; }
+       std::vector<std::string> get_names() const;
+       void set_name(DeviceSpec device_spec, const std::string &name);
+
+       void set_input_mapping(const InputMapping &input_mapping);
+       InputMapping get_input_mapping() const;
+
        void set_locut_cutoff(float cutoff_hz)
        {
                locut_cutoff_hz = cutoff_hz;
@@ -125,13 +151,13 @@ public:
        {
                std::unique_lock<std::mutex> lock(compressor_mutex);
                final_makeup_gain_auto = false;
-               final_makeup_gain = pow(10.0f, gain_db / 20.0f);
+               final_makeup_gain = from_db(gain_db);
        }
 
        float get_final_makeup_gain_db()
        {
                std::unique_lock<std::mutex> lock(compressor_mutex);
-               return 20.0 * log10(final_makeup_gain);
+               return to_db(final_makeup_gain);
        }
 
        void set_final_makeup_gain_auto(bool enabled)
@@ -147,14 +173,24 @@ public:
        }
 
 private:
+       struct AudioDevice {
+               std::unique_ptr<ResamplingQueue> resampling_queue;
+               int64_t next_local_pts = 0;
+               std::string name;
+               // Which channels we consider interesting (ie., are part of some input_mapping).
+               std::set<unsigned> interesting_channels;
+       };
+       AudioDevice *find_audio_device(DeviceSpec device_spec);
+
+       void find_sample_src_from_device(const std::vector<float> *samples_card, DeviceSpec device_spec, int source_channel, const float **srcptr, unsigned *stride);
+       void fill_audio_bus(const std::vector<float> *samples_card, const InputMapping::Bus &bus, unsigned num_samples, float *output);
+       void reset_device_mutex_held(DeviceSpec device_spec);
+
        unsigned num_cards;
 
-       struct CaptureCard {
-               std::mutex audio_mutex;
-               std::unique_ptr<ResamplingQueue> resampling_queue;  // Under audio_mutex.
-               int64_t next_local_pts = 0;  // Beginning of next frame, in TIMEBASE units. Under audio_mutex.
-       };
-       CaptureCard cards[MAX_CARDS];
+       mutable std::mutex audio_mutex;
+
+       AudioDevice cards[MAX_CARDS];  // Under audio_mutex.
 
        StereoFilter locut;  // Default cutoff 120 Hz, 24 dB/oct.
        std::atomic<float> locut_cutoff_hz;
@@ -180,6 +216,9 @@ 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.
+
+       InputMapping input_mapping;  // Under audio_mutex.
+       std::atomic<float> fader_volume_db[MAX_BUSES] {{ 0.0f }};
 };
 
 #endif  // !defined(_AUDIO_MIXER_H)