Howdy, Stranger!

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

Supported by

[solved] Record audio response with variable time span

edited January 2014 in OpenSesame

Hi.

I was hoping someone here on the forum might have the knowledge and time to help me with a problem of mine.

I have an experiment where participants will be listening to some audio stimuli and afterwards describe them verbally. I want to record their response, but the recording phase will not have a set time span. Essentially, when the participant feel they have said what they want to say, they will click a button and progress to the next audio stimulus and recording phase.

Is there any way to accomplish such a thing in OpenSesame? I looked at the inline_script described in http://forum.cogsci.nl/index.php?p=/discussion/107/solved-recording-speech-response/p1, but it only allows for a fixed recording time. The audio plug-in essentially does what I want to accomplish, but it relies on Pymedia. Pymedia is not included in the Ubuntu 13.10 repositories, and I tried compiling Pymedia from source, but ran into all sorts of problems. Mainly that it required a lot of old audio libraries and gcc-3.4.

In the builder I image it would look something like this.

  • Sequence.

    • Sampler (plays an audio file).
    • Start audio recording.
    • Feedback (wait for keypress)
    • Stop audio recording and write to file.
  • Next sequence.

Any help is much appreciated

Njord

Comments

  • edited 2:02PM

    Hi Njord,

    A little bit down in the same discussion that you're referring to, you will find a little code snippet that you can use to stop recording when a key is pressed. It sounds like this is exactly what you're looking for:

    Cheers!
    Sebastiaan

  • edited January 2014

    Thank you very much for your help, Sebastiaan.

    I got the script working and it is recording .wav files without a hitch, but I ran into another problem.

    In the folder in which my OpenSesame experiment is saved, I want to create a sub-folder named after the "Participant ID". All the recorded sound files from one participant will then be saved in this folder.

    In the script, I have these statements accomplishing this task:

    SUBDIR = "./%d/" % self.get('subject_nr')
    os.makedirs(SUBDIR)
    WAVE_OUTPUT_FILENAME = "%d_%d.wav" % (self.get('subject_nr'),
    self.get('count_inline_script'))
    wf = wave.open( "./" + SUBDIR + WAVE_OUTPUT_FILENAME, 'wb')
    

    This works well, except that the sub-directory is created in my $HOME folder on Ubuntu, and not in the folder where the experiment resides.

    I know that this could easily be solved by specifying absolute paths, but I am going to move the experiment to a lab computer when it is finished. Therefore I want to use variable paths, so that I don't have to go back and change all the paths.

    Do you know, or somebody else, how this can be accomplished?

    • Njord
  • edited 2:02PM

    Hi Njord,

    You could determine the path of the directory in which your OpenSesame is placed by using the following piece of Python inline code:

    currentPath = self.get('experiment_path')
    

    Does that help?

    Cheers,

    Lotje

    Did you like my answer? Feel free to Buy Me A Coffee :)

  • edited 2:02PM

    lvanderlinden, that was just the piece of code I needed. :)

  • edited February 2014

    I though I would share my finished inline script so that others that might have a similar problem can use and modify it to their needs.

    I am using this script to collect open-ended responses to a stimulus. When the participant is done giving their response and press the space bar, the experiment continues. But I also wanted to insert a time out, so that if one participant doesn't press the key, falls asleep, is uncooperative, the experiment will not halt at this part.

    It has to be inserted into an inline_script to work.

    """ Record a pre-defined amout of audio  and save to a WAVE file. """
    
    import pyaudio
    import wave
    from openexp.keyboard import keyboard
    
    # Name of sub-directory where WAVE files are placed
    current_experiment_path = self.get('experiment_path')
    subdir_recording = '/recording/'
    
    # Variables for Pyaudio
    chunk = 1024
    format = pyaudio.paInt16
    channels = 1
    rate = 44100
    
    # Set variable for the labelling of the recorded WAVE file.
    wave_output_filename = '%s.wav' % self.get('subject_nr')
    
    #Audio recording stuff
    p = pyaudio.PyAudio()
    
    stream = p.open(format = format,
                    channels = channels,
                    rate = rate,
                    input = True,
                    frames_per_buffer = chunk)
    
    # Import keyboard and set accepted key
    recorder_keyboard = keyboard(exp, keylist=['space'])
    
    # Determine the timeout in ms
    timeout = 60000
    
    # Create an empty list for audio recording
    frames = []
    
    # Determine the timestamp of the start of the response interval
    start_time = self.time()
    current_time = self.time()
    
    # Record audio until space is pressed
    while (current_time - start_time) < timeout:
    
        # Record data audio data
        data = stream.read(chunk)
        # Add the data to a buffer (a list of chunks)
        frames.append(data)
    
        # Get new timestamp
        current_time = self.time()
    
        # Check if space key has been pressed
        key = recorder_keyboard.get_key(timeout=0)[0]
    
        # If space is pressed, advance to next part of the experiment
        if key == 'space':
            # Record data audio data
            data = stream.read(chunk)
            # Add the data to a buffer (a list of chunks)
            frames.append(data)
            break
    
    else:
        # Record data audio data
        data = stream.read(chunk)
        # Add the data to a buffer (a list of chunks)
        frames.append(data)
    
    # Close the audio recording stream
    stream.close()
    p.terminate()
    
    # write data to WAVE file
    wf = wave.open(current_experiment_path + subdir_recording + wave_output_filename, 'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(p.get_sample_size(format))
    wf.setframerate(rate)
    wf.writeframes(''.join(frames))
    wf.close()
    
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