adjust volume per sound with .aud?

Hello,

i am using the aud module to play sounds, and i wonder if there is a way to lower the volume (like the sound brick) per sound, instead of:

device.volume = 0.5

because this changes all sounds volume.

use more devices.

You can grab the handle.

Here’s a class I wrap around some AUD functions:


import aud
import functools


DEVICE = aud.device()

SETTINGS = {
    'sound_effect_volume':0.5,
}


@functools.lru_cache(100)
def create_factory(path):
    '''This is cached, so it only runs the first times - or if there have been 100 more recently used sounds'''
    print("Loading Sound {}".format(path))    fac = aud.Factory(path)
    fac.buffer()
    return fac



class SinglePlay(object):
     '''Plays a sound once'''
    def __init__(self, path):
        self._factory = create_factory(path)
        self.handle = DEVICE.play(self._factory)
        self.volume = 1.0
        self.pitch = self.handle.pitch


    def pause(self):
        '''Pauses the sound'''
        self.handle.pause()


    def resume(self):
        '''Resumes the sound'''
        self.handle.resume()


    @property
    def volume(self):
        '''The volume of the effect, corrected by the effect volume attribute'''
        return self.handle.volume / SETTINGS['sound_effect_volume']


    @volume.setter
    def volume(self, val):
        self.handle.volume = val * SETTINGS['sound_effect_volume']


    @property
    def pitch(self):
        '''The pitch of the sound'''
        return self.handle.pitch


    @pitch.setter
    def pitch(self, val):
        self.handle.pitch = val

You can use it like:


handle = PlaySound(path_to_file)
handle.volume = 0.5
handle.pitch = 0.7

As you can see, I catch the handle from device.play(), and I then bind the properties of volume and pitch as a pass-through to the handle. Pretty much that class just helps with creation of sounds. I should see if I can subclass the handle instead, because doing the pass-through is a pain.

thanks for the example, i will try it out, but does that class work alongside sound bricks?
because if i do the device.volume = 0.5 in python. all the sound bricks lower their sound as well.

It work on a per-sound basis.