code: device selection becomes an option to audio_thread
This commit is contained in:
parent
35c979e30c
commit
09ed01b687
@ -9,7 +9,7 @@ use log::{debug, info};
|
|||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use vbplay::AudioThread;
|
use vbplay::{AudioThread, DeviceSelection};
|
||||||
|
|
||||||
mod soundclips;
|
mod soundclips;
|
||||||
mod state;
|
mod state;
|
||||||
@ -21,7 +21,9 @@ async fn main() {
|
|||||||
jinja.set_loader(path_loader("templates"));
|
jinja.set_loader(path_loader("templates"));
|
||||||
let template_engine = Engine::from(jinja);
|
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<Mutex<AudioThread>> = Arc::new(Mutex::new(audio));
|
let audio: Arc<Mutex<AudioThread>> = Arc::new(Mutex::new(audio));
|
||||||
|
|
||||||
let app_state = AppState {
|
let app_state = AppState {
|
||||||
@ -29,6 +31,7 @@ async fn main() {
|
|||||||
clips: soundclips::scan_directory_for_clips("E:/sounds/soundboard/all", &["mp3"])
|
clips: soundclips::scan_directory_for_clips("E:/sounds/soundboard/all", &["mp3"])
|
||||||
.expect("No Soundclips found."),
|
.expect("No Soundclips found."),
|
||||||
player: audio,
|
player: audio,
|
||||||
|
playback: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Set the address to run our application on
|
// Set the address to run our application on
|
||||||
|
@ -12,6 +12,7 @@ pub struct AppState {
|
|||||||
pub engine: TemplateEngine,
|
pub engine: TemplateEngine,
|
||||||
pub clips: Vec<SoundClip>,
|
pub clips: Vec<SoundClip>,
|
||||||
pub player: Arc<Mutex<AudioThread>>,
|
pub player: Arc<Mutex<AudioThread>>,
|
||||||
|
pub playback: Option<Arc<Mutex<AudioThread>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromRef<AppState> for TemplateEngine {
|
impl FromRef<AppState> for TemplateEngine {
|
||||||
|
@ -35,26 +35,40 @@ fn select_device(host: &Host, i: usize) -> Option<Device> {
|
|||||||
host.output_devices().unwrap().nth(i)
|
host.output_devices().unwrap().nth(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_virtual_audio_cable() -> Option<Device> {
|
fn select_device_by_pattern(pattern: &str) -> Option<Device> {
|
||||||
let host = cpal::default_host();
|
let host = cpal::default_host();
|
||||||
let devices = list_devices(&host);
|
let devices = list_devices(&host);
|
||||||
info!("Devices: {:?}", devices);
|
info!("Devices: {:?}", devices);
|
||||||
|
|
||||||
let pattern = ".*VB-Audio Virtual Cable.*";
|
|
||||||
if let Some(index) = find_device_index_regex(devices, pattern) {
|
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 select_device(&host, index);
|
||||||
}
|
}
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn select_device_by_id(index: usize) -> Option<Device> {
|
||||||
|
let host = cpal::default_host();
|
||||||
|
return select_device(&host, index);
|
||||||
|
}
|
||||||
|
|
||||||
pub type AudioThread = Sender<Command>;
|
pub type AudioThread = Sender<Command>;
|
||||||
|
|
||||||
pub fn audio_thread() -> AudioThread {
|
pub fn audio_thread(select_device: DeviceSelection) -> AudioThread {
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
thread::spawn(move || {
|
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
|
// Create a rodio Sink connected to our device
|
||||||
let (_stream, stream_handle) = rodio::OutputStream::try_from_device(&device).unwrap();
|
let (_stream, stream_handle) = rodio::OutputStream::try_from_device(&device).unwrap();
|
||||||
let sink = rodio::Sink::try_new(&stream_handle).unwrap();
|
let sink = rodio::Sink::try_new(&stream_handle).unwrap();
|
||||||
|
Loading…
Reference in New Issue
Block a user