Python pygame.mixer() Examples

The following are 30 code examples of pygame.mixer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pygame , or try the search function .
Example #1
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def load_sound(file):
    if not pygame.mixer: return dummysound()
    file = os.path.join(main_dir, 'data', file)
    try:
        sound = pygame.mixer.Sound(file)
        return sound
    except pygame.error:
        print ('Warning, unable to load, %s' % file)
    return dummysound()



# each type of game object gets an init and an
# update function. the update function is called
# once per frame, and it is when each object should
# change it's current position and state. the Player
# object actually gets a "move" function instead of
# update, since it is passed extra information about
# the keyboard 
Example #2
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def load_sound(file):
    if not pygame.mixer: return dummysound()
    file = os.path.join('data', file)
    try:
        sound = pygame.mixer.Sound(file)
        return sound
    except pygame.error:
        print ('Warning, unable to load,', file)
    return dummysound()



# each type of game object gets an init and an
# update function. the update function is called
# once per frame, and it is when each object should
# change it's current position and state. the Player
# object actually gets a "move" function instead of
# update, since it is passed extra information about
# the keyboard 
Example #3
Source File: resources.py    From stuntcat with GNU Lesser General Public License v2.1 6 votes vote down vote up
def music(amusic=None, load=True, play=True, stop=False):
    """ For loading and playing music.

    ::Example::

    music('bla.ogg', load=True, play=True)
    music(stop=True)
    """
    # perhaps the mixer is not included or initialised.
    if pygame.mixer and pygame.mixer.get_init():
        if load and not stop:
            pygame.mixer.music.load(music_path(amusic))
        if play and stop is None or stop is False:
            pygame.mixer.music.play()
        elif stop:
            pygame.mixer.music.stop() 
Example #4
Source File: resources.py    From stuntcat with GNU Lesser General Public License v2.1 6 votes vote down vote up
def sfx(snd, play=False, stop=False):
    global _sfx_cache
    snd_key = snd
    if snd_key in _sfx_cache:
        asound = _sfx_cache[snd_key]
    else:
        path = os.path.join(data_path(), 'sounds', snd)
        asound = pygame.mixer.Sound(path)
        _sfx_cache[snd_key] = asound

    # print(snd_key, play, stop, time.time())
    if play:
        asound.play()
    if stop:
        asound.stop()
    return asound 
Example #5
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def load_sound(file):
    if not pygame.mixer: return dummysound()
    file = os.path.join(main_dir, 'data', file)
    try:
        sound = pygame.mixer.Sound(file)
        return sound
    except pygame.error:
        print ('Warning, unable to load, %s' % file)
    return dummysound()



# each type of game object gets an init and an
# update function. the update function is called
# once per frame, and it is when each object should
# change it's current position and state. the Player
# object actually gets a "move" function instead of
# update, since it is passed extra information about
# the keyboard 
Example #6
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def load_sound(file):
    if not pygame.mixer: return dummysound()
    file = os.path.join(main_dir, 'data', file)
    try:
        sound = pygame.mixer.Sound(file)
        return sound
    except pygame.error:
        print ('Warning, unable to load, %s' % file)
    return dummysound()



# each type of game object gets an init and an
# update function. the update function is called
# once per frame, and it is when each object should
# change it's current position and state. the Player
# object actually gets a "move" function instead of
# update, since it is passed extra information about
# the keyboard 
Example #7
Source File: resource.py    From MCEdit-Unified with ISC License 6 votes vote down vote up
def get_sound(*names, **kwds):
    if sound_cache is None:
        return dummy_sound
    path = _resource_path("sounds", names, **kwds)
    sound = sound_cache.get(path)
    if not sound:
        try:
            from pygame.mixer import Sound
        except ImportError as e:
            no_sound(e)
            return dummy_sound
        try:
            sound = Sound(path)
        except pygame.error as e:
            missing_sound(e, path)
            return dummy_sound
        sound_cache[path] = sound
    return sound 
Example #8
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def load_sound(file):
    if not pygame.mixer: return dummysound()
    file = os.path.join('data', file)
    try:
        sound = pygame.mixer.Sound(file)
        return sound
    except pygame.error:
        print ('Warning, unable to load,', file)
    return dummysound()



# each type of game object gets an init and an
# update function. the update function is called
# once per frame, and it is when each object should
# change it's current position and state. the Player
# object actually gets a "move" function instead of
# update, since it is passed extra information about
# the keyboard 
Example #9
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def load_sound(file):
    if not pygame.mixer: return dummysound()
    file = os.path.join(main_dir, 'data', file)
    try:
        sound = pygame.mixer.Sound(file)
        return sound
    except pygame.error:
        print ('Warning, unable to load, %s' % file)
    return dummysound()



# each type of game object gets an init and an
# update function. the update function is called
# once per frame, and it is when each object should
# change it's current position and state. the Player
# object actually gets a "move" function instead of
# update, since it is passed extra information about
# the keyboard 
Example #10
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def load_sound(file):
    if not pygame.mixer: return dummysound()
    file = os.path.join('data', file)
    try:
        sound = pygame.mixer.Sound(file)
        return sound
    except pygame.error:
        print ('Warning, unable to load,', file)
    return dummysound()



# each type of game object gets an init and an
# update function. the update function is called
# once per frame, and it is when each object should
# change it's current position and state. the Player
# object actually gets a "move" function instead of
# update, since it is passed extra information about
# the keyboard 
Example #11
Source File: chimp.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def load_sound(name):
    class NoneSound:
        def play(self): pass
    if not pygame.mixer or not pygame.mixer.get_init():
        return NoneSound()
    fullname = os.path.join(data_dir, name)
    try:
        sound = pygame.mixer.Sound(fullname)
    except pygame.error:
        print('Cannot load sound: %s' % fullname)
        raise SystemExit(str(geterror()))
    return sound


# classes for our game objects 
Example #12
Source File: _numpysndarray.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def make_sound (array):
    """pygame._numpysndarray.make_sound(array): return Sound

    Convert an array into a Sound object.
    
    Create a new playable Sound object from an array. The mixer module
    must be initialized and the array format must be similar to the mixer
    audio format.
    """
    
    return mixer.Sound (array=array) 
Example #13
Source File: _numpysndarray.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def array (sound):
    """pygame._numpysndarray.array(Sound): return array

    Copy Sound samples into an array.

    Creates a new array for the sound data and copies the samples. The
    array will always be in the format returned from
    pygame.mixer.get_init().
    """

    return numpy.array (sound, copy=True) 
Example #14
Source File: _numpysndarray.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def samples (sound):
    """pygame._numpysndarray.samples(Sound): return array

    Reference Sound samples into an array.

    Creates a new array that directly references the samples in a Sound
    object. Modifying the array will change the Sound. The array will
    always be in the format returned from pygame.mixer.get_init().
    """

    return numpy.array (sound, copy=False) 
Example #15
Source File: _numpysndarray.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def make_sound (array):
    """pygame._numpysndarray.make_sound(array): return Sound

    Convert an array into a Sound object.
    
    Create a new playable Sound object from an array. The mixer module
    must be initialized and the array format must be similar to the mixer
    audio format.
    """
    
    return mixer.Sound (array=array) 
Example #16
Source File: chimp.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def load_sound(name):
    class NoneSound:
        def play(self): pass
    if not pygame.mixer or not pygame.mixer.get_init():
        return NoneSound()
    fullname = os.path.join(data_dir, name)
    try:
        sound = pygame.mixer.Sound(fullname)
    except pygame.error:
        print('Cannot load sound: %s' % fullname)
        raise SystemExit(str(geterror()))
    return sound


# classes for our game objects 
Example #17
Source File: sound_array_demos.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def sound_from_pos(sound, start_pos, samples_per_second = None, inplace = 1):
    """  returns a sound which begins at the start_pos.
         start_pos - in seconds from the begining.
         samples_per_second - 
    """

    # see if we want to reuse the sound data or not.
    if inplace:
        a1 = pygame.sndarray.samples(sound)
    else:
        a1 = pygame.sndarray.array(sound)

    # see if samples per second has been given.  If not, query the mixer.
    #   eg. it might be set to 22050
    if samples_per_second is None:
        samples_per_second = pygame.mixer.get_init()[0]

    # figure out the start position in terms of samples.
    start_pos_in_samples = int(start_pos * samples_per_second)

    # cut the begining off the sound at the start position.
    a2 = a1[start_pos_in_samples:]

    # make the Sound instance from the array.
    sound2 = pygame.sndarray.make_sound(a2)

    return sound2 
Example #18
Source File: _numpysndarray.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def array (sound):
    """pygame._numpysndarray.array(Sound): return array

    Copy Sound samples into an array.

    Creates a new array for the sound data and copies the samples. The
    array will always be in the format returned from
    pygame.mixer.get_init().
    """

    return numpy.array (sound, copy=True) 
Example #19
Source File: _numpysndarray.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def samples (sound):
    """pygame._numpysndarray.samples(Sound): return array

    Reference Sound samples into an array.

    Creates a new array that directly references the samples in a Sound
    object. Modifying the array will change the Sound. The array will
    always be in the format returned from pygame.mixer.get_init().
    """

    return numpy.array (sound, copy=False) 
Example #20
Source File: resources.py    From QPong with Apache License 2.0 5 votes vote down vote up
def load_sound(name):
    class NoneSound:
        def play(self): pass
    if not pygame.mixer or not pygame.mixer.get_init():
        return NoneSound()
    fullname = os.path.join(data_dir, 'sound', name)
    try:
        sound = pygame.mixer.Sound(fullname)
    except pygame.error:
        print('Cannot load sound: %s' % fullname)
        raise SystemExit(str(geterror()))
    return sound 
Example #21
Source File: _numpysndarray.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def make_sound (array):
    """pygame._numpysndarray.make_sound(array): return Sound

    Convert an array into a Sound object.
    
    Create a new playable Sound object from an array. The mixer module
    must be initialized and the array format must be similar to the mixer
    audio format.
    """
    
    return mixer.Sound (array=array) 
Example #22
Source File: _numpysndarray.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def samples (sound):
    """pygame._numpysndarray.samples(Sound): return array

    Reference Sound samples into an array.

    Creates a new array that directly references the samples in a Sound
    object. Modifying the array will change the Sound. The array will
    always be in the format returned from pygame.mixer.get_init().
    """

    return numpy.array (sound, copy=False) 
Example #23
Source File: sound_array_demos.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def sound_from_pos(sound, start_pos, samples_per_second = None, inplace = 1):
    """  returns a sound which begins at the start_pos.
         start_pos - in seconds from the begining.
         samples_per_second - 
    """

    # see if we want to reuse the sound data or not.
    if inplace:
        a1 = pygame.sndarray.samples(sound)
    else:
        a1 = pygame.sndarray.array(sound)

    # see if samples per second has been given.  If not, query the mixer.
    #   eg. it might be set to 22050
    if samples_per_second is None:
        samples_per_second = pygame.mixer.get_init()[0]

    # figure out the start position in terms of samples.
    start_pos_in_samples = int(start_pos * samples_per_second)

    # cut the begining off the sound at the start position.
    a2 = a1[start_pos_in_samples:]

    # make the Sound instance from the array.
    sound2 = pygame.sndarray.make_sound(a2)

    return sound2 
Example #24
Source File: chimp.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def load_sound(name):
    class NoneSound:
        def play(self): pass
    if not pygame.mixer or not pygame.mixer.get_init():
        return NoneSound()
    fullname = os.path.join(data_dir, name)
    try:
        sound = pygame.mixer.Sound(fullname)
    except pygame.error:
        print('Cannot load sound: %s' % fullname)
        raise SystemExit(str(geterror()))
    return sound


# classes for our game objects 
Example #25
Source File: _numpysndarray.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def make_sound (array):
    """pygame._numpysndarray.make_sound(array): return Sound

    Convert an array into a Sound object.
    
    Create a new playable Sound object from an array. The mixer module
    must be initialized and the array format must be similar to the mixer
    audio format.
    """
    
    return mixer.Sound (array=array) 
Example #26
Source File: _numpysndarray.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def array (sound):
    """pygame._numpysndarray.array(Sound): return array

    Copy Sound samples into an array.

    Creates a new array for the sound data and copies the samples. The
    array will always be in the format returned from
    pygame.mixer.get_init().
    """

    return numpy.array (sound, copy=True) 
Example #27
Source File: sound_array_demos.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def sound_from_pos(sound, start_pos, samples_per_second = None, inplace = 1):
    """  returns a sound which begins at the start_pos.
         start_pos - in seconds from the begining.
         samples_per_second - 
    """

    # see if we want to reuse the sound data or not.
    if inplace:
        a1 = pygame.sndarray.samples(sound)
    else:
        a1 = pygame.sndarray.array(sound)

    # see if samples per second has been given.  If not, query the mixer.
    #   eg. it might be set to 22050
    if samples_per_second is None:
        samples_per_second = pygame.mixer.get_init()[0]

    # figure out the start position in terms of samples.
    start_pos_in_samples = int(start_pos * samples_per_second)

    # cut the begining off the sound at the start position.
    a2 = a1[start_pos_in_samples:]

    # make the Sound instance from the array.
    sound2 = pygame.sndarray.make_sound(a2)

    return sound2 
Example #28
Source File: chimp.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def load_sound(name):
    class NoneSound:
        def play(self): pass
    if not pygame.mixer or not pygame.mixer.get_init():
        return NoneSound()
    fullname = os.path.join(data_dir, name)
    try:
        sound = pygame.mixer.Sound(fullname)
    except pygame.error:
        print('Cannot load sound: %s' % fullname)
        raise SystemExit(str(geterror()))
    return sound


# classes for our game objects 
Example #29
Source File: resource.py    From MCEdit-Unified with ISC License 5 votes vote down vote up
def _i_eegecx():
    try:
        import pygame.mixer as ghfkd
        return ghfkd
    except ImportError:
        print "Music not available"
        return None 
Example #30
Source File: sound.py    From rpi_lcars with MIT License 5 votes vote down vote up
def init(audio_params):
    """Use this instead of ``pygame.mixer.init``"""
    if config.SOUND:
        pygame.mixer.init(audio_params[0], audio_params[1], audio_params[2], audio_params[3])