How to play sounds from OS?

Like, how do I get a specific sound from a certain directory wherever it be relative or absolute, and then instruct the script to load the sound? This is from OS, game logic, etc.

Pretty simple, take a look at aud reference, there’s a simple example there. Below there’s a simple example I made where I load three different sounds and play them at random pitches everytime. (125.0 KB)

The fully commented script:

import bge
import aud
import random

from bge.logic import expandPath

def main(cont):
    # Objects
    own = cont.owner
    # Sensors
    senA = cont.sensors["A"]
    senS = cont.sensors["S"]
    senD = cont.sensors["D"]
    # Variables
    handle = None
    # Load sounds if not already loaded in scene
    if not "Sounds" in own.scene:
        # Create an empty sounds dict into scene
        own.scene["Sounds"] = {}
        # Get the path of files relative to current blend
        sound1 = expandPath("//sounds/sound1.ogg")
        sound2 = expandPath("//sounds/sound2.ogg")
        sound3 = expandPath("//sounds/sound3.ogg")
        # Load the sounds as buffered factories into the sounds dict
        # NOTE: Buffer only short and repetitive sounds, it takes memory!
        own.scene["Sounds"]["Sound1"] = aud.Factory(sound1).buffer()
        own.scene["Sounds"]["Sound2"] = aud.Factory(sound2).buffer()
        own.scene["Sounds"]["Sound3"] = aud.Factory(sound3).buffer()
    # Play the sound if corresponding sensor is positive
    if senA.positive:
        handle = aud.device().play(own.scene["Sounds"]["Sound1"])
    # Play the sound if corresponding sensor is positive
    if senS.positive:
        handle = aud.device().play(own.scene["Sounds"]["Sound2"])
    # Play the sound if corresponding sensor is positive
    if senD.positive:
        handle = aud.device().play(own.scene["Sounds"]["Sound3"])
    # If sound was played since initialization
    if handle is not None:
        # Get a random value between 0.5 and 1.5
        randValue = 0.0
        while randValue < 0.5:
            randValue = random.random() * 1.5
        # Set random pitch to playing sound handle
        handle.pitch = randValue

The AUD module is really easy to use.
Additional comments redacted.

import bge, aud

# File path that is buffered to prevent performance lag on activated of sound
sfx_music = aud.Factory.buffer(aud.Factory(bge.logic.expandPath(str("//Audio/music.wav"))))

# Plays the sound
music = aud.device().play(sfx_music)

# These are additional options, there are many more
music.volume = 0.5
music.loop_count = -1

Actually aud does support 3D sound playback, it’s just a bit more tricky to setup. Example file below. (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 =
	# 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

I never could personally get those functions to work when I tried using, thank you for sharing! :smiley:

1 Like