diff --git a/src/main.rs b/src/main.rs index bf1a4a4..05ff883 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use crossterm::event::{self, Event, KeyCode}; use crossterm::terminal::{self, Clear, ClearType}; use crossterm::ExecutableCommand; use rodio::{source::Source, OutputStream, OutputStreamHandle}; +use std::fs; use std::io::stdout; use std::sync::{Arc, Mutex}; use std::time::Duration; @@ -68,18 +69,17 @@ impl App { } fn run(&mut self) { - // TODO scan directory instead - let files: Vec = vec![ - "sound/rain.mp3".into(), - "sound/thunder.mp3".into(), - "sound/wind.mp3".into(), - ]; + let files: Vec<_> = fs::read_dir("sound") + .unwrap() + .flatten() + .filter(|f| f.file_type().unwrap().is_file()) + .collect(); let mut snoud = Snoud::new(); - for filename in files { - let internal_volume = snoud.add_channel(&filename); + for file in files { + let internal_volume = snoud.add_channel(&file.path()); let ui_channel = UIChannel { - name: filename, + name: file.file_name().to_string_lossy().into(), volume: 100, internal_volume, muted: false, @@ -108,7 +108,7 @@ impl App { println!("Snoud - ambient sound player\n\r"); for (i, channel) in self.channels.iter().enumerate() { println!( - "{selection}{name}:\r\n {volume:3.0}% {status:-<21}\r\n", + "{selection} {name}:\r\n {volume:3.0}% {status:-<21}\r\n", selection = if i == self.selected { ">" } else { " " }, name = &channel.name, volume = channel.volume, diff --git a/src/sound.rs b/src/sound.rs index e62a009..ca7eba1 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -1,6 +1,7 @@ use rodio::source::{Repeat, Source}; use rodio::Decoder; use std::fs::File; +use std::path::PathBuf; use std::sync::{Arc, Mutex}; use std::time::Duration; @@ -19,7 +20,7 @@ struct SoundChannel { } impl SoundChannel { - fn new(name: &str) -> Self { + fn new(name: &PathBuf) -> Self { let file = File::open(name).expect("File not found"); let source = Decoder::new(file) .expect("Could not decode file") @@ -71,8 +72,7 @@ impl Source for Snoud { } impl Snoud { - pub fn new(/* filenames: &[String] */) -> Self { - // let channels = filenames.iter().map(SoundChannel::new).collect(); + pub fn new() -> Self { let sample_rate = 48000; Self { sample_rate, @@ -82,7 +82,7 @@ impl Snoud { } } - pub fn add_channel(&mut self, filename: &str) -> Arc> { + pub fn add_channel(&mut self, filename: &PathBuf) -> Arc> { let new = SoundChannel::new(filename); let volume_sync = new.volume_sync.clone(); self.channels.push(new);