View Full Version : Fade in and fade out an audio file
helena35
05-20-2016, 01:02 PM
Hi all,
I would like to create a spatial sound atmosphere. I made a script (very rudimentary) in which whenever the user turns the head 20 degrees, hears a different file (therefore I have 18 audio files in the scene, played simultaneously; one with volume =1, and the rest with volume = 0) Is there a way to made a fade out of the previous file and fade in of the new one in an overlapping sector of 10 degrees:
0-10 degrees: file 1 volume 1 (rest volume 0)
10-20: file 1 fade out, file 2 fade in (rest volume 0)
20-30: file 2 volume 1 (rest volume 0)
30-40: file 2 fade out, file 3 fade in (rest volume 0)....
Maybe a way of changing the volume gradually in these junction circular sector regarding the position of the head?
Thanks in advance
helena35
05-20-2016, 01:19 PM
I forgot to say that the user will be in a fixed position within the scene, and will only have the possibility of turning 360 degrees in this fixed position without displacements.
You could use the head tracker euler values to trigger custom events. There isn't built in sound fading so you would have to write a simple sound fader that reduces volume over a small period of time.
Erikvdb
05-24-2016, 03:09 AM
You could use the head tracker euler values to trigger custom events. There isn't built in sound fading so you would have to write a simple sound fader that reduces volume over a small period of time.
In this case the sound shouldn't fade over time, but rather over a number of degrees. So you could write a fader that decreases the volume of file1 with 0.1 with every degree over 10 (until volume is 0), etc.
helena35
05-29-2016, 02:57 PM
In this case the sound shouldn't fade over time, but rather over a number of degrees. So you could write a fader that decreases the volume of file1 with 0.1 with every degree over 10 (until volume is 0), etc.
Thanks both for your answers...
Any idea of how to write a fader that decreases the volume? Is there any tool with which it can be done? I am new in Vizard and I have no idea on how to make it in a simple way...
You could use vizact.onupdate to register a function that's called every frame. In that function, get the viewpoint orientation and based on that value play and fade audio files. Here's an example:
#0-10 degrees: play file 1
#10-20: fade out file 1, fade in file 2
#30-40: fade out file 2, fade in file 3
#50-60: fade out file 3, fade in file 4...
import viz
import vizact
viz.go()
viz.addChild('piazza.osgb')
intervals = [0,10,30,50]
audioFiles = ['bells.wav','fountain.wav','conversation.wav','bir ds.wav']
#create a list of audio objects, set to loop mode
audioList = []
for file in audioFiles:
audio = viz.addAudio(file)
audio.loop()
audioList.append(audio)
def fadeAudio():
yaw = viz.MainView.getEuler()[0]
print yaw
for index,interval in enumerate(intervals):
if interval < yaw <= interval + 10:
# if first iteration only play first audio file
if index == 0:
if audioList[0].getState() in [viz.MEDIA_STOPPED,viz.MEDIA_PAUSED]:
audioList[0].play()
# for all others play next file and apply fades
else:
if audioList[index].getState() in [viz.MEDIA_STOPPED,viz.MEDIA_PAUSED]:
audioList[index].play()
audioList[index].volume(0)
fadeInValue = (yaw-interval)/10
fadeOutValue = 1.0-fadeInValue
audioList[index].volume(fadeInValue)
audioList[index-1].volume(fadeOutValue)
# pause audio if volume is close to 0
if fadeOutValue < 0.01 and audioList[index-1].getState() == viz.MEDIA_RUNNING:
audioList[index-1].pause()
vizact.onupdate(0,fadeAudio)
#Spin the view to 60 degrees at 2 degrees/s.
spinAction = vizact.spinto(0,1,0,60,2)
viz.MainView.runAction(spinAction)
vBulletin® v3.8.7, Copyright ©2000-2024, vBulletin Solutions, Inc.