Vizard 8 » Reference » Multimedia » Sound mixer
8.1

Sound Mixer

Use the sound mixer library to apply volume, pitch, and filter effects to global (non-spatialized) sounds. The mixer supports playing single and multi channel WAV files.

Add a Sound Mixer

To add a mixer object, use the viz.addSoundMixer() command.

mixer = viz.addSoundMixer()

The mixer has methods for setting effect parameters and playing sounds:

mixer.setPitch(0.5)
mixer.play('sounds/beep.wav', viz.LOOP)

Multiple mixers can be added, each with its own effect settings. In addition, mixers can have parent/child relationships where the child inherits the settings of the parent. The final effect output from the child mixer is a product of both the child and parent settings. In the following example code, the sound plays at 25% of its original volume (0.5 x 0.5):

mixer = viz.addSoundMixer()
mixer.setVolume(0.5)

subMixer = viz.addSoundMixer(mixer)
subMixer.setVolume(0.5)
subMixer.play('sounds/beep.wav', viz.LOOP)

Play, Pause, and Stop Sounds

To play a sound, specify a WAV file in the mixer.play command. All effects on the mixer will be applied to the sound. To loop or preload a sound, specify the play mode after the file name:

#Preload the sound to prevent any delay when playing sound for first time
mixer.play('sounds/beep.wav', viz.SOUND_PRELOAD)
.
.
.
#Play the sound in loop mode
mixer.play('sounds/beep.wav', viz.LOOP)

Use the mixer.setPaused and mixer.stop commands to pause and stop all sounds on the mixer and sub-mixers:

mixer = viz.addSoundMixer()
mixer.play('sounds/beep.wav', viz.LOOP)

subMixer = viz.addSoundMixer(mixer)
mixer.play('sounds/platform_running.wav', viz.LOOP)

#Toggle mixer and submixer sounds with 'p' key
vizact.onkeydown('p',mixer.setPaused,viz.TOGGLE)
#Stop mixer and submixer sounds with 's' key
vizact.onkeydown('s',mixer.stop)

Note: If the mixer is paused and a sound is played, the sound will start in a paused state until the mixer is resumed.

Synchronize Sounds

To synchronize sounds put the mixer in a paused state, play all the sounds, then un-pause the mixer:

mixer.setPaused(True)
mixer.play('violin.wav')
mixer.play('cello.wav')
mixer.play('piano.wav')
mixer.setPaused(False) # All 3 sounds will now be synchronized

Effects

The sound mixer supports volume, pitch, and filter effects. Use the mixer.setPitch command to make the audio play slower or faster:

mixer.play('sounds/beep.wav', viz.LOOP)
#Slow down audio by factor of 2
vizact.onkeydown('1',mixer.setPitch,0.5)
#Play at original speed
vizact.onkeydown('2',mixer.setPitch,1)
#Speed up audio by factor of 2
vizact.onkeydown('3',mixer.setPitch,2)

To enable filtering use the mixer.setFilter command and specify a viz.AudioFilter object. The filter object defines the type, frequency, and Q factor of the filter.

Filter Type Description
viz.FILTER_NONE No filter is applied.
viz.FILTER_LOWPASS

Passes frequencies below the frequency value and attenuates frequencies above (e.g. muffled sound, music played in another room). The Q factor affects the dampening of high frequencies.

viz.FILTER_HIGHPASS Passes frequencies above the frequency value and attenuates frequencies below (e.g. telephone speaker). The Q factor affects the dampening of low frequencies.
viz.FILTER_BANDPASS Passes a range of frequencies and attenuates frequencies outside that range. The frequency value determines the center of the range and the Q factor determines the bandwidth.
viz.FILTER_NOTCH Passes frequencies outside a range and attenuates values inside. The frequency value determines the center of the range and the Q factor determines the bandwidth.

In the following code, a lowpass filter is applied to muffle the sound source:

#Add audio filter objects
filterLow = viz.AudioFilter(mode=viz.FILTER_LOWPASS,frequency=300)
filterNone = viz.AudioFilter(mode=viz.FILTER_NONE)

#Apply lowpass filter to muffle sound
vizact.onkeydown('1',mixer.setFilter,filterLow)
#Disable filtering
vizact.onkeydown('2',mixer.setFilter,filterNone)