How does the Sonic Pi code (.rb) convert or output .mid files on macOS?

How does the Sonic Pi code (.rb) convert or output .mid files on macOS?

It doesnt - but you could record midi that Sonic Pi is sending with some external program or DAW like reaper.

To follow up @amiika’s reply . I’m not quite sure what you are asking. Sonic Pi can send midi commands to an external synth using commands detailed in the language help file, howver it cannot export a midi file. It can also detect incoming midi commands, but it is up to the program to tell it what to do with them. For example it might just print the midi note values received, but it cannot directly play a midi file, although you can write programs for example to allow a midi keyboard to control a built in sonic pi synth.
I have also used external scripts to convert the information in a simple sngle instrument midifile into sonic pi commands which can play a built in sonic pi synth. Multi parts can be procesed as a series of separate parts which can then be played together.
So Sonic Pi can do quite a lot with midi commands, but it cant directly play a midi file, nor can it directly save a midi file.

I assumed that you wanted to record a midi file. While it is not possible to record all audio from Sonic Pi to a midi file, it is still possible to use midi as @robin.newman explained more. For example:

live_loop :playmidi do

  # Use midi_pc to change general midi instruments
  # See: https://www.midi.org/specifications-old/item/gm-level-1-sound-set
  midi_pc rand_i(30), port: "loopmidi_5", channel: 1

  # Loop some stuff with different instruments
  5.times do
    len = [0.25,0.5].choose
    midi (scale :D, :cargah).choose, sustain: len, port: "loopmidi_5", channel: 1
    sleep len
  end
end

You could then record this using different applications. Easiest would be to use this free webapp:
https://midi-recorder.web.app/

But it’s limited to one track. You could also create a multitrack piece and record it with DAW like Reaper (which is sort of free to use) using general midi instruments and reapers midi recording capabilities.

1 Like

@amiika @robin.newman Thank you for providing midi-recorder.web.app! So what should I do if I send the midi command to or record it with Logic Pro? What does midi (scale :D, :cargah).choose, sustain: len, port: "loopmidi_5", channel: 1 mean? How to set up a suitable one?

You need to read the documentation. See midi in help/lang. Then again the help doesnt really help you how to set up virtual MIDI instruments. With hardware it’s much easier, just plug in your keyboard to your computer :slight_smile:

I used “virtual midi out” created by Loopmidi program. You can use whatever midi out port you wish. I dont use Logic Pro, but if you can set up General Midi instrument in there you might see new output port in Sonic Pi:s MIDI outputs.