Exemplo n.º 1
0
# these will be used by sound.__init__.py
defaultInput = None
defaultOutput = audioDevice

travisCI = bool(str(os.environ.get('TRAVIS')).lower() == 'true')
logging.info("Loaded psychtoolbox audio version {}".format(
    audio.get_version_info()['version']))

# ask PTB to align verbosity with our current logging level at console
_verbosities = ((logging.DEBUG, 5), (logging.INFO, 4), (logging.EXP, 3),
                (logging.WARNING, 2), (logging.ERROR, 1))

for _logLevel, _verbos in _verbosities:
    if logging.console.level <= _logLevel:
        audio.verbosity(_verbos)
        break


def init(rate=48000, stereo=True, buffer=128):
    pass  # for compatibility with other backends


def getDevices(kind=None):
    """Returns a dict of dict of audio devices of specified `kind`

    kind can be None, 'input' or 'output'
    The dict keys are names, and items are dicts of properties
    """
    if sys.platform == 'win32':
        deviceTypes = 13  # only WASAPI drivers need apply!
Exemplo n.º 2
0
def run():
    # Samplerate:
    Fs = 48000

    # Tone frequency:
    f = 500

    ####################################################################
    ############## 1st test: play some tones ###########################
    ####################################################################

    audio.verbosity(5)

    # Open audio device: Default audio device, default opmode (playback), default
    # latency/timing-precision (low-latency, high precision), sample rate Fs,
    # stereo (2) channel output:
    stream = audio.Stream(freq=Fs, channels=2)
    stream.volume = 0.5  # PsychPortAudio('Volume', pahandle, 0.5)

    if True:
        # Length of audio vector - 1 secs of playback:
        n_samples = 1 * Fs
        # Build sinewave:
        a = np.sin(np.linspace(0, 2 * pi * f * n_samples, n_samples))
        # Replicated into two rows - One row for each stereo-channel, ie.
        # m'th row == m'th channel, n'th column = n'th sample frame:
        stereowav = np.array([a, a], order='f').transpose().astype('float32')
    else:
        # Make it less boring:
        fname = '/home/kleinerm/Music/test.wav'
        myfile = SoundFile(fname)
        stereowav = myfile.read(dtype='float32', always_2d=True)
        # stereowav = myfile.read()
        myfile.close()

    print('Type', type(stereowav), 'Shape', stereowav.shape, 'Datatype',
          stereowav.dtype, 'Order', stereowav.flags)

    t1 = GetSecs()
    stream.fill_buffer(
        stereowav)  # PsychPortAudio('FillBuffer', pahandle, stereowav);
    t2 = GetSecs()
    d1 = (1000 * (t2 - t1))
    print('FillBuffer Duration', d1, ' msecs.')

    # Start playback for one repetition (1), 1 seconds from now, wait for onset:
    stream.start(repetitions=1, when=GetSecs() + 1, wait_for_start=1)
    # PsychPortAudio('Start', pahandle, 1, GetSecs() + 1, 1)

    # Go into a loop that prints playback status once a second, while playback
    # is active:

    info = stream.status
    print(info, ' Spec ', info['Active'])
    while printstatus(stream):
        WaitSecs('YieldSecs', 1)  # didn't write a pythonic wrapper yet

    # Wait for sound to stop by itself, block until then:
    startTime, endPositionSecs, xruns, estStopTime = stream.stop()
    print('StartTime', startTime, 'secs. Stop time', estStopTime, 'secs.\n')

    # as before but 1.5*note_freq
    a = np.sin(np.linspace(0, 2 * pi * f * 1.5 * n_samples, n_samples))
    # Replicated into two rows - One row for each stereo-channel, ie.
    # m'th row == m'th channel, n'th column = n'th sample frame:
    stereowav2 = np.array([a, a], order='f').transpose().astype('float32')

    t1 = GetSecs()

    b1 = audio.Buffer(
        stream=stream, data=stereowav2
    )  # PsychPortAudio('CreateBuffer', pahandle, stereowav2);
    t2 = GetSecs()
    d2 = (1000 * (t2 - t1))
    print('CreateBuffer Duration', d2, ' msecs.')
    t1 = GetSecs()
    b1.fill_buffer(stereowav2)  # PsychPortAudio('FillBuffer', pahandle, b1)
    t2 = GetSecs()
    print('FillBuffer Duration', (1000 * (t2 - t1)), ' msecs.')
    print('d2 / d1 = ', d2 / d1)

    # PsychPortAudio('Start', pahandle, 1, GetSecs() + 1, 1)
    stream.start(when=GetSecs() + 1, wait_for_start=1)
    WaitSecs('YieldSecs', 2)
    # [startTime, endPositionSecs, xruns, estStopTime] = PsychPortAudio('Stop', pahandle, 1);
    startTime, endPositionSecs, xruns, estStopTime = stream.stop()
    print('StartTime', startTime, 'secs.xx Stop time', estStopTime, 'secs.\n')

    # Close sound device:
    stream.close()  # PsychPortAudio('Close', pahandle);