This repository was archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternet-drum-pattern-validator.go
167 lines (128 loc) · 3.91 KB
/
internet-drum-pattern-validator.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
package main
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"github.com/fatih/color"
"os"
)
type DrumPattern struct {
instruments []Instrument
}
type Instrument struct {
notes []Note
}
type Note struct {
velocity byte
}
const instructions = `
Usage: internet-drum-pattern-validator <pattern>
where <pattern> is a standard Base64 (see RFC 4648) encoded byte array
following the Internet Drum Pattern Specification, see:
https://github.com/internetofdrums/internet-drum-pattern-spec#readme
If the pattern is valid, the pattern data is formatted and written to stdout.
`
const maximumNoteDataValue = 127
const numberOfInstruments = 12
const numberOfNotesPerBeat = 4
const numberOfBeatsPerBar = 4
const numberOfNotesPerInstrument = numberOfNotesPerBeat * numberOfBeatsPerBar
const numberOfDataPartsPerDrumPattern = numberOfInstruments * numberOfNotesPerInstrument
var errorColor = color.New(color.FgRed).SprintFunc()
var successColor = color.New(color.FgGreen).SprintFunc()
func Decode(pattern string) ([]byte, error) {
result, err := base64.StdEncoding.DecodeString(pattern)
if err != nil {
return nil, err
}
return result, nil
}
func ValidatePattern(pattern []byte) error {
numberOfDataParts := len(pattern)
if numberOfDataParts != numberOfDataPartsPerDrumPattern {
return errors.New(fmt.Sprintf(
"The drum pattern contains %d data parts (bytes), but should contain exactly %d bytes.",
numberOfDataParts,
numberOfDataPartsPerDrumPattern,
))
}
for _, dataPart := range pattern {
if dataPart < 0 || dataPart > maximumNoteDataValue {
return errors.New(fmt.Sprintf(
"Encountered data value of 0x%x, which exceeds allowed value of 0x%x.",
dataPart,
maximumNoteDataValue,
))
}
}
return nil
}
func Convert(pattern []byte) DrumPattern {
instruments := make([]Instrument, numberOfInstruments)
for i := 0; i < numberOfInstruments; i++ {
patternDataChunkStartIndex := i * numberOfNotesPerInstrument
patternDataChunkEndIndex := patternDataChunkStartIndex + numberOfNotesPerInstrument
noteData := pattern[patternDataChunkStartIndex:patternDataChunkEndIndex]
notes := make([]Note, numberOfNotesPerInstrument)
for j := 0; j < numberOfNotesPerInstrument; j++ {
note := Note{
velocity: noteData[j],
}
notes[j] = note
}
instruments[i] = Instrument{notes}
}
return DrumPattern{instruments}
}
func GetFormattedPattern(pattern DrumPattern) string {
var buffer bytes.Buffer
for _, instrument := range pattern.instruments {
AppendFormattedInstrument(instrument, &buffer)
}
return buffer.String()
}
func AppendFormattedInstrument(instrument Instrument, buffer *bytes.Buffer) {
for index, note := range instrument.notes {
AppendFormattedNote(note, buffer)
if (index+1)%numberOfNotesPerBeat == 0 {
buffer.WriteString(" ")
}
}
buffer.WriteString(fmt.Sprintln())
}
func AppendFormattedNote(note Note, buffer *bytes.Buffer) {
AppendFormattedNoteDataPart(note.velocity, buffer)
buffer.WriteString(" ")
}
func AppendFormattedNoteDataPart(value byte, buffer *bytes.Buffer) {
if value == 0x00 {
buffer.WriteString("0x00")
} else {
buffer.WriteString(fmt.Sprintf("0x%X", value))
}
}
func main() {
if len(os.Args) != 2 {
fmt.Fprint(os.Stderr, instructions)
os.Exit(1)
}
pattern, decodingError := Decode(os.Args[1])
if decodingError != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf(errorColor("Could not decode drum pattern: %s."), decodingError))
os.Exit(1)
}
validationError := ValidatePattern(pattern)
if validationError != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf(errorColor("The drum pattern is invalid: %s"), validationError))
os.Exit(1)
}
fmt.Println(successColor("The drum pattern is valid!"))
fmt.Println()
drumPattern := Convert(pattern)
formattedPattern := GetFormattedPattern(drumPattern)
fmt.Println("After decoding, the pattern looks like this, " +
"where 0xXX is one note with a velocity of XX:")
fmt.Println()
fmt.Print(formattedPattern)
}