|
@@ -80,8 +80,17 @@ function SamplePlayer (ac, source, options) {
|
|
|
var opts = options || EMPTY
|
|
|
when = Math.max(ac.currentTime, when || 0)
|
|
|
player.emit('start', when, name, opts)
|
|
|
- var node = createNode(name, buffer, opts)
|
|
|
- node.id = track(name, node)
|
|
|
+ // cache
|
|
|
+ var node
|
|
|
+ for (var iterator in tracked) {
|
|
|
+ if (buffer === tracked[iterator].source.buffer) {
|
|
|
+ node = resetNode(tracked[iterator], buffer, opts)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!node) {
|
|
|
+ node = createNode(name, buffer, opts)
|
|
|
+ node.id = track(name, node)
|
|
|
+ }
|
|
|
node.env.start(when)
|
|
|
node.source.start(when)
|
|
|
player.emit('started', when, node.id, node)
|
|
@@ -142,8 +151,8 @@ function SamplePlayer (ac, source, options) {
|
|
|
|
|
|
player.emit = function (event, when, obj, opts) {
|
|
|
if (player.onevent) player.onevent(event, when, obj, opts)
|
|
|
- var fn = player['on' + event]
|
|
|
- if (fn) fn(when, obj, opts)
|
|
|
+ var fnList = player['on' + event]
|
|
|
+ if (fnList) fnList.forEach((fn) => { fn(when, obj, opts) })
|
|
|
}
|
|
|
|
|
|
return player
|
|
@@ -186,6 +195,24 @@ function SamplePlayer (ac, source, options) {
|
|
|
}
|
|
|
return node
|
|
|
}
|
|
|
+
|
|
|
+ function resetNode(oldNode, buffer, options) {
|
|
|
+ var node = oldNode
|
|
|
+ node.gain.value = 0 // the envelope will control the gain
|
|
|
+ node.connect(out)
|
|
|
+
|
|
|
+ node.env = envelope(ac, options, opts)
|
|
|
+ node.env.connect(node.gain)
|
|
|
+
|
|
|
+ node.source = ac.createBufferSource()
|
|
|
+ node.source.buffer = buffer
|
|
|
+ node.source.connect(node)
|
|
|
+ node.source.loop = options.loop || opts.loop
|
|
|
+ node.source.playbackRate.value = centsToRate(options.cents || opts.cents)
|
|
|
+ node.source.loopStart = options.loopStart || opts.loopStart
|
|
|
+ node.source.loopEnd = options.loopEnd || opts.loopEnd
|
|
|
+ return node
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
function isNum (x) { return typeof x === 'number' }
|