babylon.sound.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. module BABYLON {
  2. export class Sound {
  3. private _audioBuffer;
  4. public distanceMax: number = 10;
  5. public autoplay: boolean = false;
  6. public loop: boolean = false;
  7. private _position: Vector3 = Vector3.Zero();
  8. private _orientation: Vector3 = Vector3.Zero();
  9. private _volume: number;
  10. private _currentVolume: number;
  11. private _isLoaded: boolean = false;
  12. private _isReadyToPlay: boolean = false;
  13. private _audioEngine: BABYLON.AudioEngine;
  14. private _readyToPlayCallback;
  15. private _soundSource: AudioBufferSourceNode;
  16. private _soundPanner: PannerNode;
  17. constructor(url: string, engine: BABYLON.Engine, readyToPlayCallback, distanceMax?: number, autoplay?: boolean, loop?: boolean) {
  18. this._audioEngine = engine.getAudioEngine();;
  19. this._readyToPlayCallback = readyToPlayCallback;
  20. if (distanceMax) this.distanceMax = distanceMax;
  21. if (autoplay) this.autoplay = autoplay;
  22. if (loop) this.loop = loop;
  23. if (this._audioEngine.canUseWebAudio) {
  24. BABYLON.Tools.LoadFile(url, (data) => { this._soundLoaded(data); }, null, null, true);
  25. }
  26. }
  27. public setPosition(newPosition: Vector3) {
  28. this._position = newPosition;
  29. if (this._isReadyToPlay) {
  30. this._soundPanner.setPosition(this._position.x, this._position.y, this._position.z);
  31. }
  32. }
  33. public setOrientiation(newOrientation: Vector3) {
  34. this._orientation = newOrientation;
  35. if (this._isReadyToPlay) {
  36. this._soundPanner.setOrientation(this._orientation.x, this._orientation.y, this._orientation.z);
  37. }
  38. }
  39. public play() {
  40. if (this._isReadyToPlay) {
  41. this._soundSource = this._audioEngine.audioContext.createBufferSource();
  42. this._soundSource.buffer = this._audioBuffer;
  43. this._soundPanner = this._audioEngine.audioContext.createPanner();
  44. this._soundPanner.setPosition(this._position.x, this._position.y, this._position.z);
  45. this._soundPanner.connect(this._audioEngine.masterGain);
  46. this._soundSource.connect(this._soundPanner);
  47. this._soundSource.loop = this.loop;
  48. this._soundSource.start(0);
  49. }
  50. }
  51. public stop() {
  52. }
  53. public pause() {
  54. }
  55. public attachToMesh(meshToConnectTo: BABYLON.AbstractMesh) {
  56. meshToConnectTo.registerAfterWorldMatrixUpdate((connectedMesh: BABYLON.AbstractMesh) => this._onRegisterAfterWorldMatrixUpdate(connectedMesh));
  57. }
  58. private _onRegisterAfterWorldMatrixUpdate(connectedMesh: BABYLON.AbstractMesh) {
  59. this.setPosition(connectedMesh.position);
  60. }
  61. private _soundLoaded(audioData: ArrayBuffer) {
  62. this._isLoaded = true;
  63. this._audioEngine.audioContext.decodeAudioData(audioData, (buffer) => {
  64. this._audioBuffer = buffer;
  65. this._isReadyToPlay = true;
  66. if (this.autoplay) { this.play(); }
  67. if (this._readyToPlayCallback) { this._readyToPlayCallback(); }
  68. }, function (error) {
  69. BABYLON.Tools.Error("Error while decoding audio data: " + error.err);
  70. });
  71. }
  72. }
  73. }