-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
selection.ts
632 lines (545 loc) · 16 KB
/
selection.ts
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
import { min } from "lodash"
import cloneDeep from "lodash/cloneDeep"
import { intersects } from "../../common/geometry"
import { isNotNull, isNotUndefined } from "../../common/helpers/array"
import { tickToMillisec } from "../../common/helpers/bpm"
import {
Selection,
clampSelection,
movedSelection,
regularizedSelection,
} from "../../common/selection/Selection"
import { NoteEvent, TrackEvent, isNoteEvent } from "../../common/track"
import { NotePoint, clampNotePoint } from "../../common/transform/NotePoint"
import {
PianoNotesClipboardData,
isPianoNotesClipboardData,
} from "../clipboard/clipboardTypes"
import clipboard from "../services/Clipboard"
import RootStore from "../stores/RootStore"
import { transposeNotes } from "./song"
function eventsInSelection(events: TrackEvent[], selection: Selection) {
const selectionRect = {
x: selection.from.tick,
width: selection.to.tick - selection.from.tick,
y: selection.to.noteNumber,
height: selection.from.noteNumber - selection.to.noteNumber,
}
return events.filter(isNoteEvent).filter((b) =>
intersects(
{
x: b.tick,
width: b.duration,
y: b.noteNumber - 1, // Subtract 1 since the pitch is the lower end of the rectangle
height: 1,
},
selectionRect,
),
)
}
export const resizeSelection =
({ pianoRollStore }: RootStore) =>
(start: NotePoint, end: NotePoint) => {
const selection = regularizedSelection(
start.tick,
start.noteNumber,
end.tick,
end.noteNumber,
)
// integer containing the original coordinates.
selection.from.noteNumber = Math.ceil(selection.from.noteNumber)
selection.to.noteNumber = Math.floor(selection.to.noteNumber)
pianoRollStore.selection = clampSelection(selection)
}
export const fixSelection =
({
pianoRollStore,
pianoRollStore: { selectedTrack, selection },
}: RootStore) =>
(clearRect: boolean = false) => {
if (selectedTrack === undefined || selection === null) {
return
}
// 選択範囲を確定して選択範囲内のノートを選択状態にする
// Confirm the selection and select the notes in the selection state
pianoRollStore.selectedNoteIds = eventsInSelection(
selectedTrack.events,
selection,
).map((e) => e.id)
if (clearRect) {
pianoRollStore.selection = null
}
}
export const transposeSelection =
(rootStore: RootStore) => (deltaPitch: number) => {
const {
pianoRollStore,
pianoRollStore: { selectedTrackId, selection, selectedNoteIds },
pushHistory,
} = rootStore
pushHistory()
if (selection !== null) {
const s = movedSelection(selection, 0, deltaPitch)
pianoRollStore.selection = s
}
transposeNotes(rootStore)(deltaPitch, {
[selectedTrackId]: selectedNoteIds,
})
}
export const moveSelection = (rootStore: RootStore) => (point: NotePoint) => {
const {
pianoRollStore: { selectedTrack, selection, quantizer },
} = rootStore
if (selectedTrack === undefined || selection === null) {
return
}
// ノートと選択範囲を移動
// Move notes and selection
const quantized = clampNotePoint({
tick: quantizer.round(point.tick),
noteNumber: Math.round(point.noteNumber),
})
const dt = quantized.tick - selection.from.tick
const dn = quantized.noteNumber - selection.from.noteNumber
const to = {
tick: selection.to.tick + dt,
noteNumber: selection.to.noteNumber + dn,
}
const clampedTo = clampNotePoint(to)
const limit = {
tick: to.tick - clampedTo.tick,
noteNumber: to.noteNumber - clampedTo.noteNumber,
}
moveSelectionBy(rootStore)({
tick: dt - limit.tick,
noteNumber: dn - limit.noteNumber,
})
}
export const moveSelectionBy =
({
pianoRollStore,
pianoRollStore: { selectedTrack, selection, selectedNoteIds },
pushHistory,
}: RootStore) =>
(delta: NotePoint) => {
if (delta.tick === 0 && delta.noteNumber === 0) {
return
}
if (selectedTrack === undefined) {
return
}
pushHistory()
if (selection !== null) {
const s = movedSelection(selection, delta.tick, delta.noteNumber)
pianoRollStore.selection = s
}
selectedTrack.updateEvents(
selectedNoteIds
.map((id) => {
const n = selectedTrack.getEventById(id)
if (n == undefined || !isNoteEvent(n)) {
return null
}
const pos = clampNotePoint({
tick: n.tick + delta.tick,
noteNumber: n.noteNumber + delta.noteNumber,
})
return {
id,
...pos,
}
})
.filter(isNotNull),
)
}
export const resizeSelectionLeft = (rootStore: RootStore) => (tick: number) => {
const {
pianoRollStore,
pianoRollStore: { selection, quantizer },
} = rootStore
if (selection === null) {
return
}
// 選択範囲とノートを左方向に伸長・縮小する
// Level and reduce the selection and notes in the left direction
const fromTick = quantizer.round(tick)
const delta = fromTick - selection.from.tick
// 変形していないときは終了
// End when not deformed
if (delta === 0) {
return
}
// 選択領域のサイズがゼロになるときは終了
// End when the size of selection area becomes zero
if (selection.to.tick - fromTick <= 0 || fromTick < 0) {
return
}
// 右端を固定して長さを変更
// Fix the right end and change the length
const s = cloneDeep(selection)
s.from.tick = fromTick
pianoRollStore.selection = s
resizeNotesInSelectionLeftBy(rootStore)(delta)
}
export const resizeNotesInSelectionLeftBy =
({
pianoRollStore: { selectedNoteIds, selectedTrack },
pushHistory,
}: RootStore) =>
(deltaTick: number) => {
if (selectedTrack === undefined || selectedNoteIds.length === 0) {
return
}
pushHistory()
selectedTrack.updateEvents(
selectedNoteIds
.map((id) => {
const n = selectedTrack.getEventById(id)
if (n == undefined || !isNoteEvent(n)) {
return null
}
const duration = n.duration - deltaTick
const tick = n.tick + deltaTick
if (duration <= 0 || tick < 0) {
// 幅がゼロになる場合は変形しない
// Do not deform if the width is zero
return { id }
}
return {
id,
tick,
duration,
}
})
.filter(isNotNull),
)
}
export const resizeSelectionRight =
(rootStore: RootStore) => (tick: number) => {
const {
pianoRollStore,
pianoRollStore: { selection, quantizer },
} = rootStore
if (selection === null) {
return
}
// 選択範囲とノートを右方向に伸長・縮小する
// Return and reduce the selection and note in the right direction
const toTick = quantizer.round(tick)
const delta = toTick - selection.to.tick
// 変形していないときは終了
// End when not deformed
if (delta === 0) {
return
}
// 選択領域のサイズがゼロになるときは終了
// End when the size of selection area becomes zero
if (toTick - selection.from.tick <= 0) {
return
}
// 右端を固定して長さを変更
// Fix the right end and change the length
const s = cloneDeep(selection)
s.to.tick = toTick
pianoRollStore.selection = s
resizeNotesInSelectionRightBy(rootStore)(delta)
}
export const resizeNotesInSelectionRightBy =
({
pianoRollStore: { selectedTrack, selectedNoteIds },
pushHistory,
}: RootStore) =>
(deltaDuration: number) => {
if (selectedTrack === undefined || selectedNoteIds.length === 0) {
return
}
pushHistory()
selectedTrack.updateEvents(
selectedNoteIds
.map((id) => {
const n = selectedTrack.getEventById(id)
if (n == undefined || !isNoteEvent(n)) {
return null
}
const duration = n.duration + deltaDuration
if (duration <= 0) {
// 幅がゼロになる場合は変形しない
// Do not deform if the width is zero
return { id }
}
return {
id,
duration,
}
})
.filter(isNotNull),
)
}
export const startSelection =
({
pianoRollStore,
controlStore,
player,
controlStore: { quantizer },
}: RootStore) =>
(point: NotePoint, keepSelectedNoteIds: boolean = false) => {
if (!player.isPlaying) {
player.position = quantizer.round(point.tick)
}
controlStore.selectedEventIds = []
if (!keepSelectedNoteIds) {
// deselect the notes
pianoRollStore.selectedNoteIds = []
}
// 選択範囲の右上を pos にする
// Set the upper right corner of the selection to POS
pianoRollStore.selection = {
from: point,
to: point,
}
}
export const resetSelection =
({ pianoRollStore }: RootStore) =>
() => {
pianoRollStore.selection = null
pianoRollStore.selectedNoteIds = []
}
export const cloneSelection =
({
pianoRollStore,
pianoRollStore: { selection, selectedNoteIds, selectedTrack },
}: RootStore) =>
() => {
if (selectedTrack === undefined || selection === null) {
return
}
// 選択範囲内のノートをコピーした選択範囲を作成
// Create a selection that copies notes within selection
const notes = selectedNoteIds
.map((id) => selectedTrack.getEventById(id))
.filter(isNotUndefined)
.map((note) => ({
...note, // copy
}))
selectedTrack.addEvents(notes)
pianoRollStore.selectedNoteIds = notes.map((e) => e.id)
}
export const copySelection =
({
pianoRollStore: { selection, selectedNoteIds, selectedTrack },
}: RootStore) =>
() => {
if (selectedTrack === undefined || selectedNoteIds.length === 0) {
return
}
const selectedNotes = selectedNoteIds
.map((id) => selectedTrack.getEventById(id))
.filter(isNotUndefined)
.filter(isNoteEvent)
const startTick =
selection?.from.tick ?? min(selectedNotes.map((note) => note.tick))!
// 選択されたノートをコピー
// Copy selected note
const notes = selectedNotes.map((note) => ({
...note,
tick: note.tick - startTick, // 選択範囲からの相対位置にする
}))
const data: PianoNotesClipboardData = {
type: "piano_notes",
notes,
}
clipboard.writeText(JSON.stringify(data))
}
export const deleteSelection =
({
pianoRollStore,
pianoRollStore: { selection, selectedNoteIds, selectedTrack },
pushHistory,
}: RootStore) =>
() => {
if (
selectedTrack === undefined ||
(selectedNoteIds.length === 0 && selection === null)
) {
return
}
pushHistory()
// 選択範囲と選択されたノートを削除
// Remove selected notes and selected notes
selectedTrack.removeEvents(selectedNoteIds)
pianoRollStore.selection = null
pianoRollStore.selectedNoteIds = []
}
export const pasteSelection =
({ player, pianoRollStore: { selectedTrack }, pushHistory }: RootStore) =>
() => {
if (selectedTrack === undefined) {
return
}
// 現在位置にコピーしたノートをペースト
// Paste notes copied to the current position
const text = clipboard.readText()
if (!text || text.length === 0) {
return
}
const obj = JSON.parse(text)
if (!isPianoNotesClipboardData(obj)) {
return
}
pushHistory()
const notes = obj.notes.map((note) => ({
...note,
tick: note.tick + player.position,
}))
selectedTrack.addEvents(notes)
}
export const duplicateSelection =
({
pianoRollStore,
pianoRollStore: { selection, selectedNoteIds, selectedTrack },
pushHistory,
}: RootStore) =>
() => {
if (
selectedTrack === undefined ||
selection === null ||
selectedNoteIds.length === 0
) {
return
}
pushHistory()
// move to the end of selection
let deltaTick = selection.to.tick - selection.from.tick
const selectedNotes = selectedNoteIds
.map((id) => selectedTrack.getEventById(id))
.filter(isNotUndefined)
.filter(isNoteEvent)
if (deltaTick === 0) {
const left = Math.min(...selectedNotes.map((n) => n.tick))
const right = Math.max(...selectedNotes.map((n) => n.tick + n.duration))
deltaTick = right - left
}
const notes = selectedNotes.map((note) => ({
...note,
tick: note.tick + deltaTick,
}))
// select the created notes
const addedNotes = selectedTrack.addEvents(notes)
const s = cloneDeep(selection)
s.from.tick += deltaTick
s.to.tick += deltaTick
pianoRollStore.selection = s
pianoRollStore.selectedNoteIds = addedNotes.map((n) => n.id)
}
export const addNoteToSelection =
({ pianoRollStore }: RootStore) =>
(noteId: number) => {
pianoRollStore.selectedNoteIds.push(noteId)
}
export const removeNoteFromSelection =
({
pianoRollStore,
pianoRollStore: { selectedNoteIds, selectedTrack },
}: RootStore) =>
(noteId: number) => {
if (selectedTrack === undefined || selectedNoteIds.length === 0) {
return
}
pianoRollStore.selectedNoteIds = selectedNoteIds.filter(
(id) => id !== noteId,
)
}
export const selectNote =
({
pianoRollStore,
pianoRollStore: { selectedTrack },
controlStore,
}: RootStore) =>
(noteId: number) => {
if (selectedTrack === undefined) {
return
}
controlStore.selectedEventIds = []
pianoRollStore.selectedNoteIds = [noteId]
}
const sortedNotes = (notes: NoteEvent[]): NoteEvent[] =>
notes.filter(isNoteEvent).sort((a, b) => {
if (a.tick < b.tick) return -1
if (a.tick > b.tick) return 1
if (a.noteNumber < b.noteNumber) return -1
if (a.noteNumber > b.noteNumber) return 1
return 0
})
const selectNeighborNote = (rootStore: RootStore) => (deltaIndex: number) => {
const {
pianoRollStore: { selectedTrack, selectedNoteIds },
player,
song,
} = rootStore
if (selectedTrack === undefined || selectedNoteIds.length === 0) {
return
}
const allNotes = selectedTrack.events.filter(isNoteEvent)
const selectedNotes = sortedNotes(
selectedNoteIds
.map((id) => allNotes.find((n) => n.id === id))
.filter(isNotUndefined),
)
if (selectedNotes.length === 0) {
return
}
const channel = selectedTrack?.channel ?? 0
const firstNote = sortedNotes(selectedNotes)[0]
const notes = sortedNotes(allNotes)
const currentIndex = notes.findIndex((n) => n.id === firstNote.id)
const currentNote = notes[currentIndex]
const nextNote = notes[currentIndex + deltaIndex]
if (nextNote === undefined) {
return
}
selectNote(rootStore)(nextNote.id)
// Stop playing the current note, then play the new note.
player.stopNote({ ...currentNote, channel })
player.startNote({ ...nextNote, channel })
player.stopNote(
{ ...nextNote, channel },
tickToMillisec(nextNote.duration, 120, song.timebase) / 1000,
)
}
export const selectNextNote = (rootStore: RootStore) => () =>
selectNeighborNote(rootStore)(1)
export const selectPreviousNote = (rootStore: RootStore) => () =>
selectNeighborNote(rootStore)(-1)
export const quantizeSelectedNotes =
({
pianoRollStore: {
selectedTrack,
selectedNoteIds,
enabledQuantizer: quantizer,
},
pushHistory,
}: RootStore) =>
() => {
if (selectedTrack === undefined || selectedNoteIds.length === 0) {
return
}
pushHistory()
const notes = selectedNoteIds
.map((id) => selectedTrack.getEventById(id))
.filter(isNotUndefined)
.filter(isNoteEvent)
.map((e) => ({
...e,
tick: quantizer.round(e.tick),
}))
selectedTrack.updateEvents(notes)
}
export const selectAllNotes =
({ pianoRollStore, pianoRollStore: { selectedTrack } }: RootStore) =>
() => {
if (selectedTrack) {
pianoRollStore.selectedNoteIds = selectedTrack.events
.filter(isNoteEvent)
.map((note) => note.id)
}
}