This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaftComposer.js
205 lines (160 loc) · 4.73 KB
/
daftComposer.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
(function() {
var DaftComposer = function () {
this.sounds = {};
this.recording = false;
this.recordTime = null;
this.reqSeq = [];
this.playbackTimeout = null;
this.playbackTime = null;
this.playingBack = false;
};
DaftComposer.prototype.load = function (sounds, progressCb) {
var self = this;
this.stopAll();
this.sounds = {};
var loaded = 0;
for (var name in sounds) {
if (sounds.hasOwnProperty(name)) {
(function (name) {
var volume = sounds[name].volume ? sounds[name].volume : 1.0;
self.sounds[name] = {sound: new Howl({urls: sounds[name].urls, loop: true, volume: volume}), playing: false};
self.sounds[name].sound.on('load', function () {
loaded++;
progressCb && progressCb(name, loaded);
});
})(name);
}
}
};
DaftComposer.prototype.stopAll = function () {
for (var name in this.sounds) {
if (this.sounds.hasOwnProperty(name)) {
this.sounds[name].sound.stop();
this.sounds[name].playing = false;
}
}
};
DaftComposer.prototype.record = function () {
if (this.recording) return;
if (this.playingBack) this.stopPlayback();
this.stopAll();
this.reqSeq = [];
this.recording = true;
this.recordTime = Date.now();
};
DaftComposer.prototype.stopRecord = function () {
if (!this.recording) return;
this.stopAll();
this.recording = false;
this.reqSeq.push({time: Date.now() - this.recordTime, type: 'end'});
};
DaftComposer.prototype.toggleRecord = function () {
if (this.recording) {
this.stopRecord();
} else {
this.record();
}
};
DaftComposer.prototype.getRecord = function () {
return this.reqSeq;
};
DaftComposer.prototype._play = function (soundName) {
var sound = this.sounds[soundName];
if (!sound) return;
if (sound.playing) return;
sound.playing = true;
sound.sound.play();
if (this.recording) {
this.reqSeq.push({time: Date.now() - this.recordTime, type: 'play', sound: soundName});
}
};
DaftComposer.prototype.play = function (soundName) {
if (this.playingBack) return;
this._play(soundName);
};
DaftComposer.prototype._stop = function (soundName) {
var sound = this.sounds[soundName];
if (!sound) return;
if (!sound.playing) return;
sound.playing = false;
sound.sound.stop();
if (this.recording) {
this.reqSeq.push({time: Date.now() - this.recordTime, type: 'stop', sound: soundName});
}
};
DaftComposer.prototype.stop = function (soundName) {
if (this.playingBack) return;
this._stop(soundName);
};
DaftComposer.prototype.togglePlay = function (soundName) {
var sound = this.sounds[soundName];
if (!sound) return;
if (sound.playing) {
this.stop(soundName);
} else {
this.play(soundName);
}
};
DaftComposer.prototype.playback = function (sequence) {
if (this.playingBack) return;
this.playingBack = true;
this.stopAll();
if (this.recording) this.stopRecord();
this.playbackTime = Date.now();
if (sequence.length <= 0) return this.stopPlayback();
this._playbackStep(sequence, -1);
};
DaftComposer.prototype.stopPlayback = function () {
if (!this.playingBack) return;
if (this.playbackTimeout) clearTimeout(this.playbackTimeout);
this.stopAll();
this.playingBack = false;
};
DaftComposer.prototype.togglePlayback = function (sequence) {
if (this.playingBack) {
this.stopPlayback();
} else {
this.playback(sequence);
}
};
DaftComposer.prototype._playbackStep = function (sequence, i) {
var self = this;
if (i+1 < sequence.length) {
this.playbackTimeout = setTimeout(function () {
self._playbackStep(sequence, i+1);
}, this.playbackTime + sequence[i+1].time - Date.now())
}
if (i >= 0) {
this._doStep(sequence[i]);
}
};
DaftComposer.prototype._doStep = function (step) {
if (step.type === 'end') {
this.stopPlayback();
} else if (step.type === 'play') {
this._play(step.sound);
} else if(step.type === 'stop') {
this._stop(step.sound);
}
};
/**
* Add support for AMD (Asynchronous Module Definition) libraries such as require.js.
*/
if (typeof define === 'function' && define.amd) {
define(function() {
return {
DaftComposer: DaftComposer
};
});
}
/**
* Add support for CommonJS libraries such as browserify.
*/
if (typeof exports !== 'undefined') {
exports.DaftComposer = DaftComposer;
}
// define globally in case AMD is not available or available but not used
if (typeof window !== 'undefined') {
window.DaftComposer = DaftComposer;
}
})();