-
Notifications
You must be signed in to change notification settings - Fork 827
Expand file tree
/
Copy pathSoundChannel.js
More file actions
190 lines (148 loc) · 3.88 KB
/
SoundChannel.js
File metadata and controls
190 lines (148 loc) · 3.88 KB
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
// TODO remove SoundChannel from namespace
// namespace:
this.createjs = this.createjs || {};
createjs.soundUtils = createjs.soundUtils || {};
createjs.soundUtils.SoundChannel = (function () {
var SoundChannel = function (source, max) {
this.init(source, max);
};
var prototype = SoundChannel.prototype;
prototype.constructor = SoundChannel;
prototype.init = function (source, max) {
this.src = source;
this.max = max || this.maxDefault;
if (this.max == -1) {
this.max = this.maxDefault;
}
this._instances = [];
this.src = null;
this.max = null;
this.maxDefault = 100;
this.length = 0;
};
prototype.toString = function () {
return "[Sound SoundChannel]";
};
prototype._get = _get;
prototype._add = _add;
prototype._remove = _remove;
prototype._removeAll = _removeAll;
prototype._getSlot = _getSlot;
SoundChannel.channels = {};
SoundChannel.create = create;
SoundChannel.removeSrc = removeSrc;
SoundChannel.removeAll = removeAll;
SoundChannel.add = add;
SoundChannel.remove = remove;
SoundChannel.maxPerChannel = maxPerChannel;
SoundChannel.get = get;
return SoundChannel;
function _get(index) {
return this._instances[index];
}
function _add(instance, interrupt) {
if (!this._getSlot(interrupt, instance)) {
return false;
}
this._instances.push(instance);
this.length++;
return true;
}
function _remove(instance) {
var index = createjs.indexOf(this._instances, instance);
if (index == -1) {
return false;
}
this._instances.splice(index, 1);
this.length--;
return true;
}
function _removeAll() {
// Note that stop() removes the item from the list
for (var i=this.length-1; i>=0; i--) {
this._instances[i].stop();
}
}
function _getSlot(interrupt, instance) {
var target, replacement;
if (interrupt != Sound.INTERRUPT_NONE) {
// First replacement candidate
replacement = this._get(0);
if (replacement == null) {
return true;
}
}
for (var i = 0, l = this.max; i < l; i++) {
target = this._get(i);
// Available Space
if (target == null) {
return true;
}
// Audio is complete or not playing
if (target.playState == Sound.PLAY_FINISHED ||
target.playState == Sound.PLAY_INTERRUPTED ||
target.playState == Sound.PLAY_FAILED) {
replacement = target;
break;
}
if (interrupt == Sound.INTERRUPT_NONE) {
continue;
}
// Audio is a better candidate than the current target, according to playhead
if ((interrupt == Sound.INTERRUPT_EARLY && target.position < replacement.position) ||
(interrupt == Sound.INTERRUPT_LATE && target.position > replacement.position)) {
replacement = target;
}
}
if (replacement != null) {
replacement._interrupt();
this._remove(replacement);
return true;
}
return false;
}
function create(source, max) {
var channel = SoundChannel.get(source);
if (channel == null) {
SoundChannel.channels[source] = new SoundChannel(source, max);
return true;
}
return false;
}
function removeSrc(source) {
var channel = SoundChannel.get(source);
if (channel == null) {
return false;
}
channel._removeAll(); // this stops and removes all active instances
delete(SoundChannel.channels[source]);
return true;
}
function removeAll() {
for(var channel in SoundChannel.channels) {
SoundChannel.channels[channel]._removeAll(); // this stops and removes all active instances
}
SoundChannel.channels = {};
}
function add(instance, interrupt) {
var channel = SoundChannel.get(instance.src);
if (channel == null) {
return false;
}
return channel._add(instance, interrupt);
}
function remove(instance) {
var channel = SoundChannel.get(instance.src);
if (channel == null) {
return false;
}
channel._remove(instance);
return true;
}
function maxPerChannel() {
return p.maxDefault;
}
function get(source) {
return SoundChannel.channels[source];
}
})();