babylon.sound.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Sound = (function () {
  4. function Sound(url, engine, readyToPlayCallback, distanceMax, autoplay, loop) {
  5. var _this = this;
  6. this.distanceMax = 10;
  7. this.autoplay = false;
  8. this.loop = false;
  9. this._position = BABYLON.Vector3.Zero();
  10. this._orientation = BABYLON.Vector3.Zero();
  11. this._isLoaded = false;
  12. this._isReadyToPlay = false;
  13. this._audioEngine = engine.getAudioEngine();
  14. this._readyToPlayCallback = readyToPlayCallback;
  15. if (distanceMax)
  16. this.distanceMax = distanceMax;
  17. if (autoplay)
  18. this.autoplay = autoplay;
  19. if (loop)
  20. this.loop = loop;
  21. if (this._audioEngine.canUseWebAudio) {
  22. BABYLON.Tools.LoadFile(url, function (data) {
  23. _this._soundLoaded(data);
  24. }, null, null, true);
  25. }
  26. }
  27. Sound.prototype.setPosition = function (newPosition) {
  28. this._position = newPosition;
  29. if (this._isReadyToPlay) {
  30. this._soundPanner.setPosition(this._position.x, this._position.y, this._position.z);
  31. }
  32. };
  33. Sound.prototype.setOrientiation = function (newOrientation) {
  34. this._orientation = newOrientation;
  35. if (this._isReadyToPlay) {
  36. this._soundPanner.setOrientation(this._orientation.x, this._orientation.y, this._orientation.z);
  37. }
  38. };
  39. Sound.prototype.play = function () {
  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. Sound.prototype.stop = function () {
  52. };
  53. Sound.prototype.pause = function () {
  54. };
  55. Sound.prototype.attachToMesh = function (meshToConnectTo) {
  56. var _this = this;
  57. meshToConnectTo.registerAfterWorldMatrixUpdate(function (connectedMesh) {
  58. return _this._onRegisterAfterWorldMatrixUpdate(connectedMesh);
  59. });
  60. };
  61. Sound.prototype._onRegisterAfterWorldMatrixUpdate = function (connectedMesh) {
  62. this.setPosition(connectedMesh.position);
  63. };
  64. Sound.prototype._soundLoaded = function (audioData) {
  65. var _this = this;
  66. this._isLoaded = true;
  67. this._audioEngine.audioContext.decodeAudioData(audioData, function (buffer) {
  68. _this._audioBuffer = buffer;
  69. _this._isReadyToPlay = true;
  70. if (_this.autoplay) {
  71. _this.play();
  72. }
  73. if (_this._readyToPlayCallback) {
  74. _this._readyToPlayCallback();
  75. }
  76. }, function (error) {
  77. BABYLON.Tools.Error("Error while decoding audio data: " + error.err);
  78. });
  79. };
  80. return Sound;
  81. })();
  82. BABYLON.Sound = Sound;
  83. })(BABYLON || (BABYLON = {}));
  84. //# sourceMappingURL=babylon.sound.js.map