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
|
package main
import (
"encoding/csv"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"math"
"os"
)
const APP_VERSION = "0.1"
var versionFlag *bool = flag.Bool("v", false, "Print the version number.")
var inputFlag *string = flag.String("i", "", "The MusicXML file.")
var outputFlag *string = flag.String("o", "", "The CSV file to generate.")
var partFlag *int = flag.Int("p", 0, "The part to process.")
func init() {
flag.Parse()
if *outputFlag == "" {
*outputFlag = *inputFlag + ".csv"
}
}
/***** MusicXML structure *****/
type XMLFile struct {
Parts []Part `xml:"part"`
}
type Part struct {
Measures []Measure `xml:"measure"`
}
type Measure struct {
Attributes Attributes `xml:"attributes"`
Directions []Direction `xml:"direction"`
Notes []Note `xml:"note"`
}
type Attributes struct {
Divisions int `xml:"divisions"`
}
type Direction struct {
Sound Sound `xml:"sound"`
}
type Sound struct {
Tempo float64 `xml:"tempo,attr"`
}
type Note struct {
Pitch Pitch `xml:"pitch"`
Duration int `xml:"duration"`
}
type Pitch struct {
Step string `xml:"step"`
Alter int `xml:"alter"`
Octave int `xml:"octave"`
}
/***** Note conversion *****/
const REFERENCE_PITCH float64 = 440
var pitchOffset = map[string]int{
"C": -9,
"D": -7,
"E": -5,
"F": -4,
"G": -2,
"A": +0,
"B": +2,
}
func calcFrequency(pitch Pitch) float64 {
if pitch.Step == "" {
return 0
}
return math.Abs(REFERENCE_PITCH * float64(pitch.Octave-3) * math.Pow(2, float64(pitchOffset[pitch.Step]+pitch.Alter)/12))
}
/***** Division calculation *****/
const DEFAULT_DIVISION = 24
func readDivisions(part Part) int {
for _, measure := range part.Measures {
if measure.Attributes.Divisions != 0 {
return measure.Attributes.Divisions
}
}
return DEFAULT_DIVISION
}
/***** Tempo calculation *****/
const DEFAULT_TEMPO = 120
func readTempo(part Part) float64 {
for _, measure := range part.Measures {
for _, direction := range measure.Directions {
if direction.Sound.Tempo != 0 {
return direction.Sound.Tempo
}
}
}
return DEFAULT_TEMPO
}
func calcDuration(noteDuration int, divisions int, tempo float64) float64 {
return 60000 / tempo * float64(noteDuration) / float64(divisions)
}
/***** Main program *****/
func main() {
if *versionFlag {
fmt.Println("MusicXML to CSV converter\n2014 Pacien TRAN-GIRARD\nVersion:", APP_VERSION)
return
}
// read and parse MusicXML file
inputFile, err := os.Open(*inputFlag)
if err != nil {
fmt.Println("Error opening MusicXML file:", err)
return
}
defer inputFile.Close()
data, err := ioutil.ReadAll(inputFile)
if err != nil {
fmt.Println("Error reading MusicXML file:", err)
return
}
var parsed XMLFile
xml.Unmarshal(data, &parsed)
fmt.Println("Parsed data:", parsed)
// create and write to CSV file
outputFile, err := os.Create(*outputFlag)
if err != nil {
fmt.Println("Error creating CSV file:", err)
return
}
defer outputFile.Close()
writer := csv.NewWriter(outputFile)
defer writer.Flush()
if *partFlag >= len(parsed.Parts) {
fmt.Println("Error: part", *partFlag, "does not exist.")
return
}
part := parsed.Parts[*partFlag]
tempo := readTempo(part)
fmt.Println("Tempo:", tempo)
divisions := readDivisions(part)
fmt.Println("Divisions:", divisions)
for _, measure := range part.Measures {
for _, note := range measure.Notes {
pitch := calcFrequency(note.Pitch)
duration := calcDuration(note.Duration, divisions, tempo)
line := []string{fmt.Sprint(pitch), fmt.Sprint(duration)}
fmt.Println(line)
err := writer.Write(line)
if err != nil {
fmt.Println("Error writing in CSV file:", err)
return
}
}
}
fmt.Println("Done.")
}
|