Example of real-time MIDI use?

Would there be some code that demonstrates real-time MIDI use? I’m thinking of the case in which I want an Instrument to write to a MIDI port and channel that I define. I’d just like some code to model it after.

Sure! I really should make a video about this, but the main idea is that you say:

from scamp import *

s = Session()
midi_part = s.new_midi_part("my midi part", "IAC")

…and then you play notes as usual, e.g.:

midi_part.play_note(70, 0.8, 1)

In the call to new_midi_part, “my midi part” is just the name of the part as it would appear on the score/sheet music and doesn’t matter much, while “IAC” should be replaced with whatever midi device you want to send the messages to. It then searches for a port number with a device of that name. (Alternatively, if you know the port, you can say, for example, s.new_midi_part("my midi part", 2))

I use “IAC” as an example, because that’s name of the virtual MIDI cable on macs that lets you loop back MIDI to another application. For windows or linux it would be different.

Anyway, that’s the basics!

If you want to specify a particular channel, you can say:

midi_part = s.new_midi_part("my midi part", "IAC", start_channel=7, num_channels=1)

By default, because SCAMP is trying to be microtonal, it juggles notes between multiple channels to avoid conflicting pitch bend messages. num_channels=1 restricts it to just a single channel, and start_channel=7 specifies which channel that should be. (Somewhat awkwardly, this is actually channel number 8 in normal MIDI terminology, because SCAMP starts counting from 0 instead of 1. I should probably change this!)

1 Like

Oh, and you mention “real-time”. Depending on your use case, you may want to use Session().run_as_server(). See the discussion here:

1 Like

Thanks Marc. By real-time I just mean not a MIDI file. It won’t do any real-time computations or interactivity. I’ll read the discussion in any case.

Thanks for the heads-up about MIDI channel offset by one.

-Mike

1 Like