Python vs Sound Actuator

So I’ve been trying to find post on here concerning the fact that the getFilename bug makes it difficult to use python to control the sound actuator. I haven’t really been able to find anyone who has successfully worked around it. in essence let’s say i want to press the spacebar and have a line of code that said play the sound of the next letter in the alphabet.

Instead of having to make 26 sound actuators I would like to just plug in the filename of each sound to one actuator. is there a way to do this?

Yes, you can play sounds though the ‘aud’ module.

http://www.blender.org/documentation/blender_python_api_2_66a_release/aud.html

I really appreciate the link but I’ve been staring at that page all day and can’t figure out how to apply it to my project

Audaspace can initially be hard to get into, but once you develop a system surrounding its use, it can become a lot easier.

For example. In one of my projects, I use this script to initialize all of the sounds that I will be using (the sounds being in a separate file in the same directory as the .blend).



import bge
import aud

con = bge.logic.getCurrentController()
ob = con.owner
device = aud.device()

if ob['Tap'] == 0:
    
    #initialize the sounds to be used#
    rollSound = aud.Factory.file(bge.logic.expandPath("//MR_sounds\Rolling_clipped.wav"))
    dingSound = aud.Factory.file(bge.logic.expandPath("//MR_sounds\Ding.wav"))
    donkSound = aud.Factory.file(bge.logic.expandPath("//MR_sounds\Donk.wav"))
    donkSound2 = aud.Factory.file(bge.logic.expandPath("//MR_sounds\Donk2.wav"))
    jumpSound = aud.Factory.file(bge.logic.expandPath("//MR_sounds\Marble_jump.wav"))
    
    #create a dictionary containing the handles that will allow us to use the sounds#
    bge.logic.hList = {'Roll': device.play(rollSound),
        'Ding': device.play(dingSound),
        'Donk': device.play(donkSound),
        'Donk2': device.play(donkSound2),
        'Jump_sound': device.play(jumpSound)}
    
    #set the initial properties for the sounds themselves#
    bge.logic.hList["Roll"].loop_count = -1
    bge.logic.hList["Roll"].volume = 0.75
    bge.logic.hList["Roll"].pitch = 0.5
    bge.logic.hList["Roll"].pause()
    
    bge.logic.hList["Ding"].loop_count = 0
    bge.logic.hList["Ding"].volume = 0.75
    bge.logic.hList["Ding"].pause()
    
    bge.logic.hList["Donk"].loop_count = 0
    bge.logic.hList["Donk"].volume = 0.5
    bge.logic.hList["Donk"].pause()
    
    bge.logic.hList["Donk2"].loop_count = 0
    bge.logic.hList["Donk2"].volume = 1
    bge.logic.hList["Donk2"].pause()
    
    bge.logic.hList["Jump_sound"].loop_count = 0
    bge.logic.hList["Jump_sound"].volume = 0.5
    bge.logic.hList["Jump_sound"].pause()
    ob['Tap'] = 1

When I need to play a sound in another script, I do (example).


sound["Jump_sound"].resume()

And if I need it to stop, I do.


sound["Jump_sound"].position = 0
sound["Jump_sound"].pause()

For me, this works in the vast majority of cases, another way might be to create your own sound class that wraps audaspace into some easy to use functions, but I’ve never been well versed in classes.

Using the aud module requires you to setup the device first and create a handle for every sound, if you want to change the sound properties, such as 3D sound, loop count, pitch, volume, …

Of course to get the sounds you need the url of the folder containing the sounds and their names. If you need to play the sounds of an alphabet I suggest you use a library, or simply use the same names for the sounds.

I’m not very sure how you would be reciting the alphabet but this is just an example of doing it pressing the space bar continuously. The url may be different for you. The sounds are named ‘a.ogg’, ‘b.ogg’, ‘c.ogg’, … Connect a keyboard sensor with Tap enabled to a python controller, Module mode. If the script is named ‘play_sound.py’, then type ‘play_sound.main’ in the field.

from bge import logic
from aud import device, Factory

class PlayAlphabet:
    
    device = device()
    url = logic.expandPath('//Sounds/Alphabet/')
    extension = '.ogg'
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
        
    def get_character(self):
        self.character = self.alphabet[0]
        self.alphabet = self.alphabet[1:] + self.character
        
    def play_character_sound(self):
        character_sound = Factory.buffer(Factory(self.url + self.character + self.extension))
        character_sound_handle = self.device.play(character_sound)
        character_sound_handle.volume = 0.5
        
    def main(self):
        self.get_character()
        self.play_character_sound()
        
play_alphabet = PlayAlphabet()
        
def main(cont):
    if not cont.sensors[0].positive:
        return
    play_alphabet.main()

Edit: Oops, Ace Dragon already gave an explanation. Anyway, hope it’s helpful.

You guys rock. I’ll try to work it out