Add VU meters
This commit is contained in:
@@ -12,6 +12,7 @@ use uuid::Uuid;
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum LayerProperty {
|
||||
Volume(f64),
|
||||
InputGain(f64),
|
||||
Muted(bool),
|
||||
Soloed(bool),
|
||||
Locked(bool),
|
||||
@@ -25,6 +26,7 @@ pub enum LayerProperty {
|
||||
#[derive(Clone, Debug)]
|
||||
enum OldValue {
|
||||
Volume(f64),
|
||||
InputGain(f64),
|
||||
Muted(bool),
|
||||
Soloed(bool),
|
||||
Locked(bool),
|
||||
@@ -85,6 +87,7 @@ impl Action for SetLayerPropertiesAction {
|
||||
if self.old_values[i].is_none() {
|
||||
self.old_values[i] = Some(match &self.property {
|
||||
LayerProperty::Volume(_) => OldValue::Volume(layer.volume()),
|
||||
LayerProperty::InputGain(_) => OldValue::InputGain(layer.layer().input_gain),
|
||||
LayerProperty::Muted(_) => OldValue::Muted(layer.muted()),
|
||||
LayerProperty::Soloed(_) => OldValue::Soloed(layer.soloed()),
|
||||
LayerProperty::Locked(_) => OldValue::Locked(layer.locked()),
|
||||
@@ -104,6 +107,7 @@ impl Action for SetLayerPropertiesAction {
|
||||
// Set new value
|
||||
match &self.property {
|
||||
LayerProperty::Volume(v) => layer.set_volume(*v),
|
||||
LayerProperty::InputGain(g) => layer.layer_mut().input_gain = *g,
|
||||
LayerProperty::Muted(m) => layer.set_muted(*m),
|
||||
LayerProperty::Soloed(s) => layer.set_soloed(*s),
|
||||
LayerProperty::Locked(l) => layer.set_locked(*l),
|
||||
@@ -128,6 +132,7 @@ impl Action for SetLayerPropertiesAction {
|
||||
if let Some(old_value) = &self.old_values[i] {
|
||||
match old_value {
|
||||
OldValue::Volume(v) => layer.set_volume(*v),
|
||||
OldValue::InputGain(g) => layer.layer_mut().input_gain = *g,
|
||||
OldValue::Muted(m) => layer.set_muted(*m),
|
||||
OldValue::Soloed(s) => layer.set_soloed(*s),
|
||||
OldValue::Locked(l) => layer.set_locked(*l),
|
||||
@@ -159,6 +164,7 @@ impl Action for SetLayerPropertiesAction {
|
||||
if let Some(&track_id) = backend.layer_to_track_map.get(&layer_id) {
|
||||
match &self.property {
|
||||
LayerProperty::Volume(v) => controller.set_track_volume(track_id, *v as f32),
|
||||
LayerProperty::InputGain(g) => controller.set_input_gain(*g as f32),
|
||||
LayerProperty::Muted(m) => controller.set_track_mute(track_id, *m),
|
||||
LayerProperty::Soloed(s) => controller.set_track_solo(track_id, *s),
|
||||
_ => {} // Locked/Opacity/Visible/CameraEnabled are UI-only
|
||||
@@ -183,6 +189,7 @@ impl Action for SetLayerPropertiesAction {
|
||||
if let Some(old_value) = &self.old_values[i] {
|
||||
match old_value {
|
||||
OldValue::Volume(v) => controller.set_track_volume(track_id, *v as f32),
|
||||
OldValue::InputGain(g) => controller.set_input_gain(*g as f32),
|
||||
OldValue::Muted(m) => controller.set_track_mute(track_id, *m),
|
||||
OldValue::Soloed(s) => controller.set_track_solo(track_id, *s),
|
||||
_ => {} // Locked/Opacity/Visible are UI-only
|
||||
@@ -196,6 +203,7 @@ impl Action for SetLayerPropertiesAction {
|
||||
fn description(&self) -> String {
|
||||
let property_name = match &self.property {
|
||||
LayerProperty::Volume(_) => "volume",
|
||||
LayerProperty::InputGain(_) => "input gain",
|
||||
LayerProperty::Muted(_) => "mute",
|
||||
LayerProperty::Soloed(_) => "solo",
|
||||
LayerProperty::Locked(_) => "lock",
|
||||
|
||||
@@ -60,6 +60,8 @@ pub trait LayerTrait {
|
||||
fn set_locked(&mut self, locked: bool);
|
||||
}
|
||||
|
||||
fn default_input_gain() -> f64 { 1.0 }
|
||||
|
||||
/// Base layer structure
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Layer {
|
||||
@@ -84,6 +86,10 @@ pub struct Layer {
|
||||
/// Audio volume (1.0 = 100%, affects nested audio layers/clips)
|
||||
pub volume: f64,
|
||||
|
||||
/// Input gain for recording (1.0 = unity, range 0.0–4.0)
|
||||
#[serde(default = "default_input_gain")]
|
||||
pub input_gain: f64,
|
||||
|
||||
/// Audio mute state
|
||||
pub muted: bool,
|
||||
|
||||
@@ -108,6 +114,7 @@ impl Layer {
|
||||
visible: true,
|
||||
opacity: 1.0,
|
||||
volume: 1.0, // 100% volume
|
||||
input_gain: 1.0,
|
||||
muted: false,
|
||||
soloed: false,
|
||||
locked: false,
|
||||
@@ -125,6 +132,7 @@ impl Layer {
|
||||
visible: true,
|
||||
opacity: 1.0,
|
||||
volume: 1.0,
|
||||
input_gain: 1.0,
|
||||
muted: false,
|
||||
soloed: false,
|
||||
locked: false,
|
||||
|
||||
@@ -814,6 +814,11 @@ struct EditorApp {
|
||||
region_selection: Option<lightningbeam_core::selection::RegionSelection>,
|
||||
region_select_mode: lightningbeam_core::tool::RegionSelectMode,
|
||||
|
||||
// VU meter levels
|
||||
input_level: f32,
|
||||
output_level: f32,
|
||||
track_levels: HashMap<daw_backend::TrackId, f32>,
|
||||
|
||||
/// Cache for MIDI event data (keyed by backend midi_clip_id)
|
||||
/// Prevents repeated backend queries for the same MIDI clip
|
||||
/// Format: (timestamp, note_number, velocity, is_note_on)
|
||||
@@ -1057,6 +1062,9 @@ impl EditorApp {
|
||||
polygon_sides: 5, // Default to pentagon
|
||||
region_selection: None,
|
||||
region_select_mode: lightningbeam_core::tool::RegionSelectMode::default(),
|
||||
input_level: 0.0,
|
||||
output_level: 0.0,
|
||||
track_levels: HashMap::new(),
|
||||
midi_event_cache: HashMap::new(), // Initialize empty MIDI event cache
|
||||
audio_duration_cache: HashMap::new(), // Initialize empty audio duration cache
|
||||
audio_pools_with_new_waveforms: HashSet::new(), // Track pool indices with new raw audio
|
||||
@@ -4672,6 +4680,18 @@ impl eframe::App for EditorApp {
|
||||
);
|
||||
ctx.request_repaint();
|
||||
}
|
||||
AudioEvent::InputLevel(peak) => {
|
||||
self.input_level = self.input_level.max(peak);
|
||||
}
|
||||
AudioEvent::OutputLevel(peak) => {
|
||||
self.output_level = self.output_level.max(peak);
|
||||
}
|
||||
AudioEvent::TrackLevels(levels) => {
|
||||
for (track_id, peak) in levels {
|
||||
let entry = self.track_levels.entry(track_id).or_insert(0.0);
|
||||
*entry = entry.max(peak);
|
||||
}
|
||||
}
|
||||
_ => {} // Ignore other events for now
|
||||
}
|
||||
}
|
||||
@@ -4686,6 +4706,38 @@ impl eframe::App for EditorApp {
|
||||
}
|
||||
}
|
||||
|
||||
// Update input monitoring based on active layer
|
||||
if let Some(controller) = &self.audio_controller {
|
||||
let should_monitor = self.active_layer_id.map_or(false, |layer_id| {
|
||||
let doc = self.action_executor.document();
|
||||
if let Some(layer) = doc.get_layer(&layer_id) {
|
||||
matches!(layer, lightningbeam_core::layer::AnyLayer::Audio(a) if a.audio_layer_type == lightningbeam_core::layer::AudioLayerType::Sampled)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
if let Ok(mut ctrl) = controller.try_lock() {
|
||||
ctrl.set_input_monitoring(should_monitor);
|
||||
}
|
||||
}
|
||||
|
||||
// Decay VU meter levels (~1.5s full fall at 60fps)
|
||||
{
|
||||
let decay = 0.97f32;
|
||||
self.input_level *= decay;
|
||||
self.output_level *= decay;
|
||||
for level in self.track_levels.values_mut() {
|
||||
*level *= decay;
|
||||
}
|
||||
// Request repaint while any level is visible
|
||||
let any_active = self.input_level > 0.001
|
||||
|| self.output_level > 0.001
|
||||
|| self.track_levels.values().any(|&v| v > 0.001);
|
||||
if any_active {
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
|
||||
let _post_events_ms = _frame_start.elapsed().as_secs_f64() * 1000.0;
|
||||
|
||||
// Request continuous repaints when playing to update time display
|
||||
@@ -4925,6 +4977,27 @@ impl eframe::App for EditorApp {
|
||||
}
|
||||
});
|
||||
|
||||
// Mix output VU meter (thin bar below menu)
|
||||
if self.app_mode != AppMode::StartScreen && self.output_level > 0.001 {
|
||||
egui::TopBottomPanel::top("mix_meter").exact_height(4.0).show(ctx, |ui| {
|
||||
let rect = ui.available_rect_before_wrap();
|
||||
let level = self.output_level.min(1.0);
|
||||
let filled_width = rect.width() * level;
|
||||
let color = if level > 0.9 {
|
||||
egui::Color32::from_rgb(220, 50, 50)
|
||||
} else if level > 0.7 {
|
||||
egui::Color32::from_rgb(220, 200, 50)
|
||||
} else {
|
||||
egui::Color32::from_rgb(50, 200, 80)
|
||||
};
|
||||
let filled_rect = egui::Rect::from_min_size(
|
||||
rect.left_top(),
|
||||
egui::vec2(filled_width, rect.height()),
|
||||
);
|
||||
ui.painter().rect_filled(filled_rect, 0.0, color);
|
||||
});
|
||||
}
|
||||
|
||||
// Render start screen or editor based on app mode
|
||||
if self.app_mode == AppMode::StartScreen {
|
||||
self.render_start_screen(ctx);
|
||||
@@ -5075,6 +5148,10 @@ impl eframe::App for EditorApp {
|
||||
target_format: self.target_format,
|
||||
pending_menu_actions: &mut pending_menu_actions,
|
||||
clipboard_manager: &mut self.clipboard_manager,
|
||||
input_level: self.input_level,
|
||||
output_level: self.output_level,
|
||||
track_levels: &self.track_levels,
|
||||
track_to_layer_map: &self.track_to_layer_map,
|
||||
waveform_stereo: self.config.waveform_stereo,
|
||||
project_generation: &mut self.project_generation,
|
||||
script_to_edit: &mut self.script_to_edit,
|
||||
|
||||
@@ -244,6 +244,13 @@ pub struct SharedPaneState<'a> {
|
||||
pub pending_menu_actions: &'a mut Vec<crate::menu::MenuAction>,
|
||||
/// Clipboard manager for cut/copy/paste operations
|
||||
pub clipboard_manager: &'a mut lightningbeam_core::clipboard::ClipboardManager,
|
||||
// VU meter levels
|
||||
pub input_level: f32,
|
||||
#[allow(dead_code)] // Used by mix meter in main.rs, available to panes
|
||||
pub output_level: f32,
|
||||
pub track_levels: &'a std::collections::HashMap<daw_backend::TrackId, f32>,
|
||||
#[allow(dead_code)] // Available for panes that need reverse track->layer lookup
|
||||
pub track_to_layer_map: &'a std::collections::HashMap<daw_backend::TrackId, Uuid>,
|
||||
/// Whether to show waveforms as stacked stereo (true) or combined mono (false)
|
||||
pub waveform_stereo: bool,
|
||||
/// Generation counter - incremented on project load to force reloads
|
||||
|
||||
@@ -1260,6 +1260,9 @@ impl TimelinePane {
|
||||
pending_actions: &mut Vec<Box<dyn lightningbeam_core::action::Action>>,
|
||||
_document: &lightningbeam_core::document::Document,
|
||||
context_layers: &[&lightningbeam_core::layer::AnyLayer],
|
||||
layer_to_track_map: &std::collections::HashMap<uuid::Uuid, daw_backend::TrackId>,
|
||||
track_levels: &std::collections::HashMap<daw_backend::TrackId, f32>,
|
||||
input_level: f32,
|
||||
) {
|
||||
// Background for header column
|
||||
let header_style = theme.style(".timeline-header", ui.ctx());
|
||||
@@ -1659,6 +1662,10 @@ impl TimelinePane {
|
||||
(response, temp_slider_value)
|
||||
}).inner;
|
||||
|
||||
// Block layer drag while interacting with the slider
|
||||
if volume_response.0.dragged() || volume_response.0.has_focus() {
|
||||
self.layer_control_clicked = true;
|
||||
}
|
||||
if volume_response.0.changed() {
|
||||
self.layer_control_clicked = true;
|
||||
// Map slider position (0.0-1.0) back to volume (0.0-2.0)
|
||||
@@ -1678,6 +1685,93 @@ impl TimelinePane {
|
||||
));
|
||||
}
|
||||
|
||||
// Input gain slider for sampled audio layers (below volume slider)
|
||||
if let lightningbeam_core::layer::AnyLayer::Audio(audio_layer) = layer_for_controls {
|
||||
if audio_layer.audio_layer_type == lightningbeam_core::layer::AudioLayerType::Sampled {
|
||||
let gain_slider_rect = egui::Rect::from_min_size(
|
||||
egui::pos2(controls_right - slider_width, controls_top + 22.0),
|
||||
egui::vec2(slider_width, 16.0),
|
||||
);
|
||||
let current_gain = audio_layer.layer.input_gain;
|
||||
|
||||
// Map gain (0.0-4.0) to slider (0.0-1.0): linear
|
||||
let mut slider_val = (current_gain / 4.0) as f32;
|
||||
let gain_response = ui.scope_builder(egui::UiBuilder::new().max_rect(gain_slider_rect), |ui| {
|
||||
let slider = egui::Slider::new(&mut slider_val, 0.0..=1.0f32)
|
||||
.show_value(false);
|
||||
ui.add(slider)
|
||||
}).inner;
|
||||
|
||||
// Block layer drag while interacting with the slider
|
||||
if gain_response.dragged() || gain_response.has_focus() {
|
||||
self.layer_control_clicked = true;
|
||||
}
|
||||
if gain_response.changed() {
|
||||
self.layer_control_clicked = true;
|
||||
let new_gain = (slider_val * 4.0) as f64;
|
||||
pending_actions.push(Box::new(
|
||||
lightningbeam_core::actions::SetLayerPropertiesAction::new(
|
||||
layer_id,
|
||||
lightningbeam_core::actions::LayerProperty::InputGain(new_gain),
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
// Label
|
||||
let label_rect = egui::Rect::from_min_size(
|
||||
egui::pos2(gain_slider_rect.min.x - 26.0, controls_top + 22.0),
|
||||
egui::vec2(24.0, 16.0),
|
||||
);
|
||||
ui.painter().text(
|
||||
label_rect.center(),
|
||||
egui::Align2::CENTER_CENTER,
|
||||
"Gain",
|
||||
egui::FontId::proportional(9.0),
|
||||
egui::Color32::from_gray(140),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Per-layer VU meter bar (4px tall at bottom of header)
|
||||
{
|
||||
// Look up the track level for this layer
|
||||
let mut level = 0.0f32;
|
||||
if let Some(&track_id) = layer_to_track_map.get(&layer_id) {
|
||||
if let Some(&track_level) = track_levels.get(&track_id) {
|
||||
level = track_level;
|
||||
}
|
||||
}
|
||||
|
||||
// For active sampled audio layer, show max of track level and input level
|
||||
let is_active_sampled_audio = active_layer_id.map_or(false, |id| id == layer_id)
|
||||
&& matches!(layer_for_controls, lightningbeam_core::layer::AnyLayer::Audio(a) if a.audio_layer_type == lightningbeam_core::layer::AudioLayerType::Sampled);
|
||||
if is_active_sampled_audio {
|
||||
level = level.max(input_level);
|
||||
}
|
||||
|
||||
if level > 0.001 {
|
||||
let meter_height = 4.0;
|
||||
let meter_rect = egui::Rect::from_min_size(
|
||||
egui::pos2(header_rect.min.x, header_rect.max.y - meter_height - 1.0),
|
||||
egui::vec2(header_rect.width(), meter_height),
|
||||
);
|
||||
let clamped = level.min(1.0);
|
||||
let filled_width = meter_rect.width() * clamped;
|
||||
let color = if clamped > 0.9 {
|
||||
egui::Color32::from_rgb(220, 50, 50)
|
||||
} else if clamped > 0.7 {
|
||||
egui::Color32::from_rgb(220, 200, 50)
|
||||
} else {
|
||||
egui::Color32::from_rgb(50, 200, 80)
|
||||
};
|
||||
let filled = egui::Rect::from_min_size(
|
||||
meter_rect.left_top(),
|
||||
egui::vec2(filled_width, meter_rect.height()),
|
||||
);
|
||||
ui.painter().rect_filled(filled, 0.0, color);
|
||||
}
|
||||
}
|
||||
|
||||
// Separator line at bottom
|
||||
ui.painter().line_segment(
|
||||
[
|
||||
@@ -4278,7 +4372,7 @@ impl PaneRenderer for TimelinePane {
|
||||
|
||||
// Render layer header column with clipping
|
||||
ui.set_clip_rect(layer_headers_rect.intersect(original_clip_rect));
|
||||
self.render_layer_headers(ui, layer_headers_rect, shared.theme, shared.active_layer_id, shared.focus, &mut shared.pending_actions, document, &context_layers);
|
||||
self.render_layer_headers(ui, layer_headers_rect, shared.theme, shared.active_layer_id, shared.focus, &mut shared.pending_actions, document, &context_layers, shared.layer_to_track_map, shared.track_levels, shared.input_level);
|
||||
|
||||
// Render time ruler (clip to ruler rect)
|
||||
ui.set_clip_rect(ruler_rect.intersect(original_clip_rect));
|
||||
|
||||
Reference in New Issue
Block a user