record: keep a track per audio source and let the review mute them

The recorder put every source on one mixer track, so the file had one audio
stream and the review screen could not tell the game's sound from the
microphone or from Discord.

The daemon now gives each source a track: 1 for the game, 2 for the
microphone, then one per application in the settings, up to OBS's six. Every
track above the first gets an audio encoder and the output is told which
tracks to write, which is what libobs's muxer reads to lay the streams out in
track order. The timeline's new `audio_tracks` names what each stream holds,
with empty names for the tracks no source fed.

The app opens one player per stream, copied out of the clip into the audio
cache, and the transport carries a toggle per channel. A channel that is off
is neither heard nor kept: clip and highlight exports mix only the tracks
left on. Recordings made before this change have one stream and play as they
did.
This commit is contained in:
vhaudiquet committed 2026-11-19 18:37:20 +01:00
1 parent 4f2ee4256a
commit eabc3780c9
13 files changed
+545 -46

No files matched your search

+177 -28
View File
@@ -18,11 +18,8 @@ pub struct Review {
/// Set while that file is on its way to the screen.
pub loading: bool,
pub player: Option<Player>,
/// The clip's soundtrack, played by its own player. Rune's video player asks Media Foundation
/// for pictures and sound from one reader, and that reader stamps the sound from its own
/// clock instead of the clip's, so the sound never lines up with the clock and plays silent.
/// An audio-only player reads the same file and needs no video, so its sound starts at zero.
pub sound: Option<Player>,
/// The clip's audio tracks, one player each, so the channels are heard apart.
pub sounds: Vec<Channel>,
/// The picture time the soundtrack was last put back to, to spot a window that stopped drawing.
steady_at: f64,
pub time: f64,
@@ -38,10 +35,12 @@ pub struct Review {
pub volume: f32,
pub muted: bool,
pub error: Option<String>,
/// Why the channels could not be opened, when the clip has more than one.
pub channel_error: Option<String>,
}
impl Review {
/// The video player only draws; the soundtrack player carries the gain and the mute.
/// The video player only draws; the audio players carry the gain and the mute.
fn mute_video(player: &Player) {
player.set_muted(true);
}
@@ -52,21 +51,86 @@ impl Review {
if self.volume > 0.0 {
self.muted = false;
}
if let Some(sound) = &self.sound {
sound.set_volume(self.volume);
sound.set_muted(self.muted);
for channel in &self.sounds {
channel.sound.set_volume(self.volume);
channel.sound.set_muted(!channel.heard(self.muted));
}
}
pub fn toggle_muted(&mut self) {
self.muted = !self.muted;
if let Some(sound) = &self.sound {
sound.set_muted(self.muted);
for channel in &self.sounds {
channel.sound.set_muted(!channel.heard(self.muted));
}
}
/// Start the soundtrack's player, at the place the viewer is watching.
fn open_sound(&mut self, path: &str) {
/// Turn one track on or off, and make the change audible now rather than at the next seek.
pub fn toggle_channel(&mut self, index: usize) {
let Some(channel) = self.sounds.get_mut(index) else {
return;
};
channel.on = !channel.on;
channel.sound.set_muted(!channel.heard(self.muted));
}
/// The audio streams a clip or highlight keeps: the tracks the viewer left on.
pub fn kept_tracks(&self) -> Vec<usize> {
self.sounds
.iter()
.filter(|channel| channel.on)
.map(|channel| channel.stream)
.collect()
}
/// Start the soundtrack's players, at the place the viewer is watching.
pub fn open_sound(&mut self, path: &str) {
self.sounds.clear();
self.channel_error = None;
let labels = self
.game
.as_ref()
.map(|game| game.audio_tracks.clone())
.unwrap_or_default();
if labels.len() < 2 {
// One track, or a recording made before the recorder split them: the file's own
// sound is the whole mix, and an audio player reads it as it is.
let label = labels
.first()
.cloned()
.unwrap_or_else(|| "All audio".to_string());
let channel = self.channel(0, label, path);
self.sounds.push(channel);
return;
}
let stem = self
.game
.as_ref()
.and_then(|game| game.video_file.clone())
.or_else(|| {
std::path::Path::new(path)
.file_name()
.map(|name| name.to_string_lossy().to_string())
})
.unwrap_or_default();
let stem = stem.rsplit_once('.').map(|(name, _)| name).unwrap_or(&stem);
let files = match crate::export::extract_tracks(path, stem, labels.len()) {
Ok(files) => files,
Err(e) => {
self.channel_error = Some(e);
return;
}
};
for (stream, file) in files.iter().enumerate() {
// A track no source fed is silent in the file; it gets no toggle.
let Some(label) = labels.get(stream).filter(|label| !label.trim().is_empty()) else {
continue;
};
let channel = self.channel(stream, label.clone(), &file.to_string_lossy());
self.sounds.push(channel);
}
}
fn channel(&self, stream: usize, label: String, path: &str) -> Channel {
let sound = Player::audio(path.to_string());
sound.set_looping(false);
sound.set_volume(self.volume);
@@ -79,7 +143,12 @@ impl Review {
if playing {
sound.play();
}
self.sound = Some(sound);
Channel {
label,
stream,
sound,
on: true,
}
}
fn playback(&mut self) {
@@ -91,15 +160,14 @@ impl Review {
player.play();
}
}
if let Some(sound) = &self.sound {
for channel in &self.sounds {
if playing {
sound.pause();
channel.sound.pause();
} else {
sound.play();
channel.sound.play();
}
}
}
/// The Clip button: start a selection at the viewer's place, or drop the one on screen.
pub fn toggle_clip(&mut self) {
self.clip = match self.clip {
@@ -136,11 +204,33 @@ impl Review {
player.seek(at);
self.time = at;
}
if let Some(sound) = &self.sound {
sound.seek(at);
for channel in &self.sounds {
channel.sound.seek(at);
}
}
}
/// One audio track of the clip, copied to a file of its own and played on its own, so the
/// channels are heard apart and can be kept apart.
/// Rune's video player asks Media Foundation for pictures and sound from one reader, and that
/// reader stamps the sound from its own clock instead of the clip's, so the sound never lines
/// up with the clock and plays silent. Audio-only players read one track each and need no
/// video, so their sound starts at zero.
pub struct Channel {
/// What the track carries, as the recorder named it.
pub label: String,
/// The stream this track is, in the file.
pub stream: usize,
pub sound: Player,
/// Off means the viewer does not hear it, and a clip or highlight does not keep it.
pub on: bool,
}
impl Channel {
/// Whether this track goes into the current mix, given the master mute.
fn heard(&self, muted: bool) -> bool {
self.on && !muted
}
}
/// A clip being chosen on the scrubber: where it starts and where it ends.
#[derive(Clone, Copy)]
@@ -166,7 +256,7 @@ impl Default for Review {
game: None,
loading: false,
player: None,
sound: None,
sounds: Vec::new(),
steady_at: 0.0,
time: 0.0,
duration: 0.0,
@@ -181,6 +271,7 @@ impl Default for Review {
// headroom, and the transport bar can change it.
volume: 0.8,
muted: false,
channel_error: None,
}
}
}
@@ -229,7 +320,7 @@ pub fn show(app: &mut App, game: Timeline) {
player.set_waker(waker.clone());
}
app.review.player = Some(player);
app.review.sound = None;
app.review.sounds.clear();
app.review.steady_at = 0.0;
Review::mute_video(app.review.player.as_ref().expect("just set"));
}
@@ -240,7 +331,7 @@ fn follow_sound(app: &mut App) {
let Some(player) = &app.review.player else {
return;
};
if app.review.sound.is_some() || player.has_sound() != Some(true) {
if !app.review.sounds.is_empty() || player.has_sound() != Some(true) {
return;
}
let Some(path) = app
@@ -259,7 +350,7 @@ pub fn leave(app: &mut App) {
app.review_rx = None;
app.review.loading = false;
app.review.player = None;
app.review.sound = None;
app.review.sounds.clear();
app.review.game = None;
app.review.highlights.clear();
app.review.time = 0.0;
@@ -272,7 +363,10 @@ pub fn leave(app: &mut App) {
/// Sound from a second player drifts from the pictures by a few milliseconds a minute. Past a
/// quarter second it is worth a seek, and short of that a seek would be audible.
fn keep_in_step(app: &mut App) {
let (Some(player), Some(sound)) = (&app.review.player, &app.review.sound) else {
let (Some(player), Some(sound)) = (
&app.review.player,
app.review.sounds.first().map(|channel| &channel.sound),
) else {
return;
};
if !player.is_playing() {
@@ -626,12 +720,64 @@ fn transport(app: &App, p: Paint) -> El {
.w_full()
.gap(space::S2)
.child(bar);
if let Some(line) = channels(app, p) {
column = column.child(line);
}
if let Some(clip) = app.review.clip {
column = column.child(clip_row(app, p, clip));
}
column
}
/// One toggle per audio track, shown once the clip carries more than one. A track that is off
/// is neither heard nor kept in an export.
fn channels(app: &App, p: Paint) -> Option<El> {
if app.review.sounds.len() < 2 {
return None;
}
let mut line = row().key("channels").center_y().gap(space::S2).child(
text("Audio")
.key("channels-label")
.font(body(11.5))
.color(p.muted()),
);
for (index, channel) in app.review.sounds.iter().enumerate() {
let label = channel.label.clone();
let on = channel.on;
line = line.child(
el().key(format!("channel-{index}"))
.role("switch")
.label(label.clone())
.checked(on)
.focusable(true)
.h(24.0)
.px(10.0)
.center()
.radius(radius::MD)
.fill(if on { p.accent } else { p.t.control_off })
.hover(p.t.hover)
.press(p.t.press)
.on_press(move |app: &mut App| app.review.toggle_channel(index))
.child(
text(label)
.key("label")
.font(type_::LABEL)
.color(if on { p.accent_ink } else { p.muted() })
.a11y_hidden(),
),
);
}
if let Some(error) = &app.review.channel_error {
line = line.child(
text(error.clone())
.key("channels-error")
.font(body(11.5))
.color(p.warning),
);
}
Some(line)
}
/// The row under the transport while a clip is being chosen: the two ends, its length, and the
/// two things to do with it.
fn clip_row(app: &App, p: Paint, clip: Clip) -> El {
@@ -673,9 +819,10 @@ fn clip_row(app: &App, p: Paint, clip: Clip) -> El {
Some(clip) => (clip.start, clip.end),
None => return,
};
let audio = app.review.kept_tracks();
std::thread::spawn(move || {
let result =
crate::export::export_clip(&video, start, end, &name, true, "high");
crate::export::export_clip(&video, start, end, &name, true, "high", &audio);
let _ = tx.send(result);
});
app.review.export = Some(rx);
@@ -907,9 +1054,11 @@ fn highlights(app: &App, p: Paint, game: &Timeline) -> El {
let (tx, rx) = channel();
let Some(video) = video.clone() else { return };
let name = name.clone();
let audio = app.review.kept_tracks();
std::thread::spawn(move || {
let result =
crate::export::export_clip(&video, start, end, &name, true, "high");
let result = crate::export::export_clip(
&video, start, end, &name, true, "high", &audio,
);
let _ = tx.send(result);
});
app.review.export = Some(rx);