-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsqribe.go
356 lines (317 loc) · 7.98 KB
/
sqribe.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main
import (
"flag"
"fmt"
"github.com/sqweek/dialog"
"github.com/sqweek/fs"
"io"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
"time"
"github.com/sqweek/sqribe/audio"
"github.com/sqweek/sqribe/log"
"github.com/sqweek/sqribe/midi"
"github.com/sqweek/sqribe/plumb"
"github.com/sqweek/sqribe/score"
"github.com/sqweek/sqribe/wave"
)
type PendingLoad struct {
files FileContext
s State
wav *wave.Waveform
wavDone chan error
}
var G struct {
/* global state */
files FileContext
score *score.Score
wav *wave.Waveform
/* plumbing */
plumb struct {
selection *plumb.Port
score *plumb.Port
}
/* ui stuff */
ww *WaveWidget
mixw *MixWidget
instMenu MenuWidget
noteMenu MenuWidget
overlay *OverlayWidget
font struct {
luxi *Font
}
kb struct {
shift bool
}
}
var ZeroTime time.Time
var AudioExtensions = []string{"mp3", "ogg", "m4a", "wma", "mov", "mp4", "flv", "wmv"}
func open(filename string) error {
ld, err := Load(filename)
if err != nil {
return err
}
ld.Pivot()
return nil
}
func Load(filename string) (ld PendingLoad, err error) {
ld.files, ld.s, err = Open(filename)
if ld.files.ViaState {
if _, err := os.Stat(ld.files.Audio); os.IsNotExist(err) {
dlg := dialog.Message("Cannot find audio file! Last known path: %s\nWould you like to browse for the file?", ld.files.Audio)
if dlg.YesNo() {
filedlg := dialog.File().Title("sqribe - locate audio for " + ld.files.Audio).Filter("Audio Files", AudioExtensions...)
if f, err := filedlg.Load(); err == nil {
ld.files.Audio = f
} else if err != dialog.Cancelled {
log.UI.Printf("locating audio for %s: %v", ld.files.Audio, err)
}
}
}
}
if err = os.MkdirAll(App.Cache, 0777); err != nil {
return
}
ld.wavDone = make(chan error)
ld.wav, err = wave.NewWaveform(ld.files.Audio, filepath.Join(App.Cache, *cachefile), ld.wavDone)
if err != nil {
return
}
return
}
// Finishes the load; pivots memory model to new pending context
func (ld *PendingLoad) Pivot() {
// point of no return; nothing errors after this and we transition to the new file
ld.s.Restore()
G.files = ld.files
G.wav = ld.wav
old := G.ww.SetWaveform(ld.wav)
if old != nil {
go old.Close()
}
go func() {
erri := <-ld.wavDone // wait for audio to finish loading
switch err := erri.(type) {
case wave.IOError:
alert(err.Error())
case wave.DecodeError:
/* some mp3s apparently contain stupid stuff like ID3 tags in the middle of
* the last data frame, so a user alert is not raised here... */
}
/* Send some spurious change events now that waveform's NSamples is stable. */
G.plumb.score.C <- score.BeatChanged{}
G.plumb.score.C <- score.StaffChanged{}
}()
}
func save() error {
if G.files.Audio == "" {
return nil // save with no audio loaded is a no-op
}
if !G.files.Timestamp.IsZero() {
// user needs to be able to recover from these errors
st, err := os.Stat(G.files.State)
if err != nil && !os.IsNotExist(err) {
return err // not a very clear error...
}
if st.ModTime() != G.files.Timestamp {
return fmt.Errorf("file has changed since loading")
}
}
s := CaptureState()
if err := SaveState(&G.files, s); err != nil {
return err
}
if st, err := os.Stat(G.files.State); err == nil {
G.files.Timestamp = st.ModTime()
} else {
log.FS.Println("warning: couldn't retreive timestamp:", err)
G.files.Timestamp = ZeroTime
}
return nil
}
func mustMkFont(filename string, size int) *Font {
font, err := NewFont(filename, size)
if err != nil {
panic(err)
}
return font
}
var initialTime = flag.Duration("time", 0, "position initial view at this time (eg 1m32s)")
var profile = flag.String("prof", "", "write cpu profile to file")
var cachefile = flag.String("cache", "", "cache file name")
func alert(format string, args... interface{}) {
msg := fmt.Sprintf(format, args...)
log.Printf("ERROR %s", msg)
dialog.Message("%s", msg).Title("sqribe - error").Error()
}
func fatal(args... interface{}) {
msg := fmt.Sprint(args...)
log.Printf("FATAL %s", msg)
dialog.Message("%s", msg).Title("sqribe - fatal error").Error()
os.Exit(1)
}
func main() {
flag.Parse()
if err := fsinit("net.sqweek.sqribe", "sqribe"); err != nil {
fatal(err)
}
if *cachefile == "" {
main_parent()
} else {
wderun(main_child)
//XXX should avoid closing GUI if save fails
if err := save(); err != nil {
log.FS.Println(err)
}
}
}
type TeeLogger struct {
writers []io.Writer
errs int
}
func (t *TeeLogger) Write(buf []byte) (n int, err error) {
for i, w := range t.writers {
if w == nil {
continue
}
n, err = w.Write(buf)
if err != nil {
// ignore Writers that have errored. report last error only
t.errs++
t.writers[i] = nil
if t.errs == len(t.writers) {
return 0, err
}
}
}
return n, err
}
func main_parent() {
host, err := os.Hostname()
if err != nil {
log.Println("os.Hostname failed:", err)
host = "localhost"
}
cachename := fmt.Sprintf("%s.%d", host, os.Getpid())
cmd := exec.Command(os.Args[0])
cmd.Args = make([]string, len(os.Args) + 1)
cmd.Args[0] = os.Args[0]
cmd.Args[1] = fmt.Sprintf("-cache=%s", cachename)
for i := 1; i < len(os.Args); i++ {
cmd.Args[i+1] = os.Args[i]
}
logname := fmt.Sprintf("%s.%d.log", host, os.Getpid())
logpath := filepath.Join(App.Cache, logname)
logfile, err := fs.CreateIn(App.Cache, logname)
var logger io.Writer
if err != nil {
log.Println("error creating log file: ", err)
logger = os.Stderr
} else {
logger = &TeeLogger{writers: []io.Writer{os.Stderr, logfile}}
}
cmd.Stdout = logger
cmd.Stderr = logger
status := 0
err = cmd.Start()
if err != nil {
fatal("launch error:", err)
}
sigs := make(chan os.Signal)
signal.Notify(sigs)
go func() {
for sig := range sigs {
// propagate signal to child process. we'll die when it dies.
cmd.Process.Signal(sig)
}
}()
state, err := cmd.Process.Wait()
if err != nil {
log.Println("wait error:", err)
}
if state != nil && !state.Success() {
status = 1
}
os.Remove(filepath.Join(App.Cache, cachename))
if status == 0 && logfile != nil {
logfile.Close()
os.Remove(logpath)
}
os.Exit(status)
}
func main_child() {
var err error
log.Println("sqribe version unknown")
confinit()
if *profile != "" {
f, err := os.Create(*profile)
if err != nil {
fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
err = audio.Open()
if err != nil {
fatal(err)
}
G.plumb.selection = plumb.MkPort()
G.plumb.score = plumb.MkPort()
G.score = score.MkScore(G.plumb.score)
G.font.luxi = mustMkFont(MustFind("luxisr.ttf"), 10)
G.noteMenu = mkMenu(StringMenuOps{}, "1/16", "1/8", "1/4", "1/2", "1", "2", "3", "4")
G.noteMenu.SetDefault("1")
G.instMenu = mkMenu(StringMenuOps{toStr: func(item interface{})string {return midi.InstName(item.(int))}}, midi.InstPiano, midi.InstEPiano, midi.InstGuitar, midi.InstEGuitar, midi.InstMuteGuitar, midi.InstViolin, midi.InstHarp, midi.InstVoice)
soundfont := Cfg.FS.SoundFont
if soundfont == "" {
soundfont = MustFind("FluidR3_GM.sf2")
} else if _, err = os.Stat(soundfont); err != nil {
fatal(err)
}
Synth, err = SynthInit(audio.SampleRate, soundfont)
if err != nil {
fatal(err)
}
audioFile := flag.Arg(0)
var ld PendingLoad
var lderr error
if len(audioFile) > 0 {
ld, lderr = Load(audioFile)
if lderr != nil {
alert("%v", lderr)
}
}
redraw := make(chan Widget, 10)
G.ww = NewWaveWidget(redraw)
G.ww.SetScore(G.score)
G.mixw = NewMixWidget(redraw)
G.overlay = NewOverlayWidget(redraw)
var view SavedView
if len(audioFile) > 0 && lderr == nil {
view = ld.s.View()
}
wg := InitWde(redraw, view)
// 1. audio callback thread
// 2. ui event goroutine
// 3. ui painting goroutine
// 4. sample prefetch goroutine
// 5. synth goroutine
// 6. feedback goroutine
// 7. quantizer
// 8. io cache fetcher
// 9. audio decoder
runtime.GOMAXPROCS(6)
if len(audioFile) > 0 && lderr == nil {
ld.Pivot()
if *initialTime != 0 && G.wav != nil {
G.ww.ScrollToFrame(G.wav.FrameAtTime(*initialTime))
}
}
redraw <- nil
wg.Wait()
audio.Shutdown()
}