diff --git a/src/main.rs b/src/main.rs index 392dfaf..124cde5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use log::{debug, info}; use serde::Serialize; use std::sync::{Arc, Mutex}; -use vbplay::AudioThread; +use vbplay::{AudioThread, DeviceSelection}; mod soundclips; mod state; @@ -21,7 +21,9 @@ async fn main() { jinja.set_loader(path_loader("templates")); let template_engine = Engine::from(jinja); - let audio = vbplay::audio_thread(); + let audio = vbplay::audio_thread(DeviceSelection::FindByPattern( + ".*VB-Audio Virtual Cable.*".to_owned(), + )); let audio: Arc> = Arc::new(Mutex::new(audio)); let app_state = AppState { @@ -29,6 +31,7 @@ async fn main() { clips: soundclips::scan_directory_for_clips("E:/sounds/soundboard/all", &["mp3"]) .expect("No Soundclips found."), player: audio, + playback: None, }; // Set the address to run our application on diff --git a/src/state.rs b/src/state.rs index 6e18373..b7c064d 100644 --- a/src/state.rs +++ b/src/state.rs @@ -12,6 +12,7 @@ pub struct AppState { pub engine: TemplateEngine, pub clips: Vec, pub player: Arc>, + pub playback: Option>>, } impl FromRef for TemplateEngine { diff --git a/src/vbplay.rs b/src/vbplay.rs index 956427a..56a7bf5 100644 --- a/src/vbplay.rs +++ b/src/vbplay.rs @@ -35,26 +35,40 @@ fn select_device(host: &Host, i: usize) -> Option { host.output_devices().unwrap().nth(i) } -fn find_virtual_audio_cable() -> Option { +fn select_device_by_pattern(pattern: &str) -> Option { let host = cpal::default_host(); let devices = list_devices(&host); info!("Devices: {:?}", devices); - let pattern = ".*VB-Audio Virtual Cable.*"; if let Some(index) = find_device_index_regex(devices, pattern) { - info!("Selecting Device: \"{}\" at index {:?}", pattern, index); + info!( + "Selecting Device from pattern: \"{}\" at index {:?}", + pattern, index + ); return select_device(&host, index); } return None; } +fn select_device_by_id(index: usize) -> Option { + let host = cpal::default_host(); + return select_device(&host, index); +} + pub type AudioThread = Sender; -pub fn audio_thread() -> AudioThread { +pub fn audio_thread(select_device: DeviceSelection) -> AudioThread { let (tx, rx) = mpsc::channel(); thread::spawn(move || { - let device = find_virtual_audio_cable().unwrap(); + let device = match select_device { + DeviceSelection::SelectFirst => select_device_by_id(0).unwrap(), + DeviceSelection::SelectById(id) => select_device_by_id(id).unwrap(), + DeviceSelection::FindByPattern(pattern) => { + select_device_by_pattern(pattern.as_ref()).unwrap() + } + }; + // Create a rodio Sink connected to our device let (_stream, stream_handle) = rodio::OutputStream::try_from_device(&device).unwrap(); let sink = rodio::Sink::try_new(&stream_handle).unwrap();