View Single Post
  #1  
Old 02-04-2016, 07:47 AM
jelly jelly is offline
Member
 
Join Date: Feb 2016
Posts: 38
playing sound and displaying picture - condition within loop

Dear all,

I am a programming beginner and I started using Vizard because I am trying to design a little experiment for my studies. Here is what is supposed to happen and below is what I have so far:

The screen should show an upper and a lower window. Each window will receive a simple texture (.png or .jpeg) to display two different pictures. In the upper window, there would be the task instruction and in the lower window, there would be a "default" picture. At the same time, a beep sound will play 10 times at random intervals (from 1 to 4 seconds). What I want: 7 out of ten random times, just as a sound cue plays, the lower screen should change from the default picture to Picture A and 3 out of 10 random times to Picture B.

Code:
#Set background colour to white:
viz.clearcolor(viz.WHITE)

taskpic = 'instruction.jpg'
pictureA = 'picA.jpg'
pictureB = 'picB.jpg'
picturedefault = 'default.png'
cue = viz.addAudio('dong.wav')
SoundDuration = cue.getDuration()

import viz 															
import time
import viztask
import random														
viz.go()

def exp(): #this is the function that triggers my experiment

	screen2 = viz.addTexQuad(size=0.5)            #upper screen window
	screen2.setPosition([0,2.1,1.3])
	screen2.setSize([0.4, 0.4])

	screen3 = viz.addTexQuad(size=0.5)            #lower screen window
	screen3.setPosition([0,1.6,1.3])
	screen3.setSize([0.4, 0.4])

	picture_task = viz.add(taskpic)
	defaultpic = viz.add(default)

	screen2.texture(picture_task)			#picture with instruction is added as a texture to upper screen
	screen3.texture(defaultpic)			#default picture is added as a texture to upper screen
	
	def playCueLoop():
		count = 0
		while count < 10:
			count += 1
			for x in count:
				if x <= 7:
					cue.play()
					yield viztask.waitTime(SoundDuration+random.randrange(1,4))
					cue.stop()
					PicA = viz.add(PictureA)
					screen3.texture(PicA)
					yield viztask.waitTime()
				elif x <= 3:
					cue.play()
					yield viztask.waitTime(SoundDuration+random.randrange(1,4))
					cue.stop()
					PicB = viz.add(PictureB)
					screen3.texture(PicB)
					yield viztask.waitTime()
				
	#viztask.schedule(playCueLoop)

viztask.schedule(exp)
I know there must be a lot of things wrong in this code (apart from the if statement - maybe the rationale behind the changing of the screen windows?), but it sort of worked until I had the if condition (7 and 3 out of 10). I've played around with it and sometimes I still get the sound to play, but at no point have I managed to change the lower screen, even without a complex condition attached to it.

I would be happy to receive some pointers in the right direction! At the moment there are probably a lot of things to improve and it is impossible for me to isolate one aspect to test it around.

Thank you very much in advance for your help!
Reply With Quote