Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | 1x 129x 129x 645x 611x | import { A } from '@ember/array'; import EmberObject from '@ember/object'; import { on } from '@ember/object/evented'; /** * This class represents a single connection in a Sound instance's connections * array. It is mostly just a wrapper around an AudioNode instance. It defines * some standards for how to handle the behaviors of different AudioNode types. * Most connections create their corresponding AudioNode immediately, but some * AudioNodes are "throw-away" and have to be created each time a Sound instance * is played. * * Most properties in this class just define how to go about getting/creating an * AudioNode instance and setting it on this class' `node` property. Some define * how to set properties on the AudioNode instance after it has been created. * * @public * @class Connection */ const Connection = EmberObject.extend({ /** * The name of the connection. This is the name that can be used to * get an AudioNode instance via the * {{#crossLink "Connectable/getNodeFrom:method"}}{{/crossLink}} method, or a * Connection instance via the * {{#crossLink "Connectable/getConnection"}}{{/crossLink}} method. * * @public * @property name * @type {string} */ name: null, /** * If an AudioNode instance already exists and is accessible to the Sound * instance, the path to the node can be placed here. If this value is * specified, all options except `name` become useless. If `node` is specified, * it will override this option and the AudioNode supplied to `node` will be * used. * * @example * // Uses the Audio Node instance from: * // soundInstance.get('audioContext.destination') * { * name: 'destination', * path: 'audioContext.destination' * } * * @public * @property path * @type {string} */ path: null, /** * If `createCommand` is specified, the object at this location (relative to * the Sound instance) will be used as the "source" of the `createCommand`. * * @example * // Creates the AudioNode by calling: * // this.get('audioContext')[createCommand](); * { * source: 'audioContext' * createCommand: createGain * } * * @public * @property source * @type {string} */ source: null, /** * If `source` is specified, this method will be called on the object that was * retrieved from `source`. The value returned from this method is set on the * `node` property. * * @example * // results in the `node` property being created like: * // this.get('audioContext').createGain(); * { * source: 'audioContext' * createCommand: 'createGain' * } * * @public * @property createCommand * @type {string} */ createCommand: null, /** * An array of POJOs that specify properties that need to be set on a node * when any of the {{#crossLink "Playable/play:method"}}{{/crossLink}} methods * are called. For instance, an * {{#crossLink "AudioBufferSourceNode"}}{{/crossLink}} must be created at * play time, because they can only be played once and then they are * immediately thrown away. * * Valid keys are: * * `attrNameOnNode` {string} Determines which property on the node should be * set to the value. This can be a nested accessor (ie. `'gain.value'`). * * `relativePath` {string} Determines where on `this` (the Sound instance) to * get the value. This can be a nested accessor (ie. `'gainNode.gain.value'`). * * `value` {mixed} The direct value to set. If used along with `relativePath`, * this will act as a default value and the value at `relativePath` will take * precedence. * * @example * // Causes gainNode.gain.value = soundInstance.get('gainValue') || 1; * // to be called at play-time * * { * name: 'gainNode', * onPlaySetAttrsOnNode: [ * { * attrNameOnNode: 'gain.value', * relativePath: 'gainValue', * value: 1 * } * ] * } * * @public * @property onPlaySetAttrsOnNode * @type {Ember.MutableArray} * @default Ember.A() via _initArrays */ onPlaySetAttrsOnNode: null, /** * Items in this array are set at play-time on the `node` via an exponential * ramp that ends at the specified time. * * A convenience setter method called * {{#crossLink "Connection/onPlaySet:method"}}{{/crossLink}} exists for this * array and should be used unless it does not allow enough freedom for your * use-case. * * @example * // at play time: connection.node.gain.exponentialRampToValueAtTime(0.1, 1) * { * key: 'gain', * value: 0.1, * endTime: 1 * } * // the same thing can be accomplished like: * connection.onPlaySet('gain').to(0.1).endingAt(1) * * @public * @property exponentialRampToValuesAtTime * @type {Ember.MutableArray} * @default Ember.A() via _initArrays */ exponentialRampToValuesAtTime: null, /** * Items in this array are set at play-time on the `node` via a linear ramp * that ends at the specified time. * * A convenience setter method called * {{#crossLink "Connection/onPlaySet:method"}}{{/crossLink}} exists for this * array and should be used unless it does not allow enough freedom for your * use-case. * * @example * // at play time: connection.node.gain.linearRampToValueAtTime(0.1, 1) * { * key: 'gain', * value: 0.1, * endTime: 1 * } * // the same thing can be accomplished like: * connection.onPlaySet('gain').to(0.1).endingAt(1, 'linear') * * @public * @property linearRampToValuesAtTime * @type {Ember.MutableArray} * @default Ember.A() via _initArrays */ linearRampToValuesAtTime: null, /** * Items in this array are set at play-time on the `node` via an exponential * ramp that ends at the specified time. * * A convenience setter method called * {{#crossLink "Connection/onPlaySet:method"}}{{/crossLink}} exists for this * array and should be used unless it does not allow enough freedom for your * use-case. * * @example * // at play time: connection.node.gain.setValueAtTime(0.1, 1) * { * key: 'gain', * value: 0.1, * startTime: 1 * } * // the same thing can be accomplished like: * connection.onPlaySet('gain').to(0.1).at(1) * * @public * @property setValuesAtTime * @type {Ember.MutableArray} * @default Ember.A() via _initArrays */ setValuesAtTime: null, /** * Items in this array are set immediately at play-time on the `node`. * * A convenience setter method called * {{#crossLink "Connection/onPlaySet:method"}}{{/crossLink}} exists for this * array and should be used unless it does not allow enough freedom for your * use-case. * * @example * // at play time: connection.node.gain.setValueAtTime(0.1, audioContext.currentTime) * { * key: 'gain', * value: 0.1 * } * // the same thing can be accomplished like: * connection.onPlaySet('gain').to(0.1) * * @public * @property startingValues * @type {Ember.MutableArray} * @default Ember.A() via _initArrays */ startingValues: null, /** * This is the main attraction here in connection-land. All the other * properties in the Connection class exist to create or mutate this property. * Houses an AudioNode instance that will be used by an instance of the Sound * class. * * If this property is set directly, all of the other properties on this class * (except `name`) are rendered useless. * * @public * @property node * @type {AudioNode} */ node: null, /** * If this is true, the AudioNode will be created every time the consuming * Sound instance is played. * * @public * @property createdOnPlay * @type {boolean} * @default false */ createdOnPlay: false, /** * Allows an AudioNode's values to be set at a specific time * relative to the moment that it is played, every time it is played. * * Especially useful for creating/shaping an "envelope" (think "ADSR"). * * @example * // results in an oscillator that starts at 150Hz and quickly drops * // down to 0.01Hz each time it's played * const kick = audio.createOscillator({ name: 'kick' }); * const osc = kick.getConnection('audioSource'); * * osc.onPlaySet('frequency').to(150).at(0); * osc.onPlaySet('frequency').to(0.01).at(0.1); * * @public * @method onPlaySet * @todo document 'exponential' and 'linear' options */ onPlaySet(key) { const startingValues = this.get('startingValues'); const exponentialValues = this.get('exponentialRampToValuesAtTime'); const linearValues = this.get('linearRampToValuesAtTime'); const valuesAtTime = this.get('setValuesAtTime'); return { to(value) { const startValue = { key, value }; startingValues.pushObject(startValue); return { at(startTime) { startingValues.removeObject(startValue); valuesAtTime.pushObject({ key, value, startTime }); }, endingAt(endTime, type='exponential') { startingValues.removeObject(startValue); switch (type) { case 'exponential': exponentialValues.pushObject({ key, value, endTime }); break; case 'linear': linearValues.pushObject({ key, value, endTime }); break; } } }; } }; }, /** * Convenience method that uses * {{#crossLink "Connection/onPlaySet:method"}}{{/crossLink}} twice to set an * initial value, and a ramped value in succession. * * Especially useful for creating/shaping an "envelope" (think "ADSR"). * * @example * // results in an oscillator that starts at 150Hz and quickly drops * // down to 0.01Hz each time it's played * const kick = audio.createOscillator({ name: 'kick' }); * const osc = kick.getConnection('audioSource'); * * osc.onPlayRamp('frequency').from(150).to(0.01).in(0.1); * * @public * @method onPlaySet */ onPlayRamp(key) { const onPlaySet = this.onPlaySet.bind(this); return { from(startValue) { return { to(endValue) { return { in(endTime) { onPlaySet(key).to(startValue); onPlaySet(key).to(endValue).endingAt(endTime); } }; } }; } }; }, /** * If any of the array types are null on init, set them to an * Ember.MutableArray * * @private * @method _initArrays */ _initArrays: on('init', function() { const arrays = [ 'onPlaySetAttrsOnNode', 'exponentialRampToValuesAtTime', 'linearRampToValuesAtTime', 'setValuesAtTime', 'startingValues' ]; arrays.map((name) => { if (!this.get(name)) { this.set(name, A()); } }); }) }); export default Connection; |