Howdy, Stranger!

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

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

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

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

  • 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!!!

  • Hi Bob,

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

    Eduard

    Buy Me A Coffee

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

  • 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

    Buy Me A Coffee

  • 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!!

  • 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

    Buy Me A Coffee

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

  • 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

    Buy Me A Coffee

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

  • 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

  • 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!

  • 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?

  • 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!!!

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