Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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()
# 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);
####################################################################
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()
# 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:
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);
# 1) On Windows, the Windows Query Performance Counter API is used using
# ctypes access.
# 2) On other OS's, if the Python version being used is 2.6 or lower,
# time.time is used. For Python 2.7 and above, the timeit.default_timer
# function is used.
try:
import psychtoolbox
havePTB = True
except ImportError:
havePTB = False
if havePTB:
# def getTime():
# secs, wallTime, error = psychtoolbox.GetSecs('allclocks')
# return wallTime
getTime = psychtoolbox.GetSecs
elif sys.platform == 'win32':
from ctypes import byref, c_int64, windll
_fcounter = c_int64()
_qpfreq = c_int64()
windll.Kernel32.QueryPerformanceFrequency(byref(_qpfreq))
_qpfreq = float(_qpfreq.value)
_winQPC = windll.Kernel32.QueryPerformanceCounter
def getTime():
_winQPC(byref(_fcounter))
return _fcounter.value / _qpfreq
elif sys.platform == "darwin":
# Monotonic getTime with absolute origin. Suggested by @aforren1, and
# copied from github.com/aforren1/toon/blob/master/toon/input/mac_clock.py
import ctypes
_libc = ctypes.CDLL('/usr/lib/libc.dylib', use_errno=True)
# create helper class to store data