Howdy, Stranger!

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

Supported by

record audio until some keypress

edited May 2016 in OpenSesame

I would like to use the following code found elsewhere to record participants' voice using mac. This code allows me to record their voice exactly for three seconds, but I would rather record until they press some key (for example, space). Is there any way to implement this? Thanks

import pyaudio

import wave

import sys

chunk = 1024

FORMAT = pyaudio.paInt16

CHANNELS = 1

RATE = 44100

RECORD_SECONDS = 3

WAVE_OUTPUT_FILENAME = "/Users/myname/Desktop/%d_%d.wav" % (self.get('subject_nr'), self.get('count_recorder'))

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = chunk)

print "* recording"
all = []
for i in range(0, RATE / chunk * RECORD_SECONDS):
data = stream.read(chunk)
all.append(data)
print "* done recording"

stream.close()

p.terminate()

data = ''.join(all)

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')

wf.setnchannels(CHANNELS)

wf.setsampwidth(p.get_sample_size(FORMAT))

wf.setframerate(RATE)

wf.writeframes(data)

wf.close()

Comments

Sign In or Register to comment.