View Single Post
  #6  
Old 05-30-2016, 06:04 AM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,465
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:

Code:
#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','birds.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)

Last edited by Jeff; 05-31-2016 at 05:27 AM.
Reply With Quote