is this okay?
This commit is contained in:
parent
3f1848f5b7
commit
334237cac3
65
backend/alsa_wrapper/alsa_wrapper.go
Normal file
65
backend/alsa_wrapper/alsa_wrapper.go
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
package alsa_wrapper
|
||||||
|
|
||||||
|
/*
|
||||||
|
#ifndef ARREMI
|
||||||
|
#define ARREMI
|
||||||
|
#cgo LDFLAGS: -lasound
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "alsa_wrapper.h"
|
||||||
|
#endif
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewClient to register new ALSA client.
|
||||||
|
func NewClient(name string) error {
|
||||||
|
cDevName := C.CString(name)
|
||||||
|
defer C.free(unsafe.Pointer(cDevName))
|
||||||
|
status = int(C.new_client(cDevName))
|
||||||
|
if status != 0 {
|
||||||
|
stage, errCode := resolveErrCode(status)
|
||||||
|
switch stage {
|
||||||
|
case 1:
|
||||||
|
return fmt.Errorf("Error while opening sequencer. %d", errCode)
|
||||||
|
case 2:
|
||||||
|
return fmt.Errorf("Error while getting sequencer id. %d", errCode)
|
||||||
|
case 3:
|
||||||
|
return fmt.Errorf("Error while setting sequencer name. %d", errCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPort to reigister new MIDI port.
|
||||||
|
func NewPort(name string) error {
|
||||||
|
cPortName := C.CString(name)
|
||||||
|
defer C.free(unsafe.Pointer(cPortName))
|
||||||
|
status = int(C.new_port(cPortName))
|
||||||
|
if status != 0 {
|
||||||
|
return fmt.Errorf("Error while createing sequencer port. %d", status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendData to ALSA
|
||||||
|
func SendData(p []byte) error {
|
||||||
|
cData := (*C.char)(unsafe.Pointer(&p[0]))
|
||||||
|
defer C.free(unsafe.Pointer(cData))
|
||||||
|
var status = int(C.send_data(cData, C.int(len(p))))
|
||||||
|
if status != 0 {
|
||||||
|
stage, errCode := resolveErrCode(status)
|
||||||
|
switch stage {
|
||||||
|
case 1:
|
||||||
|
return 0, fmt.Errorf("Error while creating MIDI event. %d", errCode)
|
||||||
|
case 2:
|
||||||
|
return 0, fmt.Errorf("Error while encoding MIDI event. %d", errCode)
|
||||||
|
case 3:
|
||||||
|
return 0, fmt.Errorf("Error while sending data. %d", errCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveErrCode(code int) (int, int) {
|
||||||
|
return code >> 16, code & 0xffff
|
||||||
|
}
|
@ -1,18 +1,7 @@
|
|||||||
package backend
|
package backend
|
||||||
|
|
||||||
/*
|
|
||||||
#ifndef ARREMI
|
|
||||||
#define ARREMI
|
|
||||||
#cgo LDFLAGS: -lasound
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "alsa_wrapper.h"
|
|
||||||
#endif
|
|
||||||
*/
|
|
||||||
import "C"
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
alsa "github.com/tonychee7000/Arremi/backend/alsa_wrapper"
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
"github.com/tonychee7000/Arremi/consts"
|
"github.com/tonychee7000/Arremi/consts"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,30 +19,16 @@ func NewMIDIDevice() (*MIDIDevice, error) {
|
|||||||
|
|
||||||
// Init the client and source
|
// Init the client and source
|
||||||
func (midiDev *MIDIDevice) Init() error {
|
func (midiDev *MIDIDevice) Init() error {
|
||||||
var status int
|
var err error
|
||||||
|
|
||||||
midiDev.Signal = make(chan int, 4096)
|
midiDev.Signal = make(chan int, 4096)
|
||||||
|
err = alsa.NewClient(consts.ClientName)
|
||||||
cDevName := C.CString(consts.ClientName)
|
if err != nil {
|
||||||
defer C.free(unsafe.Pointer(cDevName))
|
return err
|
||||||
status = int(C.new_client(cDevName))
|
|
||||||
if status != 0 {
|
|
||||||
stage, errCode := resolveErrCode(status)
|
|
||||||
switch stage {
|
|
||||||
case 1:
|
|
||||||
return fmt.Errorf("Error while opening sequencer. %d", errCode)
|
|
||||||
case 2:
|
|
||||||
return fmt.Errorf("Error while getting sequencer id. %d", errCode)
|
|
||||||
case 3:
|
|
||||||
return fmt.Errorf("Error while setting sequencer name. %d", errCode)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cPortName := C.CString(consts.SourceName)
|
err = alsa.NewPort(consts.SourceName)
|
||||||
defer C.free(unsafe.Pointer(cPortName))
|
if err != nil {
|
||||||
status = int(C.new_port(cPortName))
|
return err
|
||||||
if status != 0 {
|
|
||||||
return fmt.Errorf("Error while createing sequencer port. %d", status)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -61,20 +36,7 @@ func (midiDev *MIDIDevice) Init() error {
|
|||||||
|
|
||||||
func (midiDev *MIDIDevice) Write(p []byte) (int, error) {
|
func (midiDev *MIDIDevice) Write(p []byte) (int, error) {
|
||||||
midiDev.Signal <- 1
|
midiDev.Signal <- 1
|
||||||
cData := (*C.char)(unsafe.Pointer(&p[0]))
|
alsa.SendData(p)
|
||||||
defer C.free(unsafe.Pointer(cData))
|
|
||||||
var status = int(C.send_data(cData, C.int(len(p))))
|
|
||||||
if status != 0 {
|
|
||||||
stage, errCode := resolveErrCode(status)
|
|
||||||
switch stage {
|
|
||||||
case 1:
|
|
||||||
return 0, fmt.Errorf("Error while creating MIDI event. %d", errCode)
|
|
||||||
case 2:
|
|
||||||
return 0, fmt.Errorf("Error while encoding MIDI event. %d", errCode)
|
|
||||||
case 3:
|
|
||||||
return 0, fmt.Errorf("Error while sending data. %d", errCode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return len(p), nil
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +48,3 @@ func (midiDev *MIDIDevice) AllNoteOff() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveErrCode(code int) (int, int) {
|
|
||||||
return code >> 16, code & 0xffff
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user