play3d method

SoundHandle play3d(
  1. AudioSource sound,
  2. double posX,
  3. double posY,
  4. double posZ, {
  5. double velX = 0,
  6. double velY = 0,
  7. double velZ = 0,
  8. int busId = 0,
  9. double volume = 1,
  10. bool paused = false,
  11. bool looping = false,
  12. Duration loopingStartAt = Duration.zero,
  13. Duration? loopingEndAt,
  14. int? loopingStartOffsetAt,
  15. int? loopingEndOffsetAt,
  16. double scale = 1,
})

This function is the 3D version of the play call.

The coordinate system is right handed.

          Y
          ^
          |
          |
          |
          --------> X
         /
        /
       Z

The listener position is (0, 0, 0) by default but can be changed with set3dListenerParameters.

The parameters posX, posY and posZ are the audio source's position coordinates.

The parameters velX, velY and velZ are the audio source's velocity. Defaults to (0, 0, 0).

busId is the bus on which to play the sound. By default it is 0, which means the sound will be played on the default player engine. If a mixing bus has already been created, you can provide its busId to play the sound on that bus or you can use the comfy Bus.play method. See Bus for more information.

The rest of the parameters are equivalent to the non-3D version of this method (play).

As with play, the start of the sound is quantized to output buffer boundaries. Use play3dClocked when the timing of the sounds matters (scheduled or rhythmic playback); use play3d for one-shot, reactive 3D sounds where the lowest latency is preferred.

Note: by default, the maximum number of sounds you can play is 16 and it can be changed with setMaxActiveVoiceCount. If this limit is reached and other instances of the same sound are played, the oldest one will be stopped to make room to play the new sound. If there are no instances of the sound and the max limit is reached, a warning will be printed and the sound will not play. This is not an error: no exception is thrown and the returned handle does not address any voice.

An unpaused voice requests output-device startup only after it has been created successfully, and that startup runs off the UI thread. This method therefore does not report output-device failures; with paused set to true no device is requested at all. Use startAudioDevice when you need to observe a device-start failure.

scale relative playback speed multiplier (1.0 = normal speed).

loopingStartOffsetAt optional exact frame offset to restart looping from.

loopingEndOffsetAt optional exact frame offset to loop before. Note: frame offset looping and Duration-based looping are mutually exclusive.

Returns the SoundHandle of this new sound.

Throws SoLoudNotInitializedException if the engine is not initialized.

Throws ArgumentError if posX, posY, posZ, velX, velY, velZ, volume or scale is NaN or infinite.

Throws SoLoudBufferStreamCanBePlayedOnlyOnceCppException if we try to play a BufferStream using release buffer type more than once.

Throws SoLoudFailedToStartPlaybackCppException if the audio engine could not create a voice for this sound.

Throws SoLoudFileNotFoundException if sound streams from a file (LoadMode.disk) that has been deleted or moved since it was loaded.

Implementation

SoundHandle play3d(
  AudioSource sound,
  double posX,
  double posY,
  double posZ, {
  double velX = 0,
  double velY = 0,
  double velZ = 0,
  int busId = 0,
  double volume = 1,
  bool paused = false,
  bool looping = false,
  Duration loopingStartAt = Duration.zero,
  Duration? loopingEndAt,
  int? loopingStartOffsetAt,
  int? loopingEndOffsetAt,
  double scale = 1,
}) {
  if (!isInitialized) {
    throw const SoLoudNotInitializedException();
  }
  checkFinite(posX, 'posX');
  checkFinite(posY, 'posY');
  checkFinite(posZ, 'posZ');
  checkFinite(velX, 'velX');
  checkFinite(velY, 'velY');
  checkFinite(velZ, 'velZ');
  checkFinite(volume, 'volume');
  checkFinite(scale, 'scale');
  validateLoopRegion(
    start: loopingStartAt,
    end: loopingEndAt,
    startOffset: loopingStartOffsetAt,
    endOffset: loopingEndOffsetAt,
  );

  final ret = _controller.soLoudFFI.play3d(
    sound.soundHash,
    posX,
    posY,
    posZ,
    velX: velX,
    velY: velY,
    velZ: velZ,
    busId: busId,
    volume: volume,
    paused: paused,
    looping: looping,
    loopingStartAt: loopingStartAt,
    loopingEndAt: loopingEndAt,
    loopingStartOffsetAt: loopingStartOffsetAt,
    loopingEndOffsetAt: loopingEndOffsetAt,
    scale: scale,
  );

  if (!_checkPlaybackResult(ret, from: 'play3d()')) {
    // Non-blocking failure: nothing is playing, so don't register
    // the zeroed handle against the audio source.
    return ret.newHandle;
  }

  _registerHandle(sound, ret.newHandle, from: 'play3d()');
  return ret.newHandle;
}