Howdy, Stranger!

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

Supported by

Play music randomly as background

edited March 2017 in OpenSesame

Hi,
I want to design my own experiment. In each trial, I want to have one piece of music as background. And in my whole experiment, there will be around 50 trials, during which I want to play 50 different music pieces as background randomly. I also need participants to do some tasks repeatedly while one piece of music are playing.

  1. Do I need to upload every pieces of music separately into "sampler" item? Are there any easy ways of doing that?
  2. How can I make them play randomly without influencing other items in my experiment?
  3. All my music pieces are 1 min long, if I want to end the music after the participant have finished the process in one trial, which may be earlier than 1 min. How can I achieve it?
  4. I do not know if there is way that can make music as variables and play them randomly. I mean I want to let the program write logs to remember the randomly music pieces.
  5. BTW, I also want to open two experiments in Open Sesame in order to change my experiment easily, but I cannot do that. I wonder if I can open two osexp files at the same time?
    Thank you so much!

Comments

  • Hi,

    Welcome to the forum and thanks for your interest in OpenSesame!

    I think you could do the following:

    • Build an experimental structure with a typical trial_sequence (explained in the step-by-step tutorial). Your trial_sequence should at least contain a sampler item (to play the music), a response-collection item (for example a keyboard_response item) and a logger item (to save the data).
    • Put your 50 different sound files in the file pool (again, the tutorial explains you how to do this)
    • Next add three inline_script items to your experiment
    1. One at the beginning of the experiment. Here, we will create a list containing all the names of the sound files (see the comments in the code below for more info)
    2. One at the beginning of the trial_sequence. Here, we will determine the sound file that will be played during the current trial (regardless of the other variables that you will manipulate on the current trial)
    3. One after response collection (to stop playing as soon as participants gave a response)

    Your experimental structure should look something like so:

    • Next, we need to use some Python code in the inline_script items:

    • In the first one (which I called "sound_list"), place the following code in the Prepare phase tab:

    # Make a list of all the sounds in the file pool:
    # NOTE: this list should contain (at least) as many sound files as there 
    # are trials. I used 9 files, but you will need at least 50
    
    l_sounds = []
    
    for path in pool:
        l_sounds.append(path)
    
    # Shuffle the list
    import random
    random.shuffle(l_sounds)
    
    • In the second (which I called "determine_sound_file"), again in the Prepare phase tab:
    # Pop one sound file, WITHOUT replacement, from the list that we created in the 
    # inline_script item 'sound_list'. This is the sound that will play on the 
    # background of the current trial
    
    var.sound_file = l_sounds.pop()
    
    • And the last one ("stop playing"), here in the Run phase tab:
    import pygame.mixer
    pygame.mixer.stop()
    
    • Finally, open the sampler item, and set its duration to 0, and use the square-brackets method in the box 'Sound file' (see screenshot above).

    I attached a working example script to this post.

    I hope this will get you started!

    BTW, I also want to open two experiments in Open Sesame in order to change my experiment easily, but I cannot do that. I wonder if I can open two osexp files at the same time?

    I think you could just launch the program twice.

    Good luck,

    Lotje

    Did you like my answer? Feel free to Buy Me A Coffee :)

  • edited May 23

    Hi, I have been looking around for some advice on how to build an experiment, and this forum comes the closest to achieving what I want. I'm still running into a problem, however, and was hoping for some clarification.

    I believe I want to do something similar to the original post. In my experiment, I want to have a sound file playing in the background, across multiple response collections, without being interrupted. In my case, I only need one sound file, rather than 50 which are randomly sorted as in the original post, so compiling a list of sounds should not be an issue. However, every time I try to implement the steps outlined above by @lvanderlinden, the sound stops playing after the first response is collected, and does not continue over subsequent responses. The instructions potentially indicate that this is the expected outcome -- "Add [an] inline script.. after response collection (to stop playing as soon as participants gave a response)". Rather than the sound stopping after a response is given, I want it to keep playing across multiple responses.

    Here is a screenshot from my experiment design, to illustrate what I am trying to to.

    In a loop item I've named "Ocean_block", I want a sound file (specifically, ocean waves) to play in the background while multiple responses are collected. The figure below shows how the task should proceed:

    (The task involves judging whether the three numbers are equidistant -- pressing "z" on the keyboard indicates they are the same distance apart, while pressing "m" indicates they are not.)

    Any advice on how I can input a sound file to play uninterrupted across multiple responses? The sound would also need to stop playing at the end of the "Ocean_block" loop, once the final response has been collected.

    Let me know if I can provide any clarification, and thank you!

  • Hi, I am trying to build an experiment in Open Sesame, and this forum comes the closest to achieving what I'm trying to do. I want to have a sound file which plays across multiple responses without being interrupted. When I follow the instructions provided above, however, the sound file stops playing after the first response. The post by @lvanderlinden potentially indicates that this is what's supposed to happen: "Next add three inline_script items to your experiment... One after response collection (to stop playing as soon as participants gave a response)". If I interpret this correctly, it is saying that the sound will stop playing after a response is provided. However, I want the same sound to continue playing across multiple responses.

    This is what the experiment design currently looks like, without a sound sampler being inputted yet:

    In the "Ocean_block" loop, I want a sound file (specifically, ocean ambience) to play across multiple trial responses. The following figure shows how the task should proceed:

    If the three numbers which appear in the center are judged as equidistant, participants press "z" for "Same". If the numbers are judged as being unequally spaced, participants press "m" for "Different".

    Any advice on how I could input a sound file which plays across multiple responses, without being interrupted? The sound would also need to stop after the "Ocean_block" loop is finished.

    Let me know if I can provide any clarification, and thank you!

  • Hi @JKellogg,

    Right, the inline_script with the pygame.mixer.stop()will stop the sound. You can take it out and the tune will continue for longer. However, I don't think this will solve your problem necessarily. The reason for that is that the playback is started in the trial loop itself, so that when the next trial starts, the next tune will also be started.

    Is there only one sound file that you want to play? And is it long enough to play throughout all trials? If so, you can do that you suggest yourself, namely putting it in the block_sequence_2 before the Ocean_sounds sequence. Then the sound should stay active throughout all trials. To explicitly stop the sound after the block, you can add an inline-Script after the ocean_sounds sequence, with items['sampler].stop() (or something like that, check the python documentation of the sampler)

    Does that make sense?

    Eduard

    Buy Me A Coffee

  • Hi @JKellogg,

    Just reading quickly through this thread, I think that @eduard's solution might work well for you. You'd have to start the sound playing, then run a loop containing your multiple trials, and once out of the loop, run code to stop the mixer.

    Running the sound in the background while several sketchpads are presented works (https://forum.cogsci.nl/discussion/7766/music-on-open-sesame), the issue, as Eduard pointed out, is that if your code stopping the mixed is run at the end of every trial, your sound will stop instead of continuing. An alternative would be to include the mixer and the code stopping the mixer within your loop but use "Run if" and use conditions relative to the trial number (such that it only starts the mixer when the 1st trial is running, and only runs the code stopping the mixer on the last trial of the loop).

    Just some quick thoughts.

    Fabrice.

    Buy Me A Coffee

  • edited May 26

    Hi @eduard and @Fab, thank you so much for your replies -- I was away for a few days and was not able to respond. I will try to implement these strategies and will return here if they don't work. Thank you again!

    And yes, to answer @eduard's question, there is only one sound that I want to play, and it is long enough to extend across all the trials (about 10 minutes). So the suggested fix should in principle work.

    (Also, apologies for accidentally reposting my same question above.)

  • I have been trying to implement the method suggested above by @eduard for embedding the audio file in the block_sequence_2 before the Ocean_sounds sequence, so it stays active throughout the trials. I'm not quite sure, however, how to make the trials begin while the audio is still playing. Here is a snipping of the experiment overview.


    After the Instructions_Part2d sketchpad, I added a blank sketchpad into the block_sequence_2 -- otherwise the Instructions sketchpad does not go away when the Ocean_audio sampler starts. After the audio starts, however, it just continues playing, without the trial beginning. The letter_display_2 sketchpad which houses the trial has a duration of 0.

    Any help would be appreciated!

  • Hi @JKellogg,

    It is normal that the instructions_part2d stays on the screen while the sound begins to play because you have displayed nothing else on the screen. The mixer is an audio object, so it won't change what is being displayed.

    As for the sound playing to the end before the program moving to the next object, I suspect it is because you haven't set the duration of the mixer to 0 ms (see the example I referred to earlier: https://forum.cogsci.nl/discussion/2889/play-music-randomly-as-background#latest).

    Hope this helps.

    Fabrice.

    Buy Me A Coffee

  • edited June 3

    Thank you, @eduard and @Fab for the help -- I've been able to get the experiment to work! I added an inline script before the trial sequence with the following code (also taken in part from this page):


    from openexp.sampler import sampler

    src = exp.get_file('Ocean_clip_1.wav')

    my_sampler = sampler(exp, src)

    my_sampler.play()


    And another inline script after it which says:


    my_sampler.stop()


    This has done the trick -- the sound file now plays uninterrupted throughout the trial sequence, and then stops once it's done.

    Thank you again, this has been very helpful!

  • Hi @JKellogg,

    Great to hear it's working! Now all you need is a nice set of data! 🤞

    Good luck with your experiment!

    Fabrice.

    Buy Me A Coffee

  • Great!

    By the way, your code is a little outdated. Currently it should be

    src = pool['Ocean_clip_1.wav']
    my_sampler = Sampler(src)
    my_sampler.play()
    

    I think.

    Good luck!

    Eduard

    Buy Me A Coffee

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games