| 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
206
207
208 |
1×
1×
1×
1×
12×
12×
12×
12×
12×
12×
12×
12×
11×
12×
10×
9×
9×
9×
9×
9×
9×
9×
9×
9×
9×
9×
9×
9×
12×
12×
2×
2×
2×
3×
3×
3×
3×
12×
8×
8×
8×
12×
25×
25×
25×
12×
1×
9×
9×
9×
9×
1×
9×
9×
9×
9×
9×
9×
9×
9×
9×
9×
9×
9×
9×
3×
3×
3×
3×
9×
18×
1×
1×
9×
9×
9×
36×
36×
9×
9×
9×
1×
| /* global AudioBuffer */
'use strict'
var ADSR = require('adsr')
var EMPTY = {}
var DEFAULTS = {
gain: 1,
attack: 0.01,
decay: 0.1,
sustain: 0.9,
release: 0.3,
loop: false,
cents: 0,
loopStart: 0,
loopEnd: 0
}
/**
* Create a sample player.
*
* @param {AudioContext} ac - the audio context
* @param {ArrayBuffer|Object<String,ArrayBuffer>} source
* @param {Onject} options - (Optional) an options object
* @return {player} the player
* @example
* var SamplePlayer = require('sample-player')
* var ac = new AudioContext()
* var snare = SamplePlayer(ac, <AudioBuffer>)
* snare.play()
*/
function SamplePlayer (ac, source, options) {
var connected = false
var nextId = 0
var tracked = {}
var out = ac.createGain()
out.gain.value = 1
var opts = Object.assign({}, DEFAULTS, options)
/**
* @namespace
*/
var player = { context: ac, out: out, opts: opts }
if (source instanceof AudioBuffer) player.buffer = source
else player.buffers = source
/**
* Start a sample buffer.
*
* The returned object has a function `stop(when)` to stop the sound.
*
* @param {String} name - the name of the buffer. If the source of the
* SamplePlayer is one sample buffer, this parameter is not required
* @param {Float} when - (Optional) when to start (current time if by default)
* @param {Object} options - additional sample playing options
* @return {AudioNode} an audio node with a `stop` function
* @example
* var sample = player(ac, <AudioBuffer>).connect(ac.destination)
* sample.start()
* sample.start(5, { gain: 0.7 }) // name not required since is only one AudioBuffer
* @example
* var drums = player(ac, { snare: <AudioBuffer>, kick: <AudioBuffer>, ... }).connect(ac.destination)
* drums.start('snare')
* drums.start('snare', 0, { gain: 0.3 })
*/
player.start = function (name, when, options) {
// if only one buffer, reorder arguments
if (player.buffer && name !== null) return player.start(null, name, when)
var buffer = name ? player.buffers[name] : player.buffer
Iif (!buffer) {
console.warn('Buffer ' + name + ' not found.')
return
} else Iif (!connected) {
console.warn('SamplePlayer not connected to any node.')
return
}
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)
node.env.start(when)
node.source.start(when)
player.emit('started', when, node.id, node)
Iif (opts.duration) node.stop(when + opts.duration)
return node
}
/**
* An alias for `player.start`
* @see player.start
* @since 0.3.0
*/
player.play = player.start
/**
* Stop some or all samples
*
* @param {Float} when - (Optional) an absolute time in seconds (or currentTime
* if not specified)
* @param {Array} nodes - (Optional) an array of nodes or nodes ids to stop
* @return {Array} an array of ids of the stoped samples
*
* @example
* var longSound = player(ac, <AudioBuffer>).connect(ac.destination)
* longSound.start(ac.currentTime)
* longSound.start(ac.currentTime + 1)
* longSound.start(ac.currentTime + 2)
* longSound.stop(ac.currentTime + 3) // stop the three sounds
*/
player.stop = function (when, ids) {
var node
ids = ids || Object.keys(tracked)
return ids.map(function (id) {
node = tracked[id]
Iif (!node) return null
node.stop(when)
return node.id
})
}
/**
* Connect the player to a destination node
*
* @param {AudioNode} destination - the destination node
* @return {AudioPlayer} the player
* @chainable
* @example
* var sample = player(ac, <AudioBuffer>).connect(ac.destination)
*/
player.connect = function (dest) {
connected = true
out.connect(dest)
return player
}
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)
}
return player
// =============== PRIVATE FUNCTIONS ============== //
function track (name, node) {
node.id = nextId++
tracked[node.id] = node
node.source.onended = function () {
var now = ac.currentTime
node.source.disconnect()
node.env.disconnect()
node.disconnect()
player.emit('ended', now, node.id, node)
}
return node.id
}
function createNode (name, buffer, options) {
var node = ac.createGain()
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
node.stop = function (when) {
var time = when || ac.currentTime
player.emit('stop', time, name)
var stopAt = node.env.stop(time)
node.source.stop(stopAt)
}
return node
}
}
function isNum (x) { return typeof x === 'number' }
var PARAMS = ['attack', 'decay', 'sustain', 'release']
function envelope (ac, options, opts) {
var env = ADSR(ac)
var adsr = options.adsr || opts.adsr
PARAMS.forEach(function (name, i) {
Iif (adsr) env[name] = adsr[i]
else env[name] = options[name] || opts[name]
})
env.value.value = isNum(options.gain) ? options.gain
: isNum(opts.gain) ? opts.gain : 1
return env
}
/*
* Get playback rate for a given pitch change (in cents)
* Basic [math](http://www.birdsoft.demon.co.uk/music/samplert.htm):
* f2 = f1 * 2^( C / 1200 )
*/
function centsToRate (cents) { return cents ? Math.pow(2, cents / 1200) : 1 }
module.exports = SamplePlayer
|