Howdy, Stranger!

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

Supported by

[solved] How to use a sound sample during a reaction time task

edited March 2014 in OpenSesame

Hi everybody,

We have just made a reaction time task but we want to add a sound sample to the task. Our experiment looks as followed:

image

We want the sound to start at "experimentloop_1" and it must continu troughout the experiment. We're not sure how to do this because we've added several loops. The reason for adding all those loops is that we have added breaks to the experiment but we want the sound sample to continu during the breaks and also during the task.

Our sound sample is a .wav format and the duration is 6 minutes, our experiment lasts for about 20 minutes and the sample must sound fluently troughout the experiment..

Please help :(

For the Dutch moderators:

Hoi,

Wij hebben een reactietijd taak gemaakt en zouden hier graag een geluidsbestand (.wav, 6 minuten) aan toe willen voegen. Zoals je in het plaatje kan zien, is er voorafgaand aan het echte experiment nog een vragenlijst, oefenblok, baseline meting etc. Wij willen dat het geluid pas vanaf "experimentloop_1" begint te spelen en dat hij pas aan het eind van het experiment uit gaat. Omdat een reactietijd taak van 20 minuten nogal lang is voor een participant hebben we pauzes ingelast van ongeveer 3 minuten, maar we willen dat het geluid ook tijdens de pauzes te horen is. Voor de pauzes en reactietijdtaken hebben we verschillende loops aangemaakt dus we weten niet zo goed of we in iedere loop een geluidsbestand moeten zetten of niet, want het geluid moet wel onafgebroken te horen zijn. Hopelijk is dit verhaal een beetje duidelijk en kan iemand ons helpen :)

Comments

  • edited March 2014

    Hi guys,

    The solution is actually very simple, if you employ a bit of inline scripting. Add the following code to the Run phase of an inline_script item directly before "experimentloop1".

    EDIT 24 March 2014, 17:19 GMT: corrected a silly mistake in the code.

    # we will need to import the library that
    # we will use to play the sound
    import pygame
    
    # first, we check if it is initialized; if it is
    # not, we initialize it
    if not pygame.mixer.get_init():
        pygame.mixer.init()
    
    # next, we will need to specify the sound
    # file (NOTE: this has to be added to your
    # file pool!
    sound_name = "example.ogg" # change to your own filename
    sound_path = exp.get_file(sound_name)
    
    # now we initialize the sound; this means
    # we prepare it so that it can be played
    exp.sound = pygame.mixer.Sound(sound_path)
    
    # finally, play the sound! (the -1 indicates
    # that it should play infinitely)
    exp.sound.play(-1)
    

    If you want the sound to pause at some point, insert the following code into an inline_script item at the point where you want it to pause:

    import pygame
    pygame.mixer.pause()
    

    And add the following to an inline_script item at the point where you want the sound to resume playing after a pause:

    import pygame
    pygame.mixer.unpause()
    

    If you want the sound to stop completely, insert the following code into an inline_script item at the point where you want it to stop:

    exp.sound.stop()
    

    As a general note: make sure that your sound is either in WAVE (.wav) or OGG (.ogg) format. If you have a different format, you can convert the sound to either WAVE or OGG via a converter (you can search for one online, there are tons).

    Good luck!

  • edited March 2014

    Hi Edwin,

    I've entered the code in the Run phase of the inline script. However, I get this error:

    Error: Inline script error
    In: inline_script (run phase)
    Line: 7

    Python traceback:
    AttributeError: 'module' object has no attribute 'get_init'

    Full traceback in debug window

    I have no idea what I'm doing wrong. I've literally copied and pasted your code, ive also added the file to the filepool and inserted the file name in your code. Help :(

  • edited March 2014

    Btw, this is the code as it is in our experiment right now:

    # we will need to import the library that
    # we will use to play the sound
    import pygame
    
    # first, we check if it is initialized; if it is
    # not, we initialize it
    if not pygame.get_init():
        pygame.init()
    
    # next, we will need to specify the sound
    # file (NOTE: this has to be added to your
    # file pool!
    sound_name = "5-0.wav" # change to your own filename
    sound_path = exp.get_file(sound_name)
    
    # now we initialize the sound; this means
    # we prepare it so that it can be played
    exp.sound = pygame.mixer.Sound(sound_path)
    
    # finally, play the sound! (the -1 indicates
    # that it should play infinitely)
    exp.sound.play(-1)
    
  • edited 3:11PM

    Hi guys,

    Terribly sorry, that's my mistake! The code below should work:

    # we will need to import the library that
    # we will use to play the sound
    import pygame
    
    # first, we check if it is initialized; if it is
    # not, we initialize it
    if not pygame.mixer.get_init():
        pygame.mixer.init()
    
    # next, we will need to specify the sound
    # file (NOTE: this has to be added to your
    # file pool!
    sound_name = "5-0.wav" # change to your own filename
    sound_path = exp.get_file(sound_name)
    
    # now we initialize the sound; this means
    # we prepare it so that it can be played
    exp.sound = pygame.mixer.Sound(sound_path)
    
    # finally, play the sound! (the -1 indicates
    # that it should play infinitely)
    exp.sound.play(-1)
    

    Simply forgot to add the .mixer in the initialization bit. I've updated my previous post as well, for future reference.

    Good luck!

  • edited 3:11PM

    Hi Edwin,

    IT WORKS! Thank you so much!

  • edited 3:11PM

    No problem, glad that it works! Marking this as solved.

Sign In or Register to comment.