Stream audio to spectrograph shader too
This commit is contained in:
@@ -272,24 +272,25 @@ impl Engine {
|
||||
// Forward chunk generation events from background threads
|
||||
while let Ok(event) = self.chunk_generation_rx.try_recv() {
|
||||
match event {
|
||||
AudioEvent::WaveformDecodeComplete { pool_index, samples } => {
|
||||
// Update pool entry with decoded waveform samples
|
||||
AudioEvent::WaveformDecodeComplete { pool_index, samples, decoded_frames: df, total_frames: _tf } => {
|
||||
// Update pool entry and forward samples directly to UI
|
||||
if let Some(file) = self.audio_pool.get_file_mut(pool_index) {
|
||||
let total = file.frames;
|
||||
let sr = file.sample_rate;
|
||||
let ch = file.channels;
|
||||
if let crate::audio::pool::AudioStorage::Compressed {
|
||||
ref mut decoded_for_waveform,
|
||||
ref mut decoded_frames,
|
||||
..
|
||||
} = file.storage {
|
||||
eprintln!("[ENGINE] Waveform decode complete for pool {}: {} samples", pool_index, samples.len());
|
||||
*decoded_for_waveform = samples;
|
||||
*decoded_frames = total;
|
||||
*decoded_for_waveform = samples.clone();
|
||||
*decoded_frames = df;
|
||||
}
|
||||
// Notify frontend that waveform data is ready
|
||||
// Send samples inline — UI won't need to query back
|
||||
let _ = self.event_tx.push(AudioEvent::AudioDecodeProgress {
|
||||
pool_index,
|
||||
decoded_frames: total,
|
||||
total_frames: total,
|
||||
samples,
|
||||
sample_rate: sr,
|
||||
channels: ch,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1789,26 +1790,25 @@ impl Engine {
|
||||
});
|
||||
}
|
||||
|
||||
// Spawn background thread to decode full file for waveform display
|
||||
// Spawn background thread to decode file progressively for waveform display
|
||||
let bg_tx = self.chunk_generation_tx.clone();
|
||||
let bg_path = path.to_path_buf();
|
||||
let bg_total_frames = total_frames;
|
||||
let _ = std::thread::Builder::new()
|
||||
.name(format!("waveform-decode-{}", idx))
|
||||
.spawn(move || {
|
||||
eprintln!("[WAVEFORM DECODE] Starting full decode of {:?}", bg_path);
|
||||
match crate::io::AudioFile::load(&bg_path) {
|
||||
Ok(loaded) => {
|
||||
eprintln!("[WAVEFORM DECODE] Complete: {} frames, {} channels",
|
||||
loaded.frames, loaded.channels);
|
||||
crate::io::AudioFile::decode_progressive(
|
||||
&bg_path,
|
||||
bg_total_frames,
|
||||
|audio_data, decoded_frames, total| {
|
||||
let _ = bg_tx.send(AudioEvent::WaveformDecodeComplete {
|
||||
pool_index: idx,
|
||||
samples: loaded.data,
|
||||
samples: audio_data.to_vec(),
|
||||
decoded_frames,
|
||||
total_frames: total,
|
||||
});
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("[WAVEFORM DECODE] Failed to decode {:?}: {}", bg_path, e);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
idx
|
||||
}
|
||||
|
||||
@@ -274,18 +274,22 @@ pub enum AudioEvent {
|
||||
},
|
||||
|
||||
/// Progressive decode progress for a compressed audio file's waveform data.
|
||||
/// The UI can use this to update waveform display incrementally.
|
||||
/// Carries the samples inline so the UI doesn't need to query back.
|
||||
AudioDecodeProgress {
|
||||
pool_index: usize,
|
||||
decoded_frames: u64,
|
||||
total_frames: u64,
|
||||
samples: Vec<f32>,
|
||||
sample_rate: u32,
|
||||
channels: u32,
|
||||
},
|
||||
|
||||
/// Background waveform decode completed for a compressed audio file.
|
||||
/// Background waveform decode progress/completion for a compressed audio file.
|
||||
/// Internal event — consumed by the engine to update the pool, not forwarded to UI.
|
||||
/// `decoded_frames` < `total_frames` means partial; equal means complete.
|
||||
WaveformDecodeComplete {
|
||||
pool_index: usize,
|
||||
samples: Vec<f32>,
|
||||
decoded_frames: u64,
|
||||
total_frames: u64,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -338,6 +338,122 @@ impl AudioFile {
|
||||
})
|
||||
}
|
||||
|
||||
/// Decode a compressed audio file progressively, calling `on_progress` with
|
||||
/// partial data snapshots so the UI can display waveforms as they decode.
|
||||
/// Sends updates roughly every 2 seconds of decoded audio.
|
||||
pub fn decode_progressive<P: AsRef<Path>, F>(path: P, total_frames: u64, on_progress: F)
|
||||
where
|
||||
F: Fn(&[f32], u64, u64),
|
||||
{
|
||||
let path = path.as_ref();
|
||||
|
||||
let file = match std::fs::File::open(path) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
eprintln!("[WAVEFORM DECODE] Failed to open {:?}: {}", path, e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mss = MediaSourceStream::new(Box::new(file), Default::default());
|
||||
|
||||
let mut hint = Hint::new();
|
||||
if let Some(extension) = path.extension() {
|
||||
if let Some(ext_str) = extension.to_str() {
|
||||
hint.with_extension(ext_str);
|
||||
}
|
||||
}
|
||||
|
||||
let probed = match symphonia::default::get_probe()
|
||||
.format(&hint, mss, &FormatOptions::default(), &MetadataOptions::default())
|
||||
{
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
eprintln!("[WAVEFORM DECODE] Failed to probe {:?}: {}", path, e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mut format = probed.format;
|
||||
|
||||
let track = match format.tracks().iter()
|
||||
.find(|t| t.codec_params.codec != symphonia::core::codecs::CODEC_TYPE_NULL)
|
||||
{
|
||||
Some(t) => t,
|
||||
None => {
|
||||
eprintln!("[WAVEFORM DECODE] No audio tracks in {:?}", path);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let track_id = track.id;
|
||||
let channels = track.codec_params.channels
|
||||
.map(|c| c.count() as u32)
|
||||
.unwrap_or(2);
|
||||
let sample_rate = track.codec_params.sample_rate.unwrap_or(44100);
|
||||
|
||||
let mut decoder = match symphonia::default::get_codecs()
|
||||
.make(&track.codec_params, &DecoderOptions::default())
|
||||
{
|
||||
Ok(d) => d,
|
||||
Err(e) => {
|
||||
eprintln!("[WAVEFORM DECODE] Failed to create decoder for {:?}: {}", path, e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mut audio_data = Vec::new();
|
||||
let mut sample_buf = None;
|
||||
// Send a progress update roughly every 2 seconds of audio
|
||||
// Send first update quickly (0.25s), then every 2s of audio
|
||||
let initial_interval = (sample_rate as usize * channels as usize) / 4;
|
||||
let steady_interval = (sample_rate as usize * channels as usize) * 2;
|
||||
let mut sent_first = false;
|
||||
let mut last_update_len = 0usize;
|
||||
|
||||
loop {
|
||||
let packet = match format.next_packet() {
|
||||
Ok(packet) => packet,
|
||||
Err(Error::IoError(e)) if e.kind() == std::io::ErrorKind::UnexpectedEof => break,
|
||||
Err(Error::ResetRequired) => break,
|
||||
Err(_) => break,
|
||||
};
|
||||
|
||||
if packet.track_id() != track_id {
|
||||
continue;
|
||||
}
|
||||
|
||||
match decoder.decode(&packet) {
|
||||
Ok(decoded) => {
|
||||
if sample_buf.is_none() {
|
||||
let spec = *decoded.spec();
|
||||
let duration = decoded.capacity() as u64;
|
||||
sample_buf = Some(SampleBuffer::<f32>::new(duration, spec));
|
||||
}
|
||||
if let Some(ref mut buf) = sample_buf {
|
||||
buf.copy_interleaved_ref(decoded);
|
||||
audio_data.extend_from_slice(buf.samples());
|
||||
}
|
||||
|
||||
// Send progressive update (fast initial, then periodic)
|
||||
let interval = if sent_first { steady_interval } else { initial_interval };
|
||||
if audio_data.len() - last_update_len >= interval {
|
||||
let decoded_frames = audio_data.len() as u64 / channels as u64;
|
||||
on_progress(&audio_data, decoded_frames, total_frames);
|
||||
last_update_len = audio_data.len();
|
||||
sent_first = true;
|
||||
}
|
||||
}
|
||||
Err(Error::DecodeError(_)) => continue,
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
|
||||
// Final update with all data
|
||||
let decoded_frames = audio_data.len() as u64 / channels as u64;
|
||||
on_progress(&audio_data, decoded_frames, decoded_frames.max(total_frames));
|
||||
}
|
||||
|
||||
/// Calculate the duration of the audio file in seconds
|
||||
pub fn duration(&self) -> f64 {
|
||||
self.frames as f64 / self.sample_rate as f64
|
||||
|
||||
Reference in New Issue
Block a user