babylon.sound.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. module BABYLON {
  2. export class Sound {
  3. public name: string;
  4. public autoplay: boolean = false;
  5. public loop: boolean = false;
  6. public useCustomAttenuation: boolean = false;
  7. public soundTrackId: number;
  8. public spatialSound: boolean = false;
  9. public refDistance: number = 1;
  10. public rolloffFactor: number = 1;
  11. public maxDistance: number = 100;
  12. public distanceModel: string = "linear";
  13. public panningModel: string = "HRTF";
  14. public onended: () => any;
  15. private _playbackRate: number = 1;
  16. private _startTime: number = 0;
  17. private _startOffset: number = 0;
  18. private _position: Vector3 = Vector3.Zero();
  19. private _localDirection: Vector3 = new Vector3(1, 0, 0);
  20. private _volume: number = 1;
  21. private _isLoaded: boolean = false;
  22. private _isReadyToPlay: boolean = false;
  23. private _isPlaying: boolean = false;
  24. private _isDirectional: boolean = false;
  25. private _readyToPlayCallback: () => any;
  26. private _audioBuffer: AudioBuffer;
  27. private _soundSource: AudioBufferSourceNode;
  28. private _soundPanner: PannerNode;
  29. private _soundGain: GainNode;
  30. private _inputAudioNode: AudioNode;
  31. private _ouputAudioNode: AudioNode;
  32. // Used if you'd like to create a directional sound.
  33. // If not set, the sound will be omnidirectional
  34. private _coneInnerAngle: number = 360;
  35. private _coneOuterAngle: number = 360;
  36. private _coneOuterGain: number = 0;
  37. private _scene: Scene;
  38. private _connectedMesh: AbstractMesh;
  39. private _customAttenuationFunction: (currentVolume: number, currentDistance: number, maxDistance: number, refDistance: number, rolloffFactor: number) => number;
  40. private _registerFunc: (connectedMesh: AbstractMesh) => any;
  41. /**
  42. * Create a sound and attach it to a scene
  43. * @param name Name of your sound
  44. * @param urlOrArrayBuffer Url to the sound to load async or ArrayBuffer
  45. * @param readyToPlayCallback Provide a callback function if you'd like to load your code once the sound is ready to be played
  46. * @param options Objects to provide with the current available options: autoplay, loop, volume, spatialSound, maxDistance, rolloffFactor, refDistance, distanceModel, panningModel
  47. */
  48. constructor(name: string, urlOrArrayBuffer: any, scene: Scene, readyToPlayCallback?: () => void, options?) {
  49. this.name = name;
  50. this._scene = scene;
  51. this._readyToPlayCallback = readyToPlayCallback;
  52. // Default custom attenuation function is a linear attenuation
  53. this._customAttenuationFunction = (currentVolume: number, currentDistance: number, maxDistance: number, refDistance: number, rolloffFactor: number) => {
  54. if (currentDistance < maxDistance) {
  55. return currentVolume * (1 - currentDistance / maxDistance);
  56. }
  57. else {
  58. return 0;
  59. }
  60. };
  61. if (options) {
  62. this.autoplay = options.autoplay || false;
  63. this.loop = options.loop || false;
  64. // if volume === 0, we need another way to check this option
  65. if (options.volume !== undefined) {
  66. this._volume = options.volume;
  67. }
  68. this.spatialSound = options.spatialSound || false;
  69. this.maxDistance = options.maxDistance || 100;
  70. this.useCustomAttenuation = options.useCustomAttenuation || false;
  71. this.rolloffFactor = options.rolloffFactor || 1;
  72. this.refDistance = options.refDistance || 1;
  73. this.distanceModel = options.distanceModel || "linear";
  74. this.panningModel = options.panningModel || "HRTF";
  75. this._playbackRate = options.playbackRate || 1;
  76. }
  77. if (Engine.audioEngine.canUseWebAudio) {
  78. this._soundGain = Engine.audioEngine.audioContext.createGain();
  79. this._soundGain.gain.value = this._volume;
  80. this._inputAudioNode = this._soundGain;
  81. this._ouputAudioNode = this._soundGain;
  82. if (this.spatialSound) {
  83. this._createSpatialParameters();
  84. }
  85. this._scene.mainSoundTrack.AddSound(this);
  86. // if no parameter is passed, you need to call setAudioBuffer yourself to prepare the sound
  87. if (urlOrArrayBuffer) {
  88. // If it's an URL
  89. if (typeof (urlOrArrayBuffer) === "string") {
  90. Tools.LoadFile(urlOrArrayBuffer,(data) => { this._soundLoaded(data); }, null, null, true);
  91. }
  92. else {
  93. if (urlOrArrayBuffer instanceof ArrayBuffer) {
  94. this._soundLoaded(urlOrArrayBuffer);
  95. }
  96. else {
  97. Tools.Error("Parameter must be a URL to the sound or an ArrayBuffer of the sound.");
  98. }
  99. }
  100. }
  101. }
  102. else {
  103. // Adding an empty sound to avoid breaking audio calls for non Web Audio browsers
  104. this._scene.mainSoundTrack.AddSound(this);
  105. if (!Engine.audioEngine.WarnedWebAudioUnsupported) {
  106. BABYLON.Tools.Error("Web Audio is not supported by your browser.");
  107. Engine.audioEngine.WarnedWebAudioUnsupported = true;
  108. }
  109. }
  110. }
  111. public dispose() {
  112. if (Engine.audioEngine.canUseWebAudio && this._isReadyToPlay) {
  113. if (this._isPlaying) {
  114. this.stop();
  115. }
  116. this._isReadyToPlay = false;
  117. if (this.soundTrackId === -1) {
  118. this._scene.mainSoundTrack.RemoveSound(this);
  119. }
  120. else {
  121. this._scene.soundTracks[this.soundTrackId].RemoveSound(this);
  122. }
  123. if (this._soundGain) {
  124. this._soundGain.disconnect();
  125. this._soundGain = null;
  126. }
  127. if (this._soundPanner) {
  128. this._soundPanner.disconnect();
  129. this._soundPanner = null;
  130. }
  131. if (this._soundSource) {
  132. this._soundSource.disconnect();
  133. this._soundSource = null;
  134. }
  135. this._audioBuffer = null;
  136. if (this._connectedMesh) {
  137. this._connectedMesh.unregisterAfterWorldMatrixUpdate(this._registerFunc);
  138. this._connectedMesh = null;
  139. }
  140. }
  141. }
  142. private _soundLoaded(audioData: ArrayBuffer) {
  143. this._isLoaded = true;
  144. Engine.audioEngine.audioContext.decodeAudioData(audioData, (buffer) => {
  145. this._audioBuffer = buffer;
  146. this._isReadyToPlay = true;
  147. if (this.autoplay) { this.play(); }
  148. if (this._readyToPlayCallback) { this._readyToPlayCallback(); }
  149. }, (error) => { Tools.Error("Error while decoding audio data: " + error.err); });
  150. }
  151. public setAudioBuffer(audioBuffer: AudioBuffer): void {
  152. if (Engine.audioEngine.canUseWebAudio) {
  153. this._audioBuffer = audioBuffer;
  154. this._isReadyToPlay = true;
  155. }
  156. }
  157. public updateOptions(options) {
  158. if (options) {
  159. this.loop = options.loop || this.loop;
  160. this.maxDistance = options.maxDistance || this.maxDistance;
  161. this.useCustomAttenuation = options.useCustomAttenuation || this.useCustomAttenuation;
  162. this.rolloffFactor = options.rolloffFactor || this.rolloffFactor;
  163. this.refDistance = options.refDistance || this.refDistance;
  164. this.distanceModel = options.distanceModel || this.distanceModel;
  165. this.panningModel = options.panningModel || this.panningModel;
  166. this._playbackRate = options.playbackRate || this._playbackRate;
  167. }
  168. }
  169. private _createSpatialParameters() {
  170. if (Engine.audioEngine.canUseWebAudio) {
  171. this._soundPanner = Engine.audioEngine.audioContext.createPanner();
  172. if (this.useCustomAttenuation) {
  173. // Tricks to disable in a way embedded Web Audio attenuation
  174. this._soundPanner.distanceModel = "linear";
  175. this._soundPanner.maxDistance = Number.MAX_VALUE;
  176. this._soundPanner.refDistance = 1;
  177. this._soundPanner.rolloffFactor = 1;
  178. this._soundPanner.panningModel = "HRTF";
  179. }
  180. else {
  181. this._soundPanner.distanceModel = this.distanceModel;
  182. this._soundPanner.maxDistance = this.maxDistance;
  183. this._soundPanner.refDistance = this.refDistance;
  184. this._soundPanner.rolloffFactor = this.rolloffFactor;
  185. this._soundPanner.panningModel = this.panningModel;
  186. }
  187. this._soundPanner.connect(this._ouputAudioNode);
  188. this._inputAudioNode = this._soundPanner;
  189. }
  190. }
  191. public connectToSoundTrackAudioNode(soundTrackAudioNode: AudioNode) {
  192. if (Engine.audioEngine.canUseWebAudio) {
  193. this._ouputAudioNode.disconnect();
  194. this._ouputAudioNode.connect(soundTrackAudioNode);
  195. }
  196. }
  197. /**
  198. * Transform this sound into a directional source
  199. * @param coneInnerAngle Size of the inner cone in degree
  200. * @param coneOuterAngle Size of the outer cone in degree
  201. * @param coneOuterGain Volume of the sound outside the outer cone (between 0.0 and 1.0)
  202. */
  203. public setDirectionalCone(coneInnerAngle: number, coneOuterAngle: number, coneOuterGain: number) {
  204. if (coneOuterAngle < coneInnerAngle) {
  205. Tools.Error("setDirectionalCone(): outer angle of the cone must be superior or equal to the inner angle.");
  206. return;
  207. }
  208. this._coneInnerAngle = coneInnerAngle;
  209. this._coneOuterAngle = coneOuterAngle;
  210. this._coneOuterGain = coneOuterGain;
  211. this._isDirectional = true;
  212. if (this._isPlaying && this.loop) {
  213. this.stop();
  214. this.play();
  215. }
  216. }
  217. public setPosition(newPosition: Vector3) {
  218. this._position = newPosition;
  219. if (this._isPlaying && this.spatialSound) {
  220. this._soundPanner.setPosition(this._position.x, this._position.y, this._position.z);
  221. }
  222. }
  223. public setLocalDirectionToMesh(newLocalDirection: Vector3) {
  224. this._localDirection = newLocalDirection;
  225. if (this._connectedMesh && this._isPlaying) {
  226. this._updateDirection();
  227. }
  228. }
  229. private _updateDirection() {
  230. var mat = this._connectedMesh.getWorldMatrix();
  231. var direction = Vector3.TransformNormal(this._localDirection, mat);
  232. direction.normalize();
  233. this._soundPanner.setOrientation(direction.x, direction.y, direction.z);
  234. }
  235. public updateDistanceFromListener() {
  236. if (this._connectedMesh && this.useCustomAttenuation) {
  237. var distance = this._connectedMesh.getDistanceToCamera(this._scene.activeCamera);
  238. this._soundGain.gain.value = this._customAttenuationFunction(this._volume, distance, this.maxDistance, this.refDistance, this.rolloffFactor);
  239. }
  240. }
  241. public setAttenuationFunction(callback: (currentVolume: number, currentDistance: number, maxDistance: number, refDistance: number, rolloffFactor: number) => number) {
  242. this._customAttenuationFunction = callback;
  243. }
  244. /**
  245. * Play the sound
  246. * @param time (optional) Start the sound after X seconds. Start immediately (0) by default.
  247. */
  248. public play(time?: number) {
  249. if (this._isReadyToPlay) {
  250. try {
  251. var startTime = time ? Engine.audioEngine.audioContext.currentTime + time : 0;
  252. if (!this._soundSource) {
  253. if (this.spatialSound) {
  254. this._soundPanner.setPosition(this._position.x, this._position.y, this._position.z);
  255. if (this._isDirectional) {
  256. this._soundPanner.coneInnerAngle = this._coneInnerAngle;
  257. this._soundPanner.coneOuterAngle = this._coneOuterAngle;
  258. this._soundPanner.coneOuterGain = this._coneOuterGain;
  259. if (this._connectedMesh) {
  260. this._updateDirection();
  261. }
  262. else {
  263. this._soundPanner.setOrientation(this._localDirection.x, this._localDirection.y, this._localDirection.z);
  264. }
  265. }
  266. }
  267. }
  268. this._soundSource = Engine.audioEngine.audioContext.createBufferSource();
  269. this._soundSource.buffer = this._audioBuffer;
  270. this._soundSource.connect(this._inputAudioNode);
  271. this._soundSource.loop = this.loop;
  272. this._soundSource.playbackRate.value = this._playbackRate;
  273. this._startTime = startTime;
  274. if (this.onended) {
  275. this._soundSource.onended = this.onended;
  276. }
  277. this._soundSource.start(startTime, this._startOffset % this._soundSource.buffer.duration);
  278. this._isPlaying = true;
  279. }
  280. catch (ex) {
  281. Tools.Error("Error while trying to play audio: " + this.name + ", " + ex.message);
  282. }
  283. }
  284. }
  285. /**
  286. * Stop the sound
  287. * @param time (optional) Stop the sound after X seconds. Stop immediately (0) by default.
  288. */
  289. public stop(time?: number) {
  290. if (this._isPlaying) {
  291. var stopTime = time ? Engine.audioEngine.audioContext.currentTime + time : 0;
  292. this._soundSource.stop(stopTime);
  293. this._isPlaying = false;
  294. }
  295. }
  296. public pause() {
  297. if (this._isPlaying) {
  298. this._soundSource.stop(0);
  299. this._startOffset += Engine.audioEngine.audioContext.currentTime - this._startTime;
  300. }
  301. }
  302. public setVolume(newVolume: number, time?: number) {
  303. if (Engine.audioEngine.canUseWebAudio) {
  304. if (time) {
  305. this._soundGain.gain.linearRampToValueAtTime(this._volume, Engine.audioEngine.audioContext.currentTime);
  306. this._soundGain.gain.linearRampToValueAtTime(newVolume, time);
  307. }
  308. else {
  309. this._soundGain.gain.value = newVolume;
  310. }
  311. }
  312. this._volume = newVolume;
  313. }
  314. public setPlaybackRate(newPlaybackRate: number) {
  315. this._playbackRate = newPlaybackRate;
  316. if (this._isPlaying) {
  317. this._soundSource.playbackRate.value = this._playbackRate;
  318. }
  319. }
  320. public getVolume(): number {
  321. return this._volume;
  322. }
  323. public attachToMesh(meshToConnectTo: AbstractMesh) {
  324. this._connectedMesh = meshToConnectTo;
  325. if (!this.spatialSound) {
  326. this._createSpatialParameters();
  327. this.spatialSound = true;
  328. if (this._isPlaying && this.loop) {
  329. this.stop();
  330. this.play();
  331. }
  332. }
  333. this._onRegisterAfterWorldMatrixUpdate(this._connectedMesh);
  334. this._registerFunc = (connectedMesh: AbstractMesh) => this._onRegisterAfterWorldMatrixUpdate(connectedMesh);
  335. meshToConnectTo.registerAfterWorldMatrixUpdate(this._registerFunc);
  336. }
  337. private _onRegisterAfterWorldMatrixUpdate(connectedMesh: AbstractMesh) {
  338. this.setPosition(connectedMesh.getBoundingInfo().boundingSphere.centerWorld);
  339. if (this._isDirectional && this._isPlaying) {
  340. this._updateDirection();
  341. }
  342. }
  343. }
  344. }