-
Notifications
You must be signed in to change notification settings - Fork 4
/
player.js
63 lines (52 loc) · 1.61 KB
/
player.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
function ModulePlayer(editor) {
this.editor = editor;
this.channelCount = 2;
this.sampleRate = 44100;
}
ModulePlayer.prototype = {
player: null,
editor: null,
playing: 0,
dev: null,
channelCount: 0,
sampleRate: 0,
lastRebuffer: 0,
advanceFrame: function() {
if (this.playing == PLAYING_SAMPLE)
return false;
// When we should advance to the next row when previewing a row,
// we don't do so in the playback engine but display a row advance
// in the UI.
if (this.playing == PLAYING_ROW) {
this.playing = PLAYING_SAMPLE;
editor.adjustRow(1);
editor.inhibitFurtherUpdates();
return false;
}
return true;
},
createDevice: function(player) {
this.player = player;
var self = this;
this.refill = function(sampleBuffer) {
//console.log("delta = " + (Date.now() - self.lastRebuffer) + ", asked for " + sampleBuffer.length);
if (self.playing == STOPPED)
return;
self.lastRebuffer = Date.now();
editor.triggerUpdate(self.player);
var buffer = self.playing == PLAYING_PREVIEW ?
self.player.getSamplesForChannel(sampleBuffer.length, self.editor.getPreviewSource()) :
self.player.getSamples(sampleBuffer.length, self);
for (var i = 0; i < sampleBuffer.length; i++) {
sampleBuffer[i] = buffer[i];
}
};
this.reinitDevice();
},
reinitDevice: function() {
if (this.dev)
this.dev.kill();
var preBufferSize = 12 * this.channelCount * 1024;
this.dev = audioLib.AudioDevice(this.refill, this.channelCount, preBufferSize, this.sampleRate);
}
};