-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstringrow.go
More file actions
133 lines (114 loc) · 2.81 KB
/
stringrow.go
File metadata and controls
133 lines (114 loc) · 2.81 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
package layout
import (
"fmt"
"regexp"
"slices"
"strconv"
"strings"
s3mfile "github.com/gotracker/goaudiofile/music/tracked/s3m"
"github.com/gotracker/playback/format/s3m/channel"
s3mVolume "github.com/gotracker/playback/format/s3m/volume"
"github.com/gotracker/playback/index"
"github.com/gotracker/playback/song"
)
type StringRow string
func (r StringRow) Len() int {
return len(strings.SplitAfter(string(r), "|")) - 1
}
func (r StringRow) ForEach(fn func(ch index.Channel, d song.ChannelData[s3mVolume.Volume]) (bool, error)) error {
cstrPieces := strings.SplitAfter(string(r), "|")
cstrPieces = slices.DeleteFunc(cstrPieces, func(s string) bool {
return len(s) == 0 || s == "|"
})
row := make(Row, len(cstrPieces))
for ch, cstr := range cstrPieces {
d, err := r.decodeChannel(strings.TrimSuffix(cstr, "|"))
if err != nil {
return err
}
row[ch] = d
}
return row.ForEach(fn)
}
var channelRegex = regexp.MustCompile(`^(...) +(..) +(..) +(...)$`)
func (StringRow) decodeChannel(cstr string) (channel.Data, error) {
var d channel.Data
pieces := channelRegex.FindStringSubmatch(cstr)
if len(pieces) != 5 {
return d, fmt.Errorf("could not parse channel: %q", cstr)
}
note, instrument, vol, cmd := pieces[1], pieces[2], pieces[3], pieces[4]
d.Note = s3mfile.EmptyNote
// note
if note == "^^." {
d.What |= s3mfile.PatternFlagNote
d.Note = s3mfile.StopNote
} else if note != "..." {
key := note[0:2]
oct, err := strconv.Atoi(note[2:])
if err != nil {
return d, err
}
switch key {
case "C-":
d.Note = s3mfile.Note(oct<<4 | 0)
case "C#":
d.Note = s3mfile.Note(oct<<4 | 1)
case "D-":
d.Note = s3mfile.Note(oct<<4 | 2)
case "D#":
d.Note = s3mfile.Note(oct<<4 | 3)
case "E-":
d.Note = s3mfile.Note(oct<<4 | 4)
case "F-":
d.Note = s3mfile.Note(oct<<4 | 5)
case "F#":
d.Note = s3mfile.Note(oct<<4 | 6)
case "G-":
d.Note = s3mfile.Note(oct<<4 | 7)
case "G#":
d.Note = s3mfile.Note(oct<<4 | 8)
case "A-":
d.Note = s3mfile.Note(oct<<4 | 9)
case "A#":
d.Note = s3mfile.Note(oct<<4 | 10)
case "B-":
d.Note = s3mfile.Note(oct<<4 | 11)
default:
return d, fmt.Errorf("invalid key in note: %q", note)
}
d.What |= s3mfile.PatternFlagNote
}
// instrument
if instrument != ".." {
i, err := strconv.Atoi(instrument)
if err != nil {
return d, err
}
if i > 0 {
d.What |= s3mfile.PatternFlagNote
d.Instrument = uint8(i)
}
}
// vol
if vol != ".." {
v, err := strconv.Atoi(vol)
if err != nil {
return d, err
}
d.What |= s3mfile.PatternFlagVolume
d.Volume = s3mVolume.Volume(v)
}
// cmd
if cmd != "..." {
c := cmd[0]
i, err := strconv.ParseUint(cmd[1:], 16, 8)
if err != nil {
return d, err
}
d.What |= s3mfile.PatternFlagCommand
d.Command = c - '@'
d.Info = channel.DataEffect(i)
}
return d, nil
}