/
midi.go
78 lines (59 loc) · 1.61 KB
/
midi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//-----------------------------------------------------------------------------
/*
MIDI Keyboard Driver
*/
//-----------------------------------------------------------------------------
package main
import (
"fmt"
"math"
"github.com/deadsy/libusb"
)
//-----------------------------------------------------------------------------
const NOTES_IN_OCTAVE = 12
const MIDI_NOTE_A5 = 69
const A5_FREQUENCY = 440
// return the frequency of the midi note
func midi_to_frequency(note uint) float32 {
return float32(A5_FREQUENCY * math.Pow(2.0, float64(int(note)-MIDI_NOTE_A5)/NOTES_IN_OCTAVE))
}
// return an octave number
func midi_to_octave(note uint) uint {
return note / NOTES_IN_OCTAVE
}
//-----------------------------------------------------------------------------
func major_chord(root uint) [3]uint {
return [3]uint{root, root + 4, root + 7}
}
func minor_chord(root uint) [3]uint {
return [3]uint{root, root + 3, root + 7}
}
//-----------------------------------------------------------------------------
// Korg NanoKey2
const usb_vid = 0x0944
const usb_pid = 0x0115
func midi_init() error {
var ctx libusb.Context
err := libusb.Init(&ctx)
if err != nil {
return err
}
defer libusb.Exit(ctx)
list, err := libusb.Get_Device_List(ctx)
if err != nil {
return err
}
defer libusb.Free_Device_List(list, 1)
for _, dev := range list {
dd, err := libusb.Get_Device_Descriptor(dev)
if err != nil {
return err
}
if dd.IdVendor == usb_vid && dd.IdProduct == usb_pid {
fmt.Printf("found\n")
break
}
}
return nil
}
//-----------------------------------------------------------------------------