-
Notifications
You must be signed in to change notification settings - Fork 2
/
ww.go
526 lines (461 loc) · 12 KB
/
ww.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package main
import (
"image"
"math/big"
"fmt"
"github.com/skelterjohn/go.wde"
"github.com/sqweek/sqribe/midi"
"github.com/sqweek/sqribe/score"
"github.com/sqweek/sqribe/wave"
. "github.com/sqweek/sqribe/core/types"
)
type changeMask int
const (
WAV changeMask = 1 << iota
SELXN
MIXER
SCALE
CURSOR
BEATS
VIEWPOS
LAYOUT
MAXBIT
RESET // clears layout state - not included in EVERYTHING
EVERYTHING changeMask = MAXBIT - 1
)
var yspacing = 12 // pixels between staff lines
type noteProspect struct {
delta int
beatf score.BeatPoint
staff *score.Staff
}
func (n *noteProspect) Eq(n2 *noteProspect) bool {
return n.staff == n2.staff && n.delta == n2.delta && n.beatf == n2.beatf
}
func (p *noteProspect) Δpitch(note *score.Note) int8 {
nline, _ := p.staff.LineForPitch(note.Pitch)
if nline == p.delta {
return 0
}
return int8(p.staff.PitchForLine(p.delta) - note.Pitch)
}
/* mkNote returns an existing note on the same staff line, if it exists (duration is ignored).
* Otherwise a new note is created with the given duration. */
func (p *noteProspect) mkNote(sc *score.Score, duration *big.Rat) (*score.Note, bool) {
beat, offset := sc.Quantize(p.beatf)
f := beat.FrameAtRat(offset)
next := sc.Iter(FrameRange{f, f}, p.staff)
var sn score.StaffNote
for next != nil {
sn, next = next()
if p.Δpitch(sn.Note) == 0 {
return sn.Note, true
}
}
/* no existing note found */
return &score.Note{p.staff.PitchForLine(p.delta), duration, beat, offset}, false
}
type noteDrag struct {
Δpitch int8
Δbeat *big.Rat
}
type mouseState struct {
cursor wde.Cursor
dragFn DragFn
note *noteProspect
ndelta *noteDrag
rectSelect *image.Rectangle
}
type FramePos struct {
f0 FrameN
ppix int
}
type WaveWidget struct {
WidgetCore
/* data related state */
wav *wave.Waveform
score *score.Score
iolisten <-chan *wave.Chunk
/* view related state */
pos FramePos
selection TimeRange
rect WaveLayout
notesel map[*score.Note]*score.Staff
snarf map[*score.Staff] []*score.Note // the cut/copy buffer
pasteMode bool
beatdrag map[*score.BeatRef]FrameN
/* renderer related state */
renderstate struct {
img *image.RGBA
waveRulers *image.RGBA
changed changeMask
cursor *image.RGBA
cursorPrevX int
}
mouse struct {
pos image.Point
state *mouseState
}
cursorX int
}
func NewWaveWidget(refresh chan Widget) *WaveWidget {
var ww WaveWidget
ww.pos.f0 = 0
ww.pos.ppix = 512
ww.rect.staff.Store(make(map[*score.Staff]*StaffLayout))
ww.selection = &FrameRange{0, 0}
ww.notesel = make(map[*score.Note]*score.Staff)
ww.renderstate.img = nil
ww.renderstate.changed = WAV
ww.refresh = refresh
return &ww
}
func (ww *WaveWidget) changed(mask changeMask, ev interface{}) {
ww.renderstate.changed |= mask
ww.refresh <- ww
}
func (ww *WaveWidget) SelectAudio(sel TimeRange) {
ww.selection = sel
G.plumb.selection.C <- sel
ww.changed(SELXN, sel)
}
func (ww *WaveWidget) SelectAudioSnapToBeats(start, end FrameN) {
sc := ww.score
if sc == nil {
ww.SelectAudio(FrameRange{start, end})
} else {
beats := score.BeatRange{sc.NearestBeat(start), sc.NearestBeat(end)}
ww.SelectAudio(beats)
}
}
func (ww *WaveWidget) ShuntSel(Δbeat int) {
sc := ww.score
br, ok := ww.selection.(score.BeatRange)
if ok && sc != nil {
ww.SelectAudio(sc.Shunt(br, Δbeat))
}
}
func (ww *WaveWidget) SelectedTimeRange() TimeRange {
return ww.selection
}
func (ww *WaveWidget) SetWaveform(wav *wave.Waveform) *wave.Waveform {
old := ww.wav
if old != nil {
old.CacheIgnore(ww.iolisten)
}
ww.wav = wav
if wav != nil {
iolisten := wav.CacheListen()
ww.iolisten = iolisten
go func() {
for {
chunk, ok := <-iolisten
if !ok {
return
}
if chunk == nil {
ww.ScrollPixels(0)
} else {
frng:= ww.pos.Range(ww.rect.wave.Dx())
s0, sN := wav.SampleRange(frng.MinFrame(), frng.MaxFrame())
if chunk.Intersects(s0, sN) {
ww.changed(WAV, chunk)
}
}
}
}()
}
ww.changed(WAV | VIEWPOS, wav)
return old
}
func (ww *WaveWidget) SetScore(sc *score.Score) *score.Score {
old := ww.score
if old != nil {
old.Unsub(ww)
}
ww.score = sc
if sc != nil {
events := make(chan interface{})
ww.score.Sub(ww, events)
go func() {
for ev := range events {
change := SCALE
switch ev := ev.(type) {
case score.BeatChanged:
change |= BEATS
case score.KeyChanged:
change |= MIXER
case score.StaffChanged:
gone := make([]score.StaffNote, 0, 8)
for note, staff := range ww.notesel {
if _, ok := ev.Staves[staff]; !ok {
continue
}
if staff.NoteAt(note) != note {
/* note has been removed from staff */
gone = append(gone, score.StaffNote{staff, note})
}
}
ww.deselectNotes(gone...)
if len(sc.Staves()) != len(ww.rect.staves()) {
change |= LAYOUT
}
case score.StaffMoved:
change |= LAYOUT
case score.ResetStaves:
ww.selectNotes(true) // clear selection
change |= RESET
}
// XXX could avoid redraw if the staff/beats aren't visible...
ww.changed(change, ev)
}
}()
selxn := make(chan interface{})
G.plumb.selection.Sub(&sc, selxn)
sc.InitQuantizer(selxn)
}
ww.changed(SCALE | LAYOUT, sc)
return old
}
func (ww *WaveWidget) MoveStaffTo(staff, anchor *score.Staff) {
sc := ww.score
if sc != nil {
sc.MoveStaff(staff, anchor)
}
}
func (ww *WaveWidget) SelectedNotes() []score.StaffNote {
notes := make([]score.StaffNote, 0, len(ww.notesel))
for note, staff := range ww.notesel {
notes = append(notes, score.StaffNote{staff, note})
}
return notes
}
func (pos *FramePos) Range(dx int) FrameRange {
return FrameRange{pos.f0, pos.f0 + FrameN(pos.ppix * dx)}
}
func (ww *WaveWidget) SetCursorByFrame(frame FrameN, follow bool) {
xmin, xmax := ww.rect.wave.Min.X, ww.rect.wave.Max.X
x := xmin + ww.pos.DxAtFrame(frame)
if follow && (x < xmin || x > xmax) {
ww.ScrollToPixel(x)
x = xmin + ww.pos.DxAtFrame(frame)
}
ww.cursorX = x
ww.changed(CURSOR, frame)
}
func (ww *WaveWidget) WaveRange() TimeRange {
return wave.Range(ww.wav)
}
func (ww *WaveWidget) FrameAtCursor() FrameN {
return ww.pos.FrameAtDx(ww.cursorX - ww.rect.wave.Min.X)
}
func (ww *WaveWidget) FrameAtPixel(x int) FrameN {
return ww.pos.FrameAtDx(x - ww.rect.wave.Min.X)
}
func (pos *FramePos) FrameAtDx(dx int) FrameN {
return pos.f0 + FrameN(dx * pos.ppix + pos.ppix/2)
}
func (pos *FramePos) DxAtFrame(frame FrameN) int {
return int(frame - pos.f0) / pos.ppix
}
func (ww *WaveWidget) areAllSelected(notes... score.StaffNote) bool {
for _, sn := range notes {
if _, ok := ww.notesel[sn.Note]; !ok {
return false
}
}
return true
}
func (ww *WaveWidget) toggleNotes(clear bool, notes... score.StaffNote) {
allSelected := !clear && ww.areAllSelected(notes...)
if allSelected {
ww.deselectNotes(notes...)
} else {
ww.selectNotes(clear, notes...)
}
}
func (ww *WaveWidget) deselectNotes(notes... score.StaffNote) {
newsel := make(map[*score.Note]*score.Staff)
for note, staff := range ww.notesel {
newsel[note] = staff
}
for _, sn := range notes {
delete(newsel, sn.Note)
}
ww.notesel = newsel
}
func (ww *WaveWidget) selectNotes(clear bool, notes... score.StaffNote) {
newsel := make(map[*score.Note]*score.Staff)
if !clear {
for note, staff := range ww.notesel {
newsel[note] = staff
}
}
for _, sn := range notes {
newsel[sn.Note] = sn.Staff
}
ww.notesel = newsel
}
func (ww *WaveWidget) staffContaining(pos image.Point) (*score.Staff, *StaffLayout) {
for staff, slayout := range ww.rect.staves() {
if !slayout.mix.Minimised && pos.In(slayout.r) {
return staff, slayout
}
}
return nil, nil
}
func (ww *WaveWidget) noteAtPixel(staff *score.Staff, pos image.Point) *noteProspect {
if slayout, ok := ww.rect.staves()[staff]; ok {
return ww.noteAtPixelWithMid(staff, pos, slayout.Mid())
}
return nil
}
func (ww *WaveWidget) noteAtPixelWithMid(staff *score.Staff, pos image.Point, mid int) *noteProspect {
noteY := snapto(pos.Y, mid, yspacing / 2)
delta := (mid - noteY) / (yspacing / 2)
frame := ww.FrameAtPixel(pos.X)
if beatf, ok := ww.score.ToBeat(frame); ok {
return ¬eProspect{delta, beatf, staff}
}
return nil
}
func (ww *WaveWidget) getMouseState(pos image.Point) *mouseState {
state := ww.mouse.state
cachedPos := ww.mouse.pos
if state != nil && pos.Eq(cachedPos) {
return state
}
state = ww.calcMouseState(pos)
ww.mouse.state = state
ww.mouse.pos = pos
return state
}
func (ww *WaveWidget) calcMouseState(pos image.Point) *mouseState {
state := new(mouseState)
dragFn, cursor := ww.dragState(pos)
state.dragFn = dragFn
state.cursor = cursor
if staff, slayout := ww.staffContaining(pos); staff != nil {
state.note = ww.noteAtPixelWithMid(staff, pos, slayout.Mid())
} else {
state.note = nil;
}
return state
}
func (ww *WaveWidget) ScrollToFrame(f FrameN) int {
return ww.ScrollToPixel(ww.rect.wave.Min.X + ww.pos.DxAtFrame(f))
}
func (ww *WaveWidget) ScrollToPixel(x int) int {
return ww.ScrollPixels(x - ww.rect.wave.Min.X - int(0.05 * float64(ww.rect.wave.Dx())))
}
func (ww *WaveWidget) Scroll(amount float64) int {
return ww.ScrollPixels(int(float64(ww.rect.wave.Dx()) * amount))
}
func (ww *WaveWidget) ScrollPixels(dx int) int {
if ww.Rect().Empty() || ww.wav == nil {
return 0
}
shift := FrameN(dx * ww.pos.ppix)
target := ww.wav.Clip(ww.pos.f0 + shift, FrameN((ww.rect.wave.Dx() + 1) * ww.pos.ppix))
if target == ww.pos.f0 {
return 0
}
ww.pos.f0 = target
ww.mouse.state = nil
ww.changed(WAV | CURSOR | VIEWPOS, &ww.pos)
return int(target - ww.pos.f0)
}
func (ww *WaveWidget) Zoom(factor float64) float64 {
fpp := int(float64(ww.pos.ppix) * factor)
if fpp == ww.pos.ppix {
if factor < 1.0 {
fpp--
} else if factor > 1.0 {
fpp++
}
}
if fpp < 1 {
fpp = 1
}
if wav := ww.wav; wav != nil && ww.rect.wave.Dx() > 0 {
max_frames := (12*1024*1024 / 10 / 2 / 2) * 9
max_fpp := int(max_frames / ww.rect.wave.Dx())
if fpp > max_fpp {
fpp = max_fpp
}
}
delta := float64(fpp) / float64(ww.pos.ppix)
if delta != 1.0 {
/* XXX should probably only account for cursor when mouse is over widget */
x := ww.mouse.pos.X
frameAtMouse := ww.FrameAtPixel(x)
dx := x - ww.rect.wave.Min.X
ww.pos.f0 = frameAtMouse - FrameN(dx * fpp)
ww.pos.ppix = fpp
ww.mouse.state = nil
ww.changed(WAV | CURSOR | VIEWPOS, &ww.pos)
}
return delta
}
func (ww *WaveWidget) CapturePos() (FrameN, int) {
return ww.pos.f0, ww.pos.ppix
}
func (ww *WaveWidget) RestorePos(f0 FrameN, ppix int) {
ww.pos.f0 = f0
ww.pos.ppix = ppix
ww.changed(WAV | CURSOR | VIEWPOS, &ww.pos)
}
func (ww *WaveWidget) IsMinimised(staff *score.Staff) bool {
slayout := ww.rect.staves()[staff]
return slayout != nil && slayout.mix.Minimised
}
func (ww *WaveWidget) RestoreStaffView(minimised map[*score.Staff]bool) {
ww.rect.restore(minimised)
}
func (ww *WaveWidget) Snarf() {
snarf := make(map[*score.Staff] []*score.Note)
for note, staff := range ww.notesel {
snarf[staff] = score.Merge(snarf[staff], note.Dup())
}
ww.snarf = snarf
}
func (ww *WaveWidget) PasteMode() bool {
return ww.pasteMode
}
func (ww *WaveWidget) SetPasteMode(mode bool) {
if ww.pasteMode == mode || (mode && ww.snarf == nil) {
return
}
ww.pasteMode = mode
ww.changed(SCALE, ww.snarf)
}
func (ww *WaveWidget) beatFrame(beat *score.BeatRef) FrameN {
if ww.beatdrag != nil {
if f, ok := ww.beatdrag[beat]; ok {
return f
}
}
return beat.Frame()
}
func (ww *WaveWidget) ToFrame(pt score.BeatPoint) FrameN {
b1 := pt.Beat()
f1, f2 := ww.beatFrame(b1), ww.beatFrame(b1.LNext())
return f1 + FrameN(pt.Offsetf() * float64(f2 - f1))
}
func (ww *WaveWidget) Status() string {
s := ww.getMouseState(ww.mouse.pos)
pitch := uint8(0)
delta := 0
delta2 := 0
offset := big.NewRat(1, 1)
nsharps := score.KeySig(-99)
if s.note != nil {
beatf := s.note.beatf
delta = s.note.delta
_, offset = ww.score.Quantize(beatf)
pitch = s.note.staff.PitchForLine(delta)
delta2, _ = s.note.staff.LineForPitch(pitch)
nsharps = ww.score.Key()
}
return fmt.Sprintf("line=%d (%d) pitch=%d %s offset=%v %v %v", delta, delta2, pitch, midi.PitchName(pitch), offset, nsharps, len(ww.notesel))
}