How to use psychtoolbox - 10 common examples

To help you get started, we’ve selected a few psychtoolbox examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kleinerm / Psychtoolbox-3 / PsychPython / demos / hidtest_pythonic.py View on Github external
def run():

    dev = kbDevices[0]
    # KbQueueTest
    ptb.WaitSecs('YieldSecs', 1)
    keyboard = hid.Keyboard(dev['index'])

    # keyboard.queue_create(num_slots=10000)  # done implicitly but can redo

    # keyboard.start_trapping() # stops keys going to stdout but sketchy!

    keyboard.queue_start()
    t0 = ptb.GetSecs()
    while ptb.GetSecs() < t0+5:
        ptb.WaitSecs('YieldSecs', 0.00001)
        if keyboard.flush():
            evt = keyboard.queue_get_event()
            print(evt)
    keyboard.queue_stop()
github kleinerm / Psychtoolbox-3 / PsychPython / demos / hidtest_pythonic.py View on Github external
def run():

    dev = kbDevices[0]
    # KbQueueTest
    ptb.WaitSecs('YieldSecs', 1)
    keyboard = hid.Keyboard(dev['index'])

    # keyboard.queue_create(num_slots=10000)  # done implicitly but can redo

    # keyboard.start_trapping() # stops keys going to stdout but sketchy!

    keyboard.queue_start()
    t0 = ptb.GetSecs()
    while ptb.GetSecs() < t0+5:
        ptb.WaitSecs('YieldSecs', 0.00001)
        if keyboard.flush():
            evt = keyboard.queue_get_event()
            print(evt)
    keyboard.queue_stop()
github kleinerm / Psychtoolbox-3 / PsychPython / demos / hidtest_pythonic.py View on Github external
def run():

    dev = kbDevices[0]
    # KbQueueTest
    ptb.WaitSecs('YieldSecs', 1)
    keyboard = hid.Keyboard(dev['index'])

    # keyboard.queue_create(num_slots=10000)  # done implicitly but can redo

    # keyboard.start_trapping() # stops keys going to stdout but sketchy!

    keyboard.queue_start()
    t0 = ptb.GetSecs()
    while ptb.GetSecs() < t0+5:
        ptb.WaitSecs('YieldSecs', 0.00001)
        if keyboard.flush():
            evt = keyboard.queue_get_event()
            print(evt)
    keyboard.queue_stop()
github kleinerm / Psychtoolbox-3 / PsychPython / demos / ppatest_pythonic.py View on Github external
# as before but 1.5*note_freq
    a = np.sin(np.linspace(0, 2 * pi * f * 1.5 * n_samples / Fs))

    # 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 = [a, a]
    stereowav2 = np.transpose(stereowav2)
    stereowav2 = stereowav2.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)
    # [startTime, endPositionSecs, xruns, estStopTime] = PsychPortAudio('Stop', pahandle, 1);
    startTime, endPositionSecs, xruns, estStopTime = stream.stop()
    print('StartTime', startTime, 'secs. Stop time', estStopTime, 'secs.\n');

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


    ####################################################################
github kleinerm / Psychtoolbox-3 / PsychPython / demos / hidtest_pythonic.py View on Github external
def run():

    dev = kbDevices[0]
    # KbQueueTest
    ptb.WaitSecs('YieldSecs', 1)
    keyboard = hid.Keyboard(dev['index'])

    # keyboard.queue_create(num_slots=10000)  # done implicitly but can redo

    # keyboard.start_trapping() # stops keys going to stdout but sketchy!

    keyboard.queue_start()
    t0 = ptb.GetSecs()
    while ptb.GetSecs() < t0+5:
        ptb.WaitSecs('YieldSecs', 0.00001)
        if keyboard.flush():
            evt = keyboard.queue_get_event()
            print(evt)
    keyboard.queue_stop()
github kleinerm / Psychtoolbox-3 / PsychPython / demos / ppatest_pythonic.py View on Github external
# stereowav = np.zeros([10000,2], dtype='f')
    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(pahandle):
        WaitSecs('YieldSecs', 1)  # didn't write a pythonic wrapper yet

    # Wait for sound to stop by itself, block until then:
github kleinerm / Psychtoolbox-3 / PsychPython / demos / ppatest_pythonic.py View on Github external
stereowav2 = stereowav2.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)
    # [startTime, endPositionSecs, xruns, estStopTime] = PsychPortAudio('Stop', pahandle, 1);
    startTime, endPositionSecs, xruns, estStopTime = stream.stop()
    print('StartTime', startTime, 'secs. Stop time', estStopTime, 'secs.\n');

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


    ####################################################################
    ############## 2nd test: Audio capture:#############################
    ####################################################################
    recording = audio.Stream(mode=2, freq=Fs, channels=2);  # pahandle = PsychPortAudio('Open', [], 2, 0, Fs, 2);

    # Preallocate an internal audio recording  buffer with a capacity of 10 seconds:
    recording.get_audio_data(10)  # PsychPortAudio('GetAudioData', pahandle, 10);
github kleinerm / Psychtoolbox-3 / PsychPython / demos / hidtest_pythonic.py View on Github external
def run():

    dev = kbDevices[0]
    # KbQueueTest
    ptb.WaitSecs('YieldSecs', 1)
    keyboard = hid.Keyboard(dev['index'])

    # keyboard.queue_create(num_slots=10000)  # done implicitly but can redo

    # keyboard.start_trapping() # stops keys going to stdout but sketchy!

    keyboard.queue_start()
    t0 = ptb.GetSecs()
    while ptb.GetSecs() < t0+5:
        ptb.WaitSecs('YieldSecs', 0.00001)
        if keyboard.flush():
            evt = keyboard.queue_get_event()
            print(evt)
    keyboard.queue_stop()
github kleinerm / Psychtoolbox-3 / PsychPython / demos / ppatest_pythonic.py View on Github external
audiodata.dtype);

    recording.close()  # PsychPortAudio('Close', pahandle);

    playback = audio.Stream(freq=Fs, channels=2);
    playback.volume = 1.0  # PsychPortAudio('Volume', pahandle, 1);

    # Fill in audio matrix for playback:
    playback.fill_buffer(audiodata)  # PsychPortAudio('FillBuffer', pahandle, audiodata);

    # Start playback for one repetition (1), 5 seconds from now, wait for sound onset:
    playback.start(1, GetSecs() + 1, 1)  #  PsychPortAudio('Start', pahandle, 1, GetSecs() + 1, 1)

    # Go into a loop that prints playback status once a second, as long as playback
    # is active:
    keyboard = hid.Keyboard()
    info = playback.status  # PsychPortAudio('GetStatus', pahandle);
    print(info, ' Spec ', info['Active']);
    while playback.status['Active'] and not Keyboard.check()[0]:
        WaitSecs('YieldSecs', 1);

    # Stop playback:
    # [startTime, endPositionSecs, xruns, estStopTime] = PsychPortAudio('Stop', pahandle)
    startTime, endPositionSecs, xruns, estStopTime = playback.stop()
    print('StartTime', startTime, 'secs. Stop time', estStopTime, 'secs.\n');

    # Close sound device:
    playback.close()  # PsychPortAudio('Close', pahandle);
github kleinerm / Psychtoolbox-3 / PsychPython / demos / ppatest_pythonic.py View on Github external
# 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 / Fs))

    # 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 = [a, a]

    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:
        # Fill in audio matrix for playback: columns = audio channels, rows = samples
        # Do it step-by-step for performance testing:
        stereowav = np.array(stereowav, order='f')
        # print('Type', type(stereowav), 'Shape', stereowav.shape, 'Datatype',
        #       stereowav.dtype, 'Order', stereowav.flags);
        stereowav = np.transpose(stereowav)
        # float32 is a tad faster than the default float64:
        stereowav = stereowav.astype('float32')
        # stereowav = np.zeros([10000,2], dtype='f')
    else:
        # Make it less boring:
        fname = '/home/kleinerm/Music/test.wav'
        myfile = SoundFile(fname)