Skip to content

Commit

Permalink
feat(sequencer): add go- action to sequencer
Browse files Browse the repository at this point in the history
  • Loading branch information
maxjoehnk committed Jul 9, 2024
1 parent 9266e0c commit c717bbb
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 15 deletions.
10 changes: 8 additions & 2 deletions crates/api/src/handlers/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ impl<R: RuntimeApi> SequencerHandler<R> {

#[tracing::instrument(skip(self))]
#[profiling::function]
pub fn sequence_go(&self, sequence: u32) {
self.sequencer.sequence_go(sequence);
pub fn sequence_go_forward(&self, sequence: u32) {
self.sequencer.sequence_go_forward(sequence);
}

#[tracing::instrument(skip(self))]
#[profiling::function]
pub fn sequence_go_backward(&self, sequence: u32) {
self.sequencer.sequence_go_backward(sequence);
}

#[tracing::instrument(skip(self))]
Expand Down
30 changes: 26 additions & 4 deletions crates/components/sequencer/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Sequencer {
let mut sequence_orders = self.sequence_order.borrow_mut();
for command in self.commands.1.try_iter() {
match command {
SequencerCommands::Go(sequence_id) => {
SequencerCommands::GoForward(sequence_id) => {
let state = states.entry(sequence_id).or_default();
if let Some(sequence) = sequences.get(&sequence_id) {
let was_active = state.active;
Expand All @@ -114,6 +114,20 @@ impl Sequencer {
}
}
}
SequencerCommands::GoBackward(sequence_id) => {
let state = states.entry(sequence_id).or_default();
if let Some(sequence) = sequences.get(&sequence_id) {
let was_active = state.active;
state.go_backward(sequence.value(), &self.clock, effect_engine, frame);
let is_active = state.active;
if !is_active {
sequence_orders.shift_remove(&sequence_id);
}
if is_active && !was_active {
sequence_orders.insert(sequence_id);
}
}
}
SequencerCommands::GoTo(sequence_id, cue_id) => {
let state = states.entry(sequence_id).or_default();
if let Some(sequence) = sequences.get(&sequence_id) {
Expand Down Expand Up @@ -213,10 +227,17 @@ impl Sequencer {
result
}

pub fn sequence_go(&self, sequence: u32) {
pub fn sequence_go_forward(&self, sequence: u32) {
self.commands
.0
.send(SequencerCommands::GoForward(sequence))
.unwrap();
}

pub fn sequence_go_backward(&self, sequence: u32) {
self.commands
.0
.send(SequencerCommands::Go(sequence))
.send(SequencerCommands::GoBackward(sequence))
.unwrap();
}

Expand Down Expand Up @@ -302,7 +323,8 @@ impl Sequencer {

#[derive(Debug, Clone, Copy, PartialEq)]
enum SequencerCommands {
Go(u32),
GoForward(u32),
GoBackward(u32),
GoTo(u32, u32),
Stop(u32),
DropState(u32),
Expand Down
42 changes: 42 additions & 0 deletions crates/components/sequencer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,28 @@ impl SequenceState {
}
self.update_channel_states(sequence, clock, frame);
}

pub fn go_backward(
&mut self,
sequence: &Sequence,
clock: &impl Clock,
effect_engine: &EffectEngine,
frame: ClockFrame,
) {
self.last_go = Some(SequenceTimestamp {
time: clock.now(),
beat: frame.frame,
});
self.cue_finished_at = None;
self.stop_effects(effect_engine);
if !self.active {
self.active = true;
self.active_cue_index = sequence.cues.len().saturating_sub(1);
} else {
self.prev_cue(sequence, effect_engine, clock, frame);
}
self.update_channel_states(sequence, clock, frame);
}

pub fn go_to(
&mut self,
Expand Down Expand Up @@ -237,6 +259,26 @@ impl SequenceState {
}
}

fn prev_cue(
&mut self,
sequence: &Sequence,
effect_engine: &EffectEngine,
clock: &impl Clock,
frame: ClockFrame,
) {
if self.active_cue_index == 0 {
if sequence.wrap_around {
self.active_cue_index = sequence.cues.len().saturating_sub(1);
return;
}
self.active_cue_index = 0;
self.active = false;
self.stop(sequence, effect_engine, clock, frame);
}else {
self.active_cue_index = self.active_cue_index.saturating_sub(1);
}
}

fn update_channel_states(
&mut self,
sequence: &Sequence,
Expand Down
6 changes: 3 additions & 3 deletions crates/runtime/pipeline/nodes/sequencer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl ProcessingNode for SequencerNode {
if let Some(value) = context.read_port(PLAYBACK) {
if (state.playback > 0f64) != (value > 0f64) {
if value > 0f64 {
sequencer.sequence_go(self.sequence_id);
sequencer.sequence_go_forward(self.sequence_id);
} else {
sequencer.sequence_stop(self.sequence_id);
}
Expand All @@ -91,7 +91,7 @@ impl ProcessingNode for SequencerNode {
}
if let Some(value) = context.read_port(GO_FORWARD) {
if let Some(true) = state.go_forward.update(value) {
sequencer.sequence_go(self.sequence_id);
sequencer.sequence_go_forward(self.sequence_id);
}
}
if let Some(value) = context.read_port(STOP) {
Expand All @@ -107,7 +107,7 @@ impl ProcessingNode for SequencerNode {
if sequence_state.active {
sequencer.sequence_stop(self.sequence_id);
} else {
sequencer.sequence_go(self.sequence_id);
sequencer.sequence_go_forward(self.sequence_id);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/runtime/settings/hotkeys-linux.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ add_sequence = "alt+a"
delete = "delete"
duplicate = "ctrl+d"
go_forward = "ctrl+g"
go_backward = "ctrl+shift+g"
stop = "alt+s"

[hotkeys.effects]
Expand Down
1 change: 1 addition & 0 deletions crates/runtime/settings/hotkeys-macos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ add_sequence = "alt+a"
delete = "cmd+backspace"
duplicate = "cmd+d"
go_forward = "cmd+g"
go_backward = "cmd+shift+g"
stop = "alt+s"

[hotkeys.effects]
Expand Down
1 change: 1 addition & 0 deletions crates/runtime/settings/hotkeys-windows.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ add_sequence = "alt+a"
delete = "delete"
duplicate = "ctrl+d"
go_forward = "ctrl+g"
go_backward = "ctrl+shift+g"
stop = "alt+s"

[hotkeys.effects]
Expand Down
1 change: 1 addition & 0 deletions crates/ui/lib/api/contracts/sequencer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ abstract class SequencerApi {
Future<Sequence> duplicateSequence(int sequence);

Future<void> sequenceGoForward(int sequence);
Future<void> sequenceGoBackward(int sequence);
Future<void> sequenceStop(int sequence);

Future<Sequences> deleteSequence(int sequence);
Expand Down
5 changes: 5 additions & 0 deletions crates/ui/lib/api/plugin/sequencer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class SequencerPluginApi implements SequencerApi {
await channel.invokeMethod("sequenceGo", sequence);
}

@override
Future<void> sequenceGoBackward(int sequence) async {
await channel.invokeMethod("sequenceGoBackward", sequence);
}

@override
Future<void> sequenceStop(int sequence) async {
await channel.invokeMethod("sequenceStop", sequence);
Expand Down
21 changes: 18 additions & 3 deletions crates/ui/lib/views/sequencer/sequencer_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ class _SequencerViewState extends State<SequencerView> with SingleTickerProvider
"go_forward": () {
SequencerBloc bloc = context.read();
if (bloc.state.selectedSequenceId != null) {
_sequenceGo(bloc.state.selectedSequenceId!);
_sequenceGoForward(bloc.state.selectedSequenceId!);
}
},
"go_backward": () {
SequencerBloc bloc = context.read();
if (bloc.state.selectedSequenceId != null) {
_sequenceGoBackward(bloc.state.selectedSequenceId!);
}
},
"delete": () {
Expand Down Expand Up @@ -101,13 +107,18 @@ class _SequencerViewState extends State<SequencerView> with SingleTickerProvider
PanelActionModel(
hotkeyId: "go_forward",
label: "Go+",
onClick: () => _sequenceGo(state.selectedSequenceId!),
onClick: () => _sequenceGoForward(state.selectedSequenceId!),
disabled: state.selectedSequenceId == null,
menu: Menu(items: [
MenuItem(
label: "Add Midi Mapping",
action: () => _addMidiMappingForGo(context, state))
])),
PanelActionModel(
hotkeyId: "go_backward",
label: "Go-",
onClick: () => _sequenceGoBackward(state.selectedSequenceId!),
disabled: state.selectedSequenceId == null),
PanelActionModel(
hotkeyId: "stop",
label: "Stop",
Expand Down Expand Up @@ -181,10 +192,14 @@ class _SequencerViewState extends State<SequencerView> with SingleTickerProvider
context.read<SequencerBloc>().add(DuplicateSequence(sequenceId));
}

_sequenceGo(int sequenceId) {
_sequenceGoForward(int sequenceId) {
context.read<SequencerApi>().sequenceGoForward(sequenceId);
}

_sequenceGoBackward(int sequenceId) {
context.read<SequencerApi>().sequenceGoBackward(sequenceId);
}

_sequenceStop(int sequenceId) {
context.read<SequencerApi>().sequenceStop(sequenceId);
}
Expand Down
17 changes: 14 additions & 3 deletions crates/ui/src/plugin/channels/method/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ impl<R: RuntimeApi + 'static> MethodCallHandler for SequencerChannel<R> {
}
"sequenceGo" => {
if let Value::I64(sequence) = call.args {
self.sequence_go(sequence as u32);
self.sequence_go_forward(sequence as u32);

resp.send_ok(Value::Null);
}
}
"sequenceGoBackward" => {
if let Value::I64(sequence) = call.args {
self.sequence_go_backward(sequence as u32);

resp.send_ok(Value::Null);
}
Expand Down Expand Up @@ -156,8 +163,12 @@ impl<R: RuntimeApi + 'static> SequencerChannel<R> {
self.handler.add_sequence()
}

pub fn sequence_go(&self, sequence: u32) {
self.handler.sequence_go(sequence);
pub fn sequence_go_forward(&self, sequence: u32) {
self.handler.sequence_go_forward(sequence);
}

pub fn sequence_go_backward(&self, sequence: u32) {
self.handler.sequence_go_backward(sequence);
}

pub fn sequence_stop(&self, sequence: u32) {
Expand Down

0 comments on commit c717bbb

Please sign in to comment.