Welcome!

Sign in with your CogSci, Facebook, Google, or Twitter account.

Or register to create a new account.

We'll use your information only for signing in to this forum.

Supported by

calling up the sound recorder inline

Hi everyone,

I want to call up the sound recorder in an inline script - is that possible? It seems really simple, but I can't find the answer anywhere...

The experiment is a simple random automated picture naming test & looks as below. It all works as it should in terms of displaying the stimuli; I just can't figure out how to call up the recorder in the script. I put in comments in the run phase where recording should start/stop.

I'm grateful for any advice!

Prepare phase:

    from random import shuffle
    global pic_list
    pic_list = range(1,100)
    shuffle (pic_list)

    global pic_list
    pic = pic_list.pop()

    #prepare sampler
    src=pool['ding1295ms.wav']
    my_sampler = sampler(src, volume=.4)

    #prepare fixdod
    fixdot_canvas = canvas()
    fixdot_canvas.fixdot(color='white')

    #prepare cue canvas
    cue_canvas=canvas()
    path=exp.pool[u'cue_wym.png'] #cue tells participant what language to name the pic in
    cue_canvas.image(path, scale=.1)

    #prepare target canvas
    target_canvas = canvas(background_color=u'thistle')
    path=exp.pool[u'RANstim_%2d.png' % pic] 
    target_canvas.image(path, scale=.2

    #prepare blank canvas
    blank_canvas=canvas()

Run phase:

    my_sampler.play()

    fixdot_canvas.show()
    clock.sleep(995)

    cue_canvas.show()
    clock.sleep(495)

    #Recording should start here & have to define .wav format and the output file (path + pic number)

    target_canvas.show()
    clock.sleep.(495)

    blank_canvas.show()
    clock.sleep(3995)

    #stop recording

Comments

  • BaaBobBaaBob Posts: 22

    ...of course, I also have to add the logger as well...

  • BaaBobBaaBob Posts: 22

    I've been fiddling, but still no properly running experiment. Here are my attempts so far:

    —— I coped the script from the recorder item...

        set samplerate 44100
        set recording Yes
        set output_file "C:\\Users\\me\\Documents\\results/r"
        set file_exists_action "Append suffix to filename"
        set compression "None (wav)"
        set channels Mono
    

    ... and inserted it in the run phase where I want recording to start. I also added soundrecorder.stop() at the end of the script.

    This threw a syntax error, pointing to the last e in samplerate.

    —— I tried to put the fixdot & cue in one script within a sequence, followed by the start_recorder, then a script for the target & blank canvas, then the stop_recoreder. There were two problems with this:
    a. the recordings were split second, not the 4.5 seconds from the target display until the end of the blank canvas as I intended (I suppose this is because clock.sleep pauses the whole experiment rather than determining the canvas display times while the experiment continues (is that right?)).
    b. there was only one wav file, despite the "append suffix to filename" setting.

    I hope someone has a suggestion for getting this experiment in order! Thanks in advance.

  • BaaBobBaaBob Posts: 22

    Forgive my impatience @sebastiaan, do you have any ideas about this? My other experiments are working without a hitch & I really enjoy the software. I'm off to the field in a couple days and I'm stressing out that I can't get this particular one to work. It's probably something really basic – I started with Python and OpenSesame at the same time. If you have some idea about why it isn't working, I'd really appreciate it!!!

  • eduardeduard Posts: 1,001

    Hi Bob,

    Could you upload the experiment? (And necessary files in case it depends on something)

    Eduard

  • BaaBobBaaBob Posts: 22

    Gladly! But the file is too big to post here - 201mb. Can I email it to you somehow?

  • eduardeduard Posts: 1,001

    upload it somewhere and post the link here. Or reduce the size of it. 1-2 trials would be enough to find out what is going on, I wouldn't need all the necessary files to look at the error you get, right?

    Eduard

  • BaaBobBaaBob Posts: 22

    OK, here it is.

    https://we.tl/zBEKz1hT8a

    It's not so much an error. In the first post above, I just couldn't figure out how to call on the sound recorder from the script. Later, I tried a workaround by breaking up the script in two parts and adding the recorder start and stop in the gui, but then only one wav is logged and it's way shorter than the stimulus display and the blank canvas (I thought maybe clock.sleep() also stops the recorder..?). In any case, my inexperience is getting the better of me - I'd appreciate any advice!!

  • eduardeduard Posts: 1,001

    I'm very sorry, but I have to skip this one. I just tried to install the plugins (and in particular pymedia) on my system but kept on failing. Therefore, I can't try your experiment and see what is going on. Sorry :disappointed:

    Eduard

  • BaaBobBaaBob Posts: 22

    OK, thanks for trying!!!

    Do you happen to know:
    a) if there's generally a particular command to activate the sound recorder in an inline script?
    and
    b) if clock.sleep() pauses everything, incl. the recorder, and if so whether there's another .sleep that would not pause the recorder or a way to set the display duration in the script without sleeping the clock?

    I've tried searching all this stuff in the documentation and forum, but with no luck...

  • eduardeduard Posts: 1,001

    b) clock.sleep() pauses the experiment, but it doesn't stop processes that were started and don't depend on Opensesame anymore, e.g. playback does not stop. therefore, I don't think that the clock.sleep() causes your issues. But still, you can try it out.

    a) Don't think so, but not sure.

    Eduard

  • BaaBobBaaBob Posts: 22

    ...or maybe another way to record audio without the recorder plugin? Some of the older threads discuss something like this, but I think with previous releases of the program.

  • sebastiaansebastiaan Posts: 2,856

    Hi BaaBob,

    (A quick reply without having fully read this discussion.) The sound_recorded plugin, which was created by @Daniel, is not as far as I know intended to be used from a script. However, if you search this forum for terms such as 'pyaudio record', you will find a few alternative approaches. For example in this discussion:

    Cheers!
    Sebastiaan

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • BaaBobBaaBob Posts: 22

    Thanks, @sebastiaan !!

    I have seen some of those threads about pyaudio; as a sort of noob, it all seemed way more complicated than I needed. I'll give it a go now. Just a quick follow up question, re the script in that link that you posted, do you recommend putting parts of it in the prepare phase? Or should it all go under run? I will fiddle around with it, but I am checking reaction time, so I should be as optimal as possible.

    It does seem like the sound_recorder isn't compatible with scripts. I got my it to sort-of work, by inserting it between two scripts in the sequence, but each trial would overwrite the file from the last one...so I'd end up with a single file all the time. I also tagged @Daniel in another old thread related to this issue, where someone else complained about getting a just a single file.

    I'll post the code if I can get the experiment to run. nog bedankt!

  • BaaBobBaaBob Posts: 22

    So, gentlemen, the sound_recorder is out. Thanks once again for the suggestions! I now have something that's at least functional, but still not quite right. Taking the code from the first attempt I made & @sebastiaan 's suggestion to look at PyAudio, I have come up with the following:

    preliminary script randomized the stimuli

        from random import shuffle
        global pic_list
        pic_list = range(1,100)
        shuffle (pic_list)
    

    then the prep phase of a script within the trial sequence

        import pyaudio
        import wave
        import sys
    
        global pic_list
        pic = pic_list.pop()
        var.pic = ['_%.2d' % pic]
    
        ORDER=str(self.get('count_new_inline_script')+1)
    
        #prepare sampler
        src = pool['ding1295ms.wav']
        my_sampler = sampler(src, volume=.5)
    
        #prepare fixdot
        fixdot_canvas = canvas()
        fixdot_canvas.fixdot(color='white')
    
        #prepare cue canvas
        cue_canvas = canvas()
        path = exp.pool[u'cue_wym.png']
        cue_canvas.image(path, scale=0.1)
    
    
        #prepare target canvas
        target_canvas = canvas(background_color=u'thistle')
        path = exp.pool[u'RANstim_%.2d.png' % pic]
        target_canvas.image(path, scale=.2)
    
        #prepare blank canvas
        blank_canvas = canvas()
    

    ...and the run phase

        y_sampler.play()
        fixdot_canvas.show()
        clock.sleep(995)
    
        cue_canvas.show()
        clock.sleep(495)
    
        chunk =     1024
        FORMAT = pyaudio.paInt16
        CHANNELS = 1
        RATE = 44100
        RECORD_SECONDS = 2
        WAVE_OUTPUT_FILENAME = "C:/Users/robbo110/Documents/Fieldwork 2017/RAN_test/RAN_recording/"+ORDER+"_%d.wav" % pic
        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()
    
        target_canvas.show()
        clock.sleep(495)
    
        blank_canvas.show()
        clock.sleep(995)
    

    This reliably records individual trials within the sequence. However, the timing is off. It seems like the record duration gets tacked on to the cue_canvas (so it displays for ca 2.5secods) and records during the cue_canvas display instead of the target_ and blank_ canvas as it should. The recording should start at the precise moment of target_canvas.show() and continue through the end of blank_canvas.show(). Any ideas what I'm doing wrong?

  • BaaBobBaaBob Posts: 22

    Hi Again. my experiment is now working as it should. Basically I had some things out of order. I'm pretty new with python and I didn't really understand what all the bits did at first (still, even, but it works :) ). Here is the code that works as it should. I added "import time" to the prep phase to check the timing. The run phase is as follows (commented out the time checking and stuff that was in the wrong place from my post yesterday):

        my_sampler.play()
        fixdot_canvas.show()
        clock.sleep(995)
    
        cue_canvas.show()
        clock.sleep(495)
    
        chunk = 1024
        FORMAT = pyaudio.paInt16
        CHANNELS = 1
        RATE = 44100
        RECORD_SECONDS = 2
        WAVE_OUTPUT_FILENAME = "C:/Users/robbo110/Documents/Fieldwork 2017/RAN_test/RAN_recording/"+ORDER+"_%d.wav" % pic
        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"
    
    
        target_canvas.show()
        #print time.time()
        for i in range(0, int(RATE / chunk * 0.495)): #this .495 is determining the show duration of target_canvas
            data = stream.read(chunk)
            all.append(data)
        #print time.time()
        #clock.sleep(495)
    
        blank_canvas.show()
        for i in range(0, int(RATE / chunk * 0.995)):
            data = stream.read(chunk)
            all.append(data)
        #clock.sleep(995)
    
        #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()
    

    Thanks @sebastiaan & @eduard for your suggestions!!!

    Thanked by 1eduard
Sign In or Register to comment.