is this okay?

This commit is contained in:
TonyChyi 2018-03-20 10:04:40 +08:00
parent 3f1848f5b7
commit 334237cac3
2 changed files with 74 additions and 51 deletions

View 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
}

View File

@ -1,18 +1,7 @@
package backend
/*
#ifndef ARREMI
#define ARREMI
#cgo LDFLAGS: -lasound
#include <stdlib.h>
#include "alsa_wrapper.h"
#endif
*/
import "C"
import (
"fmt"
"unsafe"
alsa "github.com/tonychee7000/Arremi/backend/alsa_wrapper"
"github.com/tonychee7000/Arremi/consts"
)
@ -30,30 +19,16 @@ func NewMIDIDevice() (*MIDIDevice, error) {
// Init the client and source
func (midiDev *MIDIDevice) Init() error {
var status int
var err error
midiDev.Signal = make(chan int, 4096)
cDevName := C.CString(consts.ClientName)
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)
}
err = alsa.NewClient(consts.ClientName)
if err != nil {
return err
}
cPortName := C.CString(consts.SourceName)
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)
err = alsa.NewPort(consts.SourceName)
if err != nil {
return err
}
return nil
@ -61,20 +36,7 @@ func (midiDev *MIDIDevice) Init() error {
func (midiDev *MIDIDevice) Write(p []byte) (int, error) {
midiDev.Signal <- 1
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)
}
}
alsa.SendData(p)
return len(p), nil
}
@ -86,7 +48,3 @@ func (midiDev *MIDIDevice) AllNoteOff() {
}
}
}
func resolveErrCode(code int) (int, int) {
return code >> 16, code & 0xffff
}