diff --git a/src/control.rs b/src/control.rs index 4f476839..086314e0 100644 --- a/src/control.rs +++ b/src/control.rs @@ -5,7 +5,7 @@ pub mod sampler; pub use self::focus::*; -use crate::{core::*, model::*, handle, App}; +use crate::{core::*, handle, App}; handle!(App |self, e| { if let Some(ref mut modal) = self.modal { @@ -18,82 +18,117 @@ handle!(App |self, e| { }); const KEYMAP: &'static [KeyBinding] = keymap!(App { - [F(1), NONE, "toggle_help", "toggle help", toggle_help], + [Char(' '), NONE, "toggle_play", "play or pause", |app: &mut App| { + app.toggle_play()?; + Ok(true) + }], + [Char('a'), CONTROL, "add_scene", "add a new scene", |app: &mut App| { + app.add_scene(None)?; + Ok(true) + }], + [Char('t'), CONTROL, "add_track", "add a new track", |app: &mut App| { + app.add_track(None)?; + Ok(true) + }], + [Char('`'), NONE, "switch_mode", "switch the display mode", |app: &mut App| { + match app.section { + 0 => {app.grid_mode = !app.grid_mode; Ok(true)}, + 1 => {app.chain_mode = !app.chain_mode; Ok(true)}, + 2 => {app.seq_mode = !app.seq_mode; Ok(true)}, + _ => Ok(false) + } + }], + [F(1), NONE, "toggle_help", "toggle help", |_: &mut App| {Ok(true)}], + [Char('r'), NONE, "toggle_record", "toggle recording", |_: &mut App| {Ok(true)}], + [Char('d'), NONE, "toggle_overdub", "toggle overdub", |_: &mut App| {Ok(true)}], + [Char('m'), NONE, "toggle_monitor", "toggle input monitoring", |_: &mut App| {Ok(true)}], + [Up, NONE, "cursor_up", "move cursor up", |app: &mut App| { + if app.entered { + match app.section { + 0 => match app.grid_mode { + false => {app.scene_cursor = app.scene_cursor.saturating_sub(1); Ok(true)}, + true => {app.track_cursor = app.track_cursor.saturating_sub(1); Ok(true)}, + }, + _ => Ok(false) + } + } else { + focus_prev(app) + } + }], + [Down, NONE, "cursor_down", "move cursor down", |app: &mut App| { + if app.entered { + match app.section { + 0 => match app.grid_mode { + false => {app.scene_cursor = app.scenes.len().min(app.scene_cursor + 1); Ok(true)}, + true => {app.track_cursor = app.tracks.len().min(app.track_cursor + 1); Ok(true)}, + }, + _ => Ok(false) + } + } else { + focus_next(app) + } + }], + [Left, NONE, "cursor_left", "move cursor left", |app: &mut App| { + match app.section { + 0 => match app.grid_mode { + false => {app.track_cursor = app.track_cursor.saturating_sub(1); Ok(true)}, + true => {app.scene_cursor = app.scene_cursor.saturating_sub(1); Ok(true)}, + }, + 1 => { + if let Some((_, track)) = app.track_mut() { + track.device = track.device.saturating_sub(1); + Ok(true) + } else { + Ok(false) + } + }, + 2 => { + app.time_cursor = app.time_cursor.saturating_sub(1); + Ok(true) + }, + _ => Ok(false) + } + }], + [Right, NONE, "cursor_right", "move cursor right", |app: &mut App| { + match app.section { + 0 => match app.grid_mode { + false => {app.track_cursor = app.tracks.len().min(app.track_cursor + 1); Ok(true)}, + true => {app.scene_cursor = app.scenes.len().min(app.scene_cursor + 1); Ok(true)}, + }, + 1 => { + if let Some((_, track)) = app.track_mut() { + track.device = (track.device + 1).min(track.devices.len().saturating_sub(1)); + Ok(true) + } else { + Ok(false) + } + }, + 2 => { + app.time_cursor = app.time_cursor + 1; + Ok(true) + }, + _ => Ok(false) + } + }], [Tab, NONE, "focus_next", "focus next area", focus_next], [Tab, SHIFT, "focus_prev", "focus previous area", focus_prev], - [Up, NONE, "cursor_up", "move cursor up", cursor_up], - [Down, NONE, "cursor_down", "move cursor down", cursor_down], - [Left, NONE, "cursor_left", "move cursor left", cursor_left], - [Right, NONE, "cursor_right", "move cursor right", cursor_right], - [Char('.'), NONE, "increment", "increment value at cursor", increment], - [Char(','), NONE, "decrement", "decrement value at cursor", decrement], + [Char('.'), NONE, "increment", "increment value", increment], + [Char(','), NONE, "decrement", "decrement value", decrement], [Delete, CONTROL, "delete", "delete track", delete], [Char('d'), CONTROL, "duplicate", "duplicate scene or track", duplicate], [Enter, NONE, "activate", "activate item at cursor", enter], [Esc, NONE, "escape", "unfocus", escape], - [Char(' '), NONE, "toggle_play", "play or pause", toggle_play], - [Char('r'), NONE, "toggle_record", "toggle recording", toggle_record], - [Char('d'), NONE, "toggle_overdub", "toggle overdub", toggle_overdub], - [Char('m'), NONE, "toggle_monitor", "toggle input monitoring", toggle_monitor], [Char('r'), CONTROL, "rename", "rename current element", rename], - [Char('t'), CONTROL, "add_track", "add a new track", add_track], - [Char('a'), CONTROL, "add_scene", "add a new scene", add_scene], - [Char('`'), NONE, "switch_mode", "switch the display mode", switch_mode], +// [Char('='), NONE, "zoom_in", "Zoom in", zoom_in], +// [Char('-'), NONE, "zoom_out", "Zoom out", zoom_out], +// [Char('a'), NONE, "note_add", "Add note", note_add], +// [Char('z'), NONE, "note_del", "Delete note", note_del], +// [CapsLock, NONE, "advance", "Toggle auto advance", nop], +// [Char('w'), NONE, "rest", "Advance by note duration", nop], +// [Char('s'), NONE, "stop_and_rewind", "Stop and rewind", stop_and_rewind], +// [Char('q'), NONE, "quantize_next", "Next quantize value", quantize_next], +// [Char('Q'), SHIFT, "quantize_prev", "Previous quantize value", quantize_prev], }); -//-pub const KEYMAP: &'static [KeyBinding] = keymap!(Sequencer { -//- [Up, NONE, "cursor_up", "move cursor up", cursor_up], -//- [Down, NONE, "cursor_down", "move cursor down", cursor_down], -//- [Left, NONE, "cursor_left", "move cursor left", cursor_left], -//- [Right, NONE, "cursor_right", "move cursor right", cursor_right], -//- [Char('.'), NONE, "cursor_inc", "increase note duration", cursor_duration_inc], -//- [Char(','), NONE, "cursor_dec", "decrease note duration", cursor_duration_dec], -//- [Char('`'), NONE, "mode_next", "Next view mode", mode_next], -//- [Char('='), NONE, "zoom_in", "Zoom in", zoom_in], -//- [Char('-'), NONE, "zoom_out", "Zoom out", zoom_out], -//- [Char('a'), NONE, "note_add", "Add note", note_add], -//- [Char('z'), NONE, "note_del", "Delete note", note_del], -//- [CapsLock, NONE, "advance", "Toggle auto advance", nop], -//- [Char('w'), NONE, "rest", "Advance by note duration", nop], -//- [Char(' '), NONE, "toggle_play", "Toggle play/pause", toggle_play], -//- [Char('r'), NONE, "toggle_record", "Toggle recording", toggle_record], -//- [Char('d'), NONE, "toggle_overdub", "Toggle overdub", toggle_overdub], -//- [Char('m'), NONE, "toggle_monitor", "Toggle input monitoring", toggle_monitor], -//- [Char('s'), NONE, "stop_and_rewind", "Stop and rewind", stop_and_rewind], -//- [Char('q'), NONE, "quantize_next", "Next quantize value", quantize_next], -//- [Char('Q'), SHIFT, "quantize_prev", "Previous quantize value", quantize_prev], -//-}); - -fn toggle_play (app: &mut App) -> Usually { - app.playing = match app.playing.expect("after jack init") { - TransportState::Stopped => { - app.transport.as_ref().unwrap().start()?; - Some(TransportState::Starting) - }, - _ => { - app.transport.as_ref().unwrap().stop()?; - app.transport.as_ref().unwrap().locate(0)?; - Some(TransportState::Stopped) - }, - }; - Ok(true) -} - -fn toggle_record (_: &mut App) -> Usually { Ok(true) } - -fn toggle_overdub (_: &mut App) -> Usually { Ok(true) } - -fn toggle_monitor (_: &mut App) -> Usually { Ok(true) } - -fn toggle_help (_: &mut App) -> Usually { Ok(true) } - -fn switch_mode (app: &mut App) -> Usually { - match app.section { - 0 => {app.grid_mode = !app.grid_mode; Ok(true)}, - 1 => {app.chain_mode = !app.chain_mode; Ok(true)}, - 2 => {app.seq_mode = !app.seq_mode; Ok(true)}, - _ => Ok(false) - } -} fn focus_next (app: &mut App) -> Usually { if app.section >= 2 { @@ -113,54 +148,6 @@ fn focus_prev (app: &mut App) -> Usually { Ok(true) } -fn cursor_up (app: &mut App) -> Usually { - if app.entered { - match app.section { - 0 => match app.grid_mode { - false => {app.scene_cursor = app.scene_cursor.saturating_sub(1); Ok(true)}, - true => {app.track_cursor = app.track_cursor.saturating_sub(1); Ok(true)}, - }, - _ => Ok(false) - } - } else { - focus_prev(app) - } -} - -fn cursor_down (app: &mut App) -> Usually { - if app.entered { - match app.section { - 0 => match app.grid_mode { - false => {app.scene_cursor = app.scenes.len().min(app.scene_cursor + 1); Ok(true)}, - true => {app.track_cursor = app.tracks.len().min(app.track_cursor + 1); Ok(true)}, - }, - _ => Ok(false) - } - } else { - focus_next(app) - } -} - -fn cursor_left (app: &mut App) -> Usually { - match app.section { - 0 => match app.grid_mode { - false => {app.track_cursor = app.track_cursor.saturating_sub(1); Ok(true)}, - true => {app.scene_cursor = app.scene_cursor.saturating_sub(1); Ok(true)}, - }, - _ => Ok(false) - } -} - -fn cursor_right (app: &mut App) -> Usually { - match app.section { - 0 => match app.grid_mode { - false => {app.track_cursor = app.tracks.len().min(app.track_cursor + 1); Ok(true)}, - true => {app.scene_cursor = app.scenes.len().min(app.scene_cursor + 1); Ok(true)}, - }, - _ => Ok(false) - } -} - fn increment (app: &mut App) -> Usually { match app.section { 0 => clip_next(app), @@ -291,20 +278,6 @@ fn escape (app: &mut App) -> Usually { } } -fn add_scene (app: &mut App) -> Usually { - let name = format!("Scene {}", app.scenes.len() + 1); - app.scenes.push(Scene::new(&name, vec![])); - app.scene_cursor = app.scenes.len(); - Ok(true) -} - -fn add_track (app: &mut App) -> Usually { - let name = format!("Track {}", app.tracks.len() + 1); - app.tracks.push(Track::new(&name, app.jack.as_ref().unwrap().as_client(), None, None)?); - app.track_cursor = app.tracks.len(); - Ok(true) -} - fn delete_track (app: &mut App) -> Usually { if app.tracks.len() > 0 { let track = app.tracks.remove(app.track_cursor); diff --git a/src/main.rs b/src/main.rs index 1a91138c..c3279dc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,6 +73,7 @@ pub fn main () -> Usually<()> { state.midi_in = Some(client.register_port("midi-in", MidiIn)?); state.transport = Some(client.transport()); state.playing = Some(TransportState::Stopped); + state.time_zoom = 24; state.jack = Some(jack); Ok(()) })) @@ -156,6 +157,20 @@ process!(App |self, _client, scope| { }); impl App { + pub fn toggle_play (&mut self) -> Usually<()> { + self.playing = match self.playing.expect("after jack init") { + TransportState::Stopped => { + self.transport.as_ref().unwrap().start()?; + Some(TransportState::Starting) + }, + _ => { + self.transport.as_ref().unwrap().stop()?; + self.transport.as_ref().unwrap().locate(0)?; + Some(TransportState::Stopped) + }, + }; + Ok(()) + } pub fn connect_tracks (&self) -> Usually<()> { //let (client, _status) = Client::new( //&format!("{}-init", &self.name), ClientOptions::NO_START_SERVER @@ -184,6 +199,18 @@ impl App { //} Ok(()) } + pub fn add_scene (&mut self, name: Option<&str>) -> Usually<&mut Scene> { + let name = name.ok_or_else(||format!("Scene {}", self.scenes.len() + 1))?; + self.scenes.push(Scene::new(&name, vec![])); + self.scene_cursor = self.scenes.len(); + Ok(&mut self.scenes[self.scene_cursor - 1]) + } + pub fn add_track (&mut self, name: Option<&str>) -> Usually<&mut Track> { + let name = name.ok_or_else(||format!("Track {}", self.tracks.len() + 1))?; + self.tracks.push(Track::new(&name, self.jack.as_ref().unwrap().as_client(), None, None)?); + self.track_cursor = self.tracks.len(); + Ok(&mut self.tracks[self.track_cursor - 1]) + } pub fn track (&self) -> Option<(usize, &Track)> { match self.track_cursor { 0 => None, _ => { let id = self.track_cursor as usize - 1; diff --git a/src/model/track.rs b/src/model/track.rs index 9d352af9..0f78dd4f 100644 --- a/src/model/track.rs +++ b/src/model/track.rs @@ -43,10 +43,32 @@ impl Track { device: 0, }) } + pub fn phrase (&self) -> Option<&Phrase> { + if let Some(phrase) = self.sequence { + return self.phrases.get(phrase) + } else { + None + } + } } impl PortList for Track { - fn midi_ins (&self) -> Usually> { Ok(vec![]) } - fn midi_outs (&self) -> Usually> { Ok(vec![self.midi_out.name()?]) } + fn midi_outs (&self) -> Usually> { + Ok(vec![self.midi_out.name()?]) + } + fn midi_ins (&self) -> Usually> { + if let Some(device) = self.devices.get(0) { + device.midi_ins() + } else { + Ok(vec![]) + } + } + fn audio_outs (&self) -> Usually> { + if let Some(device) = self.devices.get(self.devices.len().saturating_sub(1)) { + device.audio_outs() + } else { + Ok(vec![]) + } + } } diff --git a/src/view.rs b/src/view.rs index 4a31679c..35877a15 100644 --- a/src/view.rs +++ b/src/view.rs @@ -39,16 +39,17 @@ render!(App |self, buf, area| { }.draw()?.height; if self.track_cursor > 0 { - let track = self.tracks.get(self.track_cursor - 1); - y = y + ChainView { - focused: self.section == 1, - chain: track, - }.render(buf, Rect { x, y, width, height: height / 3 })?.height; + + let track = self.tracks.get(self.track_cursor - 1).unwrap(); + + y = y + ChainView { focused: self.section == 1, track: Some(track) } + .render(buf, Rect { x, y, width, height: height / 3 })?.height; y = y + SequencerView { focused: self.section == 2, ppq: self.timebase.ppq() as usize, - phrase: track.map(|t|&t.phrases[0]), + now: self.timebase.frames_pulses(self.playhead as f64) as usize, + phrase: track.phrases.get(0), time_cursor: self.time_cursor, time_start: self.time_start, time_zoom: self.time_zoom, diff --git a/src/view/chain.rs b/src/view/chain.rs index 77afee1e..be06e8c1 100644 --- a/src/view/chain.rs +++ b/src/view/chain.rs @@ -11,70 +11,92 @@ pub enum ChainViewMode { pub struct ChainView<'a> { pub focused: bool, - pub chain: Option<&'a Track>, + pub track: Option<&'a Track>, } impl<'a> Render for ChainView<'a> { fn render (&self, buf: &mut Buffer, area: Rect) -> Usually { let style = Some(if self.focused { - Style::default().green() - } else { Style::default().green().dim() + } else { + Style::default().dim() }); - let (area, _plugins) = if let Some(ref chain) = self.chain { - draw_as_row(&*chain, buf, area, style)? + let Rect { x, y, width, height } = area; + if self.track.is_none() { + let label = "No track selected"; + label.blit( + buf, + x + (width - label.len() as u16) / 2, + y + height / 2, + Some(Style::default().dim().bold()) + ); + } + lozenge_left(buf, x, y, height, style); + let (area, _plugins) = if let Some(ref chain) = self.track { + self.draw_as_row(buf, area, style)? } else { (area, vec![]) }; - let Rect { x, y, width, height} = area; - lozenge_left(buf, x, y, height, style); lozenge_right(buf, x + width - 1, y, height, style); Ok(area) } } -pub fn draw_as_row ( - state: &Track, buf: &mut Buffer, area: Rect, style: Option