⚡ A sound file whose header states no length is decoded for its length once per process, not on every place, keep or append
Browse files at this commit0f9a37188fa655e6c79f0c9172769fd82d94fdedParent: 1e406a81 changed file +12 -1
- crates/engine/src/audio.rs +12 -1
crates/engine/src/audio.rsmodified+12 -1
| @@ -1535,6 +1535,13 @@ impl AudioSession { | |||
| 1535 | return Ok(Some(total as f64 / rate)); | 1535 | return Ok(Some(total as f64 / rate)); |
| 1536 | } | 1536 | } |
| 1537 | drop(track); | 1537 | drop(track); |
| 1538 | + type Key = (PathBuf, usize, Option<std::time::SystemTime>, u64); | ||
| 1539 | + static KNOWN: std::sync::Mutex<Option<std::collections::HashMap<Key, f64>>> = std::sync::Mutex::new(None); | ||
| 1540 | + let meta = std::fs::metadata(path).ok(); | ||
| 1541 | + let key: Key = (path.to_path_buf(), stream, meta.as_ref().and_then(|m| m.modified().ok()), meta.map_or(0, |m| m.len())); | ||
| 1542 | + if let Some(secs) = KNOWN.lock().ok().and_then(|k| k.as_ref()?.get(&key).copied()) { | ||
| 1543 | + return Ok(Some(secs)); | ||
| 1544 | + } | ||
| 1538 | let Some((meta, rx)) = Self::open(path)? else { | 1545 | let Some((meta, rx)) = Self::open(path)? else { |
| 1539 | return Ok(None); | 1546 | return Ok(None); |
| 1540 | }; | 1547 | }; |
| @@ -1542,7 +1549,11 @@ impl AudioSession { | |||
| 1542 | let last = rx.into_iter().fold(0, |_, chunk| { | 1549 | let last = rx.into_iter().fold(0, |_, chunk| { |
| 1543 | chunk.start_sample + (chunk.samples.len() / channels) as u64 | 1550 | chunk.start_sample + (chunk.samples.len() / channels) as u64 |
| 1544 | }); | 1551 | }); |
| 1545 | - Ok(Some(last as f64 / f64::from(meta.sample_rate.max(1)))) | 1552 | + let secs = last as f64 / f64::from(meta.sample_rate.max(1)); |
| 1553 | + if let Ok(mut k) = KNOWN.lock() { | ||
| 1554 | + k.get_or_insert_with(Default::default).insert(key, secs); | ||
| 1555 | + } | ||
| 1556 | + Ok(Some(secs)) | ||
| 1546 | } | 1557 | } |
| 1547 | 1558 | ||
| 1548 | /// The raw AAC packets covering `segs` — the same half-open source-second | 1559 | /// The raw AAC packets covering `segs` — the same half-open source-second |