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 | 38x 38x 38x 40x 1x 1x 4x 3x 3x 1x 1x 1x 30x 30x 30x 30x 30x 33x 41x 41x 41x 41x 41x 165x 165x 27x 138x 32x 106x 105x 1x 1x 137x 164x 49x 49x 49x 25x 164x 164x 164x 164x 164x 656x 656x 164x 164x 164x 123x 123x 123x 164x | import EmberError from '@ember/error'; import { A } from '@ember/array'; import { on } from '@ember/object/evented'; import { observer, set, get } from '@ember/object'; import Mixin from '@ember/object/mixin'; import { Connection } from 'ember-audio'; /** * A mixin that allows an object to create AudioNodes and connect them together. * Depends on `audioContext` being available on the consuming object. * * @public * @class Connectable * @todo figure out how to augment Ember.MutableArray so that the connections * array can have methods like addConnection, removeConnection, addFilter, disableNode */ export default Mixin.create({ /** * An array of Connection instances. Determines which AudioNode instances are * connected to one-another and the order in which they are connected. Starts * as `null` but set to an array on `init` via the * {{#crossLink "Connectable/_initConnections:method"}}{{/crossLink}} method. * * @public * @property connections * @type {Ember.MutableArray} */ connections: null, /** * returns a connection's AudioNode from the connections array by the * connection's `name`. * * @public * @method getNodeFrom * * @param {string} name The name of the AudioNode that should be returned. * * @return {AudioNode} The requested AudioNode. */ getNodeFrom(name) { const connection = this.getConnection(name); Eif (connection) { return get(connection, 'node'); } }, /** * returns a connection from the connections array by it's name * * @public * @method getConnection * * @param {string} name The name of the AudioNode that should be returned. * * @return {Connection} The requested Connection. */ getConnection(name) { return this.connections.findBy('name', name); }, /** * Find's a connection in the connections array by it's `name` and removes it. * * @param {string} name The name of the connection that should be removed. * * @public * @method removeConnection */ removeConnection(name) { this.connections.removeObject(this.getConnection(name)); }, /** * Updates an AudioNode's property in real time by setting the property on the * connectable object (which affects the next play) and also sets it on it's * corresponding `connection.node` (which affects the audio in real time). * * If a connection is pulling a property from a connectable object via * `onPlaySetAttrsOnNode[index].relativePath`, this method will update that * property directly on the Connection's `node` as well as on the connectable * object. * * Important to note that this only works for properties that are set directly * on a connectable object and then proxied/set on a node via * `onPlaySetAttrsOnNode`. * * @example * // With `set` * const osc = audioService.createOscillator({ frequency: 440 }); * osc.play(); // playing at 440Hz * osc.set('frequency', 1000); // still playing at 440Hz * osc.stop(); * osc.play(); // now playing at 1000Hz * * @example * // With `update` * const osc = audioService.createOscillator({ frequency: 440 }); * osc.play(); // playing at 440Hz * osc.update('frequency', 1000); // playing at 1000Hz * * @public * @method update * * @param key {string} The name/path to a property on an Oscillator instance * that should be updated. * * @param value {string} The value that the property should be set to. */ update(key, value) { this.connections.map((connection) => { connection.get('onPlaySetAttrsOnNode').map((attr) => { const path = get(attr, 'relativePath'); if (key === path) { const pathChunks = path.split('.'); connection.get('node')[pathChunks.pop()].value = value; } }); }); this.set(key, value); }, /** * Initializes default connections on Sound instantiation. Runs `on('init')`. * * @protected * @method _initConnections */ _initConnections: on('init', function () { const bufferSource = Connection.create({ name: 'audioSource', createdOnPlay: true, source: 'audioContext', createCommand: 'createBufferSource', onPlaySetAttrsOnNode: [ { attrNameOnNode: 'buffer', relativePath: 'audioBuffer', }, ], }); const gain = Connection.create({ name: 'gain', source: 'audioContext', createCommand: 'createGain', }); const panner = Connection.create({ name: 'panner', source: 'audioContext', createCommand: 'createStereoPanner', }); const destination = Connection.create({ name: 'destination', path: 'audioContext.destination', }); this.set('connections', A([bufferSource, gain, panner, destination])); }), /* * Note about _watchConnectionChanges: * Yeah yeah yeah.. observers are bad. Making the connections array a computed * property doesn't work very well because complete control over when it * recalculates is needed. */ /** * Observes the connections array and runs wireConnections each time it * changes. * * @private * @method _watchConnectionChanges */ _watchConnectionChanges: observer('connections.[]', function () { this.wireConnections(); }), /** * Gets the array of Connection instances from the connections array and * returns the same array, having created any AudioNode instances that needed * to be created, and having connected the AudioNode instances to one another * in the order in which they were present in the connections array. * * @protected * @method wireConnections * * @return {array|Connection} Array of Connection instances collected from the * connections array, created, connected, and ready to play. */ wireConnections() { const createNode = this._createNode.bind(this); const setAttrsOnNode = this._setAttrsOnNode.bind(this); const wireConnection = this._wireConnection; const connections = this.connections; connections.map(createNode).map(setAttrsOnNode).map(wireConnection); }, /** * Creates an AudioNode instance for a Connection instance and sets it on it's * `node` property. Unless the Connection instance's `createdOnPlay` property * is true, does nothing if the AudioNode instance has already been created. * * Also sets any properties from a connection's `onPlaySetAttrsOnNode` array * on the node. * * @private * @method _createNode * * @param {Connection} connection A Connection instance that should have it's * node created (if needed). * * @return {Connection} The input Connection instance after having it's node * created. */ _createNode(connection) { const { path, name, createdOnPlay, source, createCommand, node } = connection; if (node && !createdOnPlay) { // The node is already created and doesn't need to be created again return connection; } else if (path) { connection.node = this.get(path); } else if (createCommand && source) { connection.node = this.get(source)[createCommand](); } else Eif (!connection.node) { throw new EmberError( `ember-audio: The ${name} connection is not configured correctly. Please fix this connection.` ); } return connection; }, /** * Gets a Connection instance's `onPlaySetAttrsOnNode` and sets them on it's * node. * * @private * @method _setAttrsOnNode * * @param {Connection} connection The Connection instance that needs it's * node's attrs set. * * @return {Connection} The input Connection instance after having it's nodes * attrs set. */ _setAttrsOnNode(connection) { connection.get('onPlaySetAttrsOnNode').map((attr) => { const { attrNameOnNode, relativePath, value } = attr; const attrValue = relativePath && this.get(relativePath) ? this.get(relativePath) : value; if (connection.node && attrNameOnNode && attrValue) { set(connection.node, attrNameOnNode, attrValue); } }); this._setConnectionValues(connection, 'exponentialRampToValuesAtTime'); this._setConnectionValues(connection, 'linearRampToValuesAtTime'); this._setConnectionValues(connection, 'setValuesAtTime'); this._setConnectionValues(connection, 'startingValues', 'setValueAtTime'); return connection; }, /** * Gets a Connection instance's `onPlaySetAttrsOnNode` and sets them on it's * node. * * @private * @method _setConnectionValues * * @param {Connection} connection The Connection instance that needs it's * node's attrs set. * * @param {string} attr The attr that needs it's values set. By default, the * method called to set this attr's value is identical in name, but the word * "Values" is singularized. In the example, `someKey` is a key that * comes from `onPlaySetAttrsOnNode`. * * @example * // this * this._setConnectionValues(connection, 'setValuesAtTime'); * // ends up like * connection.node.someKey.setValueAtTime(value, time); * * @param {string} optionalMethodKey Optionally specifies the method which * should be called to set the attr. This would override the `setValueAtTime * method in the example. */ _setConnectionValues(connection, attr, optionalMethodKey) { const currentTime = this.get('audioContext.currentTime'); connection.get(attr).map((opts) => { const time = currentTime + (opts.endTime || 0); attr = optionalMethodKey || attr.replace('Values', 'Value'); connection.node[opts.key][attr](opts.value, time); }); }, /** * Meant to be passed to a Array.prototype.map function. Connects a Connection * instance's node to the next Connection instance's node. * * @private * @method _wireConnection * * @param {Connection} connection The current Connection instance in the * iteration. * * @param {number} idx The index of the current iteration. * * @param {array|Connection} connections The original array of connections. * * @return {Connection} The input Connection instance after having it's node * connected to the next Connection instance's node. */ _wireConnection(connection, idx, connections) { const nextIdx = idx + 1; const currentNode = connection; if (nextIdx < connections.length) { const nextNode = connections[nextIdx]; // Assign nextConnection back to connections array. // Since we're working one step ahead, we don't want // each connection created twice connections[nextIdx] = nextNode; // Make the connection from current to next currentNode.node.connect(nextNode.node); } return currentNode; }, }); |