All files / addon/classes oscillator.js

90% Statements 18/20
50% Branches 2/4
85.71% Functions 6/7
90% Lines 18/20

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                                                                        1x                                                                                                     2x 2x 2x 2x     2x   2x 2x                         2x 16x   16x           2x   2x 2x                       2x                                                     2x                                           2x                               2x                                                                                                                                                                                                                                                                          
import { A } from '@ember/array';
import { on } from '@ember/object/evented';
import EmberObject from '@ember/object';
import { Connectable, Playable } from 'ember-audio/mixins';
import { Connection } from 'ember-audio';
 
/**
 * A class that represents an oscillator for a synthesizer. Capable of creating
 * and playing a waveform of specified `type` at a specified `frequency`.
 *
 * All filters from {{#crossLink "BiquadFilterNode"}}{{/crossLink}} are
 * available and can be enabled by providing a pojo for a given filter.
 *
 *     // 200Hz sine wave w/highpass at 600Hz and lowpass created but not filtering
 *     const osc = audioService.createOscillator({
 *       frequency: 200,
 *       highpass: { frequency: 600 },
 *       lowpass: {}
 *     });
 *
 *     // or
 *     import { Oscillator } from 'ember-audio';
 *     const audioContext = audioService.get('audioContext');
 *     const osc = Oscillator.create({
 *       audioContext,
 *       frequency: 200,
 *       highpass: { frequency: 600 },
 *       lowpass: {}
 *     });
 *
 * @public
 * @class Oscillator
 * @uses Connectable
 * @uses Playable
 * @todo figure out why `isPlaying` isn't working for Oscillator
 */
const Oscillator = EmberObject.extend(Connectable, Playable, {
  /**
   * Determines the type of wave output by the OscillatorNode instance.
   * Corresponds directly to `type` from
   * {{#crossLink "OscillatorNode"}}{{/crossLink}}
   *
   * @public
   * @property type
   * @type {string}
   * @default 'sine'
   */
  type: 'sine',
 
  /**
   * Determines the frequency of the wave output by the OscillatorNode instance.
   * Corresponds directly to `frequency.value` from
   * {{#crossLink "OscillatorNode"}}{{/crossLink}}
   *
   * @public
   * @property frequency
   * @type {number}
   */
  frequency: null,
 
  /**
   * Determines the `gain.value` of the GainNode instance in the `gain`
   * connection instance. Corresponds directly to `gain.value` from
   * {{#crossLink "GainNode"}}{{/crossLink}}
   *
   * @public
   * @property gain
   * @type {number}
   */
  gain: null,
 
  /**
   * Lists available filter types.
   *
   * @private
   * @property _filters
   * @type {array|string}
   */
  _filters: null,
 
  /**
   * Initializes default connections on Oscillator instantiation. Runs `on('init')`.
   *
   * @protected
   * @method _initConnections
   */
  _initConnections: on('init', function () {
    const bufferSource = this._createBufferSource();
    const gain = this._createGainNode();
    const panner = this._createPannerNode();
    const destination = this._createDestinationNode();
 
    // always start with source
    const connections = A([bufferSource]);
 
    Eif (!this._filters) {
      this._filters = [
        'lowpass',
        'highpass',
        'bandpass',
        'lowshelf',
        'highshelf',
        'peaking',
        'notch',
        'allpass',
      ];
    }
 
    // Add filters if they have been defined
    this._filters.map((filterName) => {
      const filterIsDefined = this.get(filterName) !== null;
 
      Iif (filterIsDefined) {
        connections.pushObject(this._createFilter(filterName));
      }
    });
 
    // add gain, panner, and destination connections
    connections.pushObjects([gain, panner, destination]);
 
    this.set('connections', connections);
    this.wireConnections();
  }),
 
  /**
   * Creates a Connection instance backed with an `Oscillator` node.
   *
   * @private
   * @method _createBufferSource
   *
   * @return {Connection} A connection backed with an `Oscillator` node.
   */
  _createBufferSource() {
    return Connection.create({
      name: 'audioSource',
      createdOnPlay: true,
      source: 'audioContext',
      createCommand: 'createOscillator',
      onPlaySetAttrsOnNode: [
        {
          attrNameOnNode: 'frequency.value',
          relativePath: 'frequency',
        },
        {
          attrNameOnNode: 'type',
          relativePath: 'type',
        },
      ],
    });
  },
 
  /**
   * Creates a Connection instance backed with a `Gain` node.
   *
   * @private
   * @method _createGainNode
   *
   * @return {Connection} A connection backed with a `Gain` node.
   */
  _createGainNode() {
    return Connection.create({
      name: 'gain',
      source: 'audioContext',
      createCommand: 'createGain',
      onPlaySetAttrsOnNode: [
        {
          attrNameOnNode: 'gain.value',
          relativePath: 'gain',
        },
      ],
    });
  },
 
  /**
   * Creates a Connection instance backed with a `Panner` node.
   *
   * @private
   * @method _createPannerNode
   *
   * @return {Connection} A connection backed with a `Panner` node.
   */
  _createPannerNode() {
    return Connection.create({
      name: 'panner',
      source: 'audioContext',
      createCommand: 'createStereoPanner',
    });
  },
 
  /**
   * Creates a Connection instance backed with a `Destination` node.
   *
   * @private
   * @method _createDestinationNode
   *
   * @return {Connection} A connection backed with a `Destination` node.
   */
  _createDestinationNode() {
    return Connection.create({
      name: 'destination',
      path: 'audioContext.destination',
    });
  },
 
  /**
   * Creates a Connection instance with a filter of the specified type.
   *
   * @private
   * @method _createFilter
   *
   * @param {string} type Determines what type of filter will be created.
   *
   * @return {Connection} A connection with a BiquadFilterNode of the specified
   * type.
   */
  _createFilter(type) {
    return Connection.create({
      name: type,
      source: 'audioContext',
      createCommand: 'createBiquadFilter',
      onPlaySetAttrsOnNode: [
        {
          attrNameOnNode: 'type',
          value: type,
        },
        {
          attrNameOnNode: 'frequency.value',
          relativePath: `${type}.frequency`,
        },
        {
          attrNameOnNode: 'q.value',
          relativePath: `${type}.q`,
        },
        {
          attrNameOnNode: 'gain.value',
          relativePath: `${type}.gain`,
        },
      ],
    });
  },
 
  /**
   * Settings object for the lowpass filter. The lowpass filter is disabled
   * if this is not provided. Accepts `frequency` and `q`.
   * https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode
   *
   * @public
   * @property lowpass
   * @type {object}
   */
  lowpass: null,
 
  /**
   * Settings object for the highpass filter. The highpass filter is disabled
   * if this is not provided. Accepts `frequency` and `q`.
   * https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode
   *
   * @public
   * @property highpass
   * @type {object}
   */
  highpass: null,
 
  /**
   * Settings object for the bandpass filter. The bandpass filter is disabled
   * if this is not provided. Accepts `frequency` and `q`.
   * https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode
   *
   * @public
   * @property bandpass
   * @type {object}
   */
  bandpass: null,
 
  /**
   * Settings object for the lowshelf filter. The lowshelf filter is disabled
   * if this is not provided. Accepts `frequency` and `gain`.
   * https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode
   *
   * @public
   * @property lowshelf
   * @type {object}
   */
  lowshelf: null,
 
  /**
   * Settings object for the highshelf filter. The highshelf filter is disabled
   * if this is not provided. Accepts `frequency` and `gain`.
   * https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode
   *
   * @public
   * @property highshelf
   * @type {object}
   */
  highshelf: null,
 
  /**
   * Settings object for the peaking filter. The peaking filter is disabled
   * if this is not provided. Accepts `frequency`, `q`, and `gain`.
   * https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode
   *
   * @public
   * @property peaking
   * @type {object}
   */
  peaking: null,
 
  /**
   * Settings object for the notch filter. The notch filter is disabled
   * if this is not provided. Accepts `frequency` and `q`.
   * https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode
   *
   * @public
   * @property notch
   * @type {object}
   */
  notch: null,
 
  /**
   * Settings object for the allpass filter. The allpass filter is disabled
   * if this is not provided. Accepts `frequency`, and `q`.
   * https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode
   *
   * @public
   * @property allpass
   * @type {object}
   */
  allpass: null,
});
 
export default Oscillator;