WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   playing a sound multiple times at different timings (https://forum.worldviz.com/showthread.php?t=5623)

jelly 02-01-2016 02:29 AM

playing a sound multiple times at different timings
 
Dear all,

I am new to Vizard and to programming in general. I need to use it to program an experiment for my studies and while I have already done a number of tutorials, I still feel quite insecure. I wanted to ask for your help with the following:

I am trying to create a function that will basically call my trial (displaying two 2D pictures on the screen and playing some sounds). Because I got stuck at the sounds part, I started a new Vizard file just for the sound to try out different things.

I want the sound to play 10 times. I figured a loop should do the trick, but I can't seemt o find a way to do that...I've looked at sound.loop(viz.ON) and at viz.playSound('pip.wav', viz.LOOP) but these are endless loops. Here is what I have so far:

#stimulus
pip = viz.addAudio('pip.wav')

#Import Vizard modules:
import viz import time
import viztask
import vizact

#Start
viz.go()

#Sounds

cue = 1

while cue <= 10:
pip.play() # This should be executed 10 times
#viz.playSound('pip.wav')
cue += 1

But this doesn't play the sound 10 times...Not to mention that, ideally, I would want the sound to play at randomly intersperced intervals until the last (10th) sound is played. But that will be my next challenge. First, I need to figure out how to play the sound 10 times.

I would be really greatful for any pointers in the right direction! Thank you very much in advance!

Jelly

Erikvdb 02-02-2016 02:18 AM

Here you go, I also included the random wait :cool:

Code:

import viz
import viztask
import random

viz.go()

sound = viz.addAudio('pip.wav')
soundDuration = sound.getDuration()

def playSoundLoop():
    count = 0
    while count < 10:
        count += 1
        sound.play()
        yield viztask.waitTime(soundDuration+random.randrange(1,4))
        sound.stop()
               
viztask.schedule(playSoundLoop)

Couple of notes:
soundDuration is the duration of the sound you want to play. I use that as the minimal waitTime, to make sure Vizard won't attempt to play the sound again before the first one has finished.
I say attempt, because Vizard won't play the sound from the start if that same sound object is still playing. This was the problem in your code.
This is also why I included the sound.stop() at the end, to make 100% sure the sound is stopped before we try to play it again.
The random.randrange() is the random time after the sound has played for Vizard to wait until the next sound, in this example it'll be between 1 and 4 seconds.

jelly 02-02-2016 10:35 AM

Thank you!
 
Hi Erikvdb!

First of all thank you so much for the prompt and detailed help! It really helps me understand what is happening in the code. Before I saw your reply, I eventually had managed to do this, it sort of seemed to work:

import viz
import viztask
import random

viz.go()

sound = viz.addAudio('pip.wav')

def soundloop():
for x in range (10):
yield viztask.waitTime(random.randrange(3,7,1))
pip.play()
soundtask = viztask.schedule(soundloop())

But I can understand how your code if different and better and I will play around with it a bit to see what happens if I change various bits of it.

Again, thank you so very much! :)

Erikvdb 02-04-2016 03:39 AM

No problem!
Yea it's up to you whether you want the sound to start directly after calling the function or not and if the wait has to been exactly between the end of the previous sound and the start of the next. I assume your sound is literally just a pip, so it won't matter so much, but I've ran into problems with this before, wondering why my sounds wouldn't play in quick succession and wishing someone had told me beforehand :D


All times are GMT -7. The time now is 01:49 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC