How to use the psychtoolbox.audio.Stream function in psychtoolbox

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 / 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)
github kleinerm / Psychtoolbox-3 / PsychPython / demos / ppatest_pythonic.py View on Github external
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);

    # Start audio capture immediately and wait for the capture to start.
    # We set the number of 'repetitions' to zero,
    # i.e. record until recording is manually stopped.
    recording.start(repetitions=0, stop_time=None)  # PsychPortAudio('Start', pahandle, 0, 0, 1);

    # fetch just under 10s from that buffer
    # PsychPortAudio('GetAudioData', pahandle, None, 9.9, None);
    audiodata, a, b, c = recording.get_audio_data(in_secs=9.9)
    print('Type', type(audiodata), 'Shape', audiodata.shape, 'Datatype',
          audiodata.dtype);

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