Feat: background head-preload for sample libraries
This commit is contained in:
35
src/init.rs
35
src/init.rs
@@ -121,6 +121,10 @@ pub fn init(args: InitArgs) -> Init {
|
||||
app.audio.config.sample_count += index.len();
|
||||
initial_samples.extend(index);
|
||||
}
|
||||
let preload_entries: Vec<(String, std::path::PathBuf)> = initial_samples
|
||||
.iter()
|
||||
.map(|e| (e.name.clone(), e.path.clone()))
|
||||
.collect();
|
||||
|
||||
#[cfg(feature = "desktop")]
|
||||
let mouse_x = Arc::new(AtomicU32::new(0.5_f32.to_bits()));
|
||||
@@ -168,11 +172,23 @@ pub fn init(args: InitArgs) -> Init {
|
||||
initial_samples,
|
||||
Arc::clone(&audio_sample_pos),
|
||||
) {
|
||||
Ok((s, info, analysis)) => {
|
||||
Ok((s, info, analysis, registry)) => {
|
||||
app.audio.config.sample_rate = info.sample_rate;
|
||||
app.audio.config.host_name = info.host_name;
|
||||
app.audio.config.channels = info.channels;
|
||||
sample_rate_shared.store(info.sample_rate as u32, Ordering::Relaxed);
|
||||
app.audio.sample_registry = Some(Arc::clone(®istry));
|
||||
|
||||
if !preload_entries.is_empty() {
|
||||
let sr = info.sample_rate;
|
||||
std::thread::Builder::new()
|
||||
.name("sample-preload".into())
|
||||
.spawn(move || {
|
||||
preload_sample_heads(preload_entries, sr, ®istry);
|
||||
})
|
||||
.expect("failed to spawn preload thread");
|
||||
}
|
||||
|
||||
(Some(s), Some(analysis))
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -208,3 +224,20 @@ pub fn init(args: InitArgs) -> Init {
|
||||
mouse_down,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn preload_sample_heads(
|
||||
entries: Vec<(String, std::path::PathBuf)>,
|
||||
target_sr: f32,
|
||||
registry: &doux::SampleRegistry,
|
||||
) {
|
||||
let mut batch = Vec::with_capacity(entries.len());
|
||||
for (name, path) in &entries {
|
||||
match doux::sampling::decode_sample_head(path, target_sr) {
|
||||
Ok(data) => batch.push((name.clone(), Arc::new(data))),
|
||||
Err(e) => eprintln!("preload {name}: {e}"),
|
||||
}
|
||||
}
|
||||
if !batch.is_empty() {
|
||||
registry.insert_batch(batch);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user