Howdy, Stranger!

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

Supported by

[open] Audio recording with loop

edited March 2013 in OpenSesame

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

  • edited 7:53PM

    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 ...

    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)
    

    ... to this ...

    for i in range(0, RATE / chunk * RECORD_SECONDS):
        data = stream.read(chunk)
        all.append(data)
    

    ... 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

  • edited March 2013

    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

  • edited 7:53PM

    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

  • edited 7:53PM

    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:


    """ 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 (x86)\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 = [] 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()

    Do you have any idea what's going on?

    Regards,
    Patrick

  • edited May 2013

    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

  • edited 7:53PM

    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

  • edited May 2013

    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\".

  • edited May 2018

    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?

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games