audio.adoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. = Audio in jME3
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :keywords: sound, documentation, environment
  6. :relfileprefix: ../../
  7. :imagesdir: ../..
  8. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  9. 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:http://audacity.sourceforge.net/[Audacity] to convert from other formats.
  10. == Audio Terminology
  11. * *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.
  12. * *Looping:* You can play a sound either once and then stop, or repeatedly (continuously) in a loop. +
  13. You cannot loop streamed sounds.
  14. * *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.
  15. == Creating Audio Nodes: Streamed or Buffered
  16. 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:
  17. * *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:
  18. [source,java]
  19. ----
  20. AudioNode boom = new AudioNode(assetManager, "Sound/boom.wav");
  21. AudioNode boom = new AudioNode(assetManager, "Sound/boom.wav", DataType.Buffer);
  22. ----
  23. * *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:
  24. [source,java]
  25. ----
  26. AudioNode music = new AudioNode(assetManager, "Sound/music.wav", DataType.Stream);
  27. ----
  28. == Getting AudioNode Properties
  29. [cols="2", options="header"]
  30. |===
  31. a|AudioNode Method
  32. a|Usage
  33. a|getStatus()
  34. a|Returns either AudioSource.Status.Playing, AudioSource.Status.Stopped, or AudioSource.Status.Paused.
  35. a|getVolume()
  36. a|Returns the volume.
  37. a|getPitch()
  38. a|Returns the pitch.
  39. |===
  40. [NOTE]
  41. ====
  42. There are other obvious getters to poll the status of all corresponding setters listed here.
  43. ====
  44. == Setting AudioNode Properties
  45. [cols="2", options="header"]
  46. |===
  47. a|AudioNode Method
  48. a|Usage
  49. a|setTimeOffset(0.5f)
  50. a|Play the sound starting at a 0.5 second offset from the beginning. Default is 0.
  51. a|setPitch(1)
  52. a|Makes the sound play in a higher or lower pitch. Default is 1. 2 is twice as high, .5f is half as high.
  53. a|setVolume(1)
  54. a|Sets the volume gain. 1 is the default volume, 2 is twice as loud, etc. 0 is silent/mute.
  55. a|setRefDistance(50f)
  56. 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.
  57. a|setMaxDistance(100f)
  58. 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.
  59. a|setLooping(false)
  60. a|Configures the sound so that, if it is played, it plays once and stops. No looping is the default.
  61. |===
  62. === Looping & Ambient Sounds
  63. [cols="2", options="header"]
  64. |===
  65. a|AudioNode Method
  66. a|Usage
  67. a|setPositional(false) +
  68. setDirectional(false)
  69. 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.
  70. a|setLooping(true)
  71. 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. +
  72. *Before 3.1-alpha2, Looping does not work on streamed sounds.*
  73. |===
  74. === Positional 3D Sounds
  75. [cols="2", options="header"]
  76. |===
  77. a|AudioNode Method
  78. a|Usage
  79. a|setPositional(true) +
  80. setLocalTranslation(…)
  81. 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.
  82. a|setReverbEnabled(true)
  83. 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.
  84. |===
  85. [IMPORTANT]
  86. ====
  87. Positional 3D sounds require an `AudioListener` object in the scene (representing the player's ears).
  88. ====
  89. === Directional 3D Sounds
  90. [cols="2", options="header"]
  91. |===
  92. a|AudioNode Method
  93. a|Usage
  94. a|setDirectional(true) +
  95. setDirection(…)
  96. 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.
  97. a|setInnerAngle() +
  98. setOuterAngle()
  99. 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!
  100. |===
  101. [IMPORTANT]
  102. ====
  103. Directional 3D sounds require an AudioListener object in the scene (representing the player's ears).
  104. ====
  105. == Play, Pause, Stop
  106. You play, pause, and stop a node called myAudioNode by using the respective of the following three methods:
  107. [source,java]
  108. ----
  109. myAudioNode.play();
  110. ----
  111. [source,java]
  112. ----
  113. myAudioNode.pause();
  114. ----
  115. [source,java]
  116. ----
  117. myAudioNode.stop();
  118. ----
  119. [NOTE]
  120. ====
  121. Whether an Audio Node plays continuously or only once, depends on the Loop properties you have set above!
  122. ====
  123. You can also start playing instances of an AudioNode. Use the `playInstance()` method if you need to play the same AudioNode multiple times, possibly simulatenously. Note that changes to the parameters of the original AudioNode do not affect the instances that are already playing!
  124. [source,java]
  125. ----
  126. myAudioNode.playInstance();
  127. ----
  128. == The Audio Listener
  129. 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.
  130. [source,java]
  131. ----
  132. @Override
  133. public void simpleUpdate(float tpf) {
  134. // first-person: keep the audio listener moving with the camera
  135. listener.setLocation(cam.getLocation());
  136. listener.setRotation(cam.getRotation());
  137. }
  138. ----
  139. == Setting Audio Environment Properties
  140. 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).
  141. [cols="11", options="header"]
  142. |===
  143. a|Environment
  144. a|density
  145. a|diffusion
  146. a|gain
  147. a|gainHf
  148. a|decayTime
  149. a|decayHf
  150. a|reflGain
  151. a|reflDelay
  152. a|lateGain
  153. a|lateDelay
  154. <a|Garage
  155. a|1.00f
  156. a|1.0f
  157. a|1.0f
  158. a|1.00f
  159. a|0.90f
  160. a|0.5f
  161. a|0.751f
  162. a|0.0039f
  163. a|0.661f
  164. a|0.0137f
  165. <a|Dungeon
  166. a|0.75f
  167. a|1.0f
  168. a|1.0f
  169. a|0.75f
  170. a|1.60f
  171. a|1.0f
  172. a|0.950f
  173. a|0.0026f
  174. a|0.930f
  175. a|0.0103f
  176. <a|Cavern
  177. a|0.50f
  178. a|1.0f
  179. a|1.0f
  180. a|0.50f
  181. a|2.25f
  182. a|1.0f
  183. a|0.908f
  184. a|0.0103f
  185. a|0.930f
  186. a|0.0410f
  187. a|AcousticLab
  188. a|0.50f
  189. a|1.0f
  190. a|1.0f
  191. a|1.00f
  192. a|0.28f
  193. a|1.0f
  194. a|0.870f
  195. a|0.0020f
  196. a|0.810f
  197. a|0.0080f
  198. <a|Closet
  199. a|1.00f
  200. a|1.0f
  201. a|1.0f
  202. a|1.00f
  203. a|0.15f
  204. a|1.0f
  205. a|0.600f
  206. a|0.0025f
  207. a|0.500f
  208. a|0.0006f
  209. |===
  210. . Activate a Environment preset
  211. ** Either use a default, e.g. make you scene sounds like a dungeon environment:
  212. +
  213. [source,java]
  214. ----
  215. audioRenderer.setEnvironment(new Environment(Environment.Dungeon));
  216. ----
  217. ** Or activate <<jme3/advanced/audio_environment_presets#,custom environment settings>> in the Environment constructor:
  218. +
  219. [source,java]
  220. ----
  221. audioRenderer.setEnvironment(
  222. new Environment( density, diffusion, gain, gainHf, decayTime, decayHf,
  223. reflGain, reflDelay, lateGain, lateDelay ) );
  224. ----
  225. . Activate 3D audio for certain sounds:
  226. +
  227. [source,java]
  228. ----
  229. footstepsAudio.setPositional(true);
  230. footstepsAudio.setReverbEnabled(true);
  231. ----
  232. [TIP]
  233. ====
  234. 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 <<jme3/advanced/audio_environment_presets#,examples of custom audio environment presets>> here.
  235. ====
  236. 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].
  237. [IMPORTANT]
  238. ====
  239. 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.`)
  240. ====