Jajcus / python-alsa-midi

Python interface for ALSA MIDI sequencer
MIT License
13 stars 2 forks source link

how to distinguish between CC #2

Closed Daniele71 closed 2 years ago

Daniele71 commented 2 years ago

Hello, on my midi keyboard all ancoders are mapped to CC 10. With python-Jackclient I can distinguish between them because it returns all byte, so encoders are 176-10, 177-10, 178-10... but wuth alsa-midi, ControlChangeEvent() returns only 10. Am I doing something wrong ? How to distinguish between them ? Thanks, Daniele.

Jajcus commented 2 years ago

Can you show your code? Where do you get this '10' from? And what are the exact sequences you get in python-Jackclient?

The ControlChangeEvent object should contain all the data sent by the keyboard with the ControlChange MIDI message. In some cases one parameter change can cause two events being sent (for 14 bit values).

If it is more convenient for you to work with midi bytes you can pass prefer_bytes=True to event_input() method to receive MidiBytesEvent instead of ControlChangeEvent.

Daniele71 commented 2 years ago

I just started learnig Python and PyQt and I'd like to make some midi stuff with both backends, so I'm trying to get same results from both. 10 comes from event.param. Code:

client = SequencerClient("my client") port = client.create_port("output", caps=READ_PORT) dest_port = client.list_ports(output=True)[0] inport = client.create_port("inout")

parse input event

while True: event = client.event_input() print(repr(event)) etype = str(event.type).split('.')[1] if etype == 'NOTEON': print(event.note) if etype == 'CONTROLLER': print('CTRL: '+ str(event.param) + ' Value: '+str(event.value))

Code for jackClient it's almost this: https://jackclient-python.readthedocs.io/en/0.5.3/examples.html#midi-chord-generator

for offset, indata in inport.incoming_midi_events(): if len(indata) == 3: status, pitch, vel = struct.unpack('3B', indata)

status = 176, pitch = 10 and vel is the value like event.value

I just need a way to indentify the controller with both backends

Thanks.

Jajcus commented 2 years ago

First (off-topic, sorry), you are testing event type in a very convoluted (and inefficient) way. Doing (str) on an object and then parsing the result is probably never the right way. The Python way to do it would be:

if isinstance(event, NoteOnEvent):
    ...
elif isinstance(event, ControlChangeEvent):
    ...

or:

if event.type == EventType.NOTEON:
    ...
elif event.type == EventType.CONTROLLER:
    ...

Second: Your 'status' as read from Jack includes channel number. Control change status byte is 0xBn where 'n' is channel number. So byte sequence '176 10 22' is a control change message in channel 0 (or 1 – MIDI channels can be numbered either way), for controller 10 with value 22. And '177 10 66' is control change message in channel 1 (or 2, depending how you count), controller 10 and value 66.

The channel number is available in the channel attribute of the ControlChangeEvent:

if isinstance(event, ControlChangeEvent):
    print(f"Control change received on channel {event.channel}:  controller {event.param} value: {event.value}")
Daniele71 commented 2 years ago

Thanks for the explanation (also for hints about code) but I still don't understand how to get compatible results between alsa and jack. Sorry if this has become more of a request for help than an issue.. Thanks. EDIT: found this: ei = int('0xb'+str(event.channel),0) # match jackClient output

Jajcus commented 2 years ago

Please, no str() + int(), that is a huge 'code smell'.

If you want convert this way (from ControlChangeEvent to the status code like you get from jack), then try this:

status = 0x0b + event.channel

Or, the same without hex:

status = 176 + event.channel