WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Basic soundtrack (https://forum.worldviz.com/showthread.php?t=988)

Plymouth 01-29-2007 07:00 AM

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()

Jerry 01-29-2007 10:46 AM

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()

farshizzo 01-29-2007 10:55 AM

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()


Gladsomebeast 01-29-2007 11:01 AM

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/

Gladsomebeast 01-29-2007 11:03 AM

Triple jinks!!!

farshizzo 01-29-2007 11:13 AM

Quote:

Originally Posted by Gladsomebeast (Post 3601)
Triple jinks!!!

It's spelled jinx. I guess we get off on a technicality. Your powers, they do nothing!!!

Plymouth 01-30-2007 01:34 AM

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.


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

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