-
Notifications
You must be signed in to change notification settings - Fork 1
/
muskel.go
174 lines (137 loc) · 3.7 KB
/
muskel.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
168
169
170
171
172
173
174
package muskel
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"gitlab.com/gomidi/midi/smf/smfwriter"
"gitlab.com/gomidi/muskel/file"
"gitlab.com/gomidi/muskel/image"
"gitlab.com/gomidi/muskel/score"
"gitlab.com/gomidi/muskel/smf"
"gitlab.com/gomidi/muskel/smfimport"
"gitlab.com/gomidi/muskel/xlsx"
"gitlab.com/gomidi/smfimage"
"gitlab.com/metakeule/config"
)
const MUSKEL_VERSION_FILE = "muskel_version.txt"
func ReadWDVersionFile(dir string) (*config.Version, error) {
p := filepath.Join(dir, MUSKEL_VERSION_FILE)
b, err := ioutil.ReadFile(p)
if err != nil {
return nil, err
}
v, err2 := config.ParseVersion(strings.TrimSpace(string(b)))
if err2 != nil {
return nil, err2
}
return v, nil
}
func WriteWDVersionFile(dir string) error {
p := filepath.Join(dir, MUSKEL_VERSION_FILE)
return ioutil.WriteFile(p, []byte(VERSION), 0644)
}
func Import(srcFile string, targetFile string, opts ...smfimport.Option) error {
fh, err := os.Open(srcFile)
if err != nil {
return fmt.Errorf("can't open file %q: %s", srcFile, err.Error())
}
defer fh.Close()
im := smfimport.New(srcFile, fh, opts...)
if filepath.Ext(targetFile) == ".xlsx" {
var tracksbf, scorebf bytes.Buffer
err := im.WriteTracksAndScoreTable(&tracksbf, &scorebf)
if err != nil {
return fmt.Errorf("can't import from %q: %s", srcFile, err.Error())
}
return xlsx.Write(targetFile, tracksbf.String(), scorebf.String())
} else {
tg, err := os.Create(targetFile)
if err != nil {
return fmt.Errorf("can't create file %q: %s", targetFile, err.Error())
}
defer tg.Close()
return im.WriteUnrolled(tg)
}
}
func newFile(filename string, params []string, rd io.Reader, opts ...score.Option) *file.File {
sc := score.New(filename, params, opts...)
return file.FromReader(rd, sc)
}
func Format(filename string, params []string, rd io.Reader, wr io.Writer, opts ...score.Option) error {
var f = newFile(filename, params, rd, opts...)
err := f.Parse()
if err != nil {
return err
}
return f.WriteTo(wr)
}
func Unroll(mainFile string, params []string, rd io.Reader, wr io.Writer, opts ...score.Option) error {
sc := score.New(mainFile, params, opts...)
f := file.FromReader(rd, sc)
err := f.Parse()
if err != nil {
return fmt.Errorf("parsing error: %s", err)
}
err = sc.Unroll()
if err != nil {
return err
}
sc.WriteUnrolled(wr)
return nil
}
func WriteSMF(filename string, params []string, rd io.Reader, fmtwr io.Writer, midwr io.Writer, opts ...score.Option) error {
var f = newFile(filename, params, rd, opts...)
err := f.Parse()
if err != nil {
return err
}
if fmtwr != nil {
err = f.WriteTo(fmtwr)
if err != nil {
return err
}
}
sc := f.Score.(*score.Score)
err = sc.Unroll()
if err != nil {
return err
}
return smf.WriteSMFTo(sc, midwr, "*")
}
func ParseFile(mainFile string, params []string, opts ...score.Option) (sc *score.Score, err error) {
sc = score.New(mainFile, params, opts...)
err = sc.Parse()
return
}
func Convert(mainFile string, params []string, smffile string, opts ...score.Option) error {
sc := score.New(mainFile, params, opts...)
err := sc.Parse()
if err != nil {
return err
}
err = sc.Format()
if err != nil {
return err
}
err = sc.Unroll()
if err != nil {
return err
}
return smf.WriteFile(sc, smffile)
}
func WriteSMFFile(sc *score.Score, smffile string, opts ...smfwriter.Option) error {
return smf.WriteFile(sc, smffile, opts...)
}
// WriteImage expects the given score to be unrolled
func WriteImage(sc *score.Score, outfile string, opts ...smfimage.Option) error {
var midibf bytes.Buffer
err := smf.WriteSMFTo(sc, &midibf, "*")
if err != nil {
return err
}
return image.MakeImage(sc, &midibf, outfile, opts...)
}