Example #1
0
  /**
   * Calculates the panning of this Source between fully left (-1.0f) and fully right (1.0f)
   *
   * <p>Calculated internally from the relative positions of this source and the listener.
   */
  protected void calculatePan() {
    Vector3f side = new Vector3f();
    side.cross(activeAudioListener.getOrientation(UP), activeAudioListener.getOrientation(AT));
    side.normalize();
    Vector3f vecX = new Vector3f(this.getCurrentPosition());
    Vector3f vecZ = new Vector3f(this.getCurrentPosition());
    float x = vecX.dot(side);
    float z = vecZ.dot(activeAudioListener.getOrientation(AT));
    float angle = (float) Math.atan2(x, z);
    float pan = (float) -Math.sin(angle);

    // If playing, update the pan
    if (audioChannel != null) {
      audioChannel.setPan(pan);
    }
    if (log.isDebugEnabled()) {
      log.debug("Set pan of JavaSoundAudioSource " + this.getSystemName() + " to " + pan);
    }
  }
Example #2
0
  @Override
  protected void calculateGain() {

    // Calculate distance from listener
    Vector3f distance = new Vector3f(this.getCurrentPosition());
    if (!this.isPositionRelative()) {
      distance.sub(activeAudioListener.getCurrentPosition());
    }

    float distanceFromListener = (float) Math.sqrt(distance.dot(distance));
    if (log.isDebugEnabled()) {
      log.debug(
          "Distance of JavaSoundAudioSource "
              + this.getSystemName()
              + " from Listener = "
              + distanceFromListener);
    }

    // Default value to start with (used for no distance attenuation)
    float currentGain = 1.0f;

    if (InstanceManager.audioManagerInstance().getActiveAudioFactory().isDistanceAttenuated()) {
      // Calculate gain of this source using clamped inverse distance
      // attenuation model

      distanceFromListener = Math.max(distanceFromListener, this.getReferenceDistance());
      if (log.isDebugEnabled()) {
        log.debug(
            "After initial clamping, distance of JavaSoundAudioSource "
                + this.getSystemName()
                + " from Listener = "
                + distanceFromListener);
      }
      distanceFromListener = Math.min(distanceFromListener, this.getMaximumDistance());
      if (log.isDebugEnabled()) {
        log.debug(
            "After final clamping, distance of JavaSoundAudioSource "
                + this.getSystemName()
                + " from Listener = "
                + distanceFromListener);
      }

      currentGain =
          activeAudioListener.getMetersPerUnit()
              * (this.getReferenceDistance()
                  / (this.getReferenceDistance()
                      + this.getRollOffFactor()
                          * (distanceFromListener - this.getReferenceDistance())));
      if (log.isDebugEnabled()) {
        log.debug(
            "Calculated for JavaSoundAudioSource "
                + this.getSystemName()
                + " gain = "
                + currentGain);
      }

      // Ensure that gain is between 0 and 1
      if (currentGain > 1.0f) {
        currentGain = 1.0f;
      } else if (currentGain < 0.0f) {
        currentGain = 0.0f;
      }
    }

    // Finally, adjust based on master gain for this source, the gain
    // of listener and any calculated fade gains
    currentGain *= this.getGain() * activeAudioListener.getGain() * this.getFadeGain();

    // If playing, update the gain
    if (audioChannel != null) {
      audioChannel.setGain(currentGain);
      if (log.isDebugEnabled()) {
        log.debug(
            "Set current gain of JavaSoundAudioSource "
                + this.getSystemName()
                + " to "
                + currentGain);
      }
    }
  }
 public void audioChanged(AudioEvent ev) {
   for (AudioListener l : cowal) {
     l.audioChanged(ev);
   }
 }