wip: playing symphonia-loaded samples

This commit is contained in:
🪞👃🪞 2024-07-23 18:28:20 +03:00
parent b29d3ecda2
commit bdd3e31403
5 changed files with 36 additions and 26 deletions

View file

@ -17,6 +17,7 @@ pub struct AddSampleModal {
cursor: usize,
offset: usize,
sample: Arc<RwLock<Sample>>,
voices: Arc<RwLock<Vec<Voice>>>,
_search: Option<String>,
}
@ -65,7 +66,10 @@ handle!(AddSampleModal |self,e|{
});
impl AddSampleModal {
pub fn new (sample: &Arc<RwLock<Sample>>) -> Usually<Self> {
pub fn new (
sample: &Arc<RwLock<Sample>>,
voices: &Arc<RwLock<Vec<Voice>>>
) -> Usually<Self> {
let dir = std::env::current_dir()?;
let (subdirs, files) = scan(&dir)?;
Ok(Self {
@ -76,6 +80,7 @@ impl AddSampleModal {
cursor: 0,
offset: 0,
sample: sample.clone(),
voices: voices.clone(),
_search: None
})
}
@ -93,6 +98,12 @@ impl AddSampleModal {
}
fn try_preview (&mut self) -> Usually<()> {
if let Some(path) = self.cursor_file() {
if let Ok(sample) = Sample::from_file(&path) {
*self.sample.write().unwrap() = sample;
self.voices.write().unwrap().push(
Sample::play(&self.sample, 0, &u7::from(100u8))
);
}
//load_sample(&path)?;
//let src = std::fs::File::open(&path)?;
//let mss = MediaSourceStream::new(Box::new(src), Default::default());
@ -198,9 +209,9 @@ fn scan (dir: &PathBuf) -> Usually<(Vec<OsString>, Vec<OsString>)> {
}
impl Sample {
fn from_file (path: &PathBuf) -> Usually<Arc<RwLock<Self>>> {
fn from_file (path: &PathBuf) -> Usually<Self> {
let mut sample = Self::default();
sample.name = path.to_string_lossy().into();
sample.name = path.file_name().unwrap().to_string_lossy().into();
// Use file extension if present
let mut hint = Hint::new();
if let Some(ext) = path.extension() {
@ -256,6 +267,7 @@ impl Sample {
Err(err) => return Err(err.into()),
};
};
Ok(Arc::new(RwLock::new(sample)))
sample.end = sample.channels.iter().fold(0, |l, c|l + c.len());
Ok(sample)
}
}