-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpiece.js
937 lines (757 loc) · 22.8 KB
/
piece.js
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
/*****************************************************************************
*
* This file is part of the MusicToy project. The project is
* distributed at:
* https://github.com/maximecb/MusicToy
*
* Copyright (c) 2012, Maxime Chevalier-Boisvert. All rights reserved.
*
* This software is licensed under the following license (Modified BSD
* License):
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************/
//============================================================================
// Music piece implementation
//============================================================================
/**
@class Musical piece implementation.
*/
function Piece(synthNet)
{
assert (
synthNet instanceof SynthNet || synthNet === undefined,
'invalid synth net'
);
/**
Synthesis network used by this piece
*/
this.synthNet = synthNet;
/**
Music/info tracks
*/
this.tracks = [];
/**
Current playback time/position
*/
this.playTime = 0;
/**
Loop time
*/
this.loopTime = 0;
/**
Previous update time
*/
this.prevTime = 0;
/**
Tempo in beats per minute
*/
this.beatsPerMin = 140;
/**
Time signature numerator, beats per bar
*/
this.beatsPerBar = 4;
/**
Time signature denominator, note value for each beat
*/
this.noteVal = 4;
}
/**
Add a track to the piece
*/
Piece.prototype.addTrack = function (track)
{
assert (
track instanceof Track,
'invalid track'
);
this.tracks.push(track);
return track;
}
/**
Get the time offset for a beat number. This number can be fractional.
*/
Piece.prototype.beatTime = function (beatNo)
{
var beatLen = 60 / this.beatsPerMin;
return beatLen * beatNo;
}
/**
Get the length in seconds for a note value multiple
*/
Piece.prototype.noteLen = function (len)
{
// By default, use the default note value
if (len === undefined)
len = 1;
var beatLen = 60 / this.beatsPerMin;
var barLen = beatLen * this.beatsPerBar;
var noteLen = barLen / this.noteVal;
return len * noteLen * 0.99;
}
/**
Helper methods to add notes to the track.
Produces a note-on and note-off event pair.
*/
Piece.prototype.makeNote = function (track, beatNo, note, len, vel)
{
assert (
note instanceof Note ||
typeof note === 'string',
'invalid note'
);
if (typeof note === 'string')
note = new Note(note);
// By default, the velocity is 100%
if (vel === undefined)
vel = 1;
// Convert the note time to a beat number
var time = this.beatTime(beatNo);
// Get the note length in seconds
var noteLen = this.noteLen(len);
// Create the note on and note off events
var noteOn = new NoteOnEvt(time, note, vel);
var noteOff = new NoteOffEvt(time + noteLen, note);
// Add the events to the track
track.addEvent(noteOn);
track.addEvent(noteOff);
}
/**
Set the playback position/time
*/
Piece.prototype.setTime = function (time)
{
this.playTime = time;
}
/**
Dispatch synthesis events up to the current time
*/
Piece.prototype.dispatch = function (curTime, realTime)
{
// Do the dispatch for each track
for (var i = 0; i < this.tracks.length; ++i)
{
var track = this.tracks[i];
track.dispatch(this.prevTime, curTime, realTime);
}
// Store the last update time/position
this.prevTime = curTime;
}
/**
Called when stopping the playback of a piece
*/
Piece.prototype.stop = function ()
{
// If a synthesis network is attached to this piece
if (this.synthNet !== undefined)
{
// Send an all notes off event to all synthesis nodes
var notesOffEvt = new AllNotesOffEvt();
for (var i = 0; i < this.synthNet.nodes.length; ++i)
{
var node = this.synthNet.nodes[i];
node.processEvent(notesOffEvt);
}
}
// Set the playback position past all events
this.playTime = Infinity;
}
/**
Create a handler for real-time audio generation
*/
Piece.prototype.makeHandler = function ()
{
var synthNet = this.synthNet;
var piece = this;
var sampleRate = synthNet.sampleRate;
// Output node of the synthesis network
var outNode = synthNet.outNode;
// Current playback time
var curTime = piece.playTime;
var realTime = piece.playTime;
// Audio generation function
function genAudio(evt)
{
var startTime = (new Date()).getTime();
var numChans = evt.outputBuffer.numberOfChannels
var numSamples = evt.outputBuffer.getChannelData(0).length;
// If the playback position changed, update the current time
if (piece.playTime !== curTime)
{
console.log('playback time updated');
curTime = piece.playTime;
}
assert (
numChans === outNode.numChans,
'mismatch in the number of output channels'
);
assert (
numSamples % SYNTH_BUF_SIZE === 0,
'the output buffer size must be a multiple of the synth buffer size'
);
// Until all samples are produced
for (var smpIdx = 0; smpIdx < numSamples; smpIdx += SYNTH_BUF_SIZE)
{
// Update the piece, dispatch track events
piece.dispatch(curTime, realTime);
// Generate the sample values
var values = synthNet.genOutput(realTime);
// Copy the values for each channel
for (var chnIdx = 0; chnIdx < numChans; ++chnIdx)
{
var srcBuf = outNode.getBuffer(chnIdx);
var dstBuf = evt.outputBuffer.getChannelData(chnIdx);
for (var i = 0; i < SYNTH_BUF_SIZE; ++i)
dstBuf[smpIdx + i] = srcBuf[i];
}
// Update the current time based on sample rate
curTime += SYNTH_BUF_SIZE / sampleRate;
realTime += SYNTH_BUF_SIZE / sampleRate;
// If we lust passed the loop time, go back to the start
if (piece.playTime <= piece.loopTime &&
curTime > piece.loopTime)
{
piece.dispatch(piece.loopTime + 0.01, realTime);
curTime = 0;
piece.prevTime = 0;
}
// Update the current playback position
piece.playTime = curTime;
}
var endTime = (new Date()).getTime();
var compTime = (endTime - startTime) / 1000;
var soundTime = (numSamples / synthNet.sampleRate);
var cpuUse = (100 * compTime / soundTime).toFixed(1);
//console.log('cpu use: ' + cpuUse + '%');
}
// Return the handler function
return genAudio;
}
/**
Draw the notes of a track using the canvas API
*/
Piece.prototype.drawTrack = function (
track,
canvasCtx,
topX,
topY,
width,
height,
minNote,
numOcts
)
{
// Compute the bottom-right corner coordinates
var botX = topX + width;
var botY = topY + height;
// Get the last event time
var maxTime = track.endTime();
// Compute the number of beats
var numBeats = Math.ceil((maxTime / 60) * this.beatsPerMin);
// Compute the total time for the beats
var totalTime = (numBeats / this.beatsPerMin) * 60;
//console.log('max time : ' + maxTime);
//console.log('num beats : ' + numBeats);
//console.log('total time: ' + totalTime);
var minNoteNo = Math.floor(minNote.noteNo / NOTES_PER_OCTAVE) * NOTES_PER_OCTAVE;
var numNotes = numOcts * NOTES_PER_OCTAVE;
var numWhites = numOcts * 7;
var whiteHeight = height / numWhites;
var blackHeight = whiteHeight / 2;
var pianoWidth = 40;
var blackWidth = (pianoWidth / 4) * 3;
var beatWidth = (width - pianoWidth) / numBeats;
canvasCtx.fillStyle = "grey"
canvasCtx.fillRect(topX, topY, width, height);
canvasCtx.fillStyle = "white"
canvasCtx.fillRect(topX, topY, pianoWidth, height);
canvasCtx.strokeStyle = "black";
canvasCtx.beginPath();
canvasCtx.moveTo(topX, topY);
canvasCtx.lineTo(topX + pianoWidth, topY);
canvasCtx.lineTo(topX + pianoWidth, botY);
canvasCtx.lineTo(topX, botY);
canvasCtx.closePath();
canvasCtx.stroke();
var noteExts = new Array(numNotes);
var noteIdx = 0;
// For each white note
for (var i = 0; i < numWhites; ++i)
{
var whiteBot = botY - (whiteHeight * i);
var whiteTop = whiteBot - whiteHeight;
var whiteExts = noteExts[noteIdx++] = { bot: whiteBot, top: whiteTop };
if (i > 0)
{
var prevExts = noteExts[noteIdx - 2];
whiteExts.bot = Math.min(whiteExts.bot, prevExts.top);
}
canvasCtx.strokeStyle = "black";
canvasCtx.beginPath();
canvasCtx.moveTo(topX, whiteBot);
canvasCtx.lineTo(topX + pianoWidth, whiteBot);
canvasCtx.closePath();
canvasCtx.stroke();
if ((i % 7) !== 2 && (i % 7) !== 6)
{
var blackTop = whiteTop - (blackHeight / 2);
var blackBot = whiteTop + (blackHeight / 2);
var blackExts = noteExts[noteIdx++] = { bot:blackBot, top:blackTop };
whiteExts.top = blackExts.bot;
canvasCtx.fillStyle = "black";
canvasCtx.beginPath();
canvasCtx.moveTo(topX, blackTop);
canvasCtx.lineTo(topX + blackWidth, blackTop);
canvasCtx.lineTo(topX + blackWidth, blackBot);
canvasCtx.lineTo(topX, blackBot);
canvasCtx.lineTo(topX, blackTop);
canvasCtx.closePath();
canvasCtx.fill();
}
}
// Draw the horizontal note separation lines
for (var i = 0; i < noteExts.length; ++i)
{
var exts = noteExts[i];
canvasCtx.strokeStyle = "rgb(0, 0, 125)";
canvasCtx.beginPath();
canvasCtx.moveTo(topX + pianoWidth + 1, exts.top);
canvasCtx.lineTo(botX, exts.top);
canvasCtx.closePath();
canvasCtx.stroke();
}
// Draw the vertical beat separation lines
for (var i = 1; i < numBeats; ++i)
{
var xCoord = topX + pianoWidth + (i * beatWidth);
var color;
if (i % this.beatsPerBar === 0)
color = "rgb(25, 25, 255)"
else
color = "rgb(0, 0, 125)";
canvasCtx.strokeStyle = color;
canvasCtx.beginPath();
canvasCtx.moveTo(xCoord, topY);
canvasCtx.lineTo(xCoord, botY);
canvasCtx.closePath();
canvasCtx.stroke();
}
// For each track event
for (var i = 0; i < track.events.length; ++i)
{
var event = track.events[i];
// If this is a note on event
if (event instanceof NoteOnEvt)
{
var noteNo = event.note.noteNo;
var startTime = event.time;
// Try to find the note end time
//var endTime = startTime + (60 / this.beatsPerMin);
var endTime = undefined;
for (var j = i + 1; j < track.events.length; ++j)
{
var e2 = track.events[j];
if (e2 instanceof NoteOffEvt &&
e2.note.noteNo === noteNo &&
e2.time > event.time)
{
endTime = e2.time;
break;
}
}
if (endTime === undefined)
error('COULD NOT FIND NOTE OFF');
var startFrac = startTime / totalTime;
var endFrac = endTime / totalTime;
var xStart = topX + pianoWidth + startFrac * (width - pianoWidth);
var xEnd = topX + pianoWidth + endFrac * (width - pianoWidth);
var noteIdx = noteNo - minNoteNo;
if (noteIdx >= noteExts.length)
{
console.log('note above limit');
continue;
}
//console.log(noteIdx + ': ' + xStart + ' => ' + xEnd);
var exts = noteExts[noteIdx];
canvasCtx.fillStyle = "red";
canvasCtx.strokeStyle = "black";
canvasCtx.beginPath();
canvasCtx.moveTo(xStart, exts.top);
canvasCtx.lineTo(xEnd , exts.top);
canvasCtx.lineTo(xEnd , exts.bot);
canvasCtx.lineTo(xStart, exts.bot);
canvasCtx.lineTo(xStart, exts.top);
canvasCtx.closePath();
canvasCtx.fill();
canvasCtx.stroke();
}
}
// If playback is ongoing
if (this.playTime !== 0 && maxTime !== 0)
{
// Compute the cursor line position
var cursorFrac = this.playTime / maxTime;
var cursorPos = topX + pianoWidth + cursorFrac * (width - pianoWidth);
// Draw the cursor line
canvasCtx.strokeStyle = "white";
canvasCtx.beginPath();
canvasCtx.moveTo(cursorPos, topY);
canvasCtx.lineTo(cursorPos, botY);
canvasCtx.closePath();
canvasCtx.stroke();
}
}
/**
Produce MIDI file data for a track of this piece.
The data is written into a byte array.
*/
Piece.prototype.getMIDIData = function (track)
{
var data = [];
var writeIdx = 0;
function writeByte(val)
{
assert (
val <= 0xFF,
'invalid value in writeByte'
);
data[writeIdx++] = val;
}
function writeWORD(val)
{
assert (
val <= 0xFFFF,
'invalid value in writeWORD'
);
writeByte((val >> 8) & 0xFF);
writeByte((val >> 0) & 0xFF);
}
function writeDWORD(val)
{
assert (
val <= 0xFFFFFFFF,
'invalid value in writeDWORD: ' + val
);
writeByte((val >> 24) & 0xFF);
writeByte((val >> 16) & 0xFF);
writeByte((val >> 8) & 0xFF);
writeByte((val >> 0) & 0xFF);
}
function writeVarLen(val)
{
// Higher bits must be written first
var bytes = [];
do
{
var bits = val & 0x7F;
val >>= 7;
bytes.push(bits);
} while (val !== 0);
for (var i = bytes.length - 1; i >= 0; --i)
{
var bits = bytes[i];
if (i > 0)
bits = 0x80 | bits;
writeByte(bits);
}
}
// Number of clock ticks per beat
var ticksPerBeat = 500;
// Write the file header
writeDWORD(0x4D546864); // MThd
writeDWORD(0x00000006); // Chunk size
writeWORD(0); // Type 0 MIDI file (one track)
writeWORD(1); // One track
writeWORD(ticksPerBeat); // Time division
// Write the track header
writeDWORD(0x4D54726B) // MTrk
writeDWORD(0); // Chunk size, written later
// Save the track size index
var trackSizeIdx = data.length - 4;
// Delta time conversion ratio
var ticksPerSec = (this.beatsPerMin / 60) * ticksPerBeat;
console.log('ticks per sec: ' + ticksPerSec);
// Set the tempo in microseconds per quarter node
var usPerMin = 60000000;
var mpqn = usPerMin / this.beatsPerMin;
writeVarLen(0);
writeByte(0xFF)
writeByte(0x51);
writeVarLen(3);
writeByte((mpqn >> 16) & 0xFF);
writeByte((mpqn >> 8) & 0xFF);
writeByte((mpqn >> 0) & 0xFF);
// Set the time signature
var num32Nds = Math.floor(8 * (4 / this.noteVal));
writeVarLen(0);
writeByte(0xFF)
writeByte(0x58);
writeVarLen(4);
writeByte(this.beatsPerBar); // Num
writeByte(2); // Denom 2^2 = 4
writeByte(24); // Metronome rate
writeByte(num32Nds); // 32nds per quarter note
console.log('beats per bar: ' + this.beatsPerBar);
console.log('num 32nds: ' + num32Nds);
// Set the piano program
writeVarLen(0);
writeByte(0xC0);
writeByte(0);
// For each track event
for (var i = 0; i < track.events.length; ++i)
{
var event = track.events[i];
var prevEvent = track.events[i-1];
// Event format:
// Delta Time
// Event Type Value
// MIDI Channel
// Parameter 1
// Parameter 2
var deltaTime = prevEvent? (event.time - prevEvent.time):0;
var deltaTicks = Math.ceil(ticksPerSec * deltaTime);
assert (
isNonNegInt(deltaTicks),
'invalid delta ticks: ' + deltaTicks
);
console.log(event.toString())
console.log('delta ticks: ' + deltaTicks);
// Write the event delta time
writeVarLen(deltaTicks);
if (event instanceof NoteOnEvt)
{
writeByte(0x90);
writeByte(event.note.noteNo);
// Velocity
var vel = Math.min(Math.floor(event.vel * 127), 127);
writeByte(vel);
}
else if (event instanceof NoteOffEvt)
{
writeByte(0x80);
writeByte(event.note.noteNo);
// Velocity
writeByte(0);
}
}
// Write the end of track event
writeVarLen(0);
writeByte(0xFF)
writeByte(0x2F);
writeVarLen(0);
// Write the track chunk size
var trackSize = data.length - (trackSizeIdx + 4);
console.log('track size: ' + trackSize);
writeIdx = trackSizeIdx
writeDWORD(trackSize);
return data;
}
/**
@class Synthesis event track implementation. Produces events and sends them
to a target synthesis node.
*/
function Track(target)
{
assert (
target instanceof SynthNode || target === undefined,
'invalid target node'
);
/**
Target synthesis node to send events to
*/
this.target = target;
/**
Events for this track
*/
this.events = [];
}
/**
Add an event to the track
*/
Track.prototype.addEvent = function (evt)
{
this.events.push(evt);
// If the event is being added at the end of the track, stop
if (this.events.length === 1 ||
evt.time >= this.events[this.events.length-2].time)
return;
// Sort the events
this.events.sort(function (a, b) { return a.time - b.time; });
}
/**
Get the dispatch time of the last event
*/
Track.prototype.endTime = function ()
{
if (this.events.length === 0)
return
else
return this.events[this.events.length-1].time;
}
/**
Dispatch the events between the previous update time and
the current time, inclusively.
*/
Track.prototype.dispatch = function (prevTime, curTime, realTime)
{
if (this.target === undefined)
return;
if (this.events.length === 0)
return;
// Must play all events from the previous time (inclusive) up to the
// current time (exclusive).
//
// Find the mid idx where we are at or just past the previous time.
var minIdx = 0;
var maxIdx = this.events.length - 1;
var midIdx = 0;
while (minIdx <= maxIdx)
{
midIdx = Math.floor((minIdx + maxIdx) / 2);
//console.log(midIdx);
var midTime = this.events[midIdx].time;
var leftTime = (midIdx === 0)? -Infinity:this.events[midIdx-1].time;
if (leftTime < prevTime && midTime >= prevTime)
break;
if (midTime < prevTime)
minIdx = midIdx + 1;
else
maxIdx = midIdx - 1;
}
// If no event to dispatch were found, stop
if (minIdx > maxIdx)
return;
// Dispatch all events up to the current time (exclusive)
for (var idx = midIdx; idx < this.events.length; ++idx)
{
var evt = this.events[idx];
if (evt.time >= curTime)
break;
console.log('Dispatch: ' + evt);
this.target.processEvent(evt, realTime);
}
}
/**
Clear all the events from this track
*/
Track.prototype.clear = function ()
{
this.events = [];
}
//============================================================================
// Synthesis events
//============================================================================
/**
@class Base class for all synthesis events.
*/
function SynthEvt()
{
/**
Event occurrence time
*/
this.time = 0;
}
/**
Format a synthesis event string representation
*/
SynthEvt.formatStr = function (evt, str)
{
return evt.time.toFixed(2) + ': ' + str;
}
/**
Default string representation for events
*/
SynthEvt.prototype.toString = function ()
{
return SynthEvt.formatStr(this, 'event');
}
/**
@class Note on event
*/
function NoteOnEvt(time, note, vel)
{
// By default, use the C4 note
if (note === undefined)
note = new Note(C4_NOTE_NO);
// By default, 50% velocity
if (vel === undefined)
vel = 0.5;
/**
Note
*/
this.note = note;
/**
Velocity
*/
this.vel = vel;
// Set the event time
this.time = time;
}
NoteOnEvt.prototype = new SynthEvt();
/**
Default string representation for events
*/
NoteOnEvt.prototype.toString = function ()
{
return SynthEvt.formatStr(this, 'note-on ' + this.note);
}
/**
@class Note off event
*/
function NoteOffEvt(time, note)
{
// By default, use the C4 note
if (note === undefined)
note = new Note(C4_NOTE_NO);
/**
Note
*/
this.note = note;
// Set the event time
this.time = time;
}
NoteOffEvt.prototype = new SynthEvt();
/**
Default string representation for events
*/
NoteOffEvt.prototype.toString = function ()
{
return SynthEvt.formatStr(this, 'note-off ' + this.note);
}
/**
@class All notes off event. Silences instruments.
*/
function AllNotesOffEvt(time)
{
this.time = time;
}
AllNotesOffEvt.prototype = new SynthEvt();
/**
Default string representation for events
*/
AllNotesOffEvt.prototype.toString = function ()
{
return SynthEvt.formatStr(this, 'all notes off');
}