Created
May 14, 2025 18:39
-
-
Save eliasdorneles/74b5e877800a998385a7043c1f18edcb to your computer and use it in GitHub Desktop.
Odin PortMidi 101
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "core:fmt" | |
import "core:os" | |
import "core:time" | |
import "core:strconv" | |
import "vendor:portmidi" | |
main :: proc() { | |
if len(os.args) < 2 { | |
fmt.println("Usage: send_chord <device_id>") | |
return | |
} | |
device_id := strconv.atoi(os.args[1]) | |
// Initialize PortMidi | |
err_code := portmidi.Initialize() | |
if err_code != portmidi.Error.NoError { | |
fmt.println("Failed to initialize PortMidi") | |
return | |
} | |
defer portmidi.Terminate() | |
count := portmidi.CountDevices() | |
fmt.println("These are the available output devices:") | |
for i in 0..<count { | |
info := portmidi.GetDeviceInfo(portmidi.DeviceID(i)) | |
if info.output { | |
fmt.printf("ID %d: %s (%s)\n", i, info.name, info.interf) | |
} | |
} | |
fmt.println() | |
// Open MIDI output stream | |
stream := new(portmidi.Stream) | |
defer free(stream) | |
fmt.printf("Will now open device ID %d\n", device_id) | |
err_code = portmidi.OpenOutput(stream, portmidi.DeviceID(device_id), nil, 0, nil, nil, 0) | |
if err_code != portmidi.Error.NoError { | |
fmt.println("Failed to open MIDI output") | |
return | |
} | |
defer portmidi.Close(stream^) | |
// MIDI note numbers for C4, E4, G4 | |
notes := [3]int{60, 64, 67} | |
velocity := 100 | |
channel := 0 | |
fmt.println("Okay, sending chord now...") | |
// Send Note On | |
for note in notes { | |
status := 0x90 + channel | |
message := portmidi.MessageMake(i32(status), i32(note), i32(velocity)) | |
portmidi.WriteShort(stream^, portmidi.Timestamp(0), message) | |
time.sleep(200 * time.Millisecond) | |
} | |
time.sleep(2 * time.Second) | |
// Send Note Off | |
for note in notes { | |
status := 0x80 + channel | |
message := portmidi.MessageMake(i32(status), i32(note), i32(velocity)) | |
portmidi.WriteShort(stream^, portmidi.Timestamp(0), message) | |
} | |
fmt.println("Chord sent.") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment