<jupyter_start><jupyter_text>MIDI util functions<jupyter_code>import rtmidi
from time import sleep

def open_midi_out_virtual_port(api=rtmidi.RtMidiOut.UNIX_JACK, clientName="PyMIDI"):
    """Open a midiout port."""
    # Initialize midi out port
    midiout = rtmidi.RtMidiOut(api=rtmidi.RtMidiOut.UNIX_JACK, clientName="PyMIDI")
    
    # Print some debug info
    ports = range(midiout.getPortCount())
    if ports:
        for i in ports:
            print(midiout.getPortName(i))
    
    # Open the virtual port
    print("Opening port 0!") 
    midiout.openVirtualPort()    
    
    return midiout
            
def program_change(midiout, channel, program):
    """Change the MIDI program (virtual instrument)."""
    msg = rtmidi.MidiMessage.programChange(channel, 1)
    midiout.sendMessage(msg)
    
def controller_event(midiout, channel, controller, level):
    """Change controller to have level."""
    msg = rtmidi.MidiMessage.controllerEvent(channel, controller, level)
    midiout.sendMessage(msg)
    
def volume_change(midiout, channel, level):
    """Change volume to level."""
    controller_event(midiout, channel, 7, level)
    
def play_note_on(midiout, channel, note, velocity):
    """Play note with id note with given velocity until shutoff."""
    msg = rtmidi.MidiMessage.noteOn(channel, note, velocity)
    midiout.sendMessage(msg)
    
def play_note_off(midiout, channel, note):
    """Shutoff note with id note."""
    msg = rtmidi.MidiMessage.noteOff(channel, note)
    midiout.sendMessage(msg)
    
def play_note(midiout, channel, note, velocity, time=1):
    """Play note with id note with given velocity for time seconds."""
    play_note_on(midiout, channel, note, velocity)
    sleep(time)
    play_note_off(midiout, channel, note)<jupyter_output><empty_output><jupyter_text>Open the MIDI port<jupyter_code>midiout = open_midi_out_virtual_port()
channel = 1<jupyter_output>Midi Through 14:0
Opening port 0!<jupyter_text>Play some notes<jupyter_code># Sound out a series of MIDI messages
program_change(midiout, channel, 1)

# Control change (volume)
volume_change(midiout, channel, 100)

# Play C5 a few times
for _ in range(3):
    play_note(midiout, channel, note=64, velocity=127, time=1)<jupyter_output><empty_output><jupyter_text>Close the port<jupyter_code>midiout.closePort()  # TODO: Doesn't seem to work?<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1070528967254408, 'fraction_numerical': 0.008816120906801008, 'mean_word_length': 4.490783410138249, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '491535', 'license': 'MIT', 'n_tokens_mistral': 839, 'n_tokens_neox': 816, 'n_words': 193, 'path': 'midi.ipynb', 'repo_name': 'tayden/paper-press', 'size': '2382'}