-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.ts
More file actions
178 lines (148 loc) · 4.24 KB
/
player.ts
File metadata and controls
178 lines (148 loc) · 4.24 KB
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
/* Copyright 2019 Google LLC. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
import * as mm from '@magenta/music';
import { observable } from 'mobx';
import logging, { Events } from './logging';
import { Note } from './note';
import { editor } from './index';
import { range } from 'lodash';
import * as Tone from 'tone';
import { trim, getMagentaNoteSequence } from './magenta-utils';
import {
DEFAULT_BPM,
MIN_PITCH,
MAX_PITCH,
NOTE_VELOCITY,
SOUNDFONT_URL,
TOTAL_SIXTEENTHS,
} from './constants';
export class CallbackObject extends mm.BasePlayerCallback {
constructor(private player: Player) {
super();
}
run(note: mm.NoteSequence.INote, t: number) {
const { pitch, quantizedStartStep } = note;
// this offsets the position by loopStart, so that the notes correspond to the notes in the notesMap
// which allows the notes to be highlighted red for playing
editor.setNotePlaying(pitch, quantizedStartStep + this.player.loopStart);
}
stop() {
if (this.player.shouldLoop) {
this.player.loop();
} else {
this.player.stop();
}
}
}
class Player {
playerCallbackObject = new CallbackObject(this);
@observable isPlayerLoaded = false;
@observable isPlaying = false;
@observable shouldLoop = true;
@observable loopStart = 0;
@observable loopEnd = TOTAL_SIXTEENTHS;
private player = new mm.SoundFontPlayer(
SOUNDFONT_URL,
Tone.Destination,
undefined,
undefined,
this.playerCallbackObject
);
@observable bpm = DEFAULT_BPM;
constructor() {
this.loadPlayer();
}
async loadPlayer() {
const allNotes: mm.NoteSequence.INote[] = range(
MIN_PITCH,
MAX_PITCH + 1
).map(pitch => ({
pitch,
velocity: NOTE_VELOCITY,
}));
const allNotesSeq = { notes: allNotes };
await this.player.loadSamples(allNotesSeq);
this.isPlayerLoaded = true;
}
togglePlay() {
if (editor.allNotes.length === 0) return;
if (this.isPlaying) {
this.stop();
} else {
this.start();
}
}
playNote(note: Note) {
if (this.isPlayerLoaded) {
this.playNoteDown(note);
const timePerSixteenth = (((1 / this.bpm) * 60) / 4) * 1000;
const delay = note.duration * timePerSixteenth;
setTimeout(() => this.playNoteUp(note), delay);
}
}
playNoteDown(note: Note) {
if (this.isPlayerLoaded) {
editor.setActiveNoteValue(note.pitch);
this.player.playNoteDown(note.magentaNote);
}
}
playNoteUp(note: Note) {
if (this.isPlayerLoaded) {
editor.setActiveNoteValue(null);
this.player.playNoteUp(note.magentaNote);
}
}
// Logged play method
start() {
logging.logEvent(Events.PLAY);
this._start();
}
private _start() {
if (this.isPlayerLoaded) {
if (editor.allNotes.length === 0) {
return;
}
const sequence = getMagentaNoteSequence(
editor.unmutedNotes,
this.bpm,
editor.totalSixteenths
);
const loopSequence = trim(sequence, this.loopStart, this.loopEnd, true);
// trim can give a note that has quantizedStartStep == quantizedEndStep
// when on border of trim region.
loopSequence.notes = loopSequence.notes.filter(note => {
return note.quantizedStartStep !== note.quantizedEndStep;
});
this.player.start(loopSequence);
this.isPlaying = true;
}
}
loop() {
editor.clearPlayingNotes();
this.start();
}
// Logged stop method
stop() {
logging.logEvent(Events.STOP);
this._stop();
}
private _stop() {
this.player.stop();
this.isPlaying = false;
editor.clearPlayingNotes();
}
restart() {
this._stop();
this._start();
}
}
export default new Player();