-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidi_controller.py
More file actions
374 lines (304 loc) · 12.4 KB
/
Copy pathmidi_controller.py
File metadata and controls
374 lines (304 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env python3
"""
MIDI Controller Support for DJ Mixer
Handles MIDI input and mapping to mixer controls
"""
from typing import Dict, Callable, Optional, List, Any
from dataclasses import dataclass
from enum import Enum
class MIDIControlType(Enum):
"""Types of MIDI controls"""
KNOB = "knob" # Continuous control (0-127)
FADER = "fader" # Continuous control (0-127)
BUTTON = "button" # On/Off (0 or 127)
PAD = "pad" # Trigger button
ENCODER = "encoder" # Relative encoder
@dataclass
class MIDIMapping:
"""Mapping between MIDI control and mixer function"""
control_number: int
control_type: MIDIControlType
function_name: str
min_value: float = 0.0
max_value: float = 1.0
channel: int = 0
class MIDIController:
"""MIDI controller interface for DJ Mixer"""
def __init__(self):
self.mappings: Dict[int, MIDIMapping] = {}
self.callbacks: Dict[str, Callable] = {}
self.midi_input = None
self.connected = False
self.device_name = ""
def connect(self, device_name: Optional[str] = None) -> bool:
"""
Connect to MIDI device
Args:
device_name: Name of MIDI device to connect to (None for first available)
Returns:
True if connected successfully
"""
try:
# Try to import mido (MIDI library)
import mido
# List available input devices
available_devices = mido.get_input_names()
if not available_devices:
print("No MIDI input devices found")
return False
# Select device
if device_name:
if device_name not in available_devices:
print(f"MIDI device '{device_name}' not found")
return False
self.device_name = device_name
else:
self.device_name = available_devices[0]
# Open MIDI input
self.midi_input = mido.open_input(self.device_name)
self.connected = True
print(f"Connected to MIDI device: {self.device_name}")
return True
except ImportError:
print(
"MIDI support not available. Install 'mido' package: pip install mido python-rtmidi"
)
return False
except Exception as e:
print(f"Error connecting to MIDI device: {e}")
return False
def disconnect(self) -> None:
"""Disconnect from MIDI device"""
if self.midi_input:
try:
self.midi_input.close()
except:
pass
self.midi_input = None
self.connected = False
print("MIDI device disconnected")
def get_available_devices(self) -> List[str]:
"""Get list of available MIDI input devices"""
try:
import mido
return mido.get_input_names()
except ImportError:
return []
except Exception as e:
print(f"Error getting MIDI devices: {e}")
return []
def add_mapping(
self,
control_number: int,
control_type: MIDIControlType,
function_name: str,
min_value: float = 0.0,
max_value: float = 1.0,
channel: int = 0,
) -> None:
"""Add a MIDI control mapping"""
mapping = MIDIMapping(
control_number=control_number,
control_type=control_type,
function_name=function_name,
min_value=min_value,
max_value=max_value,
channel=channel,
)
self.mappings[control_number] = mapping
def remove_mapping(self, control_number: int) -> bool:
"""Remove a MIDI control mapping"""
if control_number in self.mappings:
del self.mappings[control_number]
return True
return False
def register_callback(self, function_name: str, callback: Callable) -> None:
"""Register a callback function for a mixer function"""
self.callbacks[function_name] = callback
def unregister_callback(self, function_name: str) -> None:
"""Unregister a callback function"""
if function_name in self.callbacks:
del self.callbacks[function_name]
def _normalize_midi_value(self, midi_value: int, mapping: MIDIMapping) -> float:
"""Normalize MIDI value (0-127) to mapped range"""
normalized = midi_value / 127.0
return mapping.min_value + normalized * (mapping.max_value - mapping.min_value)
def process_message(self, message: Any) -> None:
"""Process incoming MIDI message"""
try:
# Extract message data
if hasattr(message, "control"):
control_number = message.control
value = message.value
elif hasattr(message, "note"):
control_number = message.note
value = message.velocity
else:
return
# Check if we have a mapping for this control
if control_number not in self.mappings:
return
mapping = self.mappings[control_number]
# Get callback function
if mapping.function_name not in self.callbacks:
return
callback = self.callbacks[mapping.function_name]
# Normalize value and call callback
if mapping.control_type in [MIDIControlType.KNOB, MIDIControlType.FADER]:
normalized_value = self._normalize_midi_value(value, mapping)
callback(normalized_value)
elif mapping.control_type in [MIDIControlType.BUTTON, MIDIControlType.PAD]:
# Button pressed (value > 0)
if value > 0:
callback(True)
else:
callback(False)
except Exception as e:
print(f"Error processing MIDI message: {e}")
def start_listening(self) -> None:
"""Start listening for MIDI messages (blocking)"""
if not self.connected or not self.midi_input:
print("MIDI device not connected")
return
print(f"Listening for MIDI messages from {self.device_name}...")
print("Press Ctrl+C to stop")
try:
for message in self.midi_input:
self.process_message(message)
except KeyboardInterrupt:
print("\nStopped listening")
def poll_messages(self, timeout: float = 0.0) -> None:
"""Poll for MIDI messages (non-blocking)"""
if not self.connected or not self.midi_input:
return
try:
for message in self.midi_input.iter_pending():
self.process_message(message)
except Exception as e:
print(f"Error polling MIDI: {e}")
def get_mappings(self) -> List[MIDIMapping]:
"""Get all current MIDI mappings"""
return list(self.mappings.values())
def clear_mappings(self) -> None:
"""Clear all MIDI mappings"""
self.mappings.clear()
def load_mapping_preset(self, preset_name: str) -> bool:
"""Load a predefined mapping preset"""
presets = {
"generic_dj": self._generic_dj_preset,
"pioneer_ddj": self._pioneer_ddj_preset,
"traktor_kontrol": self._traktor_kontrol_preset,
}
if preset_name not in presets:
return False
self.clear_mappings()
presets[preset_name]()
return True
def _generic_dj_preset(self) -> None:
"""Generic DJ controller mapping"""
# Crossfader
self.add_mapping(0, MIDIControlType.FADER, "crossfader", 0.0, 1.0)
# Deck 1 controls
self.add_mapping(1, MIDIControlType.FADER, "deck1_volume", 0.0, 1.0)
self.add_mapping(16, MIDIControlType.BUTTON, "deck1_play")
self.add_mapping(17, MIDIControlType.BUTTON, "deck1_cue")
# Deck 2 controls
self.add_mapping(2, MIDIControlType.FADER, "deck2_volume", 0.0, 1.0)
self.add_mapping(18, MIDIControlType.BUTTON, "deck2_play")
self.add_mapping(19, MIDIControlType.BUTTON, "deck2_cue")
# Master volume
self.add_mapping(7, MIDIControlType.FADER, "master_volume", 0.0, 1.0)
# EQ controls (Deck 1)
self.add_mapping(8, MIDIControlType.KNOB, "deck1_eq_low", 0.0, 2.0)
self.add_mapping(9, MIDIControlType.KNOB, "deck1_eq_mid", 0.0, 2.0)
self.add_mapping(10, MIDIControlType.KNOB, "deck1_eq_high", 0.0, 2.0)
# EQ controls (Deck 2)
self.add_mapping(11, MIDIControlType.KNOB, "deck2_eq_low", 0.0, 2.0)
self.add_mapping(12, MIDIControlType.KNOB, "deck2_eq_mid", 0.0, 2.0)
self.add_mapping(13, MIDIControlType.KNOB, "deck2_eq_high", 0.0, 2.0)
def _pioneer_ddj_preset(self) -> None:
"""Pioneer DDJ controller mapping"""
# Similar to generic but with Pioneer-specific CC numbers
self._generic_dj_preset()
def _traktor_kontrol_preset(self) -> None:
"""Traktor Kontrol controller mapping"""
# Similar to generic but with Traktor-specific CC numbers
self._generic_dj_preset()
def save_mappings(self, file_path: str) -> bool:
"""Save current mappings to JSON file"""
import json
try:
mappings_data = {
control_num: {
"control_number": m.control_number,
"control_type": m.control_type.value,
"function_name": m.function_name,
"min_value": m.min_value,
"max_value": m.max_value,
"channel": m.channel,
}
for control_num, m in self.mappings.items()
}
with open(file_path, "w") as f:
json.dump(mappings_data, f, indent=2)
return True
except Exception as e:
print(f"Error saving mappings: {e}")
return False
def load_mappings(self, file_path: str) -> bool:
"""Load mappings from JSON file"""
import json
try:
with open(file_path, "r") as f:
mappings_data = json.load(f)
self.clear_mappings()
for control_num, data in mappings_data.items():
control_type = MIDIControlType(data["control_type"])
self.add_mapping(
control_number=data["control_number"],
control_type=control_type,
function_name=data["function_name"],
min_value=data["min_value"],
max_value=data["max_value"],
channel=data["channel"],
)
return True
except Exception as e:
print(f"Error loading mappings: {e}")
return False
# Mock MIDI Controller for testing without hardware
class MockMIDIController(MIDIController):
"""Mock MIDI controller for testing"""
def connect(self, device_name: Optional[str] = None) -> bool:
"""Mock connection"""
self.connected = True
self.device_name = device_name or "Mock MIDI Device"
print(f"[MOCK] Connected to MIDI device: {self.device_name}")
return True
def disconnect(self) -> None:
"""Mock disconnection"""
self.connected = False
print("[MOCK] MIDI device disconnected")
def get_available_devices(self) -> List[str]:
"""Mock device list"""
return ["Mock DJ Controller 1", "Mock DJ Controller 2", "Virtual MIDI Device"]
def simulate_control_change(self, control_number: int, value: int) -> None:
"""Simulate a MIDI control change for testing"""
print(f"[MOCK] MIDI CC {control_number}: {value}")
# Create mock message object
class MockMessage:
def __init__(self, control, value):
self.control = control
self.value = value
message = MockMessage(control_number, value)
self.process_message(message)
def simulate_note(self, note_number: int, velocity: int) -> None:
"""Simulate a MIDI note event for testing"""
print(f"[MOCK] MIDI Note {note_number}: {velocity}")
# Create mock message object
class MockMessage:
def __init__(self, note, velocity):
self.note = note
self.velocity = velocity
message = MockMessage(note_number, velocity)
self.process_message(message)