Skip to content

Commit

Permalink
add mix mono option to voice window (closes #238)
Browse files Browse the repository at this point in the history
  • Loading branch information
ouwou committed Oct 21, 2023
1 parent 893c9b5 commit 9a5e820
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/audio/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,23 @@ void AudioManager::OnCapturedPCM(const int16_t *pcm, ma_uint32 frames) {
if (m_opus_buffer == nullptr || !m_should_capture) return;

const double gain = m_capture_gain;
// i have a suspicion i can cast the const away... but i wont

std::vector<int16_t> new_pcm(pcm, pcm + frames * 2);
for (auto &val : new_pcm) {
const int32_t unclamped = static_cast<int32_t>(val * gain);
val = std::clamp(unclamped, INT16_MIN, INT16_MAX);
}

if (m_mix_mono) {
for (size_t i = 0; i < frames * 2; i += 2) {
const int sample_L = new_pcm[i];
const int sample_R = new_pcm[i + 1];
const int16_t mixed = static_cast<int16_t>((sample_L + sample_R) / 2);
new_pcm[i] = mixed;
new_pcm[i + 1] = mixed;
}
}

UpdateCaptureVolume(new_pcm.data(), frames);

static std::array<float, 480> denoised_L;
Expand Down Expand Up @@ -629,6 +639,14 @@ bool AudioManager::GetSuppressNoise() const {
}
#endif

void AudioManager::SetMixMono(bool value) {
m_mix_mono = value;
}

bool AudioManager::GetMixMono() const {
return m_mix_mono;
}

AudioManager::type_signal_opus_packet AudioManager::signal_opus_packet() {
return m_signal_opus_packet;
}
Expand Down
4 changes: 4 additions & 0 deletions src/audio/manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class AudioManager {
bool GetSuppressNoise() const;
#endif

void SetMixMono(bool value);
bool GetMixMono() const;

private:
void OnCapturedPCM(const int16_t *pcm, ma_uint32 frames);

Expand Down Expand Up @@ -144,6 +147,7 @@ class AudioManager {
std::atomic<double> m_prob_threshold = 0.5;
std::atomic<float> m_vad_prob = 0.0;
std::atomic<bool> m_enable_noise_suppression = false;
std::atomic<bool> m_mix_mono = false;

std::unordered_set<uint32_t> m_muted_ssrcs;
std::unordered_map<uint32_t, double> m_volume_ssrc;
Expand Down
7 changes: 7 additions & 0 deletions src/windows/voicewindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ VoiceWindow::VoiceWindow(Snowflake channel_id)
, m_mute("Mute")
, m_deafen("Deafen")
, m_noise_suppression("Suppress Noise")
, m_mix_mono("Mix Mono")
, m_channel_id(channel_id)
, m_menu_view("View")
, m_menu_view_settings("More _Settings", true) {
Expand Down Expand Up @@ -178,6 +179,11 @@ VoiceWindow::VoiceWindow(Snowflake channel_id)
Abaddon::Get().GetAudio().SetSuppressNoise(m_noise_suppression.get_active());
});

m_mix_mono.set_active(audio.GetMixMono());
m_mix_mono.signal_toggled().connect([this]() {
Abaddon::Get().GetAudio().SetMixMono(m_mix_mono.get_active());
});

auto *playback_renderer = Gtk::make_managed<Gtk::CellRendererText>();
m_playback_combo.set_valign(Gtk::ALIGN_END);
m_playback_combo.set_hexpand(true);
Expand Down Expand Up @@ -223,6 +229,7 @@ VoiceWindow::VoiceWindow(Snowflake channel_id)
m_controls.add(m_mute);
m_controls.add(m_deafen);
m_controls.add(m_noise_suppression);
m_controls.add(m_mix_mono);
m_main.add(m_menu_bar);
m_main.add(m_controls);
m_main.add(m_vad_value);
Expand Down
1 change: 1 addition & 0 deletions src/windows/voicewindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class VoiceWindow : public Gtk::Window {
Gtk::Scale m_capture_gain;

Gtk::CheckButton m_noise_suppression;
Gtk::CheckButton m_mix_mono;

Gtk::ComboBoxText m_vad_combo;
Gtk::ComboBox m_playback_combo;
Expand Down

0 comments on commit 9a5e820

Please sign in to comment.