-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
listen.go
124 lines (106 loc) · 2.84 KB
/
listen.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
package control
import (
"errors"
"fmt"
"sync"
"time"
"github.com/emicklei/melrose/core"
"github.com/emicklei/melrose/notify"
)
type Listen struct {
mutex *sync.RWMutex
ctx core.Context
deviceID int
variableName string
isRunning bool
callback core.HasValue
notesOn map[int]int
noteChangeCount int
}
func NewListen(ctx core.Context, deviceID int, variableName string, target core.HasValue) *Listen {
return &Listen{
mutex: new(sync.RWMutex),
ctx: ctx,
deviceID: deviceID,
variableName: variableName,
callback: target,
notesOn: map[int]int{},
noteChangeCount: 0,
}
}
// Inspect implements Inspectable
func (l *Listen) Inspect(i core.Inspection) {
i.Properties["running"] = l.isRunning
i.Properties["device"] = l.deviceID
}
// Target is for replacing functions
func (l *Listen) Target() core.HasValue { return l.callback }
// SetTarget is for replacing functions
func (l *Listen) SetTarget(c core.HasValue) { l.callback = c }
// Play is part of core.Playable
func (l *Listen) Play(ctx core.Context, at time.Time) error {
if l.isRunning {
return nil
}
if !ctx.Device().HasInputCapability() {
return errors.New("input is not available for this device")
}
l.isRunning = true
ctx.Device().Listen(l.deviceID, l, l.isRunning)
return nil
}
func (l *Listen) Stop(ctx core.Context) error {
if !l.isRunning {
return nil
}
l.isRunning = false
ctx.Device().Listen(l.deviceID, l, l.isRunning)
return nil
}
func (l *Listen) IsPlaying() bool {
return l.isRunning
}
// NoteOn is part of core.NoteListener
func (l *Listen) NoteOn(channel int, n core.Note) {
l.mutex.Lock()
if core.IsDebug() {
notify.Debugf("control.listen ON %v", n)
}
l.noteChangeCount++
countCheck := l.noteChangeCount
nr := n.MIDI()
l.notesOn[nr] = countCheck
l.ctx.Variables().Put(l.variableName, n)
// release so condition can be evaluated
l.mutex.Unlock()
if e, ok := l.callback.Value().(core.Evaluatable); ok {
// only actually play the note if the hit count matches the check
cond := func() bool {
return l.isNoteOnCount(nr, countCheck)
}
e.Evaluate(l.ctx.WithCondition(cond))
}
}
func (l *Listen) isNoteOnCount(nr, countCheck int) bool {
l.mutex.RLock()
defer l.mutex.RUnlock()
// is the note still on?
count, ok := l.notesOn[nr]
// is the note on on the count
return ok && count == countCheck
}
// NoteOff is part of core.NoteListener
func (l *Listen) NoteOff(channel int, n core.Note) {
l.mutex.Lock()
defer l.mutex.Unlock()
if core.IsDebug() {
notify.Debugf("control.listen OFF %v", n)
}
delete(l.notesOn, n.MIDI())
}
func (l *Listen) ControlChange(channel, number, value int) {
}
// Storex is part of core.Storable
func (l *Listen) Storex() string {
return fmt.Sprintf("listen(%d,%s,%s)", l.deviceID, l.variableName, core.Storex(l.callback))
}