source: gs3-extensions/web-audio/trunk/js-mad/sink.js-master/src/extra/recording.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: 2.2 KB
Line 
1void function (Sink) {
2
3Sink.on('init', function (sink) {
4 sink.activeRecordings = [];
5 sink.on('postprocess', sink.recordData);
6});
7
8Sink.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*/
18Sink.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*/
31Sink.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
47function Recording (bindTo) {
48 this.boundTo = bindTo;
49 this.buffers = [];
50 bindTo.activeRecordings.push(this);
51}
52
53Recording.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
112Sink.Recording = Recording;
113
114}(this.Sink);
Note: See TracBrowser for help on using the repository browser.