Howdy, Stranger!

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

Supported by

[open] Stop Sampler playing music (using key e.g. ENTER)

edited January 2016 in OpenSesame

Currently doing a project where sentences are presented alongside background music. The background music is presented 3000 ms before the sentence is presented and then continued as long as the participant reads the sentence. This is all working, but when you push ENTER to go to the question paired with the sentence, the music keeps on playing.
The music files are around 30 - 40 seconds so what also happens when you go to trial 2, 3 and 4 is that new music files are simply added to the already playing music, so that you have 3-4 music items playing at once.

How do I get the music to stop playing alongside the sentence, by using ENTER or similar.
Atm i'm using the sampler item and not an incline code.

Ola

Comments

  • edited 4:09PM

    It would actually be nice to be able to include a white screen/ canvas during the 3 seconds of music playing without the sentence.

  • edited January 2016

    Hi Ola,

    What Josh suggested in the other thread is a good way to solve your issue. So, basically, instead of using the sampler item, you should play the files from within an inline_script, actually, two inline_scripts.

    First, you replace the sampler with the first inline_script and put this code into it:

    import pygame # import modul
    pygame.mixer.init() # initialize mixer (doing this once might be enough)
    sound_file = exp.get_file(filename) # load the sound file of a single trial
    sound = pygame.mixer.Sound(sound_file) # create an instance of the sound
    sound.play() # play the sound
    
    cv = canvas()
    cv.background_color = 'white'
    cv.show()
    clock.sleep(3000)
    

    Then, you do the other stuff you intended to present, as soon as subjects pressed a key (specify with the allowed_response argument, whichever you want). After your keyboard_response , e.g. as last thing to be executed before you start the next trial, in which you place the single line: sound.stop(). Et voila, there you go.

    Let us know, if this worked out for you.

    Good luck,

    Eduard

    Buy Me A Coffee

  • edited January 2016

    NO it doesn't work. Have three inline scripts at the moment. None of them are doing what they are supposed to do.
    script one RUN

    cv = canvas(exp)
    cv.background_color = 'white'
    sound.play() 
    
    cv.show()
    clock.sleep(3000) ... this doesn't work as it is not defined. 
    

    script one prepare

    import pygame 
    
    pygame.mixer.init() 
    sound_file = exp.get_file("300bpm.wav") 
    #... THIS is also a problem as when I used the sampler I could call my variable # [Noise] which included FOUR sound files. 
    sound = pygame.mixer.Sound(sound_file) 
    
    
    from openexp.canvas import canvas
    

    script two Prepare

    import csv 
    import random 
    list()
    
    image_path = exp.get_file('ops.csv')
    with open(image_path, 'rU') as csvfile:
        reader = csv.DictReader(csvfile, delimiter=',')
        full_list = list(reader)
        random.shuffle(full_list)
        trial_info = full_list.pop(0)
    
        sentence = trial_info['sentences'] 
        question = (trial_info['question'] +"\n \nYES (Y)             NO (N)"+'\n')
        answer = trial_info['answer']
        item = trial_info['item']
    
    
        print(item)
        print(answer) 
        print(sentence) 
        print(question) 
    

    script two run

    from openexp.canvas import canvas
    my_canvas = canvas(exp)
    my_canvas.text(sentence, center=True, x=None, y=None, max_width=None, color=None, bidi=None, html=True)
    my_canvas.show() 
    
    allowed_response = "ENTER"
    keybord_response = "ENTER"
    sound.stop()
    

    not working.. the music stops playing after 3 ms so no music here and keyboard response doesn't work.

    Sorry. Just getting slightly frustrated as nothing is working at the moment.

  • edited 4:09PM

    clock.sleep(3000) ... this doesn't work as it is not defined.

    I suppose you are running Opensesame 2.9.x? In this case clock.time() is not defined, right. Instead, you can use self.time(3000); it will have the same effect.

    sound_file = exp.get_file("300bpm.wav") ... Thisis also a problem as when I used the sampler I could call my variable # [Noise]

    Why? You can still do exact same thing. The only difference is, that you have retrieve your [Noise] variable (I suppose you define it in the loop_table):

    noise = exp.get(Noise)

    sound_file = exp.get_file(noise)

    allowed_response = "ENTER"
    keybord_response = "ENTER"
    sound.stop()

    This won't work. I either you go entirely for the option of doing this with inline_scripts. Then, I highly recommend you checking out the documentation of how to do things with inline_scripts.

    Basically, something like this will do the trick:

    from openexp.keyboard import keyboard
    kb = keyboard(exp,keylist= ['ENTER'],timeout = 27000) # waits for 27 seconds
    kb.get_key()
    
    sound.stop()
    

    Alternatively, you just use the keyboard item as you did before, and add another inline_script later on.

    I hope things make more sense now. No reason to despair.

    Eduard

    Buy Me A Coffee

Sign In or Register to comment.