Simple example for sending note on/off messages
Closed this issue · 1 comments
kamilmac commented
Something along those lines (taken form simplecoremidi lib for python):
from simplecoremidi import send_midi
import random
from time import sleep
def play_a_scale():
root_note = 60
channel = 1
note_on_action = 0x90
major_steps = [3, 1, 24, 3 , 3, 1, 14, 28, 0 , 12 , 35]
velocity = 127
note = root_note
for step in major_steps:
rand = random.randint(0,100) / 100
note = root_note + step
send_midi((note_on_action | channel,
note,
velocity ))
sleep(0.05)
send_midi((note_on_action | channel,
note,
0))
sleep(0.3)
if __name__=='__main__':
while True:
play_a_scale()
Great job!
sstark commented
Choose first available destination, send midi note 0x40 to midi channel 1, let it play for 2 seconds:
package main
import (
"fmt"
"github.com/youpy/go-coremidi"
"time"
)
func main() {
dests, err := coremidi.AllDestinations()
for _, d := range dests {
fmt.Printf("Dest: %s, %s\n", d.Manufacturer(), d.Name())
}
client, err := coremidi.NewClient("me")
if err != nil {
fmt.Println(err)
return
}
port, err := coremidi.NewOutputPort(client, "me")
if err != nil {
fmt.Println(err)
return
}
noteOn := coremidi.NewPacket([]byte{0x91, 0x40, 127})
noteOff := coremidi.NewPacket([]byte{0x81, 0x40, 127})
err = noteOn.Send(&port, &dests[0])
if err != nil {
fmt.Println("could not send midi packet")
}
time.Sleep(time.Second * 2)
err = noteOff.Send(&port, &dests[0])
if err != nil {
fmt.Println("could not send midi packet")
}
}