Exemplo n.º 1
0
def initPygame(rate=22050, bits=16, stereo=True, buffer=1024):
    """If you need a specific format for sounds you need to run this init
    function. Run this *before creating your visual.Window*.

    The format cannot be changed once initialised or once a Window has been created.

    If a Sound object is created before this function is run it will be
    executed with default format (signed 16bit stereo at 22KHz).

    For more details see pygame help page for the mixer.
    """
    global Sound, audioDriver
    Sound = SoundPygame
    audioDriver='n/a'
    if stereo==True: stereoChans=2
    else:   stereoChans=0
    if bits==16: bits=-16 #for pygame bits are signed for 16bit, signified by the minus
    mixer.init(rate, bits, stereoChans, buffer) #defaults: 22050Hz, 16bit, stereo,
    sndarray.use_arraytype("numpy")
    setRate, setBits, setStereo = mixer.get_init()
    if setRate!=rate:
        logging.warn('Requested sound sample rate was not poossible')
    if setBits!=bits:
        logging.warn('Requested sound depth (bits) was not possible')
    if setStereo!=2 and stereo==True:
        logging.warn('Requested stereo setting was not possible')
Exemplo n.º 2
0
def initPygame(rate=22050, bits=16, stereo=True, buffer=1024):
    """If you need a specific format for sounds you need to run this init
    function. Run this *before creating your visual.Window*.

    The format cannot be changed once initialised or once a Window has been
    created.

    If a Sound object is created before this function is run it will be
    executed with default format (signed 16bit stereo at 22KHz).

    For more details see pygame help page for the mixer.
    """
    global Sound, audioDriver
    Sound = SoundPygame
    audioDriver = 'n/a'
    if stereo == True:
        stereoChans = 2
    else:
        stereoChans = 0
    if bits == 16:
        # for pygame bits are signed for 16bit, signified by the minus
        bits = -16
    # defaults: 22050Hz, 16bit, stereo,
    mixer.init(rate, bits, stereoChans, buffer)
    sndarray.use_arraytype("numpy")
    setRate, setBits, setStereo = mixer.get_init()
    if setRate != rate:
        logging.warn('Requested sound sample rate was not poossible')
    if setBits != bits:
        logging.warn('Requested sound depth (bits) was not possible')
    if setStereo != 2 and stereo == True:
        logging.warn('Requested stereo setting was not possible')
Exemplo n.º 3
0
def main(arraytype=None):
    """play various sndarray effects

    If arraytype is provided then use that array package. Valid
    values are 'numeric' or 'numpy'. Otherwise default to NumPy,
    or fall back on Numeric if NumPy is not installed.

    """

    global zeros, int16, int32

    main_dir = os.path.split(os.path.abspath(__file__))[0]
    
    if arraytype is None:
        if 'numpy' in sndarray.get_arraytypes():
            sndarray.use_arraytype('numpy')
        elif 'numeric' in sndarray.get_arraytype():
            sndfarray.use_arraytype('numeric')
        else:
            raise ImportError('No array package is installed')
    else:
        sndarray.use_arraytype(arraytype)
    pygame.surfarray.use_arraytype(sndarray.get_arraytype())

    if sndarray.get_arraytype() == 'numpy':
        from numpy import zeros, int16, int32
    else:
        from Numeric import zeros, Int16 as int16, Int32 as int32

    print ("Using %s array package" % sndarray.get_arraytype())
    print ("mixer.get_init %s" % (mixer.get_init(),))
    inited = mixer.get_init()

    samples_per_second = pygame.mixer.get_init()[0]

    

    print (("-" * 30) + "\n")
    print ("loading sound")
    sound = mixer.Sound(os.path.join(main_dir, 'data', 'car_door.wav'))



    print ("-" * 30)
    print ("start positions")
    print ("-" * 30)

    start_pos = 0.1
    sound2 = sound_from_pos(sound, start_pos, samples_per_second)

    print ("sound.get_length %s" % (sound.get_length(),))
    print ("sound2.get_length %s" % (sound2.get_length(),))
    sound2.play()
    while mixer.get_busy():
        pygame.time.wait(200)

    print ("waiting 2 seconds")
    pygame.time.wait(2000)
    print ("playing original sound")

    sound.play()
    while mixer.get_busy():
        pygame.time.wait(200)

    print ("waiting 2 seconds")
    pygame.time.wait(2000)



    if 0:
        #TODO: this is broken.
        print (("-" * 30) + "\n")
        print ("Slow down the original sound.")
        rate = 0.2
        slowed_sound = slow_down_sound(sound, rate)

        slowed_sound.play()
        while mixer.get_busy():
            pygame.time.wait(200)


    print ("-" * 30)
    print ("echoing")
    print ("-" * 30)

    t1 = time.time()
    sound2 = make_echo(sound, samples_per_second)
    print ("time to make echo %i" % (time.time() - t1,))


    print ("original sound")
    sound.play()
    while mixer.get_busy():
        pygame.time.wait(200)

    print ("echoed sound")
    sound2.play()
    while mixer.get_busy():
        pygame.time.wait(200)


    sound = mixer.Sound(os.path.join(main_dir, 'data', 'secosmic_lo.wav'))

    t1 = time.time()
    sound3 = make_echo(sound, samples_per_second)
    print ("time to make echo %i" % (time.time() - t1,))

    print ("original sound")
    sound.play()
    while mixer.get_busy():
        pygame.time.wait(200)


    print ("echoed sound")
    sound3.play()
    while mixer.get_busy():
        pygame.time.wait(200)
Exemplo n.º 4
0
def main(arraytype=None):
    """play various sndarray effects

    If arraytype is provided then use that array package. Valid
    values are 'numeric' or 'numpy'. Otherwise default to NumPy,
    or fall back on Numeric if NumPy is not installed.

    """

    global zeros, int16, int32

    main_dir = os.path.split(os.path.abspath(__file__))[0]

    if arraytype is None:
        if 'numpy' in sndarray.get_arraytypes():
            sndarray.use_arraytype('numpy')
        elif 'numeric' in sndarray.get_arraytype():
            sndarray.use_arraytype('numeric')
        else:
            raise ImportError('No array package is installed')
    else:
        sndarray.use_arraytype(arraytype)
    pygame.surfarray.use_arraytype(sndarray.get_arraytype())

    if sndarray.get_arraytype() == 'numpy':
        from numpy import zeros, int16, int32
    else:
        from Numeric import zeros, Int16 as int16, Int32 as int32

    print("Using %s array package" % sndarray.get_arraytype())
    print("mixer.get_init %s" % (mixer.get_init(), ))
    inited = mixer.get_init()

    samples_per_second = pygame.mixer.get_init()[0]

    print(("-" * 30) + "\n")
    print("loading sound")
    sound = mixer.Sound(os.path.join(main_dir, 'data', 'car_door.wav'))

    print("-" * 30)
    print("start positions")
    print("-" * 30)

    start_pos = 0.1
    sound2 = sound_from_pos(sound, start_pos, samples_per_second)

    print("sound.get_length %s" % (sound.get_length(), ))
    print("sound2.get_length %s" % (sound2.get_length(), ))
    sound2.play()
    while mixer.get_busy():
        pygame.time.wait(200)

    print("waiting 2 seconds")
    pygame.time.wait(2000)
    print("playing original sound")

    sound.play()
    while mixer.get_busy():
        pygame.time.wait(200)

    print("waiting 2 seconds")
    pygame.time.wait(2000)

    if 0:
        #TODO: this is broken.
        print(("-" * 30) + "\n")
        print("Slow down the original sound.")
        rate = 0.2
        slowed_sound = slow_down_sound(sound, rate)

        slowed_sound.play()
        while mixer.get_busy():
            pygame.time.wait(200)

    print("-" * 30)
    print("echoing")
    print("-" * 30)

    t1 = time.time()
    sound2 = make_echo(sound, samples_per_second)
    print("time to make echo %i" % (time.time() - t1, ))

    print("original sound")
    sound.play()
    while mixer.get_busy():
        pygame.time.wait(200)

    print("echoed sound")
    sound2.play()
    while mixer.get_busy():
        pygame.time.wait(200)

    sound = mixer.Sound(os.path.join(main_dir, 'data', 'secosmic_lo.wav'))

    t1 = time.time()
    sound3 = make_echo(sound, samples_per_second)
    print("time to make echo %i" % (time.time() - t1, ))

    print("original sound")
    sound.play()
    while mixer.get_busy():
        pygame.time.wait(200)

    print("echoed sound")
    sound3.play()
    while mixer.get_busy():
        pygame.time.wait(200)
Exemplo n.º 5
0
#! /usr/bin/env python

from __future__ import division

import random
import wave

import numpy as np
from pygame import mixer, time, sndarray

mixer.init()
FRAMERATE, FORMAT, CHANNELS = mixer.get_init()

sndarray.use_arraytype('numpy')

_int16max = (2<<14) - 1


def play(sound):
    """Play a sound and wait for it to finish."""
    sound.play()
    while mixer.get_busy():
        time.wait(10)


def write_wav(sound, filename):
    f = wave.open(filename, 'wb')
    f.setframerate(FRAMERATE)
    f.setnchannels(2)
    f.setsampwidth(2)
    f.writeframes(sound.get_buffer().raw)