[open] Audio recording with loop
Hi all,
In my experiment, participants have to give descriptions of pictures, and I want OpenSesame to make an audio recording of every description. Each picture is visible for 5000 ms, and at the onset of each picture, OpenSesame starts to record the description of the picture for 5 seconds. So far so good, but since I have about 20 pictures, I had to include a loop + sequence, and this where things go wrong. After making the recording, OpenSesame doesn't automatically move on to the next picture in the loop. Instead, I have to press a key in order to go to the next picture and sound recording.
This is the Python code I used:
""" Record a few seconds of audio and save to a WAVE file. """
import pyaudio
import wave
import sys
from openexp.keyboard import keyboard
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
OUTPUT_FOLDER = "C:\Program Files\OpenSesame"
#WAVE_OUTPUT_FILENAME = "ppn_%d_%d_%s.wav" % (self.get('subject_nr'), self.get('count_inline_script'), self.get('plaatje'))
OUTPUT_FILE = "ppn_%d_%s.wav" % (self.get('subject_nr'), self.get('pict'))
FINAL_FILENAME = OUTPUT_FOLDER + OUTPUT_FILE
p = pyaudio.PyAudio()
stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = chunk)
print "* recording"
# Short timeout so get_key() doesn't block
my_keyboard = keyboard(exp, timeout=2)
all = []
while my_keyboard.get_key()[0] == None: # Loop until a key has been pressed
#all = []
#for i in range(0, RATE / chunk * RECORD_SECONDS):
data = stream.read(chunk)
all.append(data)
print "* done recording"
stream.close()
p.terminate()
# write data to WAVE file
data = ''.join(all)
wf = wave.open(FINAL_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
How can I get OpenSesame to move on to the next picture after the audio recording? Any help would be appreciated.
Thanks
Patrick
Comments
Hi Patrick,
Your script currently records audio until a key has been pressed. If this is not intentional then that's probably what you're running into.
If you change this bit ...
... to this ...
... the script instead records for a fixed amount of time.
As an aside, you might also be interested in the soundrecorder plug-in:
Hope this helps!
Cheers,
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Sebastiaan,
I replaced my code with yours and it's working! However, I had to set the sketchpad duration to 0 ms, otherwise OpenSesame would show the picture for 10 seconds, while only recording the last 5 seconds of the picture's appearance. I'm not Python savy, so it puzzled me at first. Did this happen because I added 5 seconds to picture duration, while the Python code already told OpenSesame to show the picture and record the sound for 5 seconds?
Thanks for your help.
Regards,
Patrick
The sketchpad duration determines for how long the sketchpad item will pause the experiment. If you set it to, say, 5000, it will pause the experiment for 5 s before advancing to the next item. If the next item is the audio recording inline_script, this means that your experiment will start recording only 5 s after the sketchpad has been shown. Does that make sense? It's a purely serial approach.
So if you want to record immediately after the sketchpad appears, you indeed set the sketchpad duration to 0.
Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Sebastiaan,
Seems I missed your last response, sorry for that.
Your tips worked perfectly on my old laptop, but if I want to record audio by using a similar python code on my new laptop (windows 8), OpenSesame gives the following error message:
Error: Inline script error
In: _inline_script (run phase)
File "wave.pyo", line 303, in __init
Python traceback:
IOError: [Errno 13] Permission denied: u'C:\Program Files (x86)\OpenSesameppn_0_1_figures.jpg.wav'
Full traceback in debug window
I thought changing part of the output folder line from 'Program Files' to 'Program Files (x86)' would solve the problem, but obviously it hasn't. I used the following Python code:
Do you have any idea what's going on?
Regards,
Patrick
Hi Patrick,
Edit: The error is actually due to a missing slash, as Edwin points out below.
This is probably a permissions issue: You don't have write permissions in the OpenSesame folder (this is quite common). The solution would simply be to select a folder where you do have write permissions, such as your 'My Documents' folder.
Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Sebastiaan,
I changed 'Program Files (x86)\Opensesame' to 'Mijn Documenten', but it did not work either. It showed the following error message:
Error: Inline script error
In: _inline_script (run phase)
File "wave.pyo", line 303, in __init
Python traceback:
IOError: [Errno 13] Permission denied: u'C:\Mijn documentenppn_0_7_happy.jpg.wav'
Full traceback in debug window
However, I believe you once told me that the soundrecorder plugin could to the job too. Therefore, I used it to circumvent the Python code problem, and it worked perfectly.
Regards,
Patrick
Judging by your error message, I'd say you're trying to access a folder that does not exist: 'C:\Mijn documentenppn_0_7_happy.jpg.wav'.
Usually, the 'My Documents' folder is not located directly in C: (it should be something like 'C:/users/USERNAME/My Documents', if I recall correctly). And even if it is, 'Mijn documentenppn_0_7_happy.jpg.wav' is probably not an existing folder name. Try adding an extra '\' to your folder name, e.g.
OUTPUT_FOLDER = "C:\Program Files (x86)\OpenSesame\"
.Hi,
I tried the script and it runs, but didn't record anything. The files are 0 seg long. Any ideas what may be the problem?