All files resonance-audio-src.js

91.19% Statements 145/159
88.66% Branches 86/97
66.67% Functions 2/3
93.29% Lines 139/149

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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470  1x 1x   1x         1x   1x                                                             57x   57x         57x     57x     57x         57x     57x 57x       73x 12x   72x 72x 72x 72x 72x   72x   72x             61x 61x 61x 61x 61x 61x 61x 61x     61x 45x             57x 57x 57x 57x   57x 2x               133x 133x 56x 56x 56x 56x 56x 56x             99x     34x 34x         34x 30x   4x                 139x 62x   139x                 131x 51x                       80x 51x                 135x 135x 135x   116x 1x 1x 1x   116x 116x 116x 116x 116x   135x                 13x   2x 2x                       72x 72x                             192x     112x       80x                               80x 73x   7x     80x 73x 73x   7x     80x                 62x                     116x                         61x 16x 16x       45x 45x     45x     45x 45x     45x               161x 45x 45x 45x   45x 45x 45x   45x                     27x   27x     27x 20x     27x   27x               27x   27x   27x       27x   27x 25x                                           130x 11x 11x   130x 130x               73x   73x   73x   28x   28x 5x 23x 23x 22x   1x 1x   23x 22x       72x       1x                                          
/* global document, AFRAME, THREE, MediaStream, HTMLMediaElement */
const { ResonanceAudio } = require('resonance-audio')
const { isVec3Set, onceWhenLoaded } = require('./utils')
 
const warn = AFRAME.utils.debug('components:resonance-audio-src:warn')
 
/**
 * The Object3D name of the visualization.
 */
const visName = 'audio-src'
 
AFRAME.registerComponent('resonance-audio-src', {
  dependencies: ['position', 'rotation'],
 
  schema: {
    src: { type: 'string' }, // asset parsing is taken over from A-Frame.
    room: { type: 'string' },
    loop: { type: 'boolean', default: true },
    autoplay: { type: 'boolean', default: true },
    gain: { type: 'number', default: ResonanceAudio.Utils.DEFAULT_SOURCE_GAIN },
    maxDistance: { type: 'number', default: ResonanceAudio.Utils.DEFAULT_MAX_DISTANCE },
    minDistance: { type: 'number', default: ResonanceAudio.Utils.DEFAULT_MIN_DISTANCE },
    directivityPattern: {
      type: 'vec2',
      default: {
        x: ResonanceAudio.Utils.DEFAULT_DIRECTIVITY_ALPHA,
        y: ResonanceAudio.Utils.DEFAULT_DIRECTIVITY_SHARPNESS
      }
    },
    sourceWidth: { type: 'number', default: ResonanceAudio.Utils.DEFAULT_SOURCE_WIDTH },
    rolloff: {
      type: 'string',
      oneOff: ResonanceAudio.Utils.ATTENUATION_ROLLOFFS,
      default: ResonanceAudio.Utils.DEFAULT_ATTENUATION_ROLLOFF
    },
    position: { type: 'vec3', default: new THREE.Vector3(Infinity, Infinity, Infinity) },
    rotation: { type: 'vec3', default: new THREE.Vector3(Infinity, Infinity, Infinity) },
    visualize: { type: 'boolean', default: false }
  },
 
  init () {
    // The room this audio source is in.
    this.room = null
    // The connection status.
    this.connected = {
      element: false,
      stream: false
    }
    // The current connected element or stream.
    this.sound = null
 
    // The Resonance audio source.
    this.resonance = null
 
    // The default audio element used when src is set to a resource string.
    this.defaultAudioEl = null
 
    // A mapping of elements and stream to their source AudioNode objects.
    // We use a mapping so the created MediaElementAudioSourceNode and MediaStreamAudioSourceNode
    // objects can be reused.
    this.mediaAudioSourceNodes = new Map()
 
    // Update on entity change.
    this.onEntityChange = this.onEntityChange.bind(this)
    this.el.addEventListener('componentchanged', this.onEntityChange)
  },
 
  update (oldData) {
    if (this.room && oldData.src !== this.data.src) {
      this.connectSrc(this.data.src)
    }
    this.el.sceneEl.object3D.updateMatrixWorld(true)
    this.updateSoundSettings()
    this.updatePlaybackSettings()
    this.toggleShowVisualization(oldData.visualize, this.data.visualize)
    this.updateResonancePosition().updateVisualization()
 
    const roomEl = this.getRoomChoice()
  
    if ((roomEl && roomEl.components && roomEl.components['resonance-audio-room']) !== this.room) {
      /**
       * Yes, this looks ugly. And this approach has a reason. The audio source needs the audio
       * room's matrixWorld to calculate the audio source's position relative to the room. This
       * means scene and the audio room have to be loaded (which they havent on the initial
       * update).
       */
      onceWhenLoaded(this.el.sceneEl, () => {
        const roomLeft = this.leaveRoom()
        const roomEntered = this.enter(roomEl)
        this.connectSrc(this.data.src)
        this.updateSoundSettings()
        this.el.sceneEl.object3D.updateMatrixWorld(true)
        this.updateResonancePosition().updateVisualization()
        Iif (roomLeft) {
          this.el.emit('audioroom-left', { src: this.el, room: roomLeft.el })
        }
        if (roomEntered) {
          this.el.emit('audioroom-entered', { src: this.el, room: roomEntered.el })
        }
      })
    }
  },
 
  remove () {
    this.el.removeEventListener('componentchanged', this.onEntityChange)
    this.disconnect()
    const roomLeft = this.leaveRoom()
    this.toggleShowVisualization(this.data.visualize, false)
 
    if (roomLeft) {
      this.el.emit('audioroom-left', { src: this.el, room: roomLeft.el })
    }
  },
 
  /**
   * Update the Resonance sound settings.
   */
  updateSoundSettings () {
    const s = this.resonance
    if (!s) { return }
    s.setGain(this.data.gain)
    s.setMinDistance(this.data.minDistance)
    s.setMaxDistance(this.data.maxDistance)
    s.setDirectivityPattern(this.data.directivityPattern.x, this.data.directivityPattern.y)
    s.setSourceWidth(this.data.sourceWidth)
    s.setRolloff(this.data.rolloff)
  },
 
  /**
   * Update the playback settings.
   */
  updatePlaybackSettings () {
    if (!this.connected.element) { return }
 
    // Update loop.
    Eif (this.data.loop) {
      this.sound.setAttribute('loop', 'true')
    } else {
      this.sound.removeAttribute('loop')
    }
    // Update autoplay.
    if (this.data.autoplay) {
      this.sound.setAttribute('autoplay', 'true')
    } else {
      this.sound.removeAttribute('autoplay')
    }
  },
 
  /**
   * Update the position in Google Resonance of this audio source, so relative to the audio room.
   * @returns {this}
   */
  updateResonancePosition () {
    if (this.resonance) {
      this.resonance.setFromMatrix(this.getMatrixRoom())
    }
    return this
  },
 
  /**
   * Toggle showing the visualization.
   * @param {boolean} previous - the previous setting
   * @param {boolean} current - the new setting
   */
  toggleShowVisualization (previous, current) {
    if (!previous && current) {
      this.el.setObject3D(
        visName,
        new THREE.Mesh(
          new THREE.SphereBufferGeometry(this.data.minDistance, 36, 18),
          new THREE.MeshStandardMaterial({
            color: 0xffffff,
            metalness: 0,
            wireframe: true,
            visible: true
          })
        )
      )
    } else if (previous && !current && this.el.getObject3D(visName)) {
      this.el.removeObject3D(visName)
    }
  },
 
  /**
   * Update the visualization's position, orientation and shape.
   * @returns {this}
   */
  updateVisualization () {
    const d = this.data
    let v = this.el.getObject3D(visName)
    if (d.visualize && v) {
      // If appearance changed, redraw.
      if (v.geometry.parameters.radius !== this.data.minDistance) {
        this.toggleShowVisualization(true, false)
        this.toggleShowVisualization(false, true)
        v = this.el.getObject3D(visName)
      }
      const m = this.getMatrixLocal()
      v.position.setFromMatrixPosition(m)
      v.quaternion.setFromRotationMatrix(m)
      v.material.color.setHex(this.room ? 0xffffff : 0xff0000)
      v.matrixWorldNeedsUpdate = true
    }
    return this
  },
 
  /**
   * When the entity's position or rotation is changed, update the Resonance audio position and
   * visualization accordingly.
   * @param {Event} evt
   */
  onEntityChange (evt) {
    if (evt.detail.name !== 'position' && evt.detail.name !== 'rotation') { return }
 
    this.el.sceneEl.object3D.updateMatrixWorld(true)
    this.updateResonancePosition().updateVisualization()
  },
 
  /**
   * Get the choice of audio Room. Checking order of room property:
   * - value is falsey: parent node is returned. This prevents using an empty string as query selector.
   * - value is an A-Frame entity: entity is returned.
   * - value is a string: document.querySelector result is returned. This might be null.
   * - else: parent node is returned.
   * @returns {HTMLElement|null}
   */
  getRoomChoice () {
    const ar = this.data.room
    return !ar
      ? this.el.parentNode
      : ar instanceof AFRAME.AEntity
        ? ar
        : typeof ar === 'string'
          ? document.querySelector(ar)
          : this.el.parentNode
  },
 
  /**
   * Get a copy of the matrixWorld of the audio source, taking into account any custom set position
   * or rotation, in world coordinates.
   * @return {THREE.Matrix4}
   */
  getMatrixWorld () {
    if (!isVec3Set(this.data.position) && !isVec3Set(this.data.rotation)) {
      // No custom position or rotation was set, so simply return a copy of the matrixWorld of the
      // current entity.
      return new THREE.Matrix4().copy(this.el.object3D.matrixWorld)
    } else {
      // Return matrixWorld calculated by multiplying the parent's matrixWorld and the local
      // matrix, as Three.js's Object3D.updateMatrixWorld() basically does.
      return new THREE.Matrix4().multiplyMatrices(
        this.el.parentNode.object3D.matrixWorld,
        this.getMatrixLocalCustom()
      )
    }
  },
 
  /**
   * Get the matrix in local coordinates. The position and rotation attributes (individually)
   * take precedence over any position and rotation components set on the current entity. The
   * scale is 1.
   * @returns {THREE.Matrix4}
   */
  getMatrixLocalCustom () {
    let localPosition, localQuaternion
 
    if (isVec3Set(this.data.position)) {
      localPosition = new THREE.Vector3(this.data.position.x, this.data.position.y, this.data.position.z)
    } else {
      localPosition = this.el.object3D.position
    }
 
    if (isVec3Set(this.data.rotation)) {
      let radians = [this.data.rotation.x, this.data.rotation.y, this.data.rotation.z].map(THREE.Math.degToRad)
      localQuaternion = new THREE.Quaternion().setFromEuler(new THREE.Euler().reorder('YXZ').fromArray(radians))
    } else {
      localQuaternion = this.el.object3D.quaternion
    }
 
    return new THREE.Matrix4().compose(localPosition, localQuaternion, { x: 1, y: 1, z: 1 })
  },
 
  /**
   * Get a matrix of the audio source's position and rotation relative to the audio room, taking
   * into account any custom set position or rotation.
   * @returns {THREE.Matrix4}
   */
  getMatrixRoom () {
    return this.getMatrixWorld().premultiply(
      new THREE.Matrix4().copy(this.room.el.object3D.matrixWorld).invert()
    )
  },
 
  /**
   * Get a matrix of position and rotation relative to its owner entity, taking into account any
   * custom set position or rotation.
   * @returns {THREE.Matrix4}
   */
  getMatrixLocal () {
    return this.getMatrixWorld().premultiply(
      new THREE.Matrix4().copy(this.el.object3D.matrixWorld).invert()
    )
  },
 
  /**
   * Enter an audio room. If the passed audio room has no resonance-audio-room component, show a
   * warning and return false.
   * @param {AFRAME.AEntity} roomEl - the room element
   * @returns {AFRAME.AComponent|boolean} the entered room component or false if it couldn't be
   *                                      entered
   */
  enter (roomEl) {
    if (!roomEl || !roomEl.components || !('resonance-audio-room' in roomEl.components)) {
      warn("can't enter audio room because it is no audio room")
      return false
    }
 
    // Store references to each other.
    this.room = roomEl.components['resonance-audio-room']
    this.room.store(this.el)
 
    // Create Resonance source.
    this.resonance = this.room.resonanceAudioScene.createSource()
 
    // Prepare default audio element.
    this.defaultAudioEl = document.createElement('audio')
    this.mediaAudioSourceNodes.set(
      this.defaultAudioEl, this.room.audioContext.createMediaElementSource(this.defaultAudioEl)
    )
    return this.room
  },
 
  /**
   * Leave the audio room if this audio source is in one.
   * @returns {AFRAME.AComponent|boolean} the room that was left or false if there was no room to leave
   */
  leaveRoom () {
    if (!this.room) { return false }
    const room = this.room
    this.room.forget(this.el)
    this.room = null
 
    this.resonance = null
    this.mediaAudioSourceNodes.delete(this.defaultAudioEl)
    this.defaultAudioEl = null
 
    return room
  },
 
  /**
   * Connect a HTMLMediaElement or MediaStream to the room's AudioContext.
   * @param {HTMLMediaElement|MediaStream} source - the audio source
   * @param {function} createSourceFn - the function that creates an AudioSourceNode based on the passed source
   * @returns {boolean} false if there was not source to connect
   */
  connect (source, createSourceFn) {
    // Don't connect a new source if there is none.
    Iif (!source) { return false }
 
    this.sound = source
 
    // Create new source AudioNode if source object didn't have one yet.
    if (!this.mediaAudioSourceNodes.has(this.sound)) {
      this.mediaAudioSourceNodes.set(this.sound, createSourceFn.call(this.room.audioContext, this.sound))
    }
    // Get elemenent source AudioNode.
    this.mediaAudioSourceNodes.get(this.sound).connect(this.resonance.input)
 
    return true
  },
 
  /**
   * Connect a media element to this resonance-audio-src.
   * @param {HTMLMediaElement} el - the media element
   */
  connectWithElement (el) {
    this.connected.element = this.connect(el, this.room.audioContext.createMediaElementSource)
 
    Iif (!this.connected.element) { return }
    // Warn when an element with a stream was connected.
    Iif (this.sound.srcObject) {
      warn("can't use a HTMLMediaElement that contains a stream. Connect the stream itself.")
    }
    // Apply playback settings.
    this.updatePlaybackSettings() // TODO this shouldn't be here
    // Play the audio.
    if (this.sound.getAttribute('autoplay')) {
      this.sound.play().then().catch(w => warn(w))
    }
  },
 
  /**
   * Connect a stream to this resonance-audio-src.
   * @param {MediaStream} stream - the stream
   */
  connectWithStream (stream) {
    this.connected.stream = this.connect(stream, this.room.audioContext.createMediaStreamSource)
 
    if (!this.connected.stream) { return }
    // Add play/pause API to sound that give a warning when accessed.
    const unavailable = () => warn("can't use play/pause on MediaStream. Manipulate the stream's source instead")
    this.sound.play = unavailable
    this.sound.pause = unavailable
  },
 
  /**
   * Disconnect HTMLMediaElement or MediaStream from this resonance-audio-src.
   */
  disconnect () {
    if (this.sound && this.resonance) {
      this.mediaAudioSourceNodes.get(this.sound).disconnect(this.resonance.input)
      this.sound = null
    }
    this.connected.element = false
    this.connected.stream = false
  },
 
  /**
   * Set a new source.
   * @param {string|HTMLMediaElement|MediaStream|null} src
   */
  connectSrc (src) {
    const errorMsg = 'invalid src value. Must be element id string, resource string, HTMLMediaElement or MediaStream'
 
    this.disconnect()
    let el
    if (!src) {
      // Do nothing, because we've already disconnected.
    } else Iif (src instanceof MediaStream) {
      this.connectWithStream(src)
    } else if (src instanceof HTMLMediaElement) {
      this.connectWithElement(src)
    } else Eif (typeof src === 'string') {
      if (src.charAt(0) === '#') {
        el = document.getElementById(src.substr(1))
      } else {
        el = this.defaultAudioEl
        el.setAttribute('src', src)
      }
      if (!el) { throw new TypeError(errorMsg) }
      this.connectWithElement(el)
    } else {
      throw new TypeError(errorMsg)
    }
    this.data.src = el || src
  }
})
 
AFRAME.registerPrimitive('a-resonance-audio-src', {
  defaultComponents: {
    'resonance-audio-src': {}
  },
  mappings: {
    src: 'resonance-audio-src.src',
    room: 'resonance-audio-src.room',
    loop: 'resonance-audio-src.loop',
    autoplay: 'resonance-audio-src.autoplay',
 
    gain: 'resonance-audio-src.gain',
    'min-distance': 'resonance-audio-src.minDistance',
    'max-distance': 'resonance-audio-src.maxDistaonce',
    'directivity-pattern': 'resonance-audio-src.directivityPattern',
    'source-width': 'resonance-audio-src.sourceWidth',
    rolloff: 'resonance-audio-src.rolloff',
    // The orientation and position are set by the rotation and position components, respectively.
 
    visualize: 'resonance-audio-src.visualize'
  }
})