Howdy, Stranger!

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

Supported by

EEG trigger and random time intervals

edited January 30 in OpenSesame

Hi all. I'm a Phd Student and completely noob to Opensesame and to coding in general. I'm trying to write a very simple experiment where I want to send a trigger to an EEG system (Neuroconn system) every time I present an auditory cue (a beep). We would like to present the audio at a random time after a keypress (between 10 and 15 seconds after the key response lets say) for N trials. And here come the part where I'm struggling with. How can I say to Open Sesame ti randomly present this audio and set a trigger (and furthermore is possible to have a high temporal resolution between the audio and the trigger?!) Thats what I produced so far, and would really appreciate a lil help :)

define form_text_display Instructions_display
set timeout infinite
set spacing 10
set rows "1;4;1"
set only_render no
set ok_text Ok
set margins "50;50;50;50"
set form_title "Title"
set form_text "Your message"
set description "A simple text display form"
set cols "1;1;1"
set _theme gray
widget 0 0 3 1 label text="[form_title]"
widget 0 1 3 1 label center=no text="[form_text]"
widget 1 2 1 1 button text="[ok_text]"

define sampler Sound_sampler
set volume 1
set stop_after 0
set sample ""
set pitch 1
set pan 0
set fade_in 0
set duration sound
set description "Plays a sound file in .wav or .ogg format"

define sequence Trial_sequence
set flush_keyboard yes
set description "Runs a number of items in sequence"
run new_sketchpad always
run Sound_sampler always
run new_inline_script always

define loop Volontary_Actions_loop
set source_file ""
set source table
set repeat 1
set order random
set description "Repeatedly runs another item"
set cycles 1
set continuous no
set break_if_on_first yes
set break_if never
setcycle 0 empty_column ""
run Trial_sequence

define sequence experiment
set flush_keyboard yes
set description "Runs a number of items in sequence"
run new_inline_script_1 always
run Instructions_display always
run Volontary_Actions_loop always

define inline_script new_inline_script
set description "Executes Python code"
global io
trigger = 1
port = 0x378
io.DlPortWritePortUchar(port, trigger)
print 'Failed to send trigger!'
set _prepare ""

define inline_script new_inline_script_1
set description "Executes Python code"
from ctypes import windll
global io
io = windll.dlportio # requires dlportio.dll !!!
print 'The parallel port couldn\'t be opened'
set _prepare ""

define sketchpad new_sketchpad
set duration 745
set description "Displays stimuli"
draw fixdot color=white show_if=always style=default x=0 y=0 z_index=0

define sketchpad welcome
set start_response_interval no
set reset_variables no
set duration keypress
set description "Displays stimuli"
draw textline center=1 color=white font_bold=no font_family=serif font_italic=no font_size=32 html=yes show_if=always text="OpenSesame 3.1 Jazzy James" x=0 y=0 z_index=0

Thanks very much to everybody would like to help me :)


  • And sorry for another question: What if I dont have a serial or parallel port?! What would be the best way to send triggers in that case ?
    Thanks everybody

  • Hi Flo,

    It depends a lot on your experiment, i.e. what happens in between the tone was presented? If it is a purely auditory experiment it is really simple. You can either randomly choose a delay, or if it supposed to be quite systematically random, you can also define intervals in the loop table and jitter them somewhat.
    Either way, you should do that in inline_scripts. I attach a small showcase experiment, to demonstrate the principle. If you want to use audio, check out the documentation:

    and furthermore is possible to have a high temporal resolution between the audio and the trigger?!)

    If you send the trigger right before you play the sounds, or vice versa, you should be good!

    And sorry for another question: What if I dont have a serial or parallel port?! What would be the best way to send triggers in that case ?

    Honestly, I've never heard about a case where neither parallel nor serial port were available. No idea what you would to in that case. Maybe USB?

    Hope this helps.


  • Dear Eduard,

    thank you very much. That alredy helped me a lot. Basically yes, we do not need anything to happen between the sounds, so we just want to send the triggers to the EEG (Which I will send before the sound) so that we can have a trigger on the EEG signal at the same time when the sound is played.

    I managed thanks to your example to make my sound play, the only thing I'm still struggling with is the number of time the sound is played. How can I determine this???
    Sorry, this are probably stupid question but its literally my first time I use Opensesame

    In attached the script (Its only a rough first draft) :)

    Thank you very much

  • Hi Flo,

    As I said, there are shload of different ways to determine how often your sound is being played. So, maybe first think about what makes most sense for your paradigm (normally/uniformly/exponentially randomly distributed). Once you decided which way to choose, I can help with the implementation. Okay?


  • Hi Eduard,

    Thanks again very much for your reply.
    Ok, let's say I want to present the sound randomly in an interval between 10 and 15 seconds for a fixed number of presentations (e.g 40 times) and then just stop. Nothing special needed.


  • Something like that:

    import random
    import numpy as np  
    src = pool['beep-07.wav']
    my_sampler = sampler(src, volume=.5)
    startTime = clock.time()
    # how many sounds
    n_events = 41
    # choose random time interval
    interval = random.randint(10000,15000)
    # choose random time points
    events = np.random.randint(0,interval,n_events)
    # sort chronologically
    # keep track of the ith sound
    counter = 0
    play = 1
    while clock.time()-startTime<interval: # do that for 13 seconds
            if clock.time()-startTime>events[counter]:
                if play:
                counter += 1
                if counter == n_events:
                    play= 0

    Note that with this code, there is no restriction how close the tones are to each other. So there is a fair chance for overlap. One solution to fix this is predefine potential timepoints that are widely enough spaced, and select a certain number of them. e.g.

    import numpy as np
    potEvents = np.arange(0,interval,40) # at least 40ms between any two events
    events =random.sample(potEvents,40)

    Goo dluck,


  • Dear Eduard,

    thank you very much again for your help. Much appreciated :)
    I will play around a bit with this.

Sign In or Register to comment.