How to play sounds from OS?

Actually aud does support 3D sound playback, it’s just a bit more tricky to setup. Example file below.

ex-play-random-sound.zip (162.0 KB)

def playSound(path, obj):
	""" Play 3D sound from path in the position of given object.
	Remember that 3D sound only works with mono audio files. """
	
	# Get the audio device and set its properties according to current camera
	device = aud.device()
	device.distance_model = aud.AUD_DISTANCE_MODEL_LINEAR
	device.listener_location = obj.scene.active_camera.worldPosition
	device.listener_orientation = obj.scene.active_camera.worldOrientation.to_quaternion()
	
	# Create an audio factory and play it, with a handle as result
	factory = aud.Factory(path)
	handle = device.play(factory)
	
	# Makes the handle behave as a 3D sound
	handle.relative = False
	handle.location = obj.worldPosition
	
	# If sound source is farther from listener than the value below, volume is zero
	handle.distance_maximum = 10
1 Like