Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Vocal Resposne

edited December 2020 in OpenSesame

Hello,

I am building experiments in which I want the participants to give a vocal response instead of a keyboard response. I do not need a recording of their responses, however, I need the onset of the vocal response to be recognized in order to log the RT, and move to the next slide in the sequence.

I am having trouble getting this done. It seems that PyAudio does not work with Pyton 3.6.6

I am attaching my inline script.

I would greatly appreciate the help.


script:

import pyaudio
import struct
import math

# A low threshold increases sensitivity, a high threshold
# reduces it.
sound_threshold = 0.5
# Maximum response time
timeout = 5000

FORMAT = pyaudio.paInt16
SHORT_NORMALIZE = (1.0/32768.0)
CHANNELS = 2
RATE = 44100
INPUT_BLOCK_TIME = 0.01
INPUT_FRAMES_PER_BLOCK = int(RATE*INPUT_BLOCK_TIME)
chunk=1024

def get_rms(block):

"""Get root mean square as a measure of loudness"""

count = len(block)/2
format = "%dh" % (count)
shorts = struct.unpack( format, block )
sum_squares = 0.0
for sample in shorts:
n = sample * SHORT_NORMALIZE
sum_squares += n*n
return math.sqrt( sum_squares / count )

# Open the mic
stream = pyaudio.PyAudio().open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
input_device_index=0,
frames_per_buffer=INPUT_FRAMES_PER_BLOCK
)

# Listen for sounds until a sound is detected or a timeout occurs.
start_time = clock.time()
while True:
if clock.time() - start_time >= timeout:
response_time = timeout
response = u'timeout'
var.loudness = None
break
try:
block = stream.read(chunk)
except IOError as e:
print (e)
loudness = get_rms(block)
if loudness > sound_threshold:
response_time = clock.time() - start_time
response = u'detected'
var.loudness = loudness
break

# Process the response
set_response(response=response, response_time=response_time)

# Close the audio stream
stream.close()
pyaudio.PyAudio().terminate()


Comments

Sign In or Register to comment.