babylon.sound.ts 18 KB

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