feat(chat): volume control for inline audio clips
Add a per-clip volume slider plus a master volume control with a "Universal volume" toggle in the chat header. - ClipPlayer gains a SetVolume command; the worker remembers gain across clips and reapplies it to each freshly connected player. - New config.clip_volume (universal level) and config.clip_volume_universal (mode toggle, default on), both persisted; old configs load at unity in universal mode. - Universal on: master and per-clip sliders drive one shared level applied to every clip. Universal off: each clip keeps its own in-memory level and the master slider is inert. - play_ready_audio applies the resolved effective gain right after Play. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+90
-2
@@ -393,6 +393,15 @@ pub enum AppMessage {
|
||||
PauseAudio,
|
||||
ResumeAudio,
|
||||
SeekAudio(crate::files::AttachmentId, f32),
|
||||
/// Adjust the universal inline-clip playback volume (`1.0` = unity) from the
|
||||
/// master slider. Persisted to config; applied live only in universal mode.
|
||||
SetClipVolume(f32),
|
||||
/// Adjust volume from a clip's own row slider. In universal mode this drives
|
||||
/// the shared level; otherwise it sets just that clip's in-memory level.
|
||||
SetClipVolumeFor(crate::files::AttachmentId, f32),
|
||||
/// Toggle whether one universal level governs every clip (checked) or each
|
||||
/// clip keeps its own level (unchecked).
|
||||
ToggleUniversalClipVolume(bool),
|
||||
/// Redraw cadence while an inline clip is active.
|
||||
AudioTick,
|
||||
/// Send the current chat input line (Enter or the Send button).
|
||||
@@ -582,6 +591,10 @@ pub struct AppState {
|
||||
/// the call capture/mixer path.
|
||||
clip_player: ClipPlayer,
|
||||
clip_status: SharedClipStatus,
|
||||
/// Per-clip playback gain used when universal clip volume is disabled
|
||||
/// (`config.clip_volume_universal == false`). In-memory only; absent clips
|
||||
/// default to unity. Universal mode ignores this and uses `config.clip_volume`.
|
||||
clip_volumes: HashMap<crate::files::AttachmentId, f32>,
|
||||
/// Last known window size, tracked so divider clamps stay valid on resize.
|
||||
/// (The divider positions themselves are persisted in `config`.)
|
||||
window_size: Size,
|
||||
@@ -781,7 +794,7 @@ impl Default for AppState {
|
||||
let selected_output = output_devices.iter().find(|d| d.name == config.output_device).cloned();
|
||||
|
||||
let background_image = load_background_bytes(&config);
|
||||
let (clip_player, clip_status) = ClipPlayer::new();
|
||||
let (clip_player, clip_status) = ClipPlayer::new(config.clip_volume);
|
||||
|
||||
Self {
|
||||
// Pre-fill the nickname with the last one used (or "Peer" by default).
|
||||
@@ -819,6 +832,7 @@ impl Default for AppState {
|
||||
invalid_audio: HashSet::new(),
|
||||
clip_player,
|
||||
clip_status,
|
||||
clip_volumes: HashMap::new(),
|
||||
chat_input: String::new(),
|
||||
window_size: Size::new(ww, wh),
|
||||
layout_picker_open: false,
|
||||
@@ -2200,6 +2214,38 @@ fn update(state: &mut AppState, message: AppMessage) -> Task<AppMessage> {
|
||||
state.clip_player.seek(seek_target(fraction, total));
|
||||
}
|
||||
}
|
||||
AppMessage::SetClipVolume(volume) => {
|
||||
// Master slider: always stores the universal level, but only the
|
||||
// active player is nudged when universal mode is actually on.
|
||||
let volume = volume.clamp(0.0, 2.0);
|
||||
state.config.clip_volume = volume;
|
||||
state.config.save();
|
||||
if state.config.clip_volume_universal {
|
||||
state.clip_player.set_volume(volume);
|
||||
}
|
||||
}
|
||||
AppMessage::SetClipVolumeFor(id, volume) => {
|
||||
let volume = volume.clamp(0.0, 2.0);
|
||||
if state.config.clip_volume_universal {
|
||||
state.config.clip_volume = volume;
|
||||
state.config.save();
|
||||
state.clip_player.set_volume(volume);
|
||||
} else {
|
||||
state.clip_volumes.insert(id, volume);
|
||||
// Only the clip the user is dragging should react immediately.
|
||||
if status_snapshot(&state.clip_status).playing_id == Some(id) {
|
||||
state.clip_player.set_volume(volume);
|
||||
}
|
||||
}
|
||||
}
|
||||
AppMessage::ToggleUniversalClipVolume(on) => {
|
||||
state.config.clip_volume_universal = on;
|
||||
state.config.save();
|
||||
// Reapply the now-effective level to whatever is currently playing.
|
||||
if let Some(id) = status_snapshot(&state.clip_status).playing_id {
|
||||
state.clip_player.set_volume(effective_clip_volume(state, id));
|
||||
}
|
||||
}
|
||||
AppMessage::AudioTick => {
|
||||
let clip = status_snapshot(&state.clip_status);
|
||||
if let Some(failure) = clip.failure {
|
||||
@@ -2512,6 +2558,16 @@ fn find_attachment_source(
|
||||
|
||||
/// Validate cached bytes and hand them to the independent clip player. A false
|
||||
/// filename hint falls back to the generic file chip without reaching rodio.
|
||||
/// Resolve the gain to use for clip `id`: the shared universal level, or the
|
||||
/// clip's own stored level (defaulting to unity) when universal mode is off.
|
||||
fn effective_clip_volume(state: &AppState, id: crate::files::AttachmentId) -> f32 {
|
||||
if state.config.clip_volume_universal {
|
||||
state.config.clip_volume
|
||||
} else {
|
||||
state.clip_volumes.get(&id).copied().unwrap_or(1.0)
|
||||
}
|
||||
}
|
||||
|
||||
fn play_ready_audio(state: &mut AppState, key: AttachmentKey) {
|
||||
let Some(AttachmentState::Ready(data)) = state.attachments.get(&key) else {
|
||||
return;
|
||||
@@ -2521,6 +2577,9 @@ fn play_ready_audio(state: &mut AppState, key: AttachmentKey) {
|
||||
let bytes = data.clone();
|
||||
state.invalid_audio.remove(&id);
|
||||
state.clip_player.play(id, bytes);
|
||||
// Apply this clip's effective gain; the command lands after Play so it
|
||||
// takes effect on the freshly connected player.
|
||||
state.clip_player.set_volume(effective_clip_volume(state, id));
|
||||
} else {
|
||||
state.invalid_audio.insert(id);
|
||||
state.status_message = "This attachment is not valid supported audio.".to_string();
|
||||
@@ -4805,6 +4864,17 @@ fn view(state: &AppState) -> Element<'_, AppMessage> {
|
||||
text(format!("{elapsed} / {duration}"))
|
||||
.size(11)
|
||||
.color(color_subtext),
|
||||
// Per-clip volume. In universal mode this shows
|
||||
// and drives the shared level; otherwise it is
|
||||
// this clip's own remembered level.
|
||||
text("🔊").size(12).color(color_subtext),
|
||||
slider(
|
||||
0.0..=2.0,
|
||||
effective_clip_volume(state, att.id),
|
||||
move |v| AppMessage::SetClipVolumeFor(att.id, v),
|
||||
)
|
||||
.step(0.01)
|
||||
.width(iced::Length::Fixed(80.0)),
|
||||
]
|
||||
.spacing(8)
|
||||
.align_y(iced::alignment::Vertical::Center),
|
||||
@@ -4868,8 +4938,26 @@ fn view(state: &AppState) -> Element<'_, AppMessage> {
|
||||
]
|
||||
.spacing(8)
|
||||
.align_y(iced::alignment::Vertical::Center);
|
||||
let chat_inner = column![
|
||||
// Chat header: title on the left, the universal-volume control on the
|
||||
// right. The master slider drives every clip when "Universal" is checked;
|
||||
// when unchecked each clip keeps its own level and this slider is inert.
|
||||
let universal = state.config.clip_volume_universal;
|
||||
let chat_header = row![
|
||||
text("Chat").size(16).color(color_blue),
|
||||
horizontal_space(),
|
||||
checkbox(universal)
|
||||
.label("Universal volume")
|
||||
.text_size(12)
|
||||
.on_toggle(AppMessage::ToggleUniversalClipVolume),
|
||||
text("🔊").size(13).color(color_subtext),
|
||||
slider(0.0..=2.0, state.config.clip_volume, AppMessage::SetClipVolume)
|
||||
.step(0.01)
|
||||
.width(iced::Length::Fixed(110.0)),
|
||||
]
|
||||
.spacing(10)
|
||||
.align_y(iced::alignment::Vertical::Center);
|
||||
let chat_inner = column![
|
||||
chat_header,
|
||||
chat_scroll,
|
||||
chat_input_row,
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user