2018-03-19 04:44:18 +00:00
|
|
|
package backend
|
|
|
|
|
2018-03-19 14:34:42 +00:00
|
|
|
import (
|
2018-03-20 02:04:40 +00:00
|
|
|
alsa "github.com/tonychee7000/Arremi/backend/alsa_wrapper"
|
2018-03-19 04:44:18 +00:00
|
|
|
"github.com/tonychee7000/Arremi/consts"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MIDIDevice implies a Writer interface.
|
|
|
|
type MIDIDevice struct {
|
2018-03-19 14:34:42 +00:00
|
|
|
Signal chan int
|
2018-03-19 04:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2018-03-20 02:04:40 +00:00
|
|
|
var err error
|
2018-03-19 14:34:42 +00:00
|
|
|
midiDev.Signal = make(chan int, 4096)
|
2018-03-20 02:04:40 +00:00
|
|
|
err = alsa.NewClient(consts.ClientName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-03-19 04:44:18 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 02:04:40 +00:00
|
|
|
err = alsa.NewPort(consts.SourceName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-03-19 14:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-03-19 04:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (midiDev *MIDIDevice) Write(p []byte) (int, error) {
|
2018-03-19 14:34:42 +00:00
|
|
|
midiDev.Signal <- 1
|
2018-03-20 02:04:40 +00:00
|
|
|
alsa.SendData(p)
|
2018-03-19 14:34:42 +00:00
|
|
|
return len(p), nil
|
2018-03-19 04:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|