Howdy, Stranger!

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

Supported by

Play sound at random points during experiment

Hello,

I am trying to have a click sound play at random points during the training portion of my experiment. The training consists of words being individually presented visually on the screen. When the participant's hear a click sound, they have to press a key as fast as possible, and then we will record the time taken for them to press the key.

I have one variable in my loop table with 20 levels (the number of words presented, named "wordorder"). I have then included an inline script within that to try and get the click sound to play randomly 5 times while the words are being presented. This is what I have so far:

I know I am doing something wrong as the clicks are playing throughout all 20 words instead of at 5 random words, but I am not sure how to resolve the issue.

Thanks for your time :)

Comments

  • Hi,

    How do you present the words then? Your example is a little too incomplete to really understand what is happening (and what isn't). Generally, if you want to make sure that your sounds is presented exactly 5 times in the word loop, you should create a sound sequence before entering the word list. Then, you can index every item of that list on every trial, and play the sound whenever you want. Does this make sense?

    By the way, if you want to make your sound entirely random, that is not just on which trials it is being played but also when inside a trial, you have to use a slightly different approach. In this case, you would have to add while loop to the mix. If this is the case, I can try to give you more help on that.

    Good luck,

    Eduard

  • Hi Eduard,

    Thanks for taking the time to respond. The words will be presented one-by-one on the screen. All the participant has to do is look at the words as they are visually presented individually, and then press a key whenever they hear a click sound.

    I am interested to see what you mean by using the while loop. The actual training phase in the experiment will have over 700 trials, so I am hoping to randomly distribute the click sound maybe 50 times throughout those trials.

  • edited February 10

    Hi Eduard,

    Sorry to bother you, but I am still unable to figure this out as I am still a beginner at python. I've played around with it but I still can't get the clicks to play at 5 random points. With the while loop that you suggested, would it start off something like (I don't know how to format the code properly on this forum so please ignore the tabbing errors):

    clicks = 0 
    while wordorder = true:
    
    for clicks in wordorder:
    if clicks < 5:
    click_sound.play()
    
    clicks = clicks + 1
    
    if clicks == 5:
    break
    

    Am I headed in the right direction? The 'wordorder' variable in my loop table is in a fixed sequential order, so I'm not sure how to randomise the distribution of the clicks while not randomising the wordorder.

  • Ah, so I've sorted out how to get the random click sounds with the below code:
    import random

        #setup sampler
        sound = pool['mouseclick.wav']
        click_sound = sampler(sound, duration=0, volume=1)
    
        #40 nonwords loop
        population = list(range(40))
    
        #select 5 random positions
        click_indices = random.sample(population, k=5)
    
        click_count = 0
    
        if click_count in click_indices:
            click_sound.play()
            click_count = click_count + 1
    
        if click_count == 5:
            click_sound.stop()
    

    But now I've run into issues with recording a subject's keyboard response to the clicks. Participants need to press the 'c' key as soon as they hear the click sound. I'd like to exclude any response that are longer than 1000ms or shorter than 100ms from analyses. If I change the keyboard timeout to 1000ms, it stops the word loop from playing automatically like I'd like it to. At the moment, I have this inline script in the prepare phase:

            my_keyboard = keyboard(keylist=['c'],timeout=0)
            start_time = self.time()
            key, end_time = my_keyboard.get_key()
            subj_click_rt = end_time - start_time
            exp.set("subj_click_rt", subj_click_rt)
    

    But my logger records subj_click_rt as 0 throughout all trials. I've tried to refer to the solution here https://forum.cogsci.nl/discussion/918/solved-response-time-timing but because my clicks will play randomly for every participant, I'm not sure how to approach this correctly.

    Here is a screenshot of my current set up

    Thanks so much again.

  • Ah, so I've sorted out how to get the random click sounds with the below code:

    import random
    
    #setup sampler
    sound = pool['mouseclick.wav']
    click_sound = sampler(sound, duration=0, volume=1)
    

    40 nonwords loop

    population = list(range(40))
    

    select 5 random positions

    click_indices = random.sample(population, k=5)
    
    click_count = 0
    
    if click_count in click_indices:
    click_sound.play()
    click_count = click_count + 1
    
    if click_count == 5:
    click_sound.stop()
    

    But now I've run into issues with recording a subject's keyboard response to the clicks. Participants need to press the 'c' key as soon as they hear the click sound. I'd like to exclude any response that are longer than 1000ms or shorter than 100ms from analyses. If I change the keyboard timeout to 1000ms, it stops the word loop from playing automatically like I'd like it to. At the moment, I have this inline script in the prepare phase:

    my_keyboard = keyboard(keylist=['c'],timeout=0) 
    start_time = self.time() 
    key, end_time = my_keyboard.get_key()
     subj_click_rt = end_time - start_time
     exp.set("subj_click_rt", subj_click_rt)
    

    But my logger records subj_click_rt as 0 throughout all trials. I've tried to refer to the solution here https://forum.cogsci.nl/discussion/918/solved-response-time-timing but because my clicks will play randomly for every participant, I'm not sure how to approach this correctly.

    Here is a screenshot of my current set up

    Thanks so much again

  • Ah, so I've sorted out how to get the random click sounds with the below code:


    import random
    
    #setup sampler
    sound = pool['mouseclick.wav']
click_sound = sampler(sound, duration=0, volume=1)
    
    #40 nonwords loop
    population = list(range(40))
    
    #select 5 random positions
    click_indices = random.sample(population, k=5)
    
    click_count = 0
    if click_count in click_indices:

      click_sound.play()

      click_count = click_count + 1
    
    if click_count == 5:

      click_sound.stop()
    

    But now I've run into issues with recording a subject's keyboard response to the clicks. Participants need to press the 'c' key as soon as they hear the click sound. I'd like to exclude any response that are longer than 1000ms or shorter than 100ms from analyses. If I change the keyboard timeout to 1000ms, it stops the word loop from playing automatically like I'd like it to. At the moment, I have this inline script in the prepare phase:

    my_keyboard = keyboard(keylist=['c'],timeout=0) 
    start_time = self.time() 
    key, end_time = my_keyboard.get_key() 
    subj_click_rt = end_time - start_time 
    exp.set("subj_click_rt", subj_click_rt)
    

    
But my logger records subj_click_rt as 0 throughout all trials. I've tried to refer to the solution here https://forum.cogsci.nl/discussion/918/solved-response-time-timing but because my clicks will play at random points for every participant, I'm not sure how to approach this correctly.

    Here is a screenshot of my current set up


    Thanks so much again.

  • Hi,

    Try this:

    <br />my_keyboard = keyboard(keylist=['c'],timeout=0) 
    start_time = clock.time() 
    while True:
        key, end_time = my_keyboard.get_key() 
        if key != None:
            var.subj_click_rt = end_time - start_time 
        break
    

    That is the basic structure of the while loop that I was talking about. In theory, you can step-by-step make it more complicated by adding the sounds and images to it as well, if needed.

    Hope this helps you.

    Eduard

  • Hi Eduard,

    I have included that, but it is still not recording the RTs to the click sounds (I either get a full column of NA or a mix of NA and zeros). I get the same result when I put all of that code in the prepare phase, or only having the keyboard setup in the prepare phase. Here is the full code I have right now: https://pastebin.com/wyC4fDcE perhaps you might spot something that I am missing or have written incorrectly.

    Thanks for your time and patience.

  • Updated code:
    https://pastebin.com/PTyKaQzM

    I am able to record RTs now, but I had to set my keyboard timeout to 450ms instead of 0ms. Now, each word in the word loop (which I had originally set for a 240ms duration on the sketchpad) is slowed down. Is there any way for me to set the keyboard time out to 1000ms without affecting the 240ms duration of the words displayed in the word loop?

    (Cross posted http://forum.cogsci.nl/index.php?p=/discussion/918/solved-response-time-timing#latest)

Sign In or Register to comment.