-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgo2addict.go
121 lines (98 loc) · 2.98 KB
/
go2addict.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
// pro2addict
// Copyright 2017 Luke Hines
// Released under the MIT License
// https://tldrlegal.com/license/mit-license
package main
import (
"bytes"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/zackslash/pro2addict/maps"
"github.com/zackslash/pro2addict/midiParse"
cli "gopkg.in/alecthomas/kingpin.v2"
)
const (
release = "1.0.1"
drumKey = "drum"
)
var (
midiInLocation = cli.Arg("in", "location of your Guitar Pro MIDI file").Required().String()
midiOutLocation = cli.Arg("out", "location to output converted (AD2) MIDI file").String()
debugMode = false
)
func main() {
cli.Version(release)
cli.Parse()
// Read and parse the MIDI file.
originalBytes, midi, err := readMIDIFile(*midiInLocation)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Read MIDI with %d tracks.\n", len(midi.Tracks))
// Convert drum notes in the MIDI data.
convertedBytes := convertDrumNotes(midi, originalBytes)
// Determine the output file location.
output := *midiOutLocation
if output == "" {
output = *midiInLocation + "-ad2.mid"
}
// Write the converted MIDI data to the output file.
if err := writeMIDIFile(output, convertedBytes); err != nil {
log.Fatal(err)
}
fmt.Printf("Done - %s\n", output)
}
// readMIDIFile reads a MIDI file from the given filePath and parses it.
// It returns the file contents, the parsed MIDI data, and an error if any.
func readMIDIFile(filePath string) ([]byte, *midiParse.MIDI, error) {
b, err := os.ReadFile(filePath)
if err != nil {
return nil, nil, err
}
midi, err := midiParse.ParseMidi(b, debugMode)
if err != nil {
return nil, nil, err
}
return b, midi, nil
}
// convertDrumNotes takes a MIDI object and the original file bytes,
// and converts the drum notes using a mapping function. It returns the modified bytes.
func convertDrumNotes(midi *midiParse.MIDI, originalBytes []byte) []byte {
res := make([]byte, len(originalBytes))
copy(res, originalBytes)
for _, t := range midi.Tracks {
if debugMode {
fmt.Printf("Track:%d/ch%d - %s - %s\n", t.Number, t.Channel, t.InstrumentName, t.TrackName)
}
if strings.Contains(strings.ToLower(t.InstrumentName), drumKey) {
res = replaceNotes(t, res)
}
}
return res
}
// replaceNotes takes a single track and the current result bytes,
// replaces the note data for drum notes, and returns the updated result bytes.
func replaceNotes(track midiParse.Track, res []byte) []byte {
for _, n := range track.Notes {
oldData := n.GetNoteData()
s := fmt.Sprintf("%d", oldData[1])
i, _ := strconv.Atoi(s)
rep := maps.GetMappedNote(i)
newData := make([]byte, len(oldData))
copy(newData, oldData)
newData[1] = byte(rep)
if debugMode {
fmt.Printf("From:%x To:%x\n", oldData, newData)
}
res = bytes.Replace(res, oldData, newData, 1)
}
return res
}
// writeMIDIFile writes the modified MIDI data to a file at the specified output location.
// It returns an error if the write fails.
func writeMIDIFile(output string, data []byte) error {
return os.WriteFile(output, data, 0644)
}