Howdy, Stranger!

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

Supported by

[solved] Recording speech response

edited May 2012 in OpenSesame

Hi,

First off, thanks for all the efforts you're putting into this package. It looks and works amazing.

I want to do a sort of picture naming experiment; a picture appears on the screen and the participant has to describe it. In my case, recording for abour five seconds during the presentation of the picture would be ideal. I tried to build on the code of the voicekey trigger example, but I get stuck at the point where I need to write the data to a wave file. The only way I've seen this done is by using the wave library (import wave), but OpenSesame complains when I put that in the code..

Any help appreciated.

Martijn

Comments

  • edited January 2014

    Hi Martijn,

    First off, thanks for all the efforts you're putting into this package. It looks and works amazing.

    Thanks!

    As luck would have it, the PyAudio documentation has an example that does exactly what you want to do, and it simply works (for me) if you paste it into an OpenSesame inline_script.

    I changed it a tiny bit to use the subject_nr and count_inline_script to determine the output file name, but aside from that the example comes straight from: http://people.csail.mit.edu/hubert/pyaudio/

    """ Record a few seconds of audio and save to a WAVE file. """
    
    import pyaudio
    import wave
    import sys
    
    chunk = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    RATE = 44100
    RECORD_SECONDS = 5
    WAVE_OUTPUT_FILENAME = "%d_%d.wav" % (self.get('subject_nr'), self.get('count_inline_script'))
    
    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()
    
    # write data to WAVE file
    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()
    

    Good luck!

    Sebastiaan

  • edited 2:18PM

    Hi Sebastiaan,

    Thanks for the reply, I tried your script (and my own script that looks very much the same), but Opensesame complains it cannot find pyaudio (on my mac) and on windows it can't find the wave module:

    Traceback (most recent call last):
    File "libopensesame/inline_script.pyc", line 112, in run
    File "", line 3, in
    ImportError: No module named pyaudio

    Traceback (most recent call last):
    File "libopensesame/inline_script.pyc", line 112, in run
    File "", line 3, in
    ImportError: No module named wave

    Do I need to install something separately?

    Thanks again,
    Martijn

  • edited 2:18PM

    Ah right, of course, the 'wave' module isn't included! You cannot really install additional packages when using the binary distribution, but you can when using the Python portable package. I haven't done this myself, but Edwin will probably be able to help you, if you ask him on the forum:

    Alternatively: I'll also include the module in the next pre-release of OpenSesame, so you could wait until 0.26-pre7 (or later) appears and you could use that.

  • edited 2:18PM

    Thanks, I'll look into the portable package. Is the pyaudio package also not included in the mac version, because that is the point where OpenSesame stops on my mac.

  • edited 2:18PM

    Indeed, PyAudio is not included in the Mac OS package. I'll make a note of it, because I think it should be included by default.

  • edited 2:18PM

    That would be great, being able to record (chunks of) speech would, especially in combination with voicekey measurements, be very welcome to a lot of people in psycholinguistics and experimental linguistics.

  • edited 2:18PM

    Just to let you know; I tried this with 0.26 pre and it works in Ubuntu, but not in windows yet (probably because the wave module hasn't been included there yet). I haven't tested it on the mac yet.

  • edited March 2012

    You rang?

    PyAudio is a single Python-file, right? Simply add it to this directory *\opensesameportable-0.25-1-win32\PortablePython_1.1_py2.6.1\App\Lib\site-packages of the portable 0.25 version (downloadable here: http://cogsci.nl/esdalmaijer/ ) and you should be ready to experiment! Same goes for the 0.26 (pre6) portable version, since I haven't included PyAudio in that one as well.

    Please do let me know if it works, I do not have PortAudio installed, so I can't check.

    Cheers!

  • edited 2:18PM

    Hi Edwin,

    Sorry for the late reply. Work got in the way. That is a great suggestion and I will try it as soon as I get the change. On the other hand, I understand that the next version of Opensesame will support pyaudio and wave, so the problem might soon be solved by default. Nevertheless it is good to know that it is fairly easy to plugin additional python modules in portable Opensesame.

    Thanks again for your reply, and I'll keep you posted,

    Martijn

  • edited 2:18PM

    Hi All,

    It's me again. I just tested speech recoding with EE. Works like a charm!

    Martijn

  • edited June 2012

    Dear Sebastiaan, (and Edwin and goodbeem)
    I have been playing around with OpenSesame for a while now: not a coding-savvy experimenter, I enjoy it immensely. Lovely job, thank you so much!

    I am putting together a phonetics study and want to record the sentences read from dialogues of four lines. However, the utterances will vary in length, and so the recording should run until 'keypress', at which point the next line would be played back:

    trial_sequence:
    sketchpad [dialogue], duration:keypress   # text shows throughout. (related question below)
    sampler: sound file:[dialogue]1.wav, duration:keypress # play recorded line 1
    record+store: response[dialogue]1__[subject_nr].wav #  record for me until subject presses key!
    sampler: sound file:[dialogue]2.wav, ... #...
    record+store response[dialogue]2__[subject_nr].wav #
    

    so, how would I include this keypress feature here?

    regarding the multi-line display, I posted a question in the (inactive) "text which appears" thread.

    Thanks again for the efforts with this great tool.

  • edited January 2014

    Hi Christerwi,

    You can change the stop criterion to anything you like. Above it was a fixed duration, but the code below shows how you can stop recording after a key has been pressed. This script is just a snippet that you need to combine with the recording part of the full script posted above.

    For more information on keyboard functions, see also http://osdoc.cogsci.nl/python-inline-code/keyboard-functions

    print "* record until keypress"
    from openexp.keyboard import keyboard
    # 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
        data = stream.read(chunk) # Record data
        all.append(data) # Add the data to a buffer (a list of chunks)
    print "* done recording"
    

    Good luck!

    Cheers,
    Sebastiaan

  • edited 2:18PM

    Hi everyone, I tried to follow the instructions here using a Mac OS installation and encountered some problems, detailed in this thread:

    http://forum.cogsci.nl/index.php?p=/discussion/comment/699#Comment_699

    The relevant part is copied here:

    "I installed PyAudio using the mpkg script provided on the PyAudio site, and then I went through the process again and did the manual installation with PortAudio. Either way I do it, I get the same error: "ImportError: No module named pyaudio".

    I have gone through the process of copying the pyaudio installation to the directories listed in the prefs under Plug-In folders, a few of which had to be created because the installation doesn't seem to generate them, and even after a restart, opensesame.app cannot find the installation of pyaudio."

Sign In or Register to comment.