-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqribe_wde.go
356 lines (335 loc) · 8.96 KB
/
sqribe_wde.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 (
"fmt"
"github.com/sqweek/dialog"
"image"
"image/color"
"image/draw"
"sync"
"time"
"github.com/skelterjohn/go.wde"
_ "github.com/skelterjohn/go.wde/init"
"github.com/sqweek/sqribe/audio"
"github.com/sqweek/sqribe/log"
"github.com/sqweek/sqribe/score"
)
func toggle(flag *bool) {
*flag = !*flag
}
type DragState struct {
fn DragFn
moved bool
button wde.Button
}
// overridden on OSX; see sqribe_darwin.go
var eventFilter func(<-chan interface{}) <-chan interface{} = nil
var RootWin wde.Window
func event(win wde.Window, redraw chan Widget, done chan bool, wg *sync.WaitGroup) {
openDlg := dialog.File().Title("sqribe - Open").Filter("Audio Files", AudioExtensions...).Filter("Sqribe Save", "sqs")
exportDlg := dialog.File().Title("sqribe - Export to MusicXML").Filter("MXML Files", "xml", "mxl")
events := win.EventChan()
if eventFilter != nil {
events = eventFilter(events)
}
defer func() {
done <- true
wg.Done()
}()
var drag DragState
var refreshTimer *time.Timer
for ei := range events {
switch e := ei.(type) {
case wde.MouseDownEvent:
if drag.button == 0 {
drag.button = e.Which
drag.moved = false
}
if e.Where.In(G.ww.Rect()) {
if drag.fn == nil {
drag.fn = G.ww.ButtonDown(e)
}
}
case wde.MouseUpEvent:
done := e.Which == drag.button
if done {
drag.button = 0
}
if drag.fn != nil {
drag.fn(e.Where, done, drag.moved)
if done {
drag.fn = nil
}
/* Hmm... continue here forces DragFn to be responsible for regular clicks.
* But it also keeps logic related to a particular UI component together. */
continue
}
if drag.moved {
/* prevent drags from being interpreted as a regular click */
continue
}
switch (e.Which) {
case wde.LeftButton:
if e.Where.In(G.ww.Rect()) {
G.ww.LeftClick(e.Where)
} else if e.Where.In(G.mixw.Rect()) {
G.mixw.LeftClick(e.Where)
}
case wde.RightButton:
if e.Where.In(G.ww.Rect()) {
G.ww.RightClick(e.Where)
} else if e.Where.In(G.mixw.Rect()) {
G.mixw.RightClick(e.Where)
}
}
case wde.MouseDraggedEvent:
drag.moved = true
if drag.fn != nil {
drag.fn(e.Where, false, true)
}
case wde.MouseMovedEvent:
var cur wde.Cursor = wde.NormalCursor
if e.Where.In(G.ww.Rect()) {
cur = G.ww.MouseMoved(e.Where)
}
win.SetCursor(cur)
case wde.ScrollEvent:
G.ww.ScrollPixels(-e.Delta.X)
case wde.MagnifyEvent:
G.ww.Zoom(1.0 / e.Magnification)
case wde.KeyDownEvent:
switch e.Key {
case wde.KeyLeftShift, wde.KeyRightShift:
G.kb.shift = true
}
case wde.KeyUpEvent:
switch e.Key {
case wde.KeyLeftShift, wde.KeyRightShift:
G.kb.shift = false
}
case wde.KeyTypedEvent:
log.UI.Println("typed", e.Key, e.Glyph, e.Chord)
switch {
case e.Chord == "shift+left_arrow":
G.ww.ShuntSel(-1)
case e.Chord == "shift+right_arrow":
G.ww.ShuntSel(1)
case e.Chord == "shift+" + wde.KeyInsert, e.Chord == "control+v", e.Key == wde.KeyInsert:
G.ww.SetPasteMode(!G.ww.PasteMode())
case e.Chord == "shift+" + wde.KeyDelete, e.Chord == "control+x":
G.ww.Snarf()
G.score.RemoveNotes(G.ww.SelectedNotes()...)
G.ww.SetPasteMode(true)
case e.Chord == "control+o":
if playState == STOPPED {
var err error
var f string
if err = save(); err == nil {
// TODO should block rather than buffer input events during dialog
if f, err = openDlg.Load(); err == nil {
err = open(f)
}
}
if err != nil && err != dialog.Cancelled {
alert("%v", err)
}
}
case e.Chord == "control+e":
go func() {
f, err := exportDlg.Save()
if err == nil {
err = ExportMXML(f)
}
if err != nil && err != dialog.Cancelled {
alert("MXML export failed: %v", err)
}
}()
case e.Chord == "control+c":
G.ww.Snarf()
G.ww.SetPasteMode(true)
case e.Chord == "control+z":
G.score.Undo()
case e.Chord == "shift+control+z", e.Chord == "control+y":
G.score.Redo()
case e.Chord == "shift+prior":
G.mixw.AdjustGain(&Mixer.Midi.Gain, 0.1)
case e.Chord == "shift+next":
G.mixw.AdjustGain(&Mixer.Midi.Gain, -0.1)
case e.Key == wde.KeyEscape:
G.ww.SetPasteMode(false)
case e.Key == wde.KeyLeftArrow:
G.ww.Scroll(-0.25)
case e.Key == wde.KeyRightArrow:
G.ww.Scroll(0.25)
case e.Key == wde.KeyUpArrow:
G.ww.Zoom(0.5)
case e.Key == wde.KeyDownArrow:
G.ww.Zoom(2.0)
case e.Key == wde.KeyF2:
G.score.KeyChange(-1)
case e.Key == wde.KeyF3:
G.score.KeyChange(1)
case e.Key == wde.KeyF5:
Synth.AdjustTuning(-10)
case e.Key == wde.KeyF6:
Synth.AdjustTuning(10)
case e.Key == wde.KeyPrior:
G.mixw.AdjustGain(&Mixer.Wave.Gain, 0.1)
case e.Key == wde.KeyNext:
G.mixw.AdjustGain(&Mixer.Wave.Gain, -0.1)
case e.Key == wde.KeySpace:
playToggle()
case e.Key == wde.KeyReturn:
if f, playing := audio.PlayingFrame(); playing {
G.score.AddBeat(f)
}
case e.Key == wde.KeyDelete:
G.score.RemoveNotes(G.ww.SelectedNotes()...)
case e.Glyph == "s":
if err := save(); err != nil {
alert("save failed: %v", err)
}
case e.Glyph == "t":
G.mixw.Toggle(&Mixer.MuteMetronome)
case e.Glyph == "a":
G.mixw.Toggle(&Mixer.Wave.Muted)
case e.Glyph == "m":
G.mixw.Toggle(&Mixer.Midi.Muted)
case e.Glyph == "q":
go G.score.QuantizeBeats()
case e.Glyph == "#":
G.score.MvNotes(1, &rZero, G.ww.SelectedNotes()...)
case e.Glyph == "@":
G.score.MvNotes(-1, &rZero, G.ww.SelectedNotes()...)
case e.Chord == "shift+8":
G.score.MvNotes(-12, &rZero, G.ww.SelectedNotes()...)
case e.Key == wde.Key8:
G.score.MvNotes(12, &rZero, G.ww.SelectedNotes()...)
case e.Glyph == "%":
rng := G.ww.SelectedTimeRange()
if beats, ok := rng.(score.BeatRange); ok {
G.score.RepeatNotes(beats)
}
}
case wde.ResizeEvent:
if refreshTimer != nil {
refreshTimer.Stop()
}
refreshTimer = time.AfterFunc(50*time.Millisecond, func() {redraw <- nil})
case wde.CloseEvent:
return
}
}
}
/* rounds sub-second duration to nearest ms/μs/ns */
func niceDur(dur time.Duration) string {
if dur >= time.Second {
return fmt.Sprintf("%.2fs", dur.Seconds())
}
switch {
case dur >= time.Millisecond:
return fmt.Sprintf("%dms", int(dur / time.Millisecond))
case dur >= time.Microsecond:
return fmt.Sprintf("%dµs", int(dur / time.Microsecond))
default:
return fmt.Sprintf("%dns", int(dur))
}
}
func quantizeStr() string {
q := G.score.QuantizeBeatStat()
if q.Nop() {
return ""
}
bpm := 60.0 * float64(time.Second) / float64(G.wav.TimeAtFrame(q.AvgFramesPerBeat()))
errd := G.wav.TimeAtFrame(*q.Error)
return fmt.Sprintf("%.1fbpm ±%v", bpm, niceDur(errd))
}
func tuningStr() string {
freq := Synth.TuningFreq()
return fmt.Sprintf("A=%.4gHz", freq)
}
func drawstatus(dst draw.Image, r image.Rectangle) {
bg := color.RGBA{0xcc, 0xcc, 0xcc, 0xff}
draw.Draw(dst, r, &image.Uniform{bg}, image.ZP, draw.Src)
G.font.luxi.Draw(dst, color.Black, r, fmt.Sprintf("%s %v %v", G.ww.Status(), quantizeStr(), tuningStr()))
}
func drawstuff(w wde.Window, redraw chan Widget, done chan bool) {
rate := time.Millisecond * 25 /* maximum refresh rate */
lastframe := time.Now().Add(-rate)
var refresh func()
merged := 0
stale := make(map[Widget]struct{})
for {
select {
case widget := <-redraw:
if widget != nil {
stale[widget] = struct{}{}
}
now := time.Now()
nextframe := lastframe.Add(rate)
if refresh != nil || now.Before(nextframe) {
merged++
if refresh == nil {
refresh = func() {
redraw <- widget
refresh = nil
}
time.AfterFunc(nextframe.Sub(now), refresh)
}
} else {
lastframe = now
screen := w.Screen()
r := screen.Bounds()
width, height := r.Dx(), r.Dy()
wvR := image.Rect(r.Min.X, r.Min.Y + 50, r.Max.X, r.Max.Y - 20)
G.ww.Draw(screen, wvR)
mixR := image.Rect(width - 90, wvR.Min.Y - 50, width, wvR.Min.Y)
G.mixw.Draw(screen, mixR)
statusR := image.Rect(0, wvR.Max.Y, width, height)
statusI := image.NewRGBA(statusR)
drawstatus(statusI, statusR)
screen.CopyRGBA(statusI, statusR)
if !G.noteMenu.Rect().Empty() {
G.noteMenu.Draw(screen, G.noteMenu.Rect())
}
if !G.instMenu.Rect().Empty() {
G.instMenu.Draw(screen, G.instMenu.Rect())
}
G.overlay.Draw(screen, screen.Bounds())
w.FlushImage()
merged = 0
lastframe = time.Now()
for k, _ := range stale {
delete(stale, k)
}
}
case <-done:
return
}
}
}
func InitWde(redraw chan Widget, view SavedView) *sync.WaitGroup {
if view.Width == 0 {
view.Width, view.Height = 800, 400
}
Glyphs.init(yspacing)
dw, err := wde.NewWindow(view.Width, view.Height)
if err != nil {
fatal(err)
}
dw.SetTitle("Sqribe")
dw.Show()
RootWin = dw
wg := sync.WaitGroup{}
wg.Add(1)
done := make(chan bool)
go drawstuff(dw, redraw, done)
go event(dw, redraw, done, &wg)
return &wg
}
func wderun(f func()) {
go func() {
f()
wde.Stop()
}()
wde.Run()
}