From c15057eac1c944ab382141b94d6f49e5cf32246a Mon Sep 17 00:00:00 2001 From: TonyChyi Date: Mon, 19 Mar 2018 12:40:34 +0800 Subject: [PATCH] f --- backend/midiDevice_darwin.go | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 backend/midiDevice_darwin.go diff --git a/backend/midiDevice_darwin.go b/backend/midiDevice_darwin.go new file mode 100644 index 0000000..cfbd7da --- /dev/null +++ b/backend/midiDevice_darwin.go @@ -0,0 +1,50 @@ +package backend + +import ( + "github.com/tonychee7000/Arremi/consts" + midi "github.com/youpy/go-coremidi" +) + +// MIDIDevice implies a Writer interface. +type MIDIDevice struct { + client midi.Client + source midi.Source + Signal chan midi.Packet +} + +// NewMIDIDevice construction func +func NewMIDIDevice() (*MIDIDevice, error) { + var mididev = new(MIDIDevice) + err := mididev.Init() + return mididev, err +} + +// Init the client and source +func (midiDev *MIDIDevice) Init() error { + var err error + + midiDev.Signal = make(chan midi.Packet, 4096) + midiDev.client, err = midi.NewClient(consts.ClientName) + if err != nil { + return err + } + + midiDev.source, err = midi.NewSource(midiDev.client, consts.SourceName) + return err +} + +func (midiDev *MIDIDevice) Write(p []byte) (int, error) { + var pack = midi.NewPacket(p, 0) + midiDev.Signal <- pack + err := pack.Received(&(midiDev.source)) + return len(p), err +} + +// AllNoteOff I don't want panic! +func (midiDev *MIDIDevice) AllNoteOff() { + for i := 0; i < 16; i++ { + for j := 0; j < 128; j++ { + midiDev.Write([]byte{byte(0x90 + i), byte(j), 0}) + } + } +}