Howdy, Stranger!

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

Supported by

Volume control for sampler item

Hello, I am trying to set up an experiment containing four audio events. I want participants to be able to control the volume of each event, while the four audio clips play at the backgorund in a loop. I figured that the volume levels can be adjusted sequentially for each audio event within a seperate loop, which would be distrupted by keypress and followed by the next loop, until all of the four sounds has the desired volume levels.

My questions are;
1- How should I define the variable for the volume level? I want to assign two keys for increasing or decresing the volume level by 0,02. Any tips would help.
2- How can I pass to the next loop, for adjusting the volume of the next event? (already tried break if statements I found on the forum, don't know what went wrong).
3- Is it possible to save the adjusted volume level for the audio file, to be used in the forthcoming loops?

Thanks!

Comments

  • Hi Cagdas,

    Maybe this script here, will help you get started:

    Most importantly, you should note that it doesn't use Opensesame Items (sampler, loop), but all "the magic" happens in an inline_script, by calling those functions directly.

    src1 = exp.pool['a.ogg']
    my_sampler1 = sampler(src1)
    src2 = exp.pool['b.ogg']
    my_sampler2 = sampler(src2)
    src3 = exp.pool['c.ogg']
    my_sampler3 = sampler(src3)
    samplers = [my_sampler1,my_sampler2,my_sampler3]
    kb = keyboard(keylist=['left','right','top','down','return','SPACE'])
    
    cv = canvas()
    sIdx = 0 
    my_sampler1.play()
    my_sampler2.play()
    my_sampler3.play()
    no_sounds = 3
    while True:
        k,t = kb.get_key(timeout=20)
        if k == 'return':
            break
    
        elif k == 'up':
            sIdx -= 1
        elif k == 'down':
            sIdx += 1
        elif k == 'right':
            if samplers[sIdx%no_sounds].volume < 1:
                samplers[sIdx%no_sounds].volume += 0.05
        elif k == 'left':
            if samplers[sIdx%no_sounds].volume > 0:
                samplers[sIdx%no_sounds].volume -= 0.05
    
        text = "Adjust volume of sound %s. \n\n\nCurrent volume  is %s"%(sIdx%no_sounds,samplers[sIdx%no_sounds].volume)
        cv.clear()
        cv.text(text)
        cv.show()
    
    
    #my_sampler1.play()
    

    I hope it makes sense. Let me know if you need more explanation.

    Eduard

    Buy Me A Coffee

  • Thank you Eduard, I will be looking into it.

  • edited June 2017

    Hi again, the code works fine and answered all my above questions. Only I couldn't solve the timing issue. I want certain delays in between the sampler items, so i used the sleep function as below

           my_sampler1.play()
            clock.sleep(750)
            my_sampler2.play()
            clock.sleep(750)
            my_sampler3.play()
            clock.sleep(2000)
    

    The problem is that the volume cannot be adjusted during the loop, but it needs to wait until all the items run once, which makes the session very slow and hard to control. Is there another way that i could put delays and also get faster response to keypresses?

  • Hi Cagdas,

    I don't really get what you mean exactly. You start playing the samplers before the loop, so even if you have those intervals between them, they will overlap once the last sounds begins to play (unless you have very short samples). In the loop itself you can easily adjust the volume online, it will change them right away. At least it does so for me.

    If your version is different, would you mind uploading it, so I can have a look?
    Eduard

    Buy Me A Coffee

  • Hi,
    Actually the code is not different, except that the above code is inserted just after the while true statment. I have very short samples, which are indeed 4x70ms clicks playing continously. Since the volume level ascends and descends in 0.02 steps, I wonder if there is a way to apply several keypresses during the loop, so that the next loop, for example, can start 0.08 step higher. Otherwise, it still works, but each keypress must wait until the the loop is finished.

    One more thing, my advisor said that it is strange that the number of clicks displayed starts from 0. Is there a way to call the first item in the samplers list as 1 instead of 0. Tried to change sIdx to 1 but didn't work.

    Thanks a lot.

  • Hi,

    Is there a way to call the first item in the samplers list as 1 instead of 0

    Change this line: text = "Adjust volume of sound %s. \n\n\nCurrent volume is %s"%(sIdx%no_sounds,samplers[sIdx%no_sounds].volume)

    to:

    text = "Adjust volume of sound %s. \n\n\nCurrent volume is %s"%((sIdx%no_sounds)+1,samplers[sIdx%no_sounds].volume)

    I have very short samples, which are indeed 4x70ms clicks playing continously.

    So, there is no overlap? How about this code below.Instead of sleeping in between the samples, this code samples key strokes. So, in theory you can update the volume almost continuously.

    src1 = exp.pool['a.ogg']
    my_sampler1 = sampler(src1)
    src2 = exp.pool['b.ogg']
    my_sampler2 = sampler(src2)
    src3 = exp.pool['c.ogg']
    my_sampler3 = sampler(src3)
    samplers = [my_sampler1,my_sampler2,my_sampler3]
    kb = keyboard(keylist=['left','right','top','down','return','SPACE'])
    
    cv = canvas()
    sIdx = 0 
    no_sounds = 3
    
    while True: 
        my_sampler1.play()
        t0 = clock.time()
        while clock.time()-t0<750:
            k1,t1 = kb.get_key(timeout=10)
            if k == 'right':
                 if samplers[sIdx%no_sounds].volume < 1:
                     samplers[sIdx%no_sounds].volume += 0.05
            elif k == 'left':
                 if samplers[sIdx%no_sounds].volume > 0:
                     samplers[sIdx%no_sounds].volume -= 0.05
            kb.flush()
        my_sampler2.play()
        t0 = clock.time()
        while clock.time()-t0<750:
            k1,t1 = kb.get_key(timeout=10)
            if k == 'right':
                 if samplers[sIdx%no_sounds].volume < 1:
                     samplers[sIdx%no_sounds].volume += 0.05
            elif k == 'left':
                 if samplers[sIdx%no_sounds].volume > 0:
                     samplers[sIdx%no_sounds].volume -= 0.05
            kb.flush()
        my_sampler3.play()
        t0 = clock.time()
        while clock.time()-t0<2000:
            k1,t1 = kb.get_key(timeout=10)
            if k == 'right':
                 if samplers[sIdx%no_sounds].volume < 1:
                     samplers[sIdx%no_sounds].volume += 0.05
            elif k == 'left':
                 if samplers[sIdx%no_sounds].volume > 0:
                     samplers[sIdx%no_sounds].volume -= 0.05
            kb.flush()
    
        k,t = kb.get_key(timeout=200)
        if k == 'return':
            break
        elif k == 'up':
            sIdx -= 1
        elif k == 'down':
            sIdx += 1
        elif k == 'right':
            if samplers[sIdx%no_sounds].volume < 1:
                samplers[sIdx%no_sounds].volume += 0.05
        elif k == 'left':
            if samplers[sIdx%no_sounds].volume > 0:
                samplers[sIdx%no_sounds].volume -= 0.05
    
        text = "Adjust volume of sound %s. \n\n\nCurrent volume  is %s"%(sIdx%no_sounds,samplers[sIdx%no_sounds].volume)
        cv.clear()
        cv.text(text)
        cv.show()
    

    Hope this works ( I didn't test it).
    Eduard

    Buy Me A Coffee

Sign In or Register to comment.