#include #include #include "audio-input-row.hpp" #include "waveform-view.hpp" #include #include #include #include #include #include #include #include #include #include namespace { QString trText(const char *key) { return QString::fromUtf8(obs_module_text(key)); } float mulToDb(float mul) { return (mul > 1e-6f) ? 20.0f * std::log10(mul) : -60.0f; } float dbToMul(float db) { return std::pow(10.0f, db / 20.0f); } QString formatDb(float db) { if (db <= -59.95f) return QStringLiteral("-∞ dB"); return QString::number(db, 'f', 1) + QStringLiteral(" dB"); } } // namespace static void rowSourceAudioCb(void *param, obs_source_t *source, const struct audio_data *data, bool muted) { (void)source; if (param && data) static_cast(param)->handleSourceAudio(data, muted); } AudioInputRow::AudioInputRow(const QString &uuid, const QString &name, bool master, QWidget *parent) : QWidget(parent), uuid_(uuid), name_(name), master_(master) { buildUi(); obs_audio_info info = {}; uint32_t rate = 48000; if (obs_get_audio_info(&info) && info.samples_per_sec) rate = info.samples_per_sec; feed_.setSampleRate(rate); view_->setFeed(&feed_); if (master_) { updateTitle(); } else { attachSource(); } } AudioInputRow::~AudioInputRow() { shutdown(); } void AudioInputRow::buildUi() { auto *root = new QHBoxLayout(this); root->setContentsMargins(4, 2, 4, 2); root->setSpacing(6); auto *left = new QVBoxLayout(); left->setSpacing(3); view_ = new WaveformView(this); view_->setCompact(true); view_->setMinimumHeight(58); view_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); view_->setContextMenuPolicy(Qt::CustomContextMenu); connect(view_, &QWidget::customContextMenuRequested, this, &AudioInputRow::showContextMenu); left->addWidget(view_, 1); if (!master_) { auto *buttons = new QHBoxLayout(); buttons->setSpacing(4); monitorButton_ = new QToolButton(this); monitorButton_->setText(trText("Monitor")); monitorButton_->setCheckable(true); monitorButton_->setToolTip(trText("Monitor.Tooltip")); connect(monitorButton_, &QToolButton::toggled, this, &AudioInputRow::onMonitorToggled); buttons->addWidget(monitorButton_); muteButton_ = new QToolButton(this); muteButton_->setText(trText("Mute")); muteButton_->setCheckable(true); muteButton_->setToolTip(trText("Mute.Tooltip")); connect(muteButton_, &QToolButton::toggled, this, &AudioInputRow::onMuteToggled); buttons->addWidget(muteButton_); filtersButton_ = new QToolButton(this); filtersButton_->setText(trText("Filters")); filtersButton_->setToolTip(trText("Filters.Tooltip")); connect(filtersButton_, &QToolButton::clicked, this, &AudioInputRow::openFilters); buttons->addWidget(filtersButton_); buttons->addStretch(1); left->addLayout(buttons); } root->addLayout(left, 1); if (!master_) { auto *right = new QVBoxLayout(); right->setSpacing(2); fader_ = new QSlider(Qt::Vertical, this); fader_->setRange(-600, 0); /* -60.0 dB .. 0.0 dB */ fader_->setValue(0); fader_->setFixedWidth(22); fader_->setToolTip(trText("Volume.Tooltip")); connect(fader_, &QSlider::valueChanged, this, &AudioInputRow::onVolumeValueChanged); right->addWidget(fader_, 1); dbLabel_ = new QLabel(QStringLiteral("0.0 dB"), this); dbLabel_->setMinimumWidth(50); dbLabel_->setAlignment(Qt::AlignCenter); { QFont f = dbLabel_->font(); f.setPointSizeF(std::max(7.0, f.pointSizeF() - 1.0)); dbLabel_->setFont(f); } right->addWidget(dbLabel_); root->addLayout(right); } setMinimumHeight(master_ ? 90 : 124); } void AudioInputRow::attachSource() { if (master_) return; obs_source_t *source = obs_get_source_by_uuid(uuid_.toUtf8().constData()); if (!source) { updateTitle(); return; } source_ = obs_source_get_ref(source); weakSource_ = obs_source_get_weak_source(source); if (source_) { obs_source_add_audio_capture_callback(static_cast(source_), rowSourceAudioCb, this); setControlsEnabled(true); syncFromSource(); } obs_source_release(source); updateTitle(); } void AudioInputRow::detachSource() { if (source_) { obs_source_remove_audio_capture_callback(static_cast(source_), rowSourceAudioCb, this); obs_source_release(static_cast(source_)); source_ = nullptr; } if (weakSource_) { obs_weak_source_release(static_cast(weakSource_)); weakSource_ = nullptr; } feed_.clear(); } void AudioInputRow::shutdown() { if (dead_) return; dead_ = true; detachSource(); } void AudioInputRow::setDisplayMode(int index) { if (!view_) return; view_->setDisplayMode(index == 1 ? WaveformView::DisplayMode::Waveform : WaveformView::DisplayMode::Spectrum); } void AudioInputRow::setSourceName(const QString &name) { if (name == name_) return; name_ = name; updateTitle(); } void AudioInputRow::updateTitle() { if (!view_) return; if (master_) { view_->setTitle(trText("Master.Title")); return; } if (name_.isEmpty()) view_->setTitle(trText("Source.Unknown")); else view_->setTitle(name_); } void AudioInputRow::tick() { if (!dead_ && view_) view_->tick(); } void AudioInputRow::refreshFromSource() { if (master_ || dead_) return; if (weakSource_) { auto *weak = static_cast(weakSource_); if (obs_weak_source_expired(weak)) { detachSource(); setControlsEnabled(false); updateTitle(); return; } } if (!source_) return; obs_source_t *source = static_cast(source_); const char *name = obs_source_get_name(source); if (name) setSourceName(QString::fromUtf8(name)); syncFromSource(); } void AudioInputRow::setControlsEnabled(bool enabled) { if (fader_) fader_->setEnabled(enabled); if (dbLabel_) dbLabel_->setEnabled(enabled); if (monitorButton_) monitorButton_->setEnabled(enabled); if (muteButton_) muteButton_->setEnabled(enabled); if (filtersButton_) filtersButton_->setEnabled(enabled); } void AudioInputRow::syncFromSource() { if (!source_ || !fader_) return; obs_source_t *source = static_cast(source_); const QSignalBlocker blockFader(fader_); const QSignalBlocker blockMute(muteButton_); const QSignalBlocker blockMonitor(monitorButton_); const float db = std::clamp(mulToDb(obs_source_get_volume(source)), -60.0f, 0.0f); fader_->setValue(static_cast(std::lround(db * 10.0f))); updateVolumeLabel(db); if (muteButton_) muteButton_->setChecked(obs_source_muted(source)); if (monitorButton_) monitorButton_->setChecked(obs_source_get_monitoring_enabled(source)); } void AudioInputRow::updateVolumeLabel(float db) { if (!dbLabel_) return; dbLabel_->setText(formatDb(db)); } void AudioInputRow::onVolumeValueChanged(int value) { updateVolumeLabel(static_cast(value) / 10.0f); if (!source_) return; const float db = static_cast(value) / 10.0f; const float mul = (value <= -600) ? 0.0f : dbToMul(db); obs_source_set_volume(static_cast(source_), mul); } void AudioInputRow::onMuteToggled(bool checked) { if (!source_) return; obs_source_set_muted(static_cast(source_), checked); } void AudioInputRow::onMonitorToggled(bool checked) { if (!source_) return; obs_source_set_monitoring_enabled(static_cast(source_), checked); } void AudioInputRow::openFilters() { if (!source_) return; obs_frontend_open_source_filters(static_cast(source_)); } void AudioInputRow::openProperties() { if (!source_) return; obs_frontend_open_source_properties(static_cast(source_)); } void AudioInputRow::showContextMenu(const QPoint &pos) { QMenu menu(this); QAction *monitor = menu.addAction(trText("Monitor")); monitor->setCheckable(true); monitor->setChecked(monitorButton_ && monitorButton_->isChecked()); monitor->setEnabled(source_ != nullptr); connect(monitor, &QAction::triggered, this, &AudioInputRow::onMonitorToggled); QAction *mute = menu.addAction(trText("Mute")); mute->setCheckable(true); mute->setChecked(muteButton_ && muteButton_->isChecked()); mute->setEnabled(source_ != nullptr); connect(mute, &QAction::triggered, this, &AudioInputRow::onMuteToggled); QAction *reset = menu.addAction(trText("Volume.Reset")); reset->setEnabled(source_ != nullptr); connect(reset, &QAction::triggered, this, [this]() { if (fader_) fader_->setValue(0); }); menu.addSeparator(); QAction *filters = menu.addAction(trText("Filters")); filters->setEnabled(source_ != nullptr); connect(filters, &QAction::triggered, this, &AudioInputRow::openFilters); QAction *props = menu.addAction(trText("Properties")); props->setEnabled(source_ != nullptr); connect(props, &QAction::triggered, this, &AudioInputRow::openProperties); menu.exec(view_->mapToGlobal(pos)); } void AudioInputRow::handleSourceAudio(const void *audioData, bool muted) { if (dead_ || !audioData) return; const auto *data = static_cast(audioData); int channels = 0; for (int i = 0; i < MAX_AV_PLANES && data->data[i]; i++) channels++; if (channels == 0 || data->frames == 0) return; float gain = 1.0f; auto *source = static_cast(source_); if (muted || (source && obs_source_muted(source))) gain = 0.0f; else if (source) gain = obs_source_get_volume(source); static std::atomic logged{false}; if (!logged.exchange(true)) blog(LOG_INFO, "[audio-waveform-dock] source audio flowing (%d ch, %u frames)", channels, data->frames); feed_.pushPlanar(data->data, channels, data->frames, gain); }