Skip to content

Commit

Permalink
Start adding output mappings for Pioneer DDJ-400
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse committed Aug 29, 2023
1 parent 9ae3a65 commit b42bbd1
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 1 deletion.
29 changes: 29 additions & 0 deletions examples/midi_dj_controller_hotplug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,33 @@ impl MidiOutputGateway<BoxedMidiOutputConnection> for KorgKaossDj {
}
}

#[derive(Default)]
struct PioneerDdj400 {
output_gateway: Option<pioneer_ddj_400::OutputGateway<BoxedMidiOutputConnection>>,
}

impl Controller for PioneerDdj400 {}

impl MidiOutputGateway<BoxedMidiOutputConnection> for PioneerDdj400 {
fn attach_midi_output_connection(
&mut self,
connection: &mut Option<BoxedMidiOutputConnection>,
) -> OutputResult<()> {
debug_assert!(self.output_gateway.is_none());
let mut output_gateway =
pioneer_ddj_400::OutputGateway::<BoxedMidiOutputConnection>::default();
output_gateway.attach_midi_output_connection(connection)?;
self.output_gateway = Some(output_gateway);
Ok(())
}

fn detach_midi_output_connection(&mut self) -> Option<BoxedMidiOutputConnection> {
self.output_gateway
.take()
.and_then(|mut output_gateway| output_gateway.detach_midi_output_connection())
}
}

fn new_midi_controller<I>(
device: &MidirDevice<I>,
output_connection: &mut Option<BoxedMidiOutputConnection>,
Expand All @@ -148,6 +175,8 @@ where
let mut controller: Box<dyn MidiController> =
if device.descriptor() == korg_kaoss_dj::MIDI_DEVICE_DESCRIPTOR {
Box::<KorgKaossDj>::default() as _
} else if device.descriptor() == pioneer_ddj_400::MIDI_DEVICE_DESCRIPTOR {
Box::<PioneerDdj400>::default() as _
} else {
return Ok(None);
};
Expand Down
31 changes: 30 additions & 1 deletion src/devices/pioneer_ddj_400/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@

use std::borrow::Cow;

use strum::{EnumCount, EnumIter, FromRepr};

use crate::{
AudioInterfaceDescriptor, ControllerDescriptor, DeviceDescriptor, MidiDeviceDescriptor,
};

pub mod input;
pub use self::input::{DeckSensor, EffectSensor, MainSensor, MidiInputEventDecoder, Sensor};

pub mod output;
pub use self::output::{
led_output_into_midi_message, DeckLed, InvalidOutputControlIndex, Led, MainLed, OutputGateway,
};

pub const AUDIO_INTERFACE_DESCRIPTOR: AudioInterfaceDescriptor = AudioInterfaceDescriptor {
num_input_channels: 0,
num_output_channels: 4,
Expand All @@ -34,14 +41,31 @@ pub const CONTROLLER_DESCRIPTOR: &ControllerDescriptor = &ControllerDescriptor {
num_effect_units: 1,
};

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, FromRepr, EnumIter, EnumCount)]
#[repr(u8)]
pub enum Deck {
/// Left
One,
/// Right
Two,
}

impl Deck {
const fn midi_channel(self) -> u8 {
match self {
Deck::One => MIDI_CHANNEL_DECK_ONE,
Deck::Two => MIDI_CHANNEL_DECK_TWO,
}
}

const fn control_index_bit_mask(self) -> u32 {
match self {
Deck::One => CONTROL_INDEX_DECK_ONE,
Deck::Two => CONTROL_INDEX_DECK_TWO,
}
}
}

const MIDI_CHANNEL_MAIN: u8 = 0x06;
const MIDI_CHANNEL_EFFECT: u8 = 0x04;
const MIDI_CHANNEL_DECK_ONE: u8 = 0x00;
Expand All @@ -66,6 +90,11 @@ const MIDI_STATUS_CC_EFFECT: u8 = MIDI_COMMAND_CC | MIDI_CHANNEL_EFFECT;
const MIDI_STATUS_CC_DECK_ONE: u8 = MIDI_COMMAND_CC | MIDI_CHANNEL_DECK_ONE;
const MIDI_STATUS_CC_DECK_TWO: u8 = MIDI_COMMAND_CC | MIDI_CHANNEL_DECK_TWO;

const MIDI_DECK_PLAYPAUSE_BUTTON: u8 = 0x0B;

const MIDI_MASTER_CUE: u8 = 0x63;
const MIDI_BEAT_FX: u8 = 0x47;

const CONTROL_INDEX_DECK_ONE: u32 = 0x0100;
const CONTROL_INDEX_DECK_TWO: u32 = 0x0200;
const CONTROL_INDEX_PERFORMANCE_DECK_ONE: u32 = 0x0300;
Expand Down
221 changes: 221 additions & 0 deletions src/devices/pioneer_ddj_400/output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
// SPDX-FileCopyrightText: The djio authors
// SPDX-License-Identifier: MPL-2.0

use derive_more::From;
use strum::{EnumCount, EnumIter, FromRepr, IntoEnumIterator as _};

use super::{
Deck, CONTROL_INDEX_DECK_BIT_MASK, CONTROL_INDEX_DECK_ONE, CONTROL_INDEX_DECK_TWO,
CONTROL_INDEX_ENUM_BIT_MASK, MIDI_BEAT_FX, MIDI_COMMAND_NOTE_ON, MIDI_DECK_PLAYPAUSE_BUTTON,
MIDI_MASTER_CUE, MIDI_STATUS_BUTTON_MAIN,
};
use crate::{
Control, ControlIndex, ControlOutputGateway, LedOutput, MidiOutputConnection,
MidiOutputGateway, OutputError, OutputResult,
};

#[derive(Debug, Clone, Copy, From)]
pub enum Led {
Main(MainLed),
Deck(Deck, DeckLed),
// TODO: Performance LEDs
}

impl Led {
#[must_use]
pub const fn deck(self) -> Option<Deck> {
match self {
Self::Main(_) => None,
Self::Deck(deck, _) => Some(deck),
}
}

#[must_use]
#[allow(clippy::missing_panics_doc)]
#[allow(clippy::match_wildcard_for_single_variants)]
pub const fn to_control_index(self) -> ControlIndex {
match self {
Self::Main(led) => ControlIndex::new(led as u32),
Self::Deck(deck, led) => ControlIndex::new(deck.control_index_bit_mask() | led as u32),
}
}
}

const LED_OFF: u8 = 0x00;
const LED_ON: u8 = 0x7f;

const fn led_to_u7(output: LedOutput) -> u8 {
match output {
LedOutput::Off => LED_OFF,
LedOutput::On => LED_ON,
}
}

/// Deck LED
#[derive(Debug, Clone, Copy, FromRepr, EnumIter, EnumCount)]
#[repr(u8)]
pub enum DeckLed {
PlayPauseButton,
CueButton,
BeatSyncButton,
LoopInButton,
LoopOutButton,
ReloopExitButton,
// -- Mixer section -- //
HeadphoneCueButton,
}

/// Main LED
#[derive(Debug, Clone, Copy, FromRepr, EnumIter, EnumCount)]
#[repr(u8)]
pub enum MainLed {
MasterCue,
BeatFx,
}

impl From<Led> for ControlIndex {
fn from(from: Led) -> Self {
from.to_control_index()
}
}

#[derive(Debug)]
pub struct InvalidOutputControlIndex;

impl TryFrom<ControlIndex> for Led {
type Error = InvalidOutputControlIndex;

fn try_from(from: ControlIndex) -> Result<Self, Self::Error> {
let value = from.value();
debug_assert!(CONTROL_INDEX_ENUM_BIT_MASK <= u8::MAX.into());
let enum_index = (value & CONTROL_INDEX_ENUM_BIT_MASK) as u8;
let deck = match value & CONTROL_INDEX_DECK_BIT_MASK {
CONTROL_INDEX_DECK_ONE => Deck::One,
CONTROL_INDEX_DECK_TWO => Deck::Two,
CONTROL_INDEX_DECK_BIT_MASK => return Err(InvalidOutputControlIndex),
_ => {
return MainLed::from_repr(enum_index)
.map(Led::Main)
.ok_or(InvalidOutputControlIndex);
}
};
DeckLed::from_repr(enum_index)
.map(|led| Led::Deck(deck, led))
.ok_or(InvalidOutputControlIndex)
}
}

#[must_use]
pub const fn led_output_into_midi_message(led: Led, output: LedOutput) -> [u8; 3] {
let (status, data1) = match led {
Led::Main(led) => match led {
MainLed::MasterCue => (MIDI_STATUS_BUTTON_MAIN, MIDI_MASTER_CUE),
MainLed::BeatFx => (MIDI_STATUS_BUTTON_MAIN, MIDI_BEAT_FX),
},
Led::Deck(deck, led) => {
let midi_channel = deck.midi_channel();
let status = MIDI_COMMAND_NOTE_ON | midi_channel;
let data1 = match led {
DeckLed::PlayPauseButton => MIDI_DECK_PLAYPAUSE_BUTTON,
DeckLed::CueButton => 0x0c,
DeckLed::BeatSyncButton => 0x58,
DeckLed::LoopInButton => 0x10,
DeckLed::LoopOutButton => 0x11,
DeckLed::ReloopExitButton => 0x4D,
DeckLed::HeadphoneCueButton => 0x54,
};
(status, data1)
}
};
let data2 = led_to_u7(output);
[status, data1, data2]
}

fn send_led_output<C: MidiOutputConnection>(
midi_output_connection: &mut C,
led: Led,
output: LedOutput,
) -> OutputResult<()> {
midi_output_connection.send_midi_output(&led_output_into_midi_message(led, output))
}

fn on_attach<C: MidiOutputConnection>(midi_output_connection: &mut C) -> OutputResult<()> {
// TODO: How to query the initial position of all knobs and faders?
turn_off_all_leds(midi_output_connection)?;
Ok(())
}

fn on_detach<C: MidiOutputConnection>(midi_output_connection: &mut C) -> OutputResult<()> {
turn_off_all_leds(midi_output_connection)?;
Ok(())
}

fn turn_off_all_leds<C: MidiOutputConnection>(midi_output_connection: &mut C) -> OutputResult<()> {
for led in MainLed::iter() {
send_led_output(midi_output_connection, led.into(), LedOutput::Off)?;
}
for deck in Deck::iter() {
for led in DeckLed::iter() {
send_led_output(midi_output_connection, Led::Deck(deck, led), LedOutput::Off)?;
}
}
Ok(())
}

#[derive(Debug)]
#[allow(missing_debug_implementations)]
pub struct OutputGateway<C> {
midi_output_connection: Option<C>,
}

impl<C> Default for OutputGateway<C> {
fn default() -> Self {
Self {
midi_output_connection: None,
}
}
}

impl<C: MidiOutputConnection> OutputGateway<C> {
pub fn send_led_output(&mut self, led: Led, output: LedOutput) -> OutputResult<()> {
let Some(midi_output_connection) = &mut self.midi_output_connection else {
return Err(OutputError::Disconnected);
};
send_led_output(midi_output_connection, led, output)
}
}

impl<C: MidiOutputConnection> ControlOutputGateway for OutputGateway<C> {
fn send_output(&mut self, output: &Control) -> OutputResult<()> {
let Control { index, value } = *output;
let led = Led::try_from(index).map_err(|InvalidOutputControlIndex| OutputError::Send {
msg: format!("No LED with control index {index}").into(),
})?;
self.send_led_output(led, value.into())
}
}

impl<C: MidiOutputConnection> MidiOutputGateway<C> for OutputGateway<C> {
fn attach_midi_output_connection(
&mut self,
midi_output_connection: &mut Option<C>,
) -> OutputResult<()> {
assert!(self.midi_output_connection.is_none());
assert!(midi_output_connection.is_some());
// Initialize the hardware
on_attach(midi_output_connection.as_mut().expect("Some"))?;
// Finally take ownership
self.midi_output_connection = midi_output_connection.take();
Ok(())
}

fn detach_midi_output_connection(&mut self) -> Option<C> {
// Release ownership
let mut midi_output_connection = self.midi_output_connection.take()?;
// Reset the hardware
if let Err(err) = on_detach(&mut midi_output_connection) {
log::warn!("Failed reset MIDI hardware on detach: {err}");
}
Some(midi_output_connection)
}
}

0 comments on commit b42bbd1

Please sign in to comment.