1 | void function (Sink) {
|
---|
2 |
|
---|
3 | Sink.on('init', function (sink) {
|
---|
4 | sink.activeRecordings = [];
|
---|
5 | sink.on('postprocess', sink.recordData);
|
---|
6 | });
|
---|
7 |
|
---|
8 | Sink.prototype.activeRecordings = null;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Starts recording the sink output.
|
---|
12 | *
|
---|
13 | * @method Sink
|
---|
14 | * @name record
|
---|
15 | *
|
---|
16 | * @return {Recording} The recording object for the recording started.
|
---|
17 | */
|
---|
18 | Sink.prototype.record = function () {
|
---|
19 | var recording = new Sink.Recording(this);
|
---|
20 | this.emit('record', [recording]);
|
---|
21 | return recording;
|
---|
22 | };
|
---|
23 | /**
|
---|
24 | * Private method that handles the adding the buffers to all the current recordings.
|
---|
25 | *
|
---|
26 | * @method Sink
|
---|
27 | * @method recordData
|
---|
28 | *
|
---|
29 | * @arg {Array} buffer The buffer to record.
|
---|
30 | */
|
---|
31 | Sink.prototype.recordData = function (buffer) {
|
---|
32 | var activeRecs = this.activeRecordings,
|
---|
33 | i, l = activeRecs.length;
|
---|
34 | for (i=0; i<l; i++) {
|
---|
35 | activeRecs[i].add(buffer);
|
---|
36 | }
|
---|
37 | };
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * A Recording class for recording sink output.
|
---|
41 | *
|
---|
42 | * @class
|
---|
43 | * @static Sink
|
---|
44 | * @arg {Object} bindTo The sink to bind the recording to.
|
---|
45 | */
|
---|
46 |
|
---|
47 | function Recording (bindTo) {
|
---|
48 | this.boundTo = bindTo;
|
---|
49 | this.buffers = [];
|
---|
50 | bindTo.activeRecordings.push(this);
|
---|
51 | }
|
---|
52 |
|
---|
53 | Recording.prototype = {
|
---|
54 | /**
|
---|
55 | * Adds a new buffer to the recording.
|
---|
56 | *
|
---|
57 | * @arg {Array} buffer The buffer to add.
|
---|
58 | *
|
---|
59 | * @method Recording
|
---|
60 | */
|
---|
61 | add: function (buffer) {
|
---|
62 | this.buffers.push(buffer);
|
---|
63 | },
|
---|
64 | /**
|
---|
65 | * Empties the recording.
|
---|
66 | *
|
---|
67 | * @method Recording
|
---|
68 | */
|
---|
69 | clear: function () {
|
---|
70 | this.buffers = [];
|
---|
71 | },
|
---|
72 | /**
|
---|
73 | * Stops the recording and unbinds it from it's host sink.
|
---|
74 | *
|
---|
75 | * @method Recording
|
---|
76 | */
|
---|
77 | stop: function () {
|
---|
78 | var recordings = this.boundTo.activeRecordings,
|
---|
79 | i;
|
---|
80 | for (i=0; i<recordings.length; i++) {
|
---|
81 | if (recordings[i] === this) {
|
---|
82 | recordings.splice(i--, 1);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | },
|
---|
86 | /**
|
---|
87 | * Joins the recorded buffers into a single buffer.
|
---|
88 | *
|
---|
89 | * @method Recording
|
---|
90 | */
|
---|
91 | join: function () {
|
---|
92 | var bufferLength = 0,
|
---|
93 | bufPos = 0,
|
---|
94 | buffers = this.buffers,
|
---|
95 | newArray,
|
---|
96 | n, i, l = buffers.length;
|
---|
97 |
|
---|
98 | for (i=0; i<l; i++) {
|
---|
99 | bufferLength += buffers[i].length;
|
---|
100 | }
|
---|
101 | newArray = new Float32Array(bufferLength);
|
---|
102 | for (i=0; i<l; i++) {
|
---|
103 | for (n=0; n<buffers[i].length; n++) {
|
---|
104 | newArray[bufPos + n] = buffers[i][n];
|
---|
105 | }
|
---|
106 | bufPos += buffers[i].length;
|
---|
107 | }
|
---|
108 | return newArray;
|
---|
109 | }
|
---|
110 | };
|
---|
111 |
|
---|
112 | Sink.Recording = Recording;
|
---|
113 |
|
---|
114 | }(this.Sink);
|
---|