SpotlightKid / python-rtmidi

Python bindings for the cross-platform MIDI I/O library RtMidi
https://spotlightkid.github.io/python-rtmidi/
Other
349 stars 65 forks source link

Understanding channels #120

Closed ghost closed 1 year ago

ghost commented 1 year ago

I would be very glad If someone would help me understand the concept of channels. In the following (from examples directory):

note_on = [NOTE_ON, 60, 112]  # channel 1, middle C, velocity 112
note_off = [NOTE_OFF, 60, 0]

How could I send a note to different channels? I.e. why can't I use channel numbers in the note_on list to sepcify the channel?

SpotlightKid commented 1 year ago

in the midi protocol, the channel is encoded as the lower nibble (4 bits) of the status (first) byte of channel mode messages.

In practical terms, just use a bit-wise OR (|) to combine the status byte with the (zero-based) channel number.

ghost commented 1 year ago

in the midi protocol, the channel is encoded as the lower nibble (4 bits) of the status (first) byte of channel mode messages.

In practical terms, just use a bit-wise OR (|) to combine the status byte with the (zero-based) channel number.

Thank you for your fast answer! I already read on channel in midi spec. Why would you use bit-wise or instead of adding/subtracting to the second byte of note_on/off messages?

And since you labeled this thread correctly as q question I would ask another question just here: could you point me to an example for playing multiple simultaneous voices through different channels? I would appreciate it very much!

SpotlightKid commented 1 year ago

Addition is also fine. Just remember that the channel is zero based and has to be <= 15.

SpotlightKid commented 1 year ago

Polyphony is just a matter of sending multiple note on events (possibly with different channels) one after another, and their respective note offs at a later time. The hard part is scheduling the sending of these messages at the right times. You can look at the sequencer example in the repo for one possible way to do that.