babylon.audioSceneComponent.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. module BABYLON {
  2. // Adds the parser to the scene parsers.
  3. AbstractScene.AddParser(SceneComponentConstants.NAME_AUDIO, (parsedData: any, scene: Scene, container: AssetContainer, rootUrl: string) => {
  4. // TODO: add sound
  5. var loadedSounds: Sound[] = [];
  6. var loadedSound: Sound;
  7. container.sounds = container.sounds || [];
  8. if (parsedData.sounds !== undefined && parsedData.sounds !== null) {
  9. for (let index = 0, cache = parsedData.sounds.length; index < cache; index++) {
  10. var parsedSound = parsedData.sounds[index];
  11. if (Engine.audioEngine.canUseWebAudio) {
  12. if (!parsedSound.url) parsedSound.url = parsedSound.name;
  13. if (!loadedSounds[parsedSound.url]) {
  14. loadedSound = Sound.Parse(parsedSound, scene, rootUrl);
  15. loadedSounds[parsedSound.url] = loadedSound;
  16. container.sounds.push(loadedSound);
  17. }
  18. else {
  19. container.sounds.push(Sound.Parse(parsedSound, scene, rootUrl, loadedSounds[parsedSound.url]));
  20. }
  21. } else {
  22. container.sounds.push(new Sound(parsedSound.name, null, scene));
  23. }
  24. }
  25. }
  26. loadedSounds = [];
  27. });
  28. export interface AbstractScene {
  29. /**
  30. * The list of sounds used in the scene.
  31. */
  32. sounds: Nullable<Array<Sound>>;
  33. }
  34. export interface Scene {
  35. /**
  36. * @hidden
  37. * Backing field
  38. */
  39. _mainSoundTrack: SoundTrack;
  40. /**
  41. * The main sound track played by the scene.
  42. * It cotains your primary collection of sounds.
  43. */
  44. mainSoundTrack: SoundTrack;
  45. /**
  46. * The list of sound tracks added to the scene
  47. * @see http://doc.babylonjs.com/how_to/playing_sounds_and_music
  48. */
  49. soundTracks: Nullable<Array<SoundTrack>>;
  50. /**
  51. * Gets a sound using a given name
  52. * @param name defines the name to search for
  53. * @return the found sound or null if not found at all.
  54. */
  55. getSoundByName(name: string): Nullable<Sound>;
  56. /**
  57. * Gets or sets if audio support is enabled
  58. * @see http://doc.babylonjs.com/how_to/playing_sounds_and_music
  59. */
  60. audioEnabled: boolean;
  61. /**
  62. * Gets or sets if audio will be output to headphones
  63. * @see http://doc.babylonjs.com/how_to/playing_sounds_and_music
  64. */
  65. headphone: boolean;
  66. }
  67. Object.defineProperty(Scene.prototype, "mainSoundTrack", {
  68. get: function (this:Scene) {
  69. let compo = this._getComponent(SceneComponentConstants.NAME_AUDIO) as AudioSceneComponent;
  70. if (!compo) {
  71. compo = new AudioSceneComponent(this);
  72. this._addComponent(compo);
  73. }
  74. if (!this._mainSoundTrack) {
  75. this._mainSoundTrack = new SoundTrack(this, { mainTrack: true });
  76. }
  77. return this._mainSoundTrack;
  78. },
  79. enumerable: true,
  80. configurable: true
  81. });
  82. Scene.prototype.getSoundByName = function(name: string): Nullable<Sound> {
  83. var index: number;
  84. for (index = 0; index < this.mainSoundTrack.soundCollection.length; index++) {
  85. if (this.mainSoundTrack.soundCollection[index].name === name) {
  86. return this.mainSoundTrack.soundCollection[index];
  87. }
  88. }
  89. if (this.soundTracks) {
  90. for (var sdIndex = 0; sdIndex < this.soundTracks.length; sdIndex++) {
  91. for (index = 0; index < this.soundTracks[sdIndex].soundCollection.length; index++) {
  92. if (this.soundTracks[sdIndex].soundCollection[index].name === name) {
  93. return this.soundTracks[sdIndex].soundCollection[index];
  94. }
  95. }
  96. }
  97. }
  98. return null;
  99. }
  100. Object.defineProperty(Scene.prototype, "audioEnabled", {
  101. get: function (this:Scene) {
  102. let compo = this._getComponent(SceneComponentConstants.NAME_AUDIO) as AudioSceneComponent;
  103. if (!compo) {
  104. compo = new AudioSceneComponent(this);
  105. this._addComponent(compo);
  106. }
  107. return compo.audioEnabled;
  108. },
  109. set: function(this:Scene, value: boolean) {
  110. let compo = this._getComponent(SceneComponentConstants.NAME_AUDIO) as AudioSceneComponent;
  111. if (!compo) {
  112. compo = new AudioSceneComponent(this);
  113. this._addComponent(compo);
  114. }
  115. if (value) {
  116. compo.enableAudio();
  117. }
  118. else {
  119. compo.disableAudio();
  120. }
  121. },
  122. enumerable: true,
  123. configurable: true
  124. });
  125. Object.defineProperty(Scene.prototype, "headphone", {
  126. get: function (this:Scene) {
  127. let compo = this._getComponent(SceneComponentConstants.NAME_AUDIO) as AudioSceneComponent;
  128. if (!compo) {
  129. compo = new AudioSceneComponent(this);
  130. this._addComponent(compo);
  131. }
  132. return compo.headphone;
  133. },
  134. set: function(this:Scene, value: boolean) {
  135. let compo = this._getComponent(SceneComponentConstants.NAME_AUDIO) as AudioSceneComponent;
  136. if (!compo) {
  137. compo = new AudioSceneComponent(this);
  138. this._addComponent(compo);
  139. }
  140. if (value) {
  141. compo.switchAudioModeForHeadphones();
  142. }
  143. else {
  144. compo.switchAudioModeForNormalSpeakers();
  145. }
  146. },
  147. enumerable: true,
  148. configurable: true
  149. });
  150. /**
  151. * Defines the sound scene component responsible to manage any sounds
  152. * in a given scene.
  153. */
  154. export class AudioSceneComponent implements ISceneSerializableComponent {
  155. /**
  156. * The component name helpfull to identify the component in the list of scene components.
  157. */
  158. public readonly name = SceneComponentConstants.NAME_AUDIO;
  159. /**
  160. * The scene the component belongs to.
  161. */
  162. public scene: Scene;
  163. private _audioEnabled = true;
  164. /**
  165. * Gets whether audio is enabled or not.
  166. * Please use related enable/disable method to switch state.
  167. */
  168. public get audioEnabled(): boolean {
  169. return this._audioEnabled;
  170. }
  171. private _headphone = false;
  172. /**
  173. * Gets whether audio is outputing to headphone or not.
  174. * Please use the according Switch methods to change output.
  175. */
  176. public get headphone(): boolean {
  177. return this._headphone;
  178. }
  179. /**
  180. * Creates a new instance of the component for the given scene
  181. * @param scene Defines the scene to register the component in
  182. */
  183. constructor(scene: Scene) {
  184. this.scene = scene;
  185. scene.soundTracks = new Array<SoundTrack>();
  186. scene.sounds = new Array<Sound>();
  187. }
  188. /**
  189. * Registers the component in a given scene
  190. */
  191. public register(): void {
  192. this.scene._afterRenderStage.registerStep(SceneComponentConstants.STEP_AFTERRENDER_AUDIO, this, this._afterRender);
  193. }
  194. /**
  195. * Rebuilds the elements related to this component in case of
  196. * context lost for instance.
  197. */
  198. public rebuild(): void {
  199. // Nothing to do here. (Not rendering related)
  200. }
  201. /**
  202. * Serializes the component data to the specified json object
  203. * @param serializationObject The object to serialize to
  204. */
  205. public serialize(serializationObject: any): void {
  206. serializationObject.sounds = [];
  207. if (this.scene.soundTracks) {
  208. for (var index = 0; index < this.scene.soundTracks.length; index++) {
  209. var soundtrack = this.scene.soundTracks[index];
  210. for (var soundId = 0; soundId < soundtrack.soundCollection.length; soundId++) {
  211. serializationObject.sounds.push(soundtrack.soundCollection[soundId].serialize());
  212. }
  213. }
  214. }
  215. }
  216. /**
  217. * Adds all the element from the container to the scene
  218. * @param container the container holding the elements
  219. */
  220. public addFromContainer(container: AbstractScene): void {
  221. if (!container.sounds) {
  222. return;
  223. }
  224. container.sounds.forEach((sound) => {
  225. sound.play();
  226. sound.autoplay = true;
  227. this.scene.mainSoundTrack.AddSound(sound);
  228. });
  229. }
  230. /**
  231. * Removes all the elements in the container from the scene
  232. * @param container contains the elements to remove
  233. */
  234. public removeFromContainer(container: AbstractScene): void {
  235. if (!container.sounds) {
  236. return;
  237. }
  238. container.sounds.forEach((sound) => {
  239. sound.stop();
  240. sound.autoplay = false;
  241. this.scene.mainSoundTrack.RemoveSound(sound);
  242. });
  243. }
  244. /**
  245. * Disposes the component and the associated ressources.
  246. */
  247. public dispose(): void {
  248. const scene = this.scene;
  249. if (scene._mainSoundTrack) {
  250. scene.mainSoundTrack.dispose();
  251. }
  252. if (scene.soundTracks) {
  253. for (var scIndex = 0; scIndex < scene.soundTracks.length; scIndex++) {
  254. scene.soundTracks[scIndex].dispose();
  255. }
  256. }
  257. }
  258. /**
  259. * Disables audio in the associated scene.
  260. */
  261. public disableAudio() {
  262. const scene = this.scene;
  263. this._audioEnabled = false;
  264. let i: number;
  265. for (i = 0; i < scene.mainSoundTrack.soundCollection.length; i++) {
  266. scene.mainSoundTrack.soundCollection[i].pause();
  267. }
  268. if (scene.soundTracks) {
  269. for (i = 0; i < scene.soundTracks.length; i++) {
  270. for (var j = 0; j < scene.soundTracks[i].soundCollection.length; j++) {
  271. scene.soundTracks[i].soundCollection[j].pause();
  272. }
  273. }
  274. }
  275. }
  276. /**
  277. * Enables audio in the associated scene.
  278. */
  279. public enableAudio() {
  280. const scene = this.scene;
  281. this._audioEnabled = true;
  282. let i: number;
  283. for (i = 0; i < scene.mainSoundTrack.soundCollection.length; i++) {
  284. if (scene.mainSoundTrack.soundCollection[i].isPaused) {
  285. scene.mainSoundTrack.soundCollection[i].play();
  286. }
  287. }
  288. if (scene.soundTracks) {
  289. for (i = 0; i < scene.soundTracks.length; i++) {
  290. for (var j = 0; j < scene.soundTracks[i].soundCollection.length; j++) {
  291. if (scene.soundTracks[i].soundCollection[j].isPaused) {
  292. scene.soundTracks[i].soundCollection[j].play();
  293. }
  294. }
  295. }
  296. }
  297. }
  298. /**
  299. * Switch audio to headphone output.
  300. */
  301. public switchAudioModeForHeadphones() {
  302. const scene = this.scene;
  303. this._headphone = true;
  304. scene.mainSoundTrack.switchPanningModelToHRTF();
  305. if (scene.soundTracks) {
  306. for (var i = 0; i < scene.soundTracks.length; i++) {
  307. scene.soundTracks[i].switchPanningModelToHRTF();
  308. }
  309. }
  310. }
  311. /**
  312. * Switch audio to normal speakers.
  313. */
  314. public switchAudioModeForNormalSpeakers() {
  315. const scene = this.scene;
  316. this._headphone = false;
  317. scene.mainSoundTrack.switchPanningModelToEqualPower();
  318. if (scene.soundTracks) {
  319. for (var i = 0; i < scene.soundTracks.length; i++) {
  320. scene.soundTracks[i].switchPanningModelToEqualPower();
  321. }
  322. }
  323. }
  324. private _afterRender() {
  325. const scene = this.scene;
  326. if (!this._audioEnabled || !scene._mainSoundTrack || !scene.soundTracks || (scene._mainSoundTrack.soundCollection.length === 0 && scene.soundTracks.length === 1)) {
  327. return;
  328. }
  329. var listeningCamera: Nullable<Camera>;
  330. var audioEngine = Engine.audioEngine;
  331. if (scene.activeCameras.length > 0) {
  332. listeningCamera = scene.activeCameras[0];
  333. } else {
  334. listeningCamera = scene.activeCamera;
  335. }
  336. if (listeningCamera && audioEngine.audioContext) {
  337. audioEngine.audioContext.listener.setPosition(listeningCamera.position.x, listeningCamera.position.y, listeningCamera.position.z);
  338. // for VR cameras
  339. if (listeningCamera.rigCameras && listeningCamera.rigCameras.length > 0) {
  340. listeningCamera = listeningCamera.rigCameras[0];
  341. }
  342. var mat = Matrix.Invert(listeningCamera.getViewMatrix());
  343. var cameraDirection = Vector3.TransformNormal(new Vector3(0, 0, -1), mat);
  344. cameraDirection.normalize();
  345. // To avoid some errors on GearVR
  346. if (!isNaN(cameraDirection.x) && !isNaN(cameraDirection.y) && !isNaN(cameraDirection.z)) {
  347. audioEngine.audioContext.listener.setOrientation(cameraDirection.x, cameraDirection.y, cameraDirection.z, 0, 1, 0);
  348. }
  349. var i: number;
  350. for (i = 0; i < scene.mainSoundTrack.soundCollection.length; i++) {
  351. var sound = scene.mainSoundTrack.soundCollection[i];
  352. if (sound.useCustomAttenuation) {
  353. sound.updateDistanceFromListener();
  354. }
  355. }
  356. if (scene.soundTracks) {
  357. for (i = 0; i < scene.soundTracks.length; i++) {
  358. for (var j = 0; j < scene.soundTracks[i].soundCollection.length; j++) {
  359. sound = scene.soundTracks[i].soundCollection[j];
  360. if (sound.useCustomAttenuation) {
  361. sound.updateDistanceFromListener();
  362. }
  363. }
  364. }
  365. }
  366. }
  367. }
  368. }
  369. }