sound.d.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import { Observable } from "../Misc/observable";
  2. import { Vector3 } from "../Maths/math.vector";
  3. import type { Nullable } from "../types";
  4. import type { Scene } from "../scene";
  5. import type { TransformNode } from "../Meshes/transformNode";
  6. import type { ISoundOptions } from "./Interfaces/ISoundOptions";
  7. /**
  8. * Defines a sound that can be played in the application.
  9. * The sound can either be an ambient track or a simple sound played in reaction to a user action.
  10. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic
  11. */
  12. export declare class Sound {
  13. /**
  14. * The name of the sound in the scene.
  15. */
  16. name: string;
  17. /**
  18. * Does the sound autoplay once loaded.
  19. */
  20. autoplay: boolean;
  21. private _loop;
  22. /**
  23. * Does the sound loop after it finishes playing once.
  24. */
  25. get loop(): boolean;
  26. set loop(value: boolean);
  27. /**
  28. * Does the sound use a custom attenuation curve to simulate the falloff
  29. * happening when the source gets further away from the camera.
  30. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-your-own-custom-attenuation-function
  31. */
  32. useCustomAttenuation: boolean;
  33. /**
  34. * The sound track id this sound belongs to.
  35. */
  36. soundTrackId: number;
  37. /**
  38. * Is this sound currently played.
  39. */
  40. isPlaying: boolean;
  41. /**
  42. * Is this sound currently paused.
  43. */
  44. isPaused: boolean;
  45. /**
  46. * Define the reference distance the sound should be heard perfectly.
  47. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-a-spatial-3d-sound
  48. */
  49. refDistance: number;
  50. /**
  51. * Define the roll off factor of spatial sounds.
  52. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-a-spatial-3d-sound
  53. */
  54. rolloffFactor: number;
  55. /**
  56. * Define the max distance the sound should be heard (intensity just became 0 at this point).
  57. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-a-spatial-3d-sound
  58. */
  59. maxDistance: number;
  60. /**
  61. * Define the distance attenuation model the sound will follow.
  62. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-a-spatial-3d-sound
  63. */
  64. distanceModel: string;
  65. /**
  66. * @internal
  67. * Back Compat
  68. **/
  69. onended: () => any;
  70. /**
  71. * Gets or sets an object used to store user defined information for the sound.
  72. */
  73. metadata: any;
  74. /**
  75. * Observable event when the current playing sound finishes.
  76. */
  77. onEndedObservable: Observable<Sound>;
  78. /**
  79. * Gets the current time for the sound.
  80. */
  81. get currentTime(): number;
  82. /**
  83. * Does this sound enables spatial sound.
  84. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-a-spatial-3d-sound
  85. */
  86. get spatialSound(): boolean;
  87. /**
  88. * Does this sound enables spatial sound.
  89. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-a-spatial-3d-sound
  90. */
  91. set spatialSound(newValue: boolean);
  92. private _spatialSound;
  93. private _panningModel;
  94. private _playbackRate;
  95. private _streaming;
  96. private _startTime;
  97. private _currentTime;
  98. private _position;
  99. private _localDirection;
  100. private _volume;
  101. private _isReadyToPlay;
  102. private _isDirectional;
  103. private _readyToPlayCallback;
  104. private _audioBuffer;
  105. private _soundSource;
  106. private _streamingSource;
  107. private _soundPanner;
  108. private _soundGain;
  109. private _inputAudioNode;
  110. private _outputAudioNode;
  111. private _coneInnerAngle;
  112. private _coneOuterAngle;
  113. private _coneOuterGain;
  114. private _scene;
  115. private _connectedTransformNode;
  116. private _customAttenuationFunction;
  117. private _registerFunc;
  118. private _isOutputConnected;
  119. private _htmlAudioElement;
  120. private _urlType;
  121. private _length?;
  122. private _offset?;
  123. private _tryToPlayTimeout;
  124. private _audioUnlockedObserver?;
  125. private _url?;
  126. /**
  127. * @internal
  128. */
  129. static _SceneComponentInitialization: (scene: Scene) => void;
  130. /**
  131. * Create a sound and attach it to a scene
  132. * @param name Name of your sound
  133. * @param urlOrArrayBuffer Url to the sound to load async or ArrayBuffer, it also works with MediaStreams and AudioBuffers
  134. * @param scene defines the scene the sound belongs to
  135. * @param readyToPlayCallback Provide a callback function if you'd like to load your code once the sound is ready to be played
  136. * @param options Objects to provide with the current available options: autoplay, loop, volume, spatialSound, maxDistance, rolloffFactor, refDistance, distanceModel, panningModel, streaming
  137. */
  138. constructor(name: string, urlOrArrayBuffer: any, scene?: Nullable<Scene>, readyToPlayCallback?: Nullable<() => void>, options?: ISoundOptions);
  139. /**
  140. * Release the sound and its associated resources
  141. */
  142. dispose(): void;
  143. /**
  144. * Gets if the sounds is ready to be played or not.
  145. * @returns true if ready, otherwise false
  146. */
  147. isReady(): boolean;
  148. /**
  149. * Get the current class name.
  150. * @returns current class name
  151. */
  152. getClassName(): string;
  153. private _audioBufferLoaded;
  154. private _soundLoaded;
  155. /**
  156. * Sets the data of the sound from an audiobuffer
  157. * @param audioBuffer The audioBuffer containing the data
  158. */
  159. setAudioBuffer(audioBuffer: AudioBuffer): void;
  160. /**
  161. * Updates the current sounds options such as maxdistance, loop...
  162. * @param options A JSON object containing values named as the object properties
  163. */
  164. updateOptions(options: ISoundOptions): void;
  165. private _createSpatialParameters;
  166. private _disableSpatialSound;
  167. private _updateSpatialParameters;
  168. /**
  169. * Switch the panning model to HRTF:
  170. * Renders a stereo output of higher quality than equalpower — it uses a convolution with measured impulse responses from human subjects.
  171. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-a-spatial-3d-sound
  172. */
  173. switchPanningModelToHRTF(): void;
  174. /**
  175. * Switch the panning model to Equal Power:
  176. * Represents the equal-power panning algorithm, generally regarded as simple and efficient. equalpower is the default value.
  177. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-a-spatial-3d-sound
  178. */
  179. switchPanningModelToEqualPower(): void;
  180. private _switchPanningModel;
  181. /**
  182. * Connect this sound to a sound track audio node like gain...
  183. * @param soundTrackAudioNode the sound track audio node to connect to
  184. */
  185. connectToSoundTrackAudioNode(soundTrackAudioNode: AudioNode): void;
  186. /**
  187. * Transform this sound into a directional source
  188. * @param coneInnerAngle Size of the inner cone in degree
  189. * @param coneOuterAngle Size of the outer cone in degree
  190. * @param coneOuterGain Volume of the sound outside the outer cone (between 0.0 and 1.0)
  191. */
  192. setDirectionalCone(coneInnerAngle: number, coneOuterAngle: number, coneOuterGain: number): void;
  193. /**
  194. * Gets or sets the inner angle for the directional cone.
  195. */
  196. get directionalConeInnerAngle(): number;
  197. /**
  198. * Gets or sets the inner angle for the directional cone.
  199. */
  200. set directionalConeInnerAngle(value: number);
  201. /**
  202. * Gets or sets the outer angle for the directional cone.
  203. */
  204. get directionalConeOuterAngle(): number;
  205. /**
  206. * Gets or sets the outer angle for the directional cone.
  207. */
  208. set directionalConeOuterAngle(value: number);
  209. /**
  210. * Sets the position of the emitter if spatial sound is enabled
  211. * @param newPosition Defines the new position
  212. */
  213. setPosition(newPosition: Vector3): void;
  214. /**
  215. * Sets the local direction of the emitter if spatial sound is enabled
  216. * @param newLocalDirection Defines the new local direction
  217. */
  218. setLocalDirectionToMesh(newLocalDirection: Vector3): void;
  219. private _updateDirection;
  220. /** @internal */
  221. updateDistanceFromListener(): void;
  222. /**
  223. * Sets a new custom attenuation function for the sound.
  224. * @param callback Defines the function used for the attenuation
  225. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-your-own-custom-attenuation-function
  226. */
  227. setAttenuationFunction(callback: (currentVolume: number, currentDistance: number, maxDistance: number, refDistance: number, rolloffFactor: number) => number): void;
  228. /**
  229. * Play the sound
  230. * @param time (optional) Start the sound after X seconds. Start immediately (0) by default.
  231. * @param offset (optional) Start the sound at a specific time in seconds
  232. * @param length (optional) Sound duration (in seconds)
  233. */
  234. play(time?: number, offset?: number, length?: number): void;
  235. private _onended;
  236. /**
  237. * Stop the sound
  238. * @param time (optional) Stop the sound after X seconds. Stop immediately (0) by default.
  239. */
  240. stop(time?: number): void;
  241. /**
  242. * Put the sound in pause
  243. */
  244. pause(): void;
  245. /**
  246. * Sets a dedicated volume for this sounds
  247. * @param newVolume Define the new volume of the sound
  248. * @param time Define time for gradual change to new volume
  249. */
  250. setVolume(newVolume: number, time?: number): void;
  251. /**
  252. * Set the sound play back rate
  253. * @param newPlaybackRate Define the playback rate the sound should be played at
  254. */
  255. setPlaybackRate(newPlaybackRate: number): void;
  256. /**
  257. * Gets the sound play back rate.
  258. * @returns the play back rate of the sound
  259. */
  260. getPlaybackRate(): number;
  261. /**
  262. * Gets the volume of the sound.
  263. * @returns the volume of the sound
  264. */
  265. getVolume(): number;
  266. /**
  267. * Attach the sound to a dedicated mesh
  268. * @param transformNode The transform node to connect the sound with
  269. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#attaching-a-sound-to-a-mesh
  270. */
  271. attachToMesh(transformNode: TransformNode): void;
  272. /**
  273. * Detach the sound from the previously attached mesh
  274. * @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#attaching-a-sound-to-a-mesh
  275. */
  276. detachFromMesh(): void;
  277. private _onRegisterAfterWorldMatrixUpdate;
  278. /**
  279. * Clone the current sound in the scene.
  280. * @returns the new sound clone
  281. */
  282. clone(): Nullable<Sound>;
  283. /**
  284. * Gets the current underlying audio buffer containing the data
  285. * @returns the audio buffer
  286. */
  287. getAudioBuffer(): Nullable<AudioBuffer>;
  288. /**
  289. * Gets the WebAudio AudioBufferSourceNode, lets you keep track of and stop instances of this Sound.
  290. * @returns the source node
  291. */
  292. getSoundSource(): Nullable<AudioBufferSourceNode>;
  293. /**
  294. * Gets the WebAudio GainNode, gives you precise control over the gain of instances of this Sound.
  295. * @returns the gain node
  296. */
  297. getSoundGain(): Nullable<GainNode>;
  298. /**
  299. * Serializes the Sound in a JSON representation
  300. * @returns the JSON representation of the sound
  301. */
  302. serialize(): any;
  303. /**
  304. * Parse a JSON representation of a sound to instantiate in a given scene
  305. * @param parsedSound Define the JSON representation of the sound (usually coming from the serialize method)
  306. * @param scene Define the scene the new parsed sound should be created in
  307. * @param rootUrl Define the rooturl of the load in case we need to fetch relative dependencies
  308. * @param sourceSound Define a sound place holder if do not need to instantiate a new one
  309. * @returns the newly parsed sound
  310. */
  311. static Parse(parsedSound: any, scene: Scene, rootUrl: string, sourceSound?: Sound): Sound;
  312. private _setOffset;
  313. private _clearTimeoutsAndObservers;
  314. }