babylon.sound.ts 18 KB

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