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::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<String> = 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,

View file

@ -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<Mutex<f32>> {
pub fn add_channel(&mut self, filename: &PathBuf) -> Arc<Mutex<f32>> {
let new = SoundChannel::new(filename);
let volume_sync = new.volume_sync.clone();
self.channels.push(new);