-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequencer_test.go
222 lines (176 loc) · 4.55 KB
/
sequencer_test.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package balafon_test
import (
"testing"
"time"
"github.com/mgnsk/balafon"
"github.com/mgnsk/balafon/internal/constants"
. "github.com/onsi/gomega"
"gitlab.com/gomidi/midi/v2"
_ "gitlab.com/gomidi/midi/v2/drivers/testdrv"
"gitlab.com/gomidi/midi/v2/smf"
)
func TestSequencerTiming(t *testing.T) {
for _, tc := range []struct {
input string
absTicks uint32
}{
{
input: ":time 1 4; :tempo 60; :assign c 60; c",
absTicks: uint32(constants.TicksPerQuarter),
},
{
input: ":time 2 8; :tempo 60; :assign c 60; c",
absTicks: uint32(constants.TicksPerQuarter),
},
{
input: ":time 2 4; :tempo 120; :assign c 60; c2",
absTicks: uint32(2 * constants.TicksPerQuarter),
},
} {
t.Run(tc.input, func(t *testing.T) {
g := NewWithT(t)
it := balafon.New()
g.Expect(it.EvalString(tc.input)).To(Succeed())
s := balafon.NewSequencer()
s.AddBars(it.Flush()...)
sm := s.Flush()
g.Expect(sm).To(HaveLen(4))
g.Expect(sm[0].Message.Type()).To(Equal(smf.MetaTimeSigMsg))
g.Expect(sm[0].AbsTicks).To(Equal(uint32(0)))
g.Expect(sm[0].AbsNanoseconds).To(Equal(int64(0)))
g.Expect(sm[1].Message.Type()).To(Equal(smf.MetaTempoMsg))
g.Expect(sm[1].AbsTicks).To(Equal(uint32(0)))
g.Expect(sm[1].AbsNanoseconds).To(Equal(int64(0)))
g.Expect(sm[2].Message.Type()).To(Equal(midi.NoteOnMsg))
g.Expect(sm[2].AbsTicks).To(Equal(uint32(0)))
g.Expect(sm[2].AbsNanoseconds).To(Equal(int64(0)))
g.Expect(sm[3].Message.Type()).To(Equal(midi.NoteOffMsg))
g.Expect(sm[3].AbsTicks).To(Equal(tc.absTicks))
g.Expect(sm[3].AbsNanoseconds).To(Equal(time.Second.Nanoseconds()))
})
}
}
func TestSequencerMultiTrackTiming(t *testing.T) {
g := NewWithT(t)
it := balafon.New()
input := `
:channel 1
:assign x 42
:channel 2
:assign k 36
:tempo 60
:time 4 4
:bar test
:channel 1
xxxx
:channel 2
kkkk
:end
:play test
`
g.Expect(it.EvalString(input)).To(Succeed())
s := balafon.NewSequencer()
s.AddBars(it.Flush()...)
sm := s.Flush()
g.Expect(sm).To(HaveLen(20))
g.Expect(sm[18].Message.Type()).To(Equal(midi.NoteOffMsg))
g.Expect(sm[18].AbsTicks).To(Equal(uint32(constants.TicksPerWhole)))
g.Expect(sm[18].AbsNanoseconds).To(Equal(4 * time.Second.Nanoseconds()))
g.Expect(sm[19].Message.Type()).To(Equal(midi.NoteOffMsg))
g.Expect(sm[19].AbsTicks).To(Equal(uint32(constants.TicksPerWhole)))
g.Expect(sm[19].AbsNanoseconds).To(Equal(4 * time.Second.Nanoseconds()))
}
func TestSilenceBetweenBars(t *testing.T) {
g := NewWithT(t)
it := balafon.New()
input := `
:assign x 42
:tempo 60
:time 2 4
:bar one
x-
:end
:bar two
-x
:end
:play one
:play two
`
g.Expect(it.EvalString(input)).To(Succeed())
s := balafon.NewSequencer()
s.AddBars(it.Flush()...)
sm := s.Flush()
g.Expect(sm.String()).To(Equal(`pos: 0 ns: 0 message: MetaTempo bpm: 60.00
pos: 0 ns: 0 message: MetaTimeSig meter: 2/4
pos: 0 ns: 0 message: NoteOn channel: 0 key: 42 velocity: 100
pos: 960 ns: 1000000000 message: NoteOff channel: 0 key: 42
pos: 960 ns: 1000000000 message: UnknownType
pos: 1920 ns: 2000000000 message: MetaTimeSig meter: 2/4
pos: 1920 ns: 2000000000 message: UnknownType
pos: 2880 ns: 3000000000 message: NoteOn channel: 0 key: 42 velocity: 100
pos: 3840 ns: 4000000000 message: NoteOff channel: 0 key: 42
`))
}
func TestTempoChange(t *testing.T) {
g := NewWithT(t)
it := balafon.New()
input := `
:assign x 42
:bar one
:time 1 4
:tempo 60
x
:end
:bar two
:time 2 4
:tempo 120
xx
:end
:play one
:play two
`
g.Expect(it.EvalString(input)).To(Succeed())
s := balafon.NewSequencer()
s.AddBars(it.Flush()...)
sm := s.Flush()
g.Expect(sm).To(HaveLen(10))
g.Expect(sm[9]).To(Equal(balafon.TrackEvent{
Message: smf.Message(midi.NoteOff(0, 42)),
AbsTicks: uint32(3 * constants.TicksPerQuarter),
AbsNanoseconds: 2 * time.Second.Nanoseconds(),
}))
}
func TestZeroDurationBarCollapse(t *testing.T) {
g := NewWithT(t)
it := balafon.New()
err := it.EvalString(`
:tempo 60
:time 1 4
:bar one
-
:end
:bar two
:program 1
:end
:bar three
-
:end
:play one
:play two
:play three
-
`)
g.Expect(err).NotTo(HaveOccurred())
s := balafon.NewSequencer()
s.AddBars(it.Flush()...)
sm := s.Flush()
g.Expect(sm.String()).To(Equal(`pos: 0 ns: 0 message: MetaTempo bpm: 60.00
pos: 0 ns: 0 message: MetaTimeSig meter: 1/4
pos: 0 ns: 0 message: UnknownType
pos: 960 ns: 1000000000 message: ProgramChange channel: 0 program: 1
pos: 960 ns: 1000000000 message: MetaTimeSig meter: 1/4
pos: 960 ns: 1000000000 message: UnknownType
pos: 1920 ns: 2000000000 message: MetaTimeSig meter: 1/4
pos: 1920 ns: 2000000000 message: UnknownType
`))
}