Skip to content

Commit

Permalink
deck: Decouple tempo inputs from Player
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Jan 20, 2024
1 parent 76d5852 commit 8d95a14
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ repos:
- yaml
# https://reuse.software
- repo: https://github.com/fsfe/reuse-tool
rev: v2.1.0
rev: v3.0.1
hooks:
- id: reuse
- repo: local
Expand Down
64 changes: 50 additions & 14 deletions src/deck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Cue {
pub enum PlayState {
/// Paused
Paused {
on_cue: bool,
playhead_on_cue: bool,
},
/// Previewing while (hot) cue is pressed
Previewing {
Expand All @@ -45,10 +45,14 @@ impl PlayState {
#[must_use]
pub const fn pioneer_cue_led_state(&self) -> LedState {
match self {
PlayState::Paused { on_cue: true }
PlayState::Paused {
playhead_on_cue: true,
}
| PlayState::Previewing { .. }
| PlayState::Playing => LedState::On,
PlayState::Paused { on_cue: false } => LedState::BlinkFast,
PlayState::Paused {
playhead_on_cue: false,
} => LedState::BlinkFast,
PlayState::Ended => LedState::Off,
}
}
Expand All @@ -63,6 +67,12 @@ impl PlayState {
}
}

#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Playhead {
pub position: Position,
pub is_playing: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Playable {
pub play_state: PlayState,
Expand Down Expand Up @@ -101,7 +111,7 @@ impl Default for Tempo {
pub struct PlaybackParams {
/// Playback rate
///
/// A value of 1.0 means normal playback speed. A value of 0.0 means paused.
/// A value of 1.0 means normal playback speed. A value of 0.0 means halt.
///
/// If the playback rate is negative, the media will be played backwards.
///
Expand Down Expand Up @@ -130,9 +140,7 @@ pub struct Player {
/// Cue
pub cue: Cue,

/// Tempo
pub tempo: Tempo,

/// Playback parameters
pub playback_params: PlaybackParams,
}

Expand All @@ -142,7 +150,6 @@ pub struct Player {
#[derive(Debug, Clone, Default, PartialEq)]
pub struct UpdatePlayer {
pub cue: Option<Cue>,
pub tempo: Option<Tempo>,
pub playback_params: Option<PlaybackParams>,
}

Expand All @@ -153,7 +160,8 @@ pub enum Input {
PlayPause(ButtonInput),
Sync(ButtonInput),
Position(SliderInput),
Tempo(CenterSliderInput),
RelativeTempo(CenterSliderInput),
PitchSemitones(Option<i8>),
}

#[cfg(feature = "observables")]
Expand All @@ -164,13 +172,41 @@ pub struct Observables {
pub player: discro::Publisher<Player>,
}

pub trait Device {
/// Get the playhead position
#[cfg(feature = "observables")]
impl Observables {
pub fn on_playhead_changed(&mut self, playhead_on_cue: bool) {
self.playable.modify(|playable| {
let Some(playable) = playable.as_mut() else {
return false;
};
match playable.play_state {
PlayState::Paused {
playhead_on_cue: paused_on_cue,
} => {
if playhead_on_cue != paused_on_cue {
playable.play_state = PlayState::Paused { playhead_on_cue };
return true;
}
}
PlayState::Ended => {
playable.play_state = PlayState::Paused { playhead_on_cue };
return true;
}
PlayState::Playing | PlayState::Previewing { .. } => (),
}
// Unchanged
false
});
}
}

pub trait Adapter {
/// Get the playhead
#[must_use]
fn playhead(&self) -> Position;
fn playhead(&self) -> Playhead;

/// Set the playhead position
fn set_playhead(&mut self, position: Position);
/// Set the playhead
fn set_playhead_position(&mut self, position: Position);

/// Update selected [`Player`] fields
fn update_player(&mut self, update_player: UpdatePlayer);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ pub use self::midi::{
pub mod deck;
#[cfg(feature = "observables")]
pub use deck::Observables as DeckObservables;
pub use deck::{Device as DeckDevice, Input as DeckInput};
pub use deck::{Adapter as DeckAdapter, Input as DeckInput};

#[cfg(feature = "experimental-param")]
pub mod param;
Expand Down

0 comments on commit 8d95a14

Please sign in to comment.