audio.adoc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. = Audio in jME3
  2. :revnumber: 2.0
  3. :revdate: 2020/07/22
  4. :keywords: sound, documentation, environment
  5. Place audio files in the `assets/Sound/` directory of your project. jME3 supports Ogg Vorbis audio compression (.ogg) and uncompressed PCM Wave (.wav) formats. You can use for example link:https://www.audacityteam.org/download/[Audacity] to convert from other formats.
  6. == Audio Terminology
  7. * *Streaming:* There are two ways to load audio data: Short audio files are to be stored entirely in memory (prebuffered), while long audio files, such as music, are streamed from the hard drive as it is played.
  8. * *Looping:* You can play a sound either once and then stop, or repeatedly (continuously) in a loop. +
  9. You cannot loop streamed sounds.
  10. * *Instance:* If you play the same audio twice, the playing is queued up and jME plays one after the other. If you play instances of sounds, several instances of the same sound can play at the same time.
  11. == Creating Audio Nodes: Streamed or Buffered
  12. The main jME audio class to look at is `com.jme3.audio.AudioNode`. When creating a new audio node you need to declare whether how you want to load this sound:
  13. * *Buffered:* By default, a new audio node is buffered. This means jME3 loads the whole file into memory before playing. Use this for short sounds. You create a buffered sound by setting DataType.Buffer, or using no DataType at all:
  14. [source,java]
  15. ----
  16. AudioNode boom = new AudioNode(assetManager, "Sound/boom.wav");
  17. AudioNode boom = new AudioNode(assetManager, "Sound/boom.wav", DataType.Buffer);
  18. ----
  19. * *Streamed:* If it is a long file such as music or a dialog, you stream the audio. Streaming means, you load and play in parallel until the sound is done. You cannot loop streams. You create a streamed sound by setting the boolean to true:
  20. [source,java]
  21. ----
  22. AudioNode music = new AudioNode(assetManager, "Sound/music.wav", DataType.Stream);
  23. ----
  24. == Getting AudioNode Properties
  25. [cols="2", options="header"]
  26. |===
  27. a|AudioNode Method
  28. a|Usage
  29. a|getStatus()
  30. a|Returns either AudioSource.Status.Playing, AudioSource.Status.Stopped, or AudioSource.Status.Paused.
  31. a|getVolume()
  32. a|Returns the volume.
  33. a|getPitch()
  34. a|Returns the pitch.
  35. |===
  36. [NOTE]
  37. ====
  38. There are other obvious getters to poll the status of all corresponding setters listed here.
  39. ====
  40. == Setting AudioNode Properties
  41. [cols="2", options="header"]
  42. |===
  43. a|AudioNode Method
  44. a|Usage
  45. a|setTimeOffset(0.5f)
  46. a|Play the sound starting at a 0.5 second offset from the beginning. Default is 0.
  47. a|setPitch(1)
  48. a|Makes the sound play in a higher or lower pitch. Default is 1. 2 is twice as high, .5f is half as high.
  49. a|setVolume(1)
  50. a|Sets the volume gain. 1 is the default volume, 2 is twice as loud, etc. 0 is silent/mute.
  51. a|setRefDistance(50f)
  52. a|The reference distance controls how far a sound can still be heard at 50% of its original volume (_this is assuming an exponential fall-off!_). A sound with a high RefDist can be heard loud over wide distances; a sound with a low refDist can only be heard when the listener is close by. Default is 10 world units.
  53. a|setMaxDistance(100f)
  54. a| The 'maximum attenuation distance' specifies how far from the source the sound stops growing more quiet (sounds in nature don't do that). Set this to a smaller value to keep the sound loud even at a distance; set this to higher value to let the sound fade out quickly. Default is 20 world units.
  55. a|setLooping(false)
  56. a|Configures the sound so that, if it is played, it plays once and stops. No looping is the default.
  57. |===
  58. === Looping & Ambient Sounds
  59. [cols="2", options="header"]
  60. |===
  61. a|AudioNode Method
  62. a|Usage
  63. a|setPositional(false) +
  64. setDirectional(false)
  65. a|All 3D effects switched off. This sound is global and plays in headspace (it appears to come from everywhere). Good for environmental ambient sounds and background music.
  66. a|setLooping(true)
  67. a|Configures the sound to be a loop: After the sound plays, it repeats from the beginning, until you call stop() or pause(). Good for music and ambient background noises. +
  68. *Before 3.1-alpha2, Looping does not work on streamed sounds.*
  69. |===
  70. === Positional 3D Sounds
  71. [cols="2", options="header"]
  72. |===
  73. a|AudioNode Method
  74. a|Usage
  75. a|setPositional(true) +
  76. setLocalTranslation(…)
  77. a|Activates 3D audio: The sound appears to come from a certain position, where it is loudest. Position the AudioNode in the 3D scene, or move it with mobile players or NPCs.
  78. a|setReverbEnabled(true)
  79. a|Reverb is a 3D echo effect that only makes sense with positional AudioNodes. Use Audio Environments to make scenes sound as if they were "`outdoors`", or "`indoors`" in a large or small room, etc. The reverb effect is defined by the `com.jme3.audio.Environment` that the `audioRenderer` is in. See "`Setting Audio Environment Properties`" below.
  80. |===
  81. [IMPORTANT]
  82. ====
  83. Positional 3D sounds require an `AudioListener` object in the scene (representing the player's ears).
  84. ====
  85. === Directional 3D Sounds
  86. [cols="2", options="header"]
  87. |===
  88. a|AudioNode Method
  89. a|Usage
  90. a|setDirectional(true) +
  91. setDirection(…)
  92. a|Activates 3D audio: This sound can only be heard from a certain direction. Specify the direction and angle in the 3D scene if you have setDirectional() true. Use this to restrict noises that should not be heard, for example, through a wall.
  93. a|setInnerAngle() +
  94. setOuterAngle()
  95. a|Set the angle in degrees for the directional audio. The angle is relative to the direction. Note: By default, both angles are 360° and the sound can be heard from all directions!
  96. |===
  97. [IMPORTANT]
  98. ====
  99. Directional 3D sounds require an AudioListener object in the scene (representing the player's ears).
  100. ====
  101. == Play, Pause, Stop
  102. You play, pause, and stop a node called myAudioNode by using the respective of the following three methods:
  103. [source,java]
  104. ----
  105. myAudioNode.play();
  106. ----
  107. [source,java]
  108. ----
  109. myAudioNode.pause();
  110. ----
  111. [source,java]
  112. ----
  113. myAudioNode.stop();
  114. ----
  115. [NOTE]
  116. ====
  117. Whether an Audio Node plays continuously or only once, depends on the Loop properties you have set above!
  118. ====
  119. You can also start playing instances of an AudioNode. Use the `playInstance()` method if you need to play the same AudioNode multiple times, possibly simultaneously. Note that changes to the parameters of the original AudioNode do not affect the instances that are already playing!
  120. [source,java]
  121. ----
  122. myAudioNode.playInstance();
  123. ----
  124. == The Audio Listener
  125. The default AudioListener object `listener` in `SimpleApplication` is the user's ear in the scene. If you use 3D audio (positional or directional sounds), you must move the AudioListener with the player: For example, for a first-person player, you move the listener with the camera. For a third-person player, you move the listener with the player avatar Geometry.
  126. [source,java]
  127. ----
  128. @Override
  129. public void simpleUpdate(float tpf) {
  130. // first-person: keep the audio listener moving with the camera
  131. listener.setLocation(cam.getLocation());
  132. listener.setRotation(cam.getRotation());
  133. }
  134. ----
  135. == Setting Audio Environment Properties
  136. Optionally, You can choose from the following environmental presets from `com.jme3.audio.Environment`. This presets influence subtle echo effects (reverb) that evoke associations of different environments in your users. That is, it makes you scene sound "`indoors`" or "`outdoors`" etc. You use Audio Environments together with `setReverbEnabled(true)` on positional AudioNodes (see above).
  137. [cols="11", options="header"]
  138. |===
  139. a|Environment
  140. a|density
  141. a|diffusion
  142. a|gain
  143. a|gainHf
  144. a|decayTime
  145. a|decayHf
  146. a|reflGain
  147. a|reflDelay
  148. a|lateGain
  149. a|lateDelay
  150. <a|Garage
  151. a|1.00f
  152. a|1.0f
  153. a|1.0f
  154. a|1.00f
  155. a|0.90f
  156. a|0.5f
  157. a|0.751f
  158. a|0.0039f
  159. a|0.661f
  160. a|0.0137f
  161. <a|Dungeon
  162. a|0.75f
  163. a|1.0f
  164. a|1.0f
  165. a|0.75f
  166. a|1.60f
  167. a|1.0f
  168. a|0.950f
  169. a|0.0026f
  170. a|0.930f
  171. a|0.0103f
  172. <a|Cavern
  173. a|0.50f
  174. a|1.0f
  175. a|1.0f
  176. a|0.50f
  177. a|2.25f
  178. a|1.0f
  179. a|0.908f
  180. a|0.0103f
  181. a|0.930f
  182. a|0.0410f
  183. a|AcousticLab
  184. a|0.50f
  185. a|1.0f
  186. a|1.0f
  187. a|1.00f
  188. a|0.28f
  189. a|1.0f
  190. a|0.870f
  191. a|0.0020f
  192. a|0.810f
  193. a|0.0080f
  194. <a|Closet
  195. a|1.00f
  196. a|1.0f
  197. a|1.0f
  198. a|1.00f
  199. a|0.15f
  200. a|1.0f
  201. a|0.600f
  202. a|0.0025f
  203. a|0.500f
  204. a|0.0006f
  205. |===
  206. . Activate a Environment preset
  207. ** Either use a default, e.g. make your scene sounds like a dungeon environment:
  208. +
  209. [source,java]
  210. ----
  211. audioRenderer.setEnvironment(new Environment(Environment.Dungeon));
  212. ----
  213. ** Or activate xref:audio/audio_environment_presets.adoc[custom environment settings] in the Environment constructor:
  214. +
  215. [source,java]
  216. ----
  217. audioRenderer.setEnvironment(
  218. new Environment( density, diffusion, gain, gainHf, decayTime, decayHf,
  219. reflGain, reflDelay, lateGain, lateDelay ) );
  220. ----
  221. . Activate 3D audio for certain sounds:
  222. +
  223. [source,java]
  224. ----
  225. footstepsAudio.setPositional(true);
  226. footstepsAudio.setReverbEnabled(true);
  227. ----
  228. [TIP]
  229. ====
  230. A sound engineer can create a custom `com.​jme3.​audio.Environment` object and specify custom environment values such as density, diffusion, gain, decay, delay… You can find many xref:audio/audio_environment_presets.adoc[examples of custom audio environment presets] here.
  231. ====
  232. Advanced users find more info about OpenAL and its features here: link:http://web.archive.org/web/20130327063429/http://connect.creativelabs.com/openal/Documentation/OpenAL_Programmers_Guide.pdf[OpenAL 1.1 Specification].
  233. [IMPORTANT]
  234. ====
  235. It depends on the hardware whether audio effects are supported (if not, you get the message `OpenAL EFX not available! Audio effects won't work.`)
  236. ====