mirror of
https://github.com/celisej567/abaddon.git
synced 2026-09-19 20:11:59 +03:00
add ability to set capture device
This commit is contained in:
@@ -1105,6 +1105,7 @@ int main(int argc, char **argv) {
|
||||
spdlog::cfg::load_env_levels();
|
||||
auto log_audio = spdlog::stdout_color_mt("audio");
|
||||
auto log_voice = spdlog::stdout_color_mt("voice");
|
||||
auto log_discord = spdlog::stdout_color_mt("discord");
|
||||
|
||||
Gtk::Main::init_gtkmm_internals(); // why???
|
||||
return Abaddon::Get().StartGTK();
|
||||
|
||||
@@ -7,13 +7,18 @@
|
||||
// clang-format on
|
||||
|
||||
AudioDevices::AudioDevices()
|
||||
: m_playback(Gtk::ListStore::create(m_playback_columns)) {
|
||||
: m_playback(Gtk::ListStore::create(m_playback_columns))
|
||||
, m_capture(Gtk::ListStore::create(m_capture_columns)) {
|
||||
}
|
||||
|
||||
Glib::RefPtr<Gtk::ListStore> AudioDevices::GetPlaybackDeviceModel() {
|
||||
return m_playback;
|
||||
}
|
||||
|
||||
Glib::RefPtr<Gtk::ListStore> AudioDevices::GetCaptureDeviceModel() {
|
||||
return m_capture;
|
||||
}
|
||||
|
||||
void AudioDevices::SetDevices(ma_device_info *pPlayback, ma_uint32 playback_count, ma_device_info *pCapture, ma_uint32 capture_count) {
|
||||
m_playback->clear();
|
||||
|
||||
@@ -23,9 +28,18 @@ void AudioDevices::SetDevices(ma_device_info *pPlayback, ma_uint32 playback_coun
|
||||
row[m_playback_columns.Name] = d.name;
|
||||
row[m_playback_columns.DeviceID] = d.id;
|
||||
}
|
||||
|
||||
m_capture->clear();
|
||||
|
||||
for (ma_uint32 i = 0; i < capture_count; i++) {
|
||||
auto &d = pCapture[i];
|
||||
auto row = *m_capture->append();
|
||||
row[m_capture_columns.Name] = d.name;
|
||||
row[m_capture_columns.DeviceID] = d.id;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<ma_device_id> AudioDevices::GetDeviceIDFromModel(const Gtk::TreeModel::iterator &iter) {
|
||||
std::optional<ma_device_id> AudioDevices::GetPlaybackDeviceIDFromModel(const Gtk::TreeModel::iterator &iter) const {
|
||||
if (iter) {
|
||||
return static_cast<ma_device_id>((*iter)[m_playback_columns.DeviceID]);
|
||||
} else {
|
||||
@@ -33,8 +47,21 @@ std::optional<ma_device_id> AudioDevices::GetDeviceIDFromModel(const Gtk::TreeMo
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<ma_device_id> AudioDevices::GetCaptureDeviceIDFromModel(const Gtk::TreeModel::iterator &iter) const {
|
||||
if (iter) {
|
||||
return static_cast<ma_device_id>((*iter)[m_capture_columns.DeviceID]);
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
AudioDevices::PlaybackColumns::PlaybackColumns() {
|
||||
add(Name);
|
||||
add(DeviceID);
|
||||
}
|
||||
|
||||
AudioDevices::CaptureColumns::CaptureColumns() {
|
||||
add(Name);
|
||||
add(DeviceID);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -14,9 +14,11 @@ public:
|
||||
AudioDevices();
|
||||
|
||||
Glib::RefPtr<Gtk::ListStore> GetPlaybackDeviceModel();
|
||||
Glib::RefPtr<Gtk::ListStore> GetCaptureDeviceModel();
|
||||
|
||||
void SetDevices(ma_device_info *pPlayback, ma_uint32 playback_count, ma_device_info *pCapture, ma_uint32 capture_count);
|
||||
std::optional<ma_device_id> GetDeviceIDFromModel(const Gtk::TreeModel::iterator &iter);
|
||||
std::optional<ma_device_id> GetPlaybackDeviceIDFromModel(const Gtk::TreeModel::iterator &iter) const;
|
||||
std::optional<ma_device_id> GetCaptureDeviceIDFromModel(const Gtk::TreeModel::iterator &iter) const;
|
||||
|
||||
private:
|
||||
class PlaybackColumns : public Gtk::TreeModel::ColumnRecord {
|
||||
@@ -28,5 +30,15 @@ private:
|
||||
};
|
||||
PlaybackColumns m_playback_columns;
|
||||
Glib::RefPtr<Gtk::ListStore> m_playback;
|
||||
|
||||
class CaptureColumns : public Gtk::TreeModel::ColumnRecord {
|
||||
public:
|
||||
CaptureColumns();
|
||||
|
||||
Gtk::TreeModelColumn<Glib::ustring> Name;
|
||||
Gtk::TreeModelColumn<ma_device_id> DeviceID;
|
||||
};
|
||||
CaptureColumns m_capture_columns;
|
||||
Glib::RefPtr<Gtk::ListStore> m_capture;
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -186,7 +186,7 @@ void AudioManager::StopCaptureDevice() {
|
||||
void AudioManager::SetPlaybackDevice(const Gtk::TreeModel::iterator &iter) {
|
||||
spdlog::get("audio")->debug("Setting new playback device");
|
||||
|
||||
const auto device_id = m_devices.GetDeviceIDFromModel(iter);
|
||||
const auto device_id = m_devices.GetPlaybackDeviceIDFromModel(iter);
|
||||
if (!device_id) {
|
||||
spdlog::get("audio")->error("Requested ID from iterator is invalid");
|
||||
return;
|
||||
@@ -215,6 +215,40 @@ void AudioManager::SetPlaybackDevice(const Gtk::TreeModel::iterator &iter) {
|
||||
}
|
||||
}
|
||||
|
||||
void AudioManager::SetCaptureDevice(const Gtk::TreeModel::iterator &iter) {
|
||||
spdlog::get("audio")->debug("Setting new capture device");
|
||||
|
||||
const auto device_id = m_devices.GetCaptureDeviceIDFromModel(iter);
|
||||
if (!device_id) {
|
||||
spdlog::get("audio")->error("Requested ID from iterator is invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
m_capture_id = *device_id;
|
||||
|
||||
ma_device_uninit(&m_capture_device);
|
||||
|
||||
m_capture_config = ma_device_config_init(ma_device_type_capture);
|
||||
m_capture_config.capture.format = ma_format_s16;
|
||||
m_capture_config.capture.channels = 2;
|
||||
m_capture_config.capture.pDeviceID = &m_capture_id;
|
||||
m_capture_config.sampleRate = 48000;
|
||||
m_capture_config.periodSizeInFrames = 480;
|
||||
m_capture_config.dataCallback = capture_data_callback;
|
||||
m_capture_config.pUserData = this;
|
||||
|
||||
if (ma_device_init(&m_context, &m_capture_config, &m_capture_device) != MA_SUCCESS) {
|
||||
spdlog::get("audio")->error("Failed to initialize new device");
|
||||
return;
|
||||
}
|
||||
|
||||
// technically this should probably try and check old state but if you are in the window to change it then you are connected
|
||||
if (ma_device_start(&m_capture_device) != MA_SUCCESS) {
|
||||
spdlog::get("audio")->error("Failed to start new device");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void AudioManager::SetCapture(bool capture) {
|
||||
m_should_capture = capture;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ public:
|
||||
void StopCaptureDevice();
|
||||
|
||||
void SetPlaybackDevice(const Gtk::TreeModel::iterator &iter);
|
||||
void SetCaptureDevice(const Gtk::TreeModel::iterator &iter);
|
||||
|
||||
void SetCapture(bool capture);
|
||||
void SetPlayback(bool playback);
|
||||
@@ -75,6 +76,7 @@ private:
|
||||
// capture
|
||||
ma_device m_capture_device;
|
||||
ma_device_config m_capture_config;
|
||||
ma_device_id m_capture_id;
|
||||
|
||||
ma_context m_context;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "abaddon.hpp"
|
||||
#include "discord.hpp"
|
||||
#include "util.hpp"
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <cinttypes>
|
||||
#include <utility>
|
||||
|
||||
@@ -2168,7 +2169,7 @@ void DiscordClient::HandleGatewayVoiceStateUpdate(const GatewayMessage &msg) {
|
||||
VoiceState data = msg.Data;
|
||||
|
||||
if (data.UserID == m_user_data.ID) {
|
||||
printf("voice session id: %s\n", data.SessionID.c_str());
|
||||
spdlog::get("discord")->debug("Voice session ID: {}", data.SessionID);
|
||||
m_voice.SetSessionID(data.SessionID);
|
||||
} else {
|
||||
if (data.GuildID.has_value() && data.Member.has_value()) {
|
||||
@@ -2197,8 +2198,8 @@ void DiscordClient::HandleGatewayVoiceStateUpdate(const GatewayMessage &msg) {
|
||||
|
||||
void DiscordClient::HandleGatewayVoiceServerUpdate(const GatewayMessage &msg) {
|
||||
VoiceServerUpdateData data = msg.Data;
|
||||
printf("endpoint: %s\n", data.Endpoint.c_str());
|
||||
printf("token: %s\n", data.Token.c_str());
|
||||
spdlog::get("discord")->debug("Voice server endpoint: {}", data.Endpoint);
|
||||
spdlog::get("discord")->debug("Voice token: {}", data.Token);
|
||||
m_voice.SetEndpoint(data.Endpoint);
|
||||
m_voice.SetToken(data.Token);
|
||||
if (data.GuildID.has_value()) {
|
||||
@@ -2206,7 +2207,7 @@ void DiscordClient::HandleGatewayVoiceServerUpdate(const GatewayMessage &msg) {
|
||||
} else if (data.ChannelID.has_value()) {
|
||||
m_voice.SetServerID(*data.ChannelID);
|
||||
} else {
|
||||
puts("no guild or channel id in voice server?");
|
||||
spdlog::get("discord")->error("No guild or channel ID in voice server?");
|
||||
}
|
||||
m_voice.SetUserID(m_user_data.ID);
|
||||
m_voice.Start();
|
||||
|
||||
@@ -121,17 +121,28 @@ VoiceWindow::VoiceWindow(Snowflake channel_id)
|
||||
m_signal_gain.emit(val / 100.0);
|
||||
});
|
||||
|
||||
auto *renderer = Gtk::make_managed<Gtk::CellRendererText>();
|
||||
auto *playback_renderer = Gtk::make_managed<Gtk::CellRendererText>();
|
||||
m_playback_combo.set_valign(Gtk::ALIGN_END);
|
||||
m_playback_combo.set_hexpand(true);
|
||||
m_playback_combo.set_halign(Gtk::ALIGN_FILL);
|
||||
m_playback_combo.set_model(Abaddon::Get().GetAudio().GetDevices().GetPlaybackDeviceModel());
|
||||
m_playback_combo.pack_start(*renderer);
|
||||
m_playback_combo.add_attribute(*renderer, "text", 0);
|
||||
m_playback_combo.pack_start(*playback_renderer);
|
||||
m_playback_combo.add_attribute(*playback_renderer, "text", 0);
|
||||
m_playback_combo.signal_changed().connect([this]() {
|
||||
Abaddon::Get().GetAudio().SetPlaybackDevice(m_playback_combo.get_active());
|
||||
});
|
||||
|
||||
auto *capture_renderer = Gtk::make_managed<Gtk::CellRendererText>();
|
||||
m_capture_combo.set_valign(Gtk::ALIGN_END);
|
||||
m_capture_combo.set_hexpand(true);
|
||||
m_capture_combo.set_halign(Gtk::ALIGN_FILL);
|
||||
m_capture_combo.set_model(Abaddon::Get().GetAudio().GetDevices().GetCaptureDeviceModel());
|
||||
m_capture_combo.pack_start(*capture_renderer);
|
||||
m_capture_combo.add_attribute(*capture_renderer, "text", 0);
|
||||
m_capture_combo.signal_changed().connect([this]() {
|
||||
Abaddon::Get().GetAudio().SetCaptureDevice(m_capture_combo.get_active());
|
||||
});
|
||||
|
||||
m_scroll.add(m_user_list);
|
||||
m_controls.add(m_mute);
|
||||
m_controls.add(m_deafen);
|
||||
@@ -141,6 +152,7 @@ VoiceWindow::VoiceWindow(Snowflake channel_id)
|
||||
m_main.add(m_capture_gain);
|
||||
m_main.add(m_scroll);
|
||||
m_main.add(m_playback_combo);
|
||||
m_main.add(m_capture_combo);
|
||||
add(m_main);
|
||||
show_all_children();
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ private:
|
||||
Gtk::Scale m_capture_gain;
|
||||
|
||||
Gtk::ComboBox m_playback_combo;
|
||||
Gtk::ComboBox m_capture_combo;
|
||||
|
||||
Snowflake m_channel_id;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user