-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
129 lines (105 loc) · 2.61 KB
/
event.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package kit
import (
"encoding/json"
"strings"
"github.com/hypebeast/go-osc/osc"
midi "gitlab.com/gomidi/midi/v2"
)
type RecordingEvent struct {
Event string
Value any
// PlaybackEvent *PlaybackEvent `json:"PlaybackEvent"`
// CursorEvent *CursorEvent `json:"CursorEvent"`
// MidiEvent *MidiEvent `json:"MidiEvent"`
// OscEvent *OscEvent `json:"OscEvent"`
}
// PlaybackEvent is for start/stop
type PlaybackEvent struct {
Click Clicks `json:"Click"`
IsRunning bool `json:"IsRunning"`
}
type MIDIEventHandler func(MidiEvent)
type OscEvent struct {
Click Clicks `json:"Click"`
Msg *osc.Message `json:"Msg"`
Source string `json:"Source"`
}
type MidiEvent struct {
Click Clicks
Tag string
Msg midi.Message
}
func NewMidiEvent(click Clicks, tag string, msg midi.Message) MidiEvent {
return MidiEvent{
Click: click,
Tag: tag,
Msg: msg,
}
}
func (me MidiEvent) Status() uint8 {
bytes := me.Msg.Bytes()
if len(bytes) == 0 {
LogWarn("Empty bytes in MidiEvent?")
return 0
}
return bytes[0]
}
func (me MidiEvent) Data1() uint8 {
bytes := me.Msg.Bytes()
if len(bytes) < 2 {
LogWarn("No Data1 byte in MidiEvent?")
return 0
}
return bytes[1]
}
////////////////////////// CursorEvent methods
func (ce CursorEvent) IsAttractGenerated() bool {
return strings.Contains(ce.Tag, "attract")
}
// XXX - can this make use of generics? (across all the Event types)
func (ce CursorEvent) Marshal() (bytes []byte, err error) {
bytes, err = json.Marshal(ce)
return
}
func CursorEventFromString(s string) (ce CursorEvent, err error) {
err = json.Unmarshal([]byte(s), &ce)
return
}
////////////////////////// MidiEvent methods
func (me MidiEvent) HasPitch() bool {
return me.Msg.Is(midi.NoteOnMsg) || me.Msg.Is(midi.NoteOffMsg)
}
func (me MidiEvent) Pitch() uint8 {
b := me.Msg.Bytes()
if len(b) == 3 {
return b[1]
}
LogWarn("MidiEvent.Pitch: bad message length")
return 0
}
func (e MidiEvent) Marshal() (bytes []byte, err error) {
bytes, err = json.Marshal(e)
return
}
func (e MidiEvent) String() string {
bytes, err := json.Marshal(e)
if err != nil {
return "{\"error\":\"Unable to Marshal CursorEvent\"}"
}
return string(bytes)
}
////////////////////////// OscEvent methods
func (e OscEvent) String() string {
bytes, err := json.Marshal(e)
if err != nil {
return "{\"error\":\"Unable to Marshal CursorEvent\"}"
}
return string(bytes)
}
func (e OscEvent) Marshal() (bytes []byte, err error) {
bytes, err = json.Marshal(e)
return
}
// func (event MidiEvent) String() string {
// return "{\"event\":\"midi\",\"msg\":\"" + event.Msg.String() + "\"}"
// }