-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlaybackDirective.js
46 lines (38 loc) · 1.15 KB
/
PlaybackDirective.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
app.directive('playback', function($window) {
return function(scope, element, attrs) {
var btn = element[0];
var useSelection = attrs.useSelection || false;
scope.isPlaying = false;
var stopPlayback = function() {
if (scope.sourceNode != null) {
scope.sourceNode.stop();
scope.sourceNode = null;
}
scope.isPlaying = false;
scope.safeApply();
};
var startPlayback = function() {
var partials = useSelection ? scope.getSelection() : scope.partials;
var buffer = scope.synthesize(partials);
var len = buffer.length;
var webAudioBuf = scope.audioContext.createBuffer(1, len, scope.sampleRate);
var arr = webAudioBuf.getChannelData(0);
for (var i=0; i<len; ++i) {
arr[i] = buffer[i];
}
var sourceNode = scope.audioContext.createBufferSource();
sourceNode.buffer = webAudioBuf;
sourceNode.connect(scope.audioContext.destination);
sourceNode.onended = function() {
scope.sourceNode = null;
stopPlayback();
btn.onclick = startPlayback;
};
sourceNode.start();
scope.sourceNode = sourceNode;
scope.isPlaying = true;
scope.safeApply();
};
btn.onclick = startPlayback;
};
});