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:
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user