-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
sync.go
85 lines (75 loc) · 1.64 KB
/
sync.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
package control
import (
"bytes"
"fmt"
"time"
"github.com/emicklei/melrose/core"
"github.com/emicklei/melrose/op"
)
type SyncPlay struct {
playables []core.HasValue
}
func NewSyncPlay(list []core.HasValue) SyncPlay {
return SyncPlay{playables: list}
}
func (s SyncPlay) Storex() string {
var b bytes.Buffer
fmt.Fprintf(&b, "sync(")
for i, each := range s.playables {
if i > 0 {
fmt.Fprintf(&b, ",")
}
fmt.Fprintf(&b, "%s", core.Storex(each))
}
fmt.Fprintf(&b, ")")
return b.String()
}
// Play implements Playable
func (s SyncPlay) Play(ctx core.Context, at time.Time) error {
return s.Evaluate(ctx)
}
// Stop implements Stoppeable
func (s SyncPlay) Stop(ctx core.Context) error {
for _, each := range s.playables {
val := each.Value()
if ply, ok := val.(core.Stoppable); ok {
_ = ply.Stop(ctx)
}
}
return nil
}
func (s SyncPlay) S() core.Sequence {
l := []core.Sequenceable{}
for _, each := range s.playables {
val := each.Value()
if s, ok := val.(core.Sequenceable); ok {
l = append(l, s)
}
}
return (op.Merge{Target: l}).S()
}
// IsPlaying implements Stoppeable
func (s SyncPlay) IsPlaying() bool {
for _, each := range s.playables {
val := each.Value()
if ply, ok := val.(core.Stoppable); ok {
if ply.IsPlaying() {
return true
}
}
}
return false
}
func (s SyncPlay) Evaluate(ctx core.Context) error {
for _, each := range s.playables {
val := each.Value()
if ply, ok := val.(core.Playable); ok {
_ = ply.Play(ctx, time.Now())
} else {
if seq, ok := val.(core.Sequenceable); ok {
_ = ctx.Device().Play(core.NoCondition, seq, ctx.Control().BPM(), time.Now())
}
}
}
return nil
}