diff --git a/crates/scraft-format/src/lib.rs b/crates/scraft-format/src/lib.rs index c54d7f934..22d69b953 100644 --- a/crates/scraft-format/src/lib.rs +++ b/crates/scraft-format/src/lib.rs @@ -12,6 +12,7 @@ mod clip; mod ids; mod markers; mod mixer; +mod room_tone; mod session; mod source; mod track; @@ -27,6 +28,7 @@ pub use markers::{Group, MarkerKind, MemoryLocation}; pub use mixer::{ Bus, ChannelFormat, INSERT_SLOTS, Insert, Mixer, OutputPath, Route, SEND_SLOTS, SendSlot, Speaker, SurroundPan, fader_db_to_pos, fader_pos_to_db, }; +pub use room_tone::RoomTone; pub use session::{ AutoName, BitDepthSetting, EditMode, EditState, FEET_FRAMES_START, MAX_COMMENT_CHARS, MAX_NAME_CHARS, SESSION_EXTENSION, Session, SessionError, Tool, TrackView, ZoomState, ZoomToggle, truncate_chars, diff --git a/crates/scraft-format/src/room_tone.rs b/crates/scraft-format/src/room_tone.rs new file mode 100644 index 000000000..0634c4092 --- /dev/null +++ b/crates/scraft-format/src/room_tone.rs @@ -0,0 +1,52 @@ +//! The session's room tone: a stretch of recorded silence used to fill gaps instead of digital +//! silence. + +use crate::SourceId; +use crate::soundcraft_time::{MAX_POSITION, Samples}; + +/// A window onto an audio source chosen as the session's room tone. Filling a gap places clips +/// that play this window, so the fill stays ordinary, editable clips on the timeline. +#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)] +pub struct RoomTone { + pub source: SourceId, + /// Where the window starts in the source (samples). + pub offset: Samples, + /// Window length (samples, > 0). + pub length: Samples, + /// Clip gain the fill clips get (dB), taken from the clip the tone was chosen from. + #[serde(default)] + pub gain_db: f32, +} + +impl RoomTone { + /// The window repaired for a source of `frames` frames, or `None` when nothing playable is left. + pub fn sanitized(mut self, frames: u64) -> Option { + let frames = Samples::try_from(frames).unwrap_or(MAX_POSITION).min(MAX_POSITION); + if self.offset < 0 || self.offset >= frames || self.length <= 0 { + return None; + } + self.length = self.length.min(frames - self.offset); + self.gain_db = if self.gain_db.is_finite() { self.gain_db.clamp(-144.0, 36.0) } else { 0.0 }; + Some(self) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn tone(offset: Samples, length: Samples) -> RoomTone { + RoomTone { source: SourceId(1), offset, length, gain_db: 0.0 } + } + + #[test] + fn a_window_past_the_source_is_cut_to_fit_or_dropped() { + assert_eq!(tone(100, 50).sanitized(1000), Some(tone(100, 50))); + assert_eq!(tone(900, 500).sanitized(1000), Some(tone(900, 100))); + assert_eq!(tone(1000, 10).sanitized(1000), None); + assert_eq!(tone(-1, 10).sanitized(1000), None); + assert_eq!(tone(0, 0).sanitized(1000), None); + let loud = RoomTone { gain_db: f32::NAN, ..tone(0, 10) }; + assert_eq!(loud.sanitized(1000).map(|t| t.gain_db), Some(0.0)); + } +} diff --git a/crates/scraft-format/src/session.rs b/crates/scraft-format/src/session.rs index 021cab8bc..9ba090958 100644 --- a/crates/scraft-format/src/session.rs +++ b/crates/scraft-format/src/session.rs @@ -378,6 +378,9 @@ pub struct Session { /// Performance). Independent of undo so a restore survives later edits. #[serde(default)] pub midi_originals: std::collections::BTreeMap, + /// The recorded silence that `edit.room_tone_fill` fills gaps with. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub room_tone: Option, #[serde(skip)] pub pool: SourcePool, /// The folder relative source paths point into: where the session was opened or last saved. @@ -427,6 +430,7 @@ impl Session { next_id: 1, comments: String::new(), midi_originals: std::collections::BTreeMap::new(), + room_tone: None, pool: SourcePool::default(), folder: None, }; @@ -867,6 +871,8 @@ impl Session { self.outputs.iter_mut().for_each(|o| truncate_chars(&mut o.name, MAX_NAME_CHARS)); self.groups.iter_mut().for_each(|g| truncate_chars(&mut g.name, MAX_NAME_CHARS)); self.sources.iter_mut().for_each(|s| truncate_chars(&mut s.name, MAX_NAME_CHARS)); + // A room tone must play part of a source the session still lists. + self.room_tone = self.room_tone.and_then(|t| self.sources.iter().find(|s| s.id == t.source).and_then(|s| t.sanitized(s.frames))); // Drop performances whose clip no longer exists anywhere (any playlist). let clip_ids: std::collections::BTreeSet = self.tracks.iter().flat_map(|t| t.playlists.iter().flat_map(|p| p.clips.iter().map(|c| c.id.0))).collect(); @@ -1306,6 +1312,30 @@ mod tests { assert!(s.next_id > t.0); } + #[test] + fn a_loaded_room_tone_must_play_a_listed_source() { + let mut s = Session::default(); + let id = SourceId(s.alloc()); + s.sources.push(crate::Source { + id, + name: "Tone".into(), + path: "Tone.wav".into(), + channels: 1, + frames: 1000, + sample_rate: 48_000, + format: crate::FileFormat::Wav, + time_reference: 0, + unsaved: false, + }); + s.room_tone = Some(crate::RoomTone { source: id, offset: 400, length: 5000, gain_db: -3.0 }); + let loaded = Session::from_json(&s.to_json().unwrap()).unwrap(); + assert_eq!(loaded.room_tone, Some(crate::RoomTone { source: id, offset: 400, length: 600, gain_db: -3.0 }), "cut to the source"); + s.room_tone = Some(crate::RoomTone { source: SourceId(999), offset: 0, length: 10, gain_db: 0.0 }); + assert_eq!(Session::from_json(&s.to_json().unwrap()).unwrap().room_tone, None, "an unknown source is dropped"); + s.room_tone = None; + assert!(!s.to_json().unwrap().contains("room_tone"), "sessions without one are written as before"); + } + #[test] fn loaded_automation_is_clamped_to_its_range() { // #884: a session file can carry points outside a lane's range; load clamps them.