-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpattern.go
More file actions
339 lines (297 loc) · 8.13 KB
/
Copy pathpattern.go
File metadata and controls
339 lines (297 loc) · 8.13 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package pattern
import (
"errors"
"github.com/gotracker/playback/format/it/channel"
"github.com/gotracker/playback/index"
"github.com/gotracker/playback/pattern"
"github.com/gotracker/playback/player/feature"
"github.com/gotracker/playback/song"
formatutil "github.com/gotracker/playback/util"
"github.com/heucuva/optional"
)
// State is the current pattern state
type State struct {
currentOrder index.Order
currentRow index.Row
ticks int
tempo int
patternDelay optional.Value[int]
finePatternDelay int
resetPatternLoops bool
SongLoop feature.SongLoop
PlayUntilOrderAndRow feature.PlayUntilOrderAndRow
loopDetect formatutil.LoopDetect // when SongLoopEnabled is false, this is used to detect song loops
loopCount int
Patterns []pattern.Pattern[channel.Data]
Orders []index.Pattern
}
// GetTempo returns the tempo of the current state
func (state *State) GetTempo() int {
return state.tempo
}
// GetSpeed returns the row speed of the current state
func (state *State) GetSpeed() int {
return state.ticks
}
// GetTicksThisRow returns the number of ticks in the current row
func (state *State) GetTicksThisRow() int {
rowLoops := 1
if patternDelay, ok := state.patternDelay.Get(); ok {
rowLoops = patternDelay
}
extraTicks := state.finePatternDelay
ticksThisRow := state.ticks*rowLoops + extraTicks
return ticksThisRow
}
// GetPatNum returns the current pattern number
func (state *State) GetPatNum() index.Pattern {
if int(state.currentOrder) >= len(state.Orders) {
return index.InvalidPattern
}
return state.Orders[state.currentOrder]
}
// GetNumRows returns the number of rows in the current pattern
func (state *State) GetNumRows() (int, error) {
rows, err := state.GetRows()
if err != nil {
return 0, err
}
if rows != nil {
return rows.NumRows(), nil
}
return 0, nil
}
// WantsStop returns true when the current pattern wants to end the song
func (state *State) WantsStop() bool {
return state.GetPatNum() == index.InvalidPattern
}
// setCurrentOrder sets the current order index
func (state *State) setCurrentOrder(order index.Order) {
state.currentOrder = order
}
func (state *State) advanceOrder() {
state.setCurrentOrder(state.currentOrder + 1)
}
// GetCurrentOrder returns the current order
func (state *State) GetCurrentOrder() index.Order {
return state.currentOrder
}
// GetNumOrders returns the number of orders in the song
func (state *State) GetNumOrders() int {
return len(state.Orders)
}
// GetCurrentPatternIdx returns the current pattern index, derived from the order list
func (state *State) GetCurrentPatternIdx() (index.Pattern, error) {
ordLen := len(state.Orders)
if ordLen == 0 {
// nothing to play, don't even try
return 0, song.ErrStopSong
}
for loopCount := 0; loopCount < ordLen; loopCount++ {
ordIdx := int(state.GetCurrentOrder())
if ordIdx >= ordLen {
if !(state.SongLoop.Count < 0 || state.loopCount < state.SongLoop.Count) {
return 0, song.ErrStopSong
}
state.setCurrentOrder(0)
continue
}
patIdx := state.Orders[ordIdx]
if patIdx == index.NextPattern {
if err := state.nextOrder(true); err != nil {
return 0, err
}
continue
}
if patIdx == index.InvalidPattern {
if err := state.nextOrder(true); err != nil {
return 0, err
}
continue // this is supposed to be a song break
}
return patIdx, nil
}
return 0, errors.New("infinite loop detected in order list")
}
// GetCurrentRow returns the current row
func (state *State) GetCurrentRow() index.Row {
return state.currentRow
}
// setCurrentRow sets the current row
func (state *State) setCurrentRow(row index.Row) error {
state.currentRow = row
rows, err := state.GetNumRows()
if err != nil {
return err
}
if int(state.GetCurrentRow()) >= rows {
if err := state.nextOrder(true); err != nil {
return err
}
}
return nil
}
// Observe will attempt to detect a song loop
func (state *State) Observe() error {
if state.SongLoop.Count >= 0 {
if state.loopDetect.Observe(state.currentOrder, state.currentRow) {
if state.SongLoop.Count == 0 || (state.SongLoop.Count > 0 && state.loopCount >= state.SongLoop.Count) {
return song.ErrStopSong
}
state.loopCount += 1
state.loopDetect.Reset()
}
}
if state.currentOrder == index.Order(state.PlayUntilOrderAndRow.Order) && state.currentRow == index.Row(state.PlayUntilOrderAndRow.Row) {
if state.SongLoop.Count >= 0 && state.loopCount >= state.SongLoop.Count {
return song.ErrStopSong
}
}
return nil
}
// nextOrder travels to the next pattern in the order list
func (state *State) nextOrder(resetRow ...bool) error {
state.advanceOrder()
state.patternDelay.Reset()
state.finePatternDelay = 0
// called only to clean up order position info
if _, err := state.GetCurrentPatternIdx(); err != nil {
return err
}
if len(resetRow) > 0 && resetRow[0] {
state.currentRow = 0
}
return nil
}
// Reset resets a pattern state back to zeroes
func (state *State) Reset() {
*state = State{
SongLoop: feature.SongLoop{
Count: 0,
},
PlayUntilOrderAndRow: feature.PlayUntilOrderAndRow{
Order: -1,
Row: -1,
},
}
}
// nextRow travels to the next row in the pattern
// or the next order in the order list if the last row has been exhausted
func (state *State) nextRow() error {
state.patternDelay.Reset()
state.finePatternDelay = 0
var patNum = state.GetPatNum()
if patNum == index.InvalidPattern {
return nil
}
if patNum == index.NextPattern {
if err := state.nextOrder(true); err != nil {
return err
}
return nil
}
rows, err := state.GetNumRows()
if err != nil {
return err
}
if state.currentRow.Increment(rows) {
if err := state.nextOrder(true); err != nil {
return err
}
}
return nil
}
// GetRows returns all the rows in the pattern
func (state *State) GetRows() (song.Rows[channel.Data], error) {
nextRow:
for loops := 0; loops < len(state.Patterns); loops++ {
var patNum = state.GetPatNum()
switch patNum {
case index.InvalidPattern:
return nil, nil
case index.NextPattern:
if err := state.nextRow(); err != nil {
return nil, err
}
continue nextRow
default:
if int(patNum) >= len(state.Patterns) {
return nil, nil
}
pattern := state.Patterns[patNum]
return pattern.GetRows(), nil
}
}
return nil, nil
}
// NeedResetPatternLoops returns the state of the resetPatternLoops variable (and resets it)
func (state *State) NeedResetPatternLoops() bool {
rpl := state.resetPatternLoops
state.resetPatternLoops = false
return rpl
}
// commitTransaction will update the order and row indexes at once, idempotently, from a row update transaction.
func (state *State) commitTransaction(txn *pattern.RowUpdateTransaction) error {
tempo, tempoSet := txn.Tempo.Get()
tempoDelta, tempoDeltaSet := txn.TempoDelta.Get()
if tempoSet || tempoDeltaSet {
newTempo := state.tempo
if tempoSet {
newTempo = tempo
}
if tempoDeltaSet {
newTempo += tempoDelta
}
state.tempo = newTempo
}
if ticks, ok := txn.Ticks.Get(); ok {
state.ticks = ticks
}
if finePatternDelay, ok := txn.FinePatternDelay.Get(); ok {
state.finePatternDelay = finePatternDelay
}
if !state.patternDelay.IsSet() {
if patternDelay, ok := txn.GetPatternDelay(); ok {
state.patternDelay.Set(patternDelay)
}
}
if txn.BreakOrder {
if err := state.nextOrder(true); err != nil {
return err
}
}
orderIdx, orderIdxSet := txn.GetOrderIdx()
rowIdx, rowIdxSet := txn.GetRowIdx()
if orderIdxSet || rowIdxSet {
if orderIdxSet {
state.setCurrentOrder(orderIdx)
if !rowIdxSet {
if err := state.setCurrentRow(0); err != nil {
return err
}
}
}
if rowIdxSet {
if !orderIdxSet && !txn.RowIdxAllowBacktrack && state.currentRow > rowIdx {
if err := state.nextOrder(); err != nil {
return err
}
}
if err := state.setCurrentRow(rowIdx); err != nil {
return err
}
}
} else if txn.AdvanceRow {
if err := state.nextRow(); err != nil {
return err
}
}
return nil
}
// StartTransaction starts a row update transaction
func (state *State) StartTransaction() *pattern.RowUpdateTransaction {
txn := pattern.RowUpdateTransaction{
CommitTransaction: state.commitTransaction,
}
return &txn
}