Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Playing Music and Global Variables
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
LegendsEnd




PostPosted: Tue May 23, 2006 8:39 pm   Post subject: Playing Music and Global Variables

I thought I could do my program without using a global variable but I've run into a problem when it comes to music, I'm using Turing 4.03 if it makes a difference.

code:

process PlayMusic (file : string)
    loop
        Music.PlayFile (file)
    end loop
end PlayMusic

procedure Program
fork PlayMusic ("BG.mp3")
%Program
end Program


When I use Music.PlayFileStop, it just restarts the process, if I make the process like so:
code:

process PlayMusic (file : string, stop : boolean)
    loop
        exit when stop = true
        Music.PlayFile (file)
    end loop
end PlayMusic


While that works if I were to just do fork PlayMusic ("a.wav",true), if I played a file first then did that, the second process would end and the first process would just continue again. I can't think of a way to stop it from another procedure however, so only thing I can think of now is a global variable. Should I be trying to make a class and find a workaround that way?
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Tue May 23, 2006 8:51 pm   Post subject: (No subject)

Does Music.PlayFileReturn() work on your version? If so, you won't need to use processes at all.

As for the lack of globabl variables, it is quite possible - as long as your play your cards correctly. Bottle things up into modules or classes, which each can have their own local variables that pervade the structure. Then, parameters, parameters, parameters - you'll be passing and parsing more than you ever thought possible, but it will all end up in your favour, as you won't have to worry about hard-coded lines floating around the place! (That's really whay global variables introduce, levels on hard-coding that are not as dynamic as parameter-passed vars).
LegendsEnd




PostPosted: Tue May 23, 2006 10:46 pm   Post subject: (No subject)

Music.PlayFileReturn does not work on my version, is there a way to create it manually? If not, would it be proper programming if I were to just throw that part of my program into a module and have a global variable type thing in it?
LegendsEnd




PostPosted: Wed May 24, 2006 11:01 am   Post subject: (No subject)

Hmm I just did the whole module method but it doesn't seem to work. My module:
code:

unit
module PlayMusic
    export Start, Stop
    var stop : boolean
    process Start (file : string)
        stop := false
        loop
            exit when stop = true
            Music.PlayFile (file)
        end loop
        Music.PlayFileStop
    end Start
    procedure Stop
        stop := true
    end Stop
end PlayMusic

Simple program:

code:

import PlayMusic
fork PlayMusic.Start ("tes4title.mp3")
PlayMusic.Stop
Delos




PostPosted: Wed May 24, 2006 2:04 pm   Post subject: (No subject)

Looking at your Play process, it seems as though it will continuously attempt to play the file until a Stop is called - which would result it a staticy 'buzz' screaming through your speakers.

If you have access to a newer version of Turing through your school, see if you can get it since PlayFileReturn() is definitely a useful procedure.

If you can't, and you want to keep plugging away with processes, then do the following:
- read the basic tuts on processes
- read the warning tuts on processes explaining their evils
- read Tony's advanced tuts on processes and concurrency
- look into Monitors. These are rarely used constructs that you could allow you to switch processes on and off (I believe the commands are something to the ring of 'wait' and 'wake'...or such analagous terms).

I don't suggest this though, since it's a lot more work than would seem necassary. If I understand, you're trying to loop some music while playing? And this music might change during the programme? In which case, you might be able to get away with changing the name of the track that is pulled by your play process...
Oh, and what was the specific error you got when you tried your Module?
LegendsEnd




PostPosted: Wed May 24, 2006 4:18 pm   Post subject: (No subject)

After a lot of messing around I got it working. It wasn't that the program wasn't working, it was just my stop procedure didn't stop it. I fixed this by playing a 1 millisecond delay after forking the play process and THEN calling the stop procedure. Apparently calling a process and then a procedure right afterwards is a bad idea.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: