Skip to content

Commit

Permalink
Utilities refactored to have reusable code
Browse files Browse the repository at this point in the history
  • Loading branch information
axltxl committed Sep 21, 2023
1 parent 6b4904e commit beb41d9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 47 deletions.
68 changes: 21 additions & 47 deletions m2fs/utils/arturia.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# -*- coding: utf-8 -*-
"""
A dedicated module of utilities for using m2fs
on an Arturia MiniLab mkII
on Arturia devices
"""

from ..simc import send_event as simc_send_evt
from ..utils import encoder

ENC_MODE_ABS = 0x0
ENC_MODE_REL_2 = 0x2
ENC_MODE_REL_3 = 0x3

ENC_MODE_ABS_MIN = 0x00
ENC_MODE_ABS_MAX = 0x7F
Expand All @@ -22,6 +20,7 @@
ENC_MODE_REL_1_CCW_MAX = 0x3F # 63
ENC_MODE_REL_1_CCW_RANGE = range(ENC_MODE_REL_1_CCW_MIN, ENC_MODE_REL_1_CCW_MAX + 1)

ENC_MODE_REL_2 = 0x2
ENC_MODE_REL_2_CW_MIN = 0x01 # 1
ENC_MODE_REL_2_CW_MAX = 0x03 # 3
ENC_MODE_REL_2_CW_RANGE = range(ENC_MODE_REL_1_CW_MIN, ENC_MODE_REL_1_CW_MAX + 1)
Expand All @@ -30,6 +29,7 @@
ENC_MODE_REL_2_CCW_MAX = 0x7F # 127
ENC_MODE_REL_2_CCW_RANGE = range(ENC_MODE_REL_1_CCW_MIN, ENC_MODE_REL_1_CCW_MAX + 1)

ENC_MODE_REL_3 = 0x3
ENC_MODE_REL_3_CW_MIN = 0x41 # 17
ENC_MODE_REL_3_CW_MAX = 0x43 # 19
ENC_MODE_REL_3_CW_RANGE = range(ENC_MODE_REL_1_CW_MIN, ENC_MODE_REL_1_CW_MAX + 1)
Expand All @@ -39,15 +39,7 @@
ENC_MODE_REL_3_CCW_RANGE = range(ENC_MODE_REL_1_CCW_MIN, ENC_MODE_REL_1_CCW_MAX + 1)


def __get_rotation(value, range_cw, range_ccw):
if value in range_cw:
return range_cw.stop - value + 1
elif value in range_ccw:
return -(range_ccw.stop - value + 1)
return 0


def get_encoder_rotation(value: int, *, mode=ENC_MODE_REL_1):
def get_encoder_rotation(cc_value: int, *, mode=ENC_MODE_REL_1):
"""
In encoders with relative modes set to 1, 2 or 3,
this will tell you whether they're being rotated and in
Expand All @@ -60,44 +52,26 @@ def get_encoder_rotation(value: int, *, mode=ENC_MODE_REL_1):
"""

if mode == ENC_MODE_REL_1:
return __get_rotation(value, ENC_MODE_REL_1_CW_RANGE, ENC_MODE_REL_1_CCW_RANGE)
return encoder.get_rotation(
cc_value, ENC_MODE_REL_1_CW_RANGE, ENC_MODE_REL_1_CCW_RANGE
)
if mode == ENC_MODE_REL_2:
return __get_rotation(value, ENC_MODE_REL_2_CW_RANGE, ENC_MODE_REL_2_CCW_RANGE)
return encoder.get_rotation(
cc_value, ENC_MODE_REL_2_CW_RANGE, ENC_MODE_REL_2_CCW_RANGE
)
if mode == ENC_MODE_REL_3:
return __get_rotation(value, ENC_MODE_REL_3_CW_RANGE, ENC_MODE_REL_3_CCW_RANGE)
return encoder.get_rotation(
cc_value, ENC_MODE_REL_3_CW_RANGE, ENC_MODE_REL_3_CCW_RANGE
)

return 0


def __send_evt(e):
if isinstance(e, tuple):
simc_send_evt(*e)
else:
simc_send_evt(e)


def send_evt_on_encoder_rotation(cc_value, *, evt_cw, evt_ccw, mode):
"""
Send event based on encoder rotation
On relative modes, this function will detect whether your Arturia
MIDI controller is doing a clockwise or a counter-clockwise rotation.
In each case, it'll send a SimEvent of your choice.
"""

er = get_encoder_rotation(cc_value, mode=mode)
if er > 0:
__send_evt(evt_cw)
elif er < 0:
__send_evt(evt_ccw)


def send_evt_on_note_toggle(note, *, evt_on, evt_off):
"""
Send event based on whether a note has been set on or off
"""
def send_event_on_encoder_rotation(
cc_value: int, *, evt_cw: int, evt_ccw: int, mode: int
):
"""Send event based on encoder rotation"""

if note.on:
__send_evt(evt_on)
else:
__send_evt(evt_off)
return encoder.send_event_on_rotation(
get_encoder_rotation(cc_value, mode=mode), evt_cw=evt_cw, evt_ccw=evt_ccw
)
45 changes: 45 additions & 0 deletions m2fs/utils/encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
"""
General MIDI encoder utilities
"""

from . import event


def get_rotation(
value: int,
range_cw: range,
range_ccw: range,
):
"""
For a CC value assigned on an encoder, it returns a
negative number if it's being rotated counter-clockwise,
a positive number if it's being rotated clockwise and
zero (0) if it's not being rotated at all.
"""

if value in range_cw:
return range_cw.stop - value + 1
elif value in range_ccw:
return -(range_ccw.stop - value + 1)
return 0


def send_event_on_rotation(
er: int, # encoder rotation value (must be something returned from a function like get_rotation)
*,
evt_cw: int,
evt_ccw: int,
):
"""
Send event based on encoder rotation
On relative modes, this function will detect whether your
MIDI controller is doing a clockwise or a counter-clockwise rotation.
In each case, it'll send a SimEvent of your choice.
"""

if er > 0:
event.send(evt_cw)
elif er < 0:
event.send(evt_ccw)
13 changes: 13 additions & 0 deletions m2fs/utils/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
"""
SimConnect event utilities
"""

from ..simc import send_event as simc_send_evt


def send(e):
if isinstance(e, tuple):
simc_send_evt(*e)
else:
simc_send_evt(e)
17 changes: 17 additions & 0 deletions m2fs/utils/note.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
"""
General MIDI note utilities
"""

from ..simc import send_event as simc_send_evt
from . import event

def send_evt_on_note_toggle(note, *, evt_on, evt_off):
"""
Send event based on whether a note has been set on or off
"""

if note.on:
event.send(evt_on)
else:
event.send(evt_off)

0 comments on commit beb41d9

Please sign in to comment.