WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 01-29-2007, 07:00 AM
Plymouth Plymouth is offline
Member
 
Join Date: May 2006
Location: Plymouth, UK
Posts: 24
Basic soundtrack

I'm trying to learn the basics of scripting in Vizard/Python and I'm having some confusion. The code below is intended to just start, stop, or change the background music, but it's getting errors that I suppose must stem from me not understanding variable declarations. If someone could give me a push in the right direction I'd really appreciate it.


music = viz.add('song1.mp3')
music.loop(viz.ON)

def ChangeToFirst():
music.rewind()
music = viz.add('song1.mp3')
music.play()

def ChangeToSecond():
music.rewind()
music = viz.add('song2.mp3')
music.play()

def StopTheMusic():
music.rewind()

def mykey(key):
if key == '1':
ChangeToFirst()
elif key == '2':
ChangeToSecond()
elif key == ' ':
StopTheMusic()
Reply With Quote
  #2  
Old 01-29-2007, 10:46 AM
Jerry Jerry is offline
Member
 
Join Date: Jun 2004
Posts: 105
Here's how I would do that.

m1 = viz.add('c:/winnt/media/canyon.mid')
m1.loop()
m2 = viz.add('c:/winnt/media/flourish.mid')
m2.loop()


def onkeydown(key):

if key == '1':
m2.stop()
m1.play()
if key == '2':
m1.stop()
m2.play()
Reply With Quote
  #3  
Old 01-29-2007, 10:55 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Jerry's method is probably the best way to do it. In your sample code, you were adding the file every time the music is changed, which is inefficient. So if you changed the music 5 times, you would have created 6 audio objects, which would waste your system resources.

Also, the error your were receiving was probably due to the fact that you were not declaring the music variable as global. In Python, if you assign a value to a global variable inside a function, you must declare that variable as global. Here is sample code:
Code:
def ChangeToFirst():
	global music
	music.rewind()
	music = viz.add('song1.mp3')
	music.play()

def ChangeToSecond():
	global music
	music.rewind()
	music = viz.add('song2.mp3')
	music.play()
Reply With Quote
  #4  
Old 01-29-2007, 11:01 AM
Gladsomebeast Gladsomebeast is offline
Member
 
Join Date: Mar 2005
Location: Isla Vizta, CA
Posts: 397
Ah, the "global" varable problem. The first sound file you create and assign to the "music" varable is a global varable.

When you use the equals sign in a fuction, it creates a *new and local* varable which only lives within the function. So your "Change" fuctions are trying to create new sound objects and playing them without reasigning the global music function. You are probably getting the "accessing varable before instatiated" error here because you are trying to rewind() the music varable, then assigning to it.

The following code avoids this by added a 'currentMusic' parameter to the fuctions which takes as an argument the global music varable:

Code:
music = viz.add('song.mp3')
music.play()

def stopOldAndPlayNewSong(currentMusic, newSongFile):
    currentMusic.stop()
    currentMusic = viz.add(newSongFile)
    currentMusic.play()

def onkey(key):
    key == '1':
         stopOldAndPlayNewSong(music, 'song1.mp3')
Also, Vizard does not have a rewind() function.


Rather than diving right into Vizard programming, I strongly suggest investing in your python skills. Here is an exclent tutorial: http://docs.python.org/tut/
__________________
Paul Elliott
WorldViz LLC
Reply With Quote
  #5  
Old 01-29-2007, 11:03 AM
Gladsomebeast Gladsomebeast is offline
Member
 
Join Date: Mar 2005
Location: Isla Vizta, CA
Posts: 397
Triple jinks!!!
__________________
Paul Elliott
WorldViz LLC
Reply With Quote
  #6  
Old 01-29-2007, 11:13 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Quote:
Originally Posted by Gladsomebeast View Post
Triple jinks!!!
It's spelled jinx. I guess we get off on a technicality. Your powers, they do nothing!!!
Reply With Quote
  #7  
Old 01-30-2007, 01:34 AM
Plymouth Plymouth is offline
Member
 
Join Date: May 2006
Location: Plymouth, UK
Posts: 24
Thanks so much. I'm reading Dive Into Python, but it's coming to me at a frustratingly slow pace. Why can't everything be programmed in C++? Sad.

Thanks for explaining about the global variables, that will come in handy in all sorts of things.
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 03:08 AM.


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