source: gs3-extensions/web-audio/trunk/js-mad/sink.js-master/src/utils/proxy.js@ 28388

Last change on this file since 28388 was 28388, checked in by davidb, 11 years ago

Set of JS, CSS, PNG etc web resources to support a mixture of audio player/document display capabilities

File size: 1.5 KB
Line 
1(function (Sink) {
2
3function Proxy (bufferSize, channelCount) {
4 Sink.EventEmitter.call(this);
5
6 this.bufferSize = isNaN(bufferSize) || bufferSize === null ? this.bufferSize : bufferSize;
7 this.channelCount = isNaN(channelCount) || channelCount === null ? this.channelCount : channelCount;
8
9 var self = this;
10 this.callback = function () {
11 return self.process.apply(self, arguments);
12 };
13
14 this.resetBuffer();
15}
16
17Proxy.prototype = {
18 buffer: null,
19 zeroBuffer: null,
20 parentSink: null,
21 bufferSize: 4096,
22 channelCount: 2,
23 offset: null,
24
25 resetBuffer: function () {
26 this.buffer = new Float32Array(this.bufferSize);
27 this.zeroBuffer = new Float32Array(this.bufferSize);
28 },
29
30 process: function (buffer, channelCount) {
31 if (this.offset === null) {
32 this.loadBuffer();
33 }
34
35 for (var i=0; i<buffer.length; i++) {
36 if (this.offset >= this.buffer.length) {
37 this.loadBuffer();
38 }
39
40 buffer[i] = this.buffer[this.offset++];
41 }
42 },
43
44 loadBuffer: function () {
45 this.offset = 0;
46 Sink.memcpy(this.zeroBuffer, 0, this.buffer, 0);
47 this.emit('audioprocess', [this.buffer, this.channelCount]);
48 }
49};
50
51Sink.Proxy = Proxy;
52
53/**
54 * Creates a proxy callback system for the sink instance.
55 * Requires Sink utils.
56 *
57 * @method Sink
58 * @method createProxy
59 *
60 * @arg {Number} !bufferSize The buffer size for the proxy.
61*/
62Sink.prototype.createProxy = function (bufferSize) {
63 var proxy = new Sink.Proxy(bufferSize, this.channelCount);
64 proxy.parentSink = this;
65
66 this.on('audioprocess', proxy.callback);
67
68 return proxy;
69};
70
71}(this.Sink));
Note: See TracBrowser for help on using the repository browser.