X3D Sound Tutorial
This is going to be a fairly short tutorial, demonstrating how to work with sound in Heilan. Note that all audio in Heilan is encoded and decoded Ambisonically, so as to place it correctly within a 3D soundfield (though you will need an appropriate speaker setup to get a full 3D soundfield out of it, and you may want to read this).
Okay, so start with an empty X3D file, something like this:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE X3D> <X3D profile='Immersive'> <Scene> </Scene> </X3D>
Now, to add sound to a scene, you need to make use of two node
types; Sound and AudioClip. Sound represents
the sound within the scene, while AudioClip is the source
of that sound; a file on your hard-drive. The following places a
sound in the centre of the scene:
<Transform translation='0 0 0'> <Sound> <AudioClip url='guitar.ogg' loop='true'/> </Sound> </Transform>As you can guess, AudioClip's loop attribute specifies that it will loop continuously.
If you're like me, you'll want some visual reference so you can see
where the sound's coming from, so add in the following after the
</Sound> line above:
<Shape> <Appearance> <Material diffuseColor='0 0 1'/> </Appearance> <Sphere/> </Shape>
Now if you put this file into Heilan you'll notice that it sounds a
bit quiet. This is because the X3D specification states that sound
should fall off by a specified amount the further away you get from
its source. By default the sound starts falling off when you're
1 unit away from the source, and falls to -20dB by 10 units (and
eventually falls away to 0 the further you get). Because Heilan
places the camera 8 units away from the centre of the scene, the
sound is attenuated.
So, to fix this, we need to change how the sound falls off. The following line demonstrates how to do this:
<Sound maxFront='20' minFront='8' maxBack='20' minBack='8'>With this, the sound doesn't get attenuated until you're more than 8 units (the min... values) away from the source, so we hear it at its full volume as soon as we load the file in Heilan. Likewise, the sound will fall off to -20dB at 20 units distance from the source.
There are ...Front and ...Back attributes because X3D actually specifies that this fall-off area can be an ellipsoid, so you can have (for example), the sound fall off faster when you're behind the sound than when you're in front of it. Note: Heilan doesn't support this ellipsoid sound at the time of writing - it assumes a sphere.
The Sound node also has an intensity attribute that you can used to attenuate or amplify the sound (1.0 = 0dB).
The AudioClip node also has some attributes which can come in
very useful. The following will add a sound to the scene which
will not start playing until 8 seconds have passed:
<Transform translation='0 -2 0'> <Sound maxFront='20' minFront='10' maxBack='20' minBack='10'> <AudioClip url='kick.ogg' startTime='8' loop='true'/> </Sound> <Shape> <Appearance> <Material diffuseColor='1 0 0'/> </Appearance> <Sphere radius='0.5'/> </Shape> </Transform>AudioClip's other attributes are:
- pauseTime - Specifies the point at which to pause the sound.
- resumeTime - Specifies the point at which to resume the sound after pausing it.
- stopTime - Specifies when to stop playing.
- pitch - Specifies how fast to play the file (1.0 = normal speed, 0.5 = half speed, 2.0 = twice the speed).
- You can download an example using some simple .ogg loops I made (released under the Creative Commons Sampling Plus license) here.