scan sound directory and load everything in it

This commit is contained in:
Crispy 2023-01-21 00:48:24 +01:00
parent 83ba461749
commit cb438c1887
2 changed files with 14 additions and 14 deletions

View file

@ -3,6 +3,7 @@ use crossterm::event::{self, Event, KeyCode};
use crossterm::terminal::{self, Clear, ClearType}; use crossterm::terminal::{self, Clear, ClearType};
use crossterm::ExecutableCommand; use crossterm::ExecutableCommand;
use rodio::{source::Source, OutputStream, OutputStreamHandle}; use rodio::{source::Source, OutputStream, OutputStreamHandle};
use std::fs;
use std::io::stdout; use std::io::stdout;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
@ -68,18 +69,17 @@ impl App {
} }
fn run(&mut self) { fn run(&mut self) {
// TODO scan directory instead let files: Vec<_> = fs::read_dir("sound")
let files: Vec<String> = vec![ .unwrap()
"sound/rain.mp3".into(), .flatten()
"sound/thunder.mp3".into(), .filter(|f| f.file_type().unwrap().is_file())
"sound/wind.mp3".into(), .collect();
];
let mut snoud = Snoud::new(); let mut snoud = Snoud::new();
for filename in files { for file in files {
let internal_volume = snoud.add_channel(&filename); let internal_volume = snoud.add_channel(&file.path());
let ui_channel = UIChannel { let ui_channel = UIChannel {
name: filename, name: file.file_name().to_string_lossy().into(),
volume: 100, volume: 100,
internal_volume, internal_volume,
muted: false, muted: false,
@ -108,7 +108,7 @@ impl App {
println!("Snoud - ambient sound player\n\r"); println!("Snoud - ambient sound player\n\r");
for (i, channel) in self.channels.iter().enumerate() { for (i, channel) in self.channels.iter().enumerate() {
println!( 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 { " " }, selection = if i == self.selected { ">" } else { " " },
name = &channel.name, name = &channel.name,
volume = channel.volume, volume = channel.volume,

View file

@ -1,6 +1,7 @@
use rodio::source::{Repeat, Source}; use rodio::source::{Repeat, Source};
use rodio::Decoder; use rodio::Decoder;
use std::fs::File; use std::fs::File;
use std::path::PathBuf;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
@ -19,7 +20,7 @@ struct SoundChannel {
} }
impl SoundChannel { impl SoundChannel {
fn new(name: &str) -> Self { fn new(name: &PathBuf) -> Self {
let file = File::open(name).expect("File not found"); let file = File::open(name).expect("File not found");
let source = Decoder::new(file) let source = Decoder::new(file)
.expect("Could not decode file") .expect("Could not decode file")
@ -71,8 +72,7 @@ impl Source for Snoud {
} }
impl Snoud { impl Snoud {
pub fn new(/* filenames: &[String] */) -> Self { pub fn new() -> Self {
// let channels = filenames.iter().map(SoundChannel::new).collect();
let sample_rate = 48000; let sample_rate = 48000;
Self { Self {
sample_rate, sample_rate,
@ -82,7 +82,7 @@ impl Snoud {
} }
} }
pub fn add_channel(&mut self, filename: &str) -> Arc<Mutex<f32>> { pub fn add_channel(&mut self, filename: &PathBuf) -> Arc<Mutex<f32>> {
let new = SoundChannel::new(filename); let new = SoundChannel::new(filename);
let volume_sync = new.volume_sync.clone(); let volume_sync = new.volume_sync.clone();
self.channels.push(new); self.channels.push(new);