Sound

Heaps provides sound management. Heaps supports 3 different formats (WAV, MP3, OGG). The availability of the formats depend on the platform (see Resource manager page for details). You can determine which formats are supported using the following example:

  1. if(hxd.res.Sound.supportedFormat(Mp3)){
  2. //Mp3 is available
  3. }
  4. if(hxd.res.Sound.supportedFormat(OggVorbis)){
  5. //Ogg format is available
  6. }

Sounds can be included in your project by specificying them at compile time in your HXML file

  1. -D resourcesPath="../relative/path/to/sound/folder/"

Once included in your project you can play a sound file via the file name of the sound you want to access.

  1. //If your audio file is named 'my_music.mp3'
  2. var musicResource:Sound = null;
  3. //If we support mp3 we have our sound
  4. if(hxd.res.Sound.supportedFormat(Mp3)){
  5. musicResource = hxd.Res.my_music;
  6. }
  7. if(musicResource != null){
  8. //Play the music and loop it
  9. musicResource.play(true);
  10. }

Channel, ChannelGroup and SoundGroup

Each Channel instance belong to one SoundGroup and one ChannelGroup that allow to mass-control certain parameters of Channels.

Channel

Channel instance represents actively playing sound. It’s possible to control following parameters on each Channel: priority: Priority of the Channel instance (when limiting maximum audible channels, lowest priority Channels get muted when limit is reached) mute: Mutes channels. Note that playback still continues when channel is muted. volume: Control volume of the Channel. fadeTo(volume, ?time = 1, ?onEnd: ()->Void): Transit channel to specified volume over set period of time with optional callback when transition ends. addEffect(e:Effect) / removeEffect(e:Effect): Add and remove effects (see below) pause: Allows to temporarily pause sound playback. duration: Represents duration of the Sound. position: Allow to control playback position of the Channel. loop: Sets channel playback to loop. queue(s:Sound): Allows to queue next Sound to play with this Channel instance. It’s possible to queue multiple sounds.

SoundGroup

Sound groups allow to control volume (multiplier), max audioble channels at once and force mono playback on Channel instances.

ChannelGroup

Channel groups allow to control priority, mute, volume, fading and effects of multiple Channels that bellong to the group. Note that channel priority takes precedence over individual channel priorities.

Manager

Audio Manager instance provides API to general management of audio. It can be obtained with hxd.snd.Manager.get() call. Primary use is access to masterVolume, spatialization listener, as well as default sound and channel groups.

  1. var manager = hxd.snd.Manager.get();
  2. manager.masterVolume = 0.5;
  3. manager.masterChannelGroup.addEffect(new hxd.snd.effect.Pitch(0.5));
  4. manager.masterSoundGroup.maxAudible = 4;
  5. manager.listener.syncCamera();
  6. // Stops all Channels that belong to SoundGroup with a name "myGroupName".
  7. manager.stopByName("myGroupName");

Effects

When playing a sound, it’s possible to attach effects to the Channel or ChannelGroup instance. Due to backend difference, some backends may not support all effect types. Following sample showcases usage of pitch effect:

  1. var sound = Res.my_sound.play();
  2. // 0.5 pitch would make it sound slowed down.
  3. var pitch = new hxd.snd.Effect(0.5);
  4. sound.addEffect(pitch);
  5. // Returns first effects with specified type
  6. trace(sound.getEffect(hxd.snd.Effect));
  7. // Remove effect from channel
  8. sound.removeEffect(pitch);

Platform-specific limitations

HTML5

  • Pitch: Due to how WebAudio works, changing pitch gradually can lead to audio cuts. This cannot be avoided due to pitch being applied only once in 128 samples and impossibility to obtain accurate current sample of audio buffer.
  • Spatialization: Velocity is not supported.
  • Spatialization: Only mono sounds are supported. (Use SoundGroup.mono to force mono playback)