Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "maolan-engine"
version = "0.4.30"
version = "0.4.31"
edition = "2024"
description = "Audio engine for the Maolan DAW with audio/MIDI tracks, routing, export, and out-of-process CLAP/VST3/LV2 plugin support"
license = "BSD-2-Clause"
Expand Down
6 changes: 6 additions & 0 deletions src/engine/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,11 @@ mod undo_consistency {
scene_index: 0,
clip_id: Some("clip".to_string()),
},
Action::TrackSetSessionSlotGroove {
track_name: "t1".to_string(),
scene_index: 0,
groove: Some(crate::groove::GrooveSettings::default()),
},
Action::TrackSetSessionSlotPlayEnabled {
track_name: "t1".to_string(),
scene_index: 0,
Expand Down Expand Up @@ -2124,6 +2129,7 @@ mod undo_consistency {
| Action::SetSessionMidiLearnBinding { .. }
| Action::TrackSetFrozen { .. }
| Action::TrackSetSessionSlot { .. }
| Action::TrackSetSessionSlotGroove { .. }
| Action::TrackSetSessionSlotPlayEnabled { .. }
| Action::TrackSetSessionSlotStopEnabled { .. }
| Action::TrackSetFolder { .. }
Expand Down
66 changes: 66 additions & 0 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,72 @@ mod tests {
assert!(!snapshot.tracks.contains_key("snap"));
}

#[cfg_attr(
all(miri, any(target_os = "freebsd", target_os = "macos")),
ignore = "Tokio runtime uses kqueue, which Miri does not support on FreeBSD or macOS"
)]
#[cfg_attr(
all(miri, target_os = "windows"),
ignore = "Tokio runtime uses Windows IOCP, which Miri does not support"
)]
#[tokio::test]
async fn copied_clip_inherits_groove_and_plugins_but_groove_edits_are_instance_scoped() {
let (mut engine, _client_rx) = make_engine_with_client();
let track = Track::new("track".to_string(), 1, 1, 0, 0, 64, 48_000.0);
let mut clip = AudioClip::new("audio/clip.wav".to_string(), 0, 48_000);
clip.id = "shared-id".to_string();
clip.groove = Some(crate::groove::GrooveSettings::default());
clip.plugin_graph_json = Some(serde_json::json!({
"plugins": [{"format": "CLAP", "uri": "example.clap"}],
"connections": []
}));
let expected_groove = clip.groove.clone();
let expected_plugins = clip.plugin_graph_json.clone();
track.audio.push_clip(clip);
insert_track(&mut engine, track);

engine
.handle_request(Action::ClipMove {
kind: Kind::Audio,
from: crate::message::ClipMoveFrom {
track_name: "track".to_string(),
clip_index: 0,
},
to: crate::message::ClipMoveTo {
track_name: "track".to_string(),
sample_offset: 48_000,
input_channel: 0,
},
copy: true,
})
.await;

{
let state = engine.state.lock();
let track = state.tracks.get("track").expect("track exists").lock();
let clips = track.audio.clips();
assert_eq!(clips.len(), 2);
assert_eq!(clips[0].id, clips[1].id);
assert_eq!(clips[1].groove, expected_groove);
assert_eq!(clips[1].plugin_graph_json, expected_plugins);
}

engine
.handle_request(Action::SetClipGroove {
track_name: "track".to_string(),
clip_index: 1,
kind: Kind::Audio,
groove: None,
})
.await;

let state = engine.state.lock();
let track = state.tracks.get("track").expect("track exists").lock();
let clips = track.audio.clips();
assert_eq!(clips[0].groove, expected_groove);
assert_eq!(clips[1].groove, None);
}

#[cfg_attr(
all(miri, any(target_os = "freebsd", target_os = "macos")),
ignore = "Tokio runtime uses kqueue, which Miri does not support on FreeBSD or macOS"
Expand Down
1 change: 1 addition & 0 deletions src/engine/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ impl Engine {
| Action::SetSessionPath(_)
| Action::SessionArmMidiLearn { .. }
| Action::TrackSetSessionSlot { .. }
| Action::TrackSetSessionSlotGroove { .. }
| Action::TrackSetSessionSlotPlayEnabled { .. }
| Action::TrackSetSessionSlotStopEnabled { .. } => {
if self.handle_session_request(a.clone()).await {
Expand Down
53 changes: 53 additions & 0 deletions src/engine/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,7 @@ impl Engine {
scene_index,
SessionSlot {
clip_id: id.clone(),
groove: None,
play_enabled,
stop_enabled,
},
Expand All @@ -1044,6 +1045,32 @@ impl Engine {
false
}

pub(crate) async fn handle_track_set_session_slot_groove(&mut self, a: Action) -> bool {
let Action::TrackSetSessionSlotGroove {
ref track_name,
scene_index,
ref groove,
} = a
else {
return false;
};
let track = match self.track_handle_or_err(track_name) {
Ok(track) => track,
Err(e) => {
self.notify_clients(Err(e)).await;
return true;
}
};
let mut track = track.lock();
if let Some(slot) = track.rt.session_slots.get_mut(&scene_index)
&& !slot.clip_id.is_empty()
{
slot.groove = groove.clone();
track.rt.clip_groove_shifters.clear();
}
false
}

pub(crate) async fn handle_track_set_session_slot_play_enabled(&mut self, a: Action) -> bool {
let Action::TrackSetSessionSlotPlayEnabled {
ref track_name,
Expand All @@ -1068,6 +1095,7 @@ impl Engine {
.entry(scene_index)
.or_insert_with(|| SessionSlot {
clip_id: String::new(),
groove: None,
play_enabled: false,
stop_enabled: false,
})
Expand Down Expand Up @@ -1100,6 +1128,7 @@ impl Engine {
.entry(scene_index)
.or_insert_with(|| SessionSlot {
clip_id: String::new(),
groove: None,
play_enabled: false,
stop_enabled: false,
})
Expand Down Expand Up @@ -1150,6 +1179,11 @@ impl Engine {
return true;
}
}
Action::TrackSetSessionSlotGroove { .. } => {
if self.handle_track_set_session_slot_groove(a.clone()).await {
return true;
}
}
Action::TrackSetSessionSlotPlayEnabled { .. } => {
if self
.handle_track_set_session_slot_play_enabled(a.clone())
Expand Down Expand Up @@ -1178,6 +1212,7 @@ pub(crate) fn undo_should_record(action: &Action) -> bool {
matches!(
action,
Action::TrackSetSessionSlot { .. }
| Action::TrackSetSessionSlotGroove { .. }
| Action::TrackSetSessionSlotPlayEnabled { .. }
| Action::TrackSetSessionSlotStopEnabled { .. }
)
Expand Down Expand Up @@ -1205,6 +1240,24 @@ pub(crate) fn undo_inverse(action: &Action, state: &State) -> Option<Action> {
})
}

Action::TrackSetSessionSlotGroove {
track_name,
scene_index,
groove: _,
} => {
let track = state.tracks.get(track_name)?;
let track_lock = track.lock();
Some(Action::TrackSetSessionSlotGroove {
track_name: track_name.clone(),
scene_index: *scene_index,
groove: track_lock
.rt
.session_slots
.get(scene_index)
.and_then(|slot| slot.groove.clone()),
})
}

Action::TrackSetSessionSlotPlayEnabled {
track_name,
scene_index,
Expand Down
36 changes: 12 additions & 24 deletions src/groove/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ impl GrooveBase {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct GrooveSettings {
pub base: GrooveBase,
pub swing_percent: f32,
pub template: Option<Box<GrooveTemplate>>,
pub quantize: f32,
pub timing: f32,
Expand All @@ -70,7 +69,6 @@ impl Default for GrooveSettings {
fn default() -> Self {
Self {
base: GrooveBase::Sixteenth,
swing_percent: 73.0,
template: None,
quantize: 0.0,
timing: 100.0,
Expand Down Expand Up @@ -100,7 +98,6 @@ impl GrooveSettings {
timing: parsed.timing,
random: parsed.random,
velocity: parsed.velocity,
..Self::default()
})
}

Expand Down Expand Up @@ -138,20 +135,11 @@ impl GrooveSettings {
}

pub fn offset_at_grid(&self, grid_index: i64, grid_samples: f64, seed: &str) -> f64 {
let timing = self.template_note_at_grid(grid_index).map_or_else(
|| {
if self.template.is_none() && grid_index.rem_euclid(2) == 1 {
((self.swing_percent.clamp(50.0, 99.0) - 50.0) * 2.0) as f64 / 100.0
} else {
0.0
}
},
|note| {
let ideal = (note.time_beats * self.base.divisions_per_beat()).round()
/ self.base.divisions_per_beat();
(note.time_beats - ideal) * self.base.divisions_per_beat()
},
);
let timing = self.template_note_at_grid(grid_index).map_or(0.0, |note| {
let ideal = (note.time_beats * self.base.divisions_per_beat()).round()
/ self.base.divisions_per_beat();
(note.time_beats - ideal) * self.base.divisions_per_beat()
});
let random = Self::random_at_grid(grid_index, seed) * 0.05 * Self::unit(self.random);
grid_samples * (timing * Self::unit(self.timing) + random)
}
Expand Down Expand Up @@ -400,29 +388,29 @@ mod tests {
use super::*;

#[test]
fn swing_16ths_73_delays_alternate_grid_positions() {
fn no_template_produces_straight_timing() {
let groove = GrooveSettings::default();
assert_eq!(groove.offset_at_grid(0, 6_000.0, "clip"), 0.0);
assert_eq!(groove.offset_at_grid(1, 6_000.0, "clip"), 2_760.0);
assert_eq!(groove.offset_at_grid(1, 6_000.0, "clip"), 0.0);
assert_eq!(groove.offset_at_grid(2, 6_000.0, "clip"), 0.0);
assert_eq!(groove.tempo_at_grid(0, 6_000.0, "clip"), 6_000.0 / 8_760.0);
assert_eq!(groove.tempo_at_grid(1, 6_000.0, "clip"), 6_000.0 / 3_240.0);
assert_eq!(groove.tempo_at_grid(0, 6_000.0, "clip"), 1.0);
assert_eq!(groove.tempo_at_grid(1, 6_000.0, "clip"), 1.0);
}

#[test]
fn zero_random_is_repeatable_and_quantize_zero_preserves_straight_notes() {
let groove = GrooveSettings::default();
assert_eq!(groove.map_midi_sample(12_000, 6_000.0, "clip", 0), 12_000);
assert_eq!(groove.map_midi_sample(6_000, 6_000.0, "clip", 0), 8_760);
assert_eq!(groove.map_midi_sample(6_000, 6_000.0, "clip", 0), 6_000);
}

#[test]
fn quantize_is_applied_before_swing() {
fn quantize_snaps_notes_to_the_grid_before_groove_mapping() {
let groove = GrooveSettings {
quantize: 100.0,
..GrooveSettings::default()
};
assert_eq!(groove.map_midi_sample(5_900, 6_000.0, "clip", 0), 8_760);
assert_eq!(groove.map_midi_sample(5_900, 6_000.0, "clip", 0), 6_000);
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,11 @@ pub enum Action {
scene_index: usize,
clip_id: Option<String>,
},
TrackSetSessionSlotGroove {
track_name: String,
scene_index: usize,
groove: Option<crate::groove::GrooveSettings>,
},
TrackSetSessionSlotPlayEnabled {
track_name: String,
scene_index: usize,
Expand Down
2 changes: 1 addition & 1 deletion src/track/clip_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ impl TrackData {
let settings = clip.groove.clone()?;
let input_count = self.audio.ins.len().max(1);
let channels = effective_channels_for(buffer_channel_count(buffer), input_count).min(2);
let key = format!("{}:{}", clip.id, clip.name);
let key = format!("{}:{}:{clip_start}", clip.id, clip.name);
let bpm = self.rt.tempo_bpm;
let valid = self
.rt
Expand Down
Loading
Loading