From 0f4b5fde73be7c8606d5812b8007cb23b8083bb6 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Mon, 8 Aug 2016 23:52:07 +0200 Subject: [PATCH] Prepare InputMappingDialog for arbitrary kinds of input source types, by storing the DeviceSpec in the QComboBox items. --- audio_mixer.cpp | 7 ++++--- audio_mixer.h | 16 +++++++++++++++- input_mapping_dialog.cpp | 33 +++++++++++++-------------------- input_mapping_dialog.h | 6 ++++-- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/audio_mixer.cpp b/audio_mixer.cpp index db4b24d..93d59f4 100644 --- a/audio_mixer.cpp +++ b/audio_mixer.cpp @@ -396,13 +396,14 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin return samples_out; } -vector AudioMixer::get_names() const +map AudioMixer::get_names() const { lock_guard lock(audio_mutex); - vector names; + map names; for (unsigned card_index = 0; card_index < num_cards; ++card_index) { + const DeviceSpec spec{ InputSourceType::CAPTURE_CARD, card_index }; const AudioDevice *device = &cards[card_index]; - names.push_back(device->name); + names.insert(make_pair(spec, device->name)); } return names; } diff --git a/audio_mixer.h b/audio_mixer.h index 5ea79de..d45fd43 100644 --- a/audio_mixer.h +++ b/audio_mixer.h @@ -36,6 +36,10 @@ 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; @@ -43,6 +47,16 @@ struct DeviceSpec { } }; +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 Bus { std::string name; @@ -67,7 +81,7 @@ public: 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 get_names() const; + std::map get_names() const; void set_name(DeviceSpec device_spec, const std::string &name); void set_input_mapping(const InputMapping &input_mapping); diff --git a/input_mapping_dialog.cpp b/input_mapping_dialog.cpp index 076cc2f..54936a8 100644 --- a/input_mapping_dialog.cpp +++ b/input_mapping_dialog.cpp @@ -51,22 +51,19 @@ void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus // Card choices. QComboBox *card_combo = new QComboBox; + unsigned current_index = 0; card_combo->addItem(QString("(none) ")); - for (const string &name : card_names) { - card_combo->addItem(QString::fromStdString(name + " ")); - } - switch (bus.device.type) { - case InputSourceType::SILENCE: - card_combo->setCurrentIndex(0); - break; - case InputSourceType::CAPTURE_CARD: - card_combo->setCurrentIndex(mapping.buses[row].device.index + 1); - break; - default: - assert(false); + for (const auto &spec_and_name : card_names) { + ++current_index; + card_combo->addItem( + QString::fromStdString(spec_and_name.second + " "), + qulonglong(DeviceSpec_to_key(spec_and_name.first))); + if (bus.device == spec_and_name.first) { + card_combo->setCurrentIndex(current_index); + } } connect(card_combo, static_cast(&QComboBox::currentIndexChanged), - bind(&InputMappingDialog::card_selected, this, row, _1)); + bind(&InputMappingDialog::card_selected, this, card_combo, row, _1)); ui->table->setCellWidget(row, 1, card_combo); setup_channel_choices_from_bus(row, bus); @@ -115,14 +112,10 @@ void InputMappingDialog::cell_changed(int row, int column) mapping.buses[row].name = ui->table->item(row, column)->text().toStdString(); } -void InputMappingDialog::card_selected(unsigned row, int index) +void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index) { - if (index == 0) { - mapping.buses[row].device.type = InputSourceType::SILENCE; - } else { - mapping.buses[row].device.type = InputSourceType::CAPTURE_CARD; - mapping.buses[row].device.index = index - 1; - } + uint64_t key = card_combo->itemData(index).toULongLong(); + mapping.buses[row].device = key_to_DeviceSpec(key); setup_channel_choices_from_bus(row, mapping.buses[row]); } diff --git a/input_mapping_dialog.h b/input_mapping_dialog.h index 90be52a..93a84b0 100644 --- a/input_mapping_dialog.h +++ b/input_mapping_dialog.h @@ -12,6 +12,8 @@ namespace Ui { class InputMappingDialog; } // namespace Ui +class QComboBox; + class InputMappingDialog : public QDialog { Q_OBJECT @@ -24,7 +26,7 @@ private: void fill_row_from_bus(unsigned row, const InputMapping::Bus &bus); void setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus); void cell_changed(int row, int column); - void card_selected(unsigned row, int index); + void card_selected(QComboBox *card_combo, unsigned row, int index); void channel_selected(unsigned row, unsigned channel, int index); void ok_clicked(); void cancel_clicked(); @@ -41,7 +43,7 @@ private: // held forever). InputMapping old_mapping; - const std::vector card_names; + const std::map card_names; }; #endif // !defined(_INPUT_MAPPING_DIALOG_H) -- 2.39.2