Howdy, Stranger!

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

Supported by

[solved] Repeat sound

edited September 2014 in OpenSesame

Hi everybody,

In my experiment, the participants hear stories (via media_player_vlc).
I would want to allow them to listen to the story a second time after the first listening.
Is it possible to make it by using custom_python ? If yes, could you indicate to me how do that or, if I have badly looked for the link towards the resolution of this problem?

Thank you in advance of your help,

Best regards,

Lucie

Comments

  • edited September 2014

    Hi Lucie,

    For starters, if people are just listening to the stories, I would use the sampler plug-in, which was specifically designed to play sounds and is more stable then the media_player_vlc plug-in (which was designed for video).

    You can use the sampler functions in an inline_script as well:

    # load the modules we need
    from openexp.canvas import canvas
    from openexp.sampler import sampler
    from openexp.keyboard import keyboard
    
    # create a new canvas
    my_canvas = canvas(exp)
    
    # create a new keyboard
    my_keyboard = keyboard(exp)
    
    # path to the sound file
    soundfile = exp.get("storyname")
    # create a new sampler
    my_sampler = sampler(exp, exp.get_file(soundfile))
    
    # play the story
    repeatsound = True
    while repeatsound:
    
        # draw a fixation dot on the new canvas
        my_canvas.clear()
        my_canvas.fixdot()
        my_canvas.show()
    
        # start playing the sound
        my_sampler.play(block=True)
    
        # present a message
        my_canvas.clear()
        my_canvas.text(Press Space to continue, or R to replay
        my_canvas.show()
    
        # wait for keyboard input
        key, presstime = my_keyboard.get_key(keylist=['space','r'], timeout=None)
    
        # if Space was pressed, do not play again
        if key == 'space':
            repeatsound = False
    

    NOTE: this assumes you have the names of the sound files defined in your loop, as variable storyname.

    Good luck!

  • edited 9:08PM

    Hi Edwin,

    Thank you for our answer.
    The repetition of sounds works now with your script.
    But the names of variables in my loop do not work.
    My construction is like '[story]_[sound].wav' .
    Could you help me for that ?

    Thank you in advance of your help,

    Best regards,

    Lucie

  • edited September 2014

    Hi Lucie,

    Change the bit that finds your sound file from:

    soundfile = exp.get("storyname")
    

    to:

    soundfile = "%s_%s.wav" % (exp.get("story"), exp.get("sound"))
    

    Does that do it for you?

    Good luck! :)

  • edited 9:08PM

    Hi Edwin,

    Thank you so much for your answer.
    Now, everything works as wished ;)

    Best regards,

    Lucie

  • edited 9:08PM

    That's great! Will mark this as solved :)

Sign In or Register to comment.