Computer Science Canada

[Tutorial]How to use sound and music in Turing

Author:  Dan [ Thu Dec 19, 2002 8:46 pm ]
Post subject:  [Tutorial]How to use sound and music in Turing

How to use sound and music in Turing


There are several different methods of adding music and sound to your games and programs. The main methods are using the play command, the Music command and the sound comand.

The Code:

Play

    play ("Notes")

    notes are a b c d e f g

    > and < will change the octave

    flats are - ; sharps are +
    to change the length of the note put a number in front of it. 1 (whole note), 2 (half note), 4 (quarter note), 8 (eight note) and 6 (sixteenth note)

    p will add rests to the song


Music
Music.PlayFile ("filename") %play a sound or music file

Music.SoundOff %stop playing the file

Note: playable file types are wav, snd, mod, and mid (mp3 too in newer turings)


Sound
sound (frequency, duration) %time is in milliseconds


How to use the code:

Note: Turing will play the whole file or string of notes before it cautions along with the rest of the code. To run music or sound and run your program at the same time you need to use a fork

Play code

Simple examples

code:

        play ("<+A") % a sharp in a lower octave
        play (">8abcdefgfedcba") %some eighth notes in a higher octave
        play ("<<1-a-b-c-d-e-f-g") %some whole notes in two octaves                    %lower then middle c and are all flats


Use other code the same time as play

code:

var i : int := 0

%process that plays music
process playstuff
    loop
          play (">abcdefgfedcb")
    end loop
end playstuff

fork playstuff %fork it to play music

loop
     i += 1
     put i, " + ", i, " = ", i + i %do some simple math
     delay (1000)
end loop



Music code

code:

var i : int := 0

process playstuff
    loop
           Music.PlayFile ("bat.MID")
    end loop
end playstuff

fork playstuff
   
loop
         i += 1
         put i, " + ", i, " = ", i + i
         delay (1000)
end loop


Note: the .mid file or the music file you use must be in the same dir as your Turing file or you must specify the dir where you put the files name.


Sound Code

Ex 1.

sound (440, 1000) % this will play a sound that is 440 herzts for 1 seconed


Ex 2.

code:

               for i : 1 .. 10
                    put i
                    sound ( 100 * i, 500 )    % Sound note for 1/2 second
               end for


Ex 3.

code:

          var i : int := 0

          %process that plays music
           process playstuff
        loop
                       sound (440, 500)
                       sound (400, 500)
               sound (500, 500)
                       sound (360, 500)
        end loop
            end playstuff

            fork playstuff %fork it to play music

            loop
        i += 1
        put i, " + ", i, " = ", i + i %do some simple math
        delay (1000)
            end loop



this is an old turale form my old site, if you have any comanets about it plzs post and let us know. also if you see anything wrong with it wich is very likey that whode also help if you posted and let us know.

Edit - Jay Lo told us of the info below

play CD files
yes! now you can make your very own CD player in turing!

ladies and gentlemen, here is your code:

Syntax:
Music.PlayFile ("cd")
- plays entire cd
Music.PlayFile ("cd:x")
- where x is the track number

Author:  cutecotton [ Mon Dec 23, 2002 1:03 am ]
Post subject: 

thank you soo much, you are a blessing from god, i was JSUT going to ask around to see how you can do that...ur a lifesaver! thank you!

Author:  Danger Zone [ Thu Jun 05, 2003 9:55 am ]
Post subject: 

I think my school has a older version of turing, all those play note commands work and stuff but when I go to listen to the thing, nothing comes out. Any Idea what may be causing this? I know different frequenceys work like
sound(100,100)
that works but never the notes like F or D etc none of them work.

Author:  Tony [ Thu Jun 05, 2003 12:58 pm ]
Post subject: 

how old is your "older" version?

Author:  PaddyLong [ Thu Jun 05, 2003 3:33 pm ]
Post subject: 

I know that the play() command works in oot 3.1

Author:  BlAcK TuRtLe [ Thu Jun 05, 2003 4:52 pm ]
Post subject: 

Wow so that's how you do it. Now I can put sound in my end of year program. Thanks alot.

Author:  JayLo [ Thu Jun 05, 2003 8:58 pm ]
Post subject: 

You forgot CD files
yes! now you can make your very own CD player in turing!

ladies and gentlemen, here is your code:

Syntax:
Music.PlayFile ("cd")
- plays entire cd
Music.PlayFile ("cd:x")
- where x is the track number

Author:  Danger Zone [ Fri Jun 06, 2003 9:01 am ]
Post subject: 

my version of turing is 4.0.0 at least that's what help says when I go to "about turing"

Author:  heythere [ Thu Jun 19, 2003 11:46 am ]
Post subject: 

if i use a lot of sound does it lag the program im running?

Author:  JSBN [ Thu Jun 19, 2003 11:49 am ]
Post subject: 

it can on a slower computer. THe computers that i have @ school really are quite bad, so on those computers, if u have sound, the program barley runs. But on my computer the program runs perfectly.

Author:  Asok [ Thu Jun 19, 2003 3:37 pm ]
Post subject: 

JSBN, those computers don't have sound cards. Laughing

Author:  JayLo [ Thu Jun 19, 2003 5:58 pm ]
Post subject: 

rawr. cdplayer.
code:
import GUI
var cdWindow : int := Window.Open ("graphics:900;300")
var cdBack : int := Pic.FileNew ("cdBack.jpg")
var streamNumber : int
var fileName : string
var maxTracks : int := 0
var cdDrive : string
var track : int := 1
var currentTrack : string := "cd:" + intstr (track)
var fontID : int := Font.New ("verdana:10")
var playPic : int := Pic.FileNew ("play.jpg")
var stopPic : int := Pic.FileNew ("stop.jpg")
var nextPic : int := Pic.FileNew ("next.jpg")
var prevPic : int := Pic.FileNew ("previous.jpg")
var playButton : int
var stopButton : int
var nextTrack : int
var previousTrack : int
var quitButton : int
var playing : boolean := false
var cdDriveField, cdDriveLabel : int
var trackID : int := Font.New ("verdana:30")

process playTrack
    Music.PlayFile (currentTrack)
end playTrack

procedure playMusic
    playing := true
    fork playTrack
end playMusic

procedure stopMusic
    Music.PlayFileStop
    playing := false
end stopMusic

procedure next
    Music.PlayFileStop
    if track = maxTracks then
        track := maxTracks
        Draw.FillBox (340, maxy div 2 - 30, 340 + Font.Width ("Track: " + intstr (track), trackID), maxy div 2 + 40, darkgrey)
        Font.Draw ("Track: " + intstr (track), 340, maxy div 2 - 30, trackID, 43)
    else
        track += 1
        Draw.FillBox (340, maxy div 2 - 30, 340 + Font.Width ("Track: " + intstr (track), trackID), maxy div 2 + 40, darkgrey)
        Font.Draw ("Track: " + intstr (track), 340, maxy div 2 - 30, trackID, 43)
    end if
    currentTrack := "cd:" + intstr (track)
    if playing then
        fork playTrack
    else
    end if
end next

procedure previous
    Music.PlayFileStop
    if track = 1 then
        track := 1
        Draw.FillBox (340, maxy div 2 - 30, 340 + Font.Width ("Track: " + intstr (track), trackID), maxy div 2 + 40, darkgrey)
        Font.Draw ("Track: " + intstr (track), 340, maxy div 2 - 30, trackID, 43)
    else
        track -= 1
        Draw.FillBox (340, maxy div 2 - 30, 340 + Font.Width ("Track: " + intstr (track), trackID), maxy div 2 + 40, darkgrey)
        Font.Draw ("Track: " + intstr (track), 340, maxy div 2 - 30, trackID, 43)
        currentTrack := "cd:" + intstr (track)
    end if
    if playing then
        fork playTrack
    else
    end if
end previous

procedure closeWindow
    quit
end closeWindow

procedure error (text : string)
    GUI.Dispose (cdDriveField)
    GUI.Dispose (cdDriveLabel)
    var key : string (1)
    Font.Draw (text, 340, maxy div 2, trackID, 43)
    Font.Draw ("Press Any Key", 360, maxy div 2 - 30, fontID, 43)
    getch (key)
    quit
end error

procedure inputText (text : string)
    if text = "c" or text = "C" then
        error ("That's not the cd drive.")
    else
        var dirCheck : int
        dirCheck := Dir.Open (text + ":\\")
        if dirCheck = 0 then
            error ("Drive Does Not Exist")
            closeWindow
        else
            streamNumber := Dir.Open (text + ":")
            fileName := Dir.Get (streamNumber)
            if index (fileName (* -4 .. *), ".cda") not= 0 then
                loop
                    fileName := Dir.Get (streamNumber)
                    exit when fileName = ""
                    maxTracks += 1
                end loop
                Dir.Close (streamNumber)
                GUI.Dispose (cdDriveField)
                GUI.Dispose (cdDriveLabel)
                Pic.Draw (cdBack, 0, 0, picMerge)
                Draw.FillBox (340, maxy div 2 - 30, 340 + Font.Width ("Track: " + intstr (track), trackID), maxy div 2 + 40, darkgrey)
                Font.Draw ("Track: " + intstr (track), 340, maxy div 2 - 30, trackID, 43)
                playButton := GUI.CreatePictureButtonFull (120, 100, playPic, playMusic, 0, 0, "'", true)
                stopButton := GUI.CreatePictureButtonFull (600, 100, stopPic, stopMusic, 0, 0, "'", true)
                nextTrack := GUI.CreatePictureButtonFull (120, 50, nextPic, next, 0, 0, "/", true)
                previousTrack := GUI.CreatePictureButtonFull (600, 50, prevPic, previous, 0, 0, "/", true)
                quitButton := GUI.CreateButtonFull (360, 10, 0, "Quit", closeWindow, 0, "/", true)
            else
                error ("Invalid CD in Drive " + text + ":\\.")
            end if
            Dir.Close (dirCheck)
        end if
    end if
end inputText

procedure startScreen
    Pic.Draw (cdBack, 0, 0, picMerge)
    cdDriveField := GUI.CreateTextFieldFull (maxx div 2 + 100, 230, 100, "", inputText, GUI.INDENT, fontID, GUI.ANY)
    cdDriveLabel := GUI.CreateLabelFull (maxx div 2, 230, "CD Drive: ", 0, 0, GUI.LEFT, fontID)
end startScreen
startScreen
loop
    exit when GUI.ProcessEvent
end loop

Author:  HyperFlexed [ Sat Nov 06, 2004 4:26 pm ]
Post subject: 

Jay Lo....

'ANY' is not in the export list of 'GUI'

I got that error when I tried your code.

Author:  atrain [ Sun Jan 30, 2005 2:00 pm ]
Post subject: 

any way to have music run in the background?? it stops the program untill the music is done playing, but i want it as background...

Author:  atrain [ Sun Jan 30, 2005 2:02 pm ]
Post subject: 

nevermind:

process DoMusic
loop
Music.PlayFile ("branden3.wav")
end loop
end DoMusic

fork DoMusic
code:

Author:  Cervantes [ Sun Jan 30, 2005 2:56 pm ]
Post subject: 

If you have Turing 4.0.5, you can use Music.PlayFileLoop or Music.PlayFileReturn.

Author:  ssr [ Sun Jan 30, 2005 4:29 pm ]
Post subject: 

just speakig of music
what music file extensions are supported in Turing
I know mp3, wav, and is that it?

Author:  Cervantes [ Sun Jan 30, 2005 4:38 pm ]
Post subject: 

and midi
Check the help file for these things.

Author:  atrain [ Sun Jan 30, 2005 9:27 pm ]
Post subject: 

mp3 was introduced in 4.0, i looked through the changelog....

my school uses macs, and the latest version they have is OOT 4.0, so i have to look things up to see if i can use them....

Author:  illu45 [ Sun Mar 13, 2005 5:44 pm ]
Post subject: 

Hey,

I was wondering, in regards to the Sound playing thing, if there was any way to include slurs or ties, or change the tempo, since its a little difficult to do some of the things I'd like to without slurs/ties or doubling note values.

Author:  [Gandalf] [ Mon Mar 14, 2005 1:39 am ]
Post subject: 

No, in the current version of Turing there are no slurs/ties and most other music features. It would improve the music capabilities and allow for more lagless background music - so I hope to see it in future versions too!

Author:  Klick [ Thu Mar 31, 2005 4:24 pm ]
Post subject: 

There should also be a way to get 2 hands in there, so nice harmony can be made, or even the capability to change instruments, then when I am ahead in comp class as usuall, I can write a symphony. Some times I think I really have no life lol.

Author:  [Gandalf] [ Thu Mar 31, 2005 5:01 pm ]
Post subject: 

Can't you just use a process Shifty and have two pianos playing at the same time?

Ya, more instruments and higher capabilities would be very nice.

Author:  GlobeTrotter [ Thu Mar 31, 2005 6:01 pm ]
Post subject: 

nope, can't just use a process. It simply won't work. It will play the files in between each other, sort of.

Author:  bolufromthesoo [ Sat May 14, 2005 7:54 pm ]
Post subject: 

Quote:
Music.SoundOff %stop playing the file


Hi, (first post @ CompSci 8)).

I was wondering where and how you use "Music.SoundOff". I have:
code:

process Theme
    Music.PlayFile ("Theme.mp3")
end Theme

%the rest of the code

but I have no idea how to stop the music when the code is finished.

I know the last post was old, but I really need help.

Author:  bolufromthesoo [ Sat May 14, 2005 9:04 pm ]
Post subject: 

Sry. for double post... I figured it out. It was suppose to be :

Quote:

Music.PlayFileStop


Can't blame me.. typo. Laughing ...right?

Author:  [Gandalf] [ Sat May 14, 2005 9:08 pm ]
Post subject: 

You could have just edited your previous post...

*still waiting for Turing update Sad *

Author:  n_n [ Mon Sep 19, 2005 2:49 pm ]
Post subject: 

I'm just wondering. Is it possible to make a song fade in/out? Specifically a MIDI. Thanks in advanced.

Author:  Tony [ Mon Sep 19, 2005 3:06 pm ]
Post subject: 

I don't think that Turing has such capabilities at the moment.
Depending on what you're trying to do, it might be possible to simply hardcode the fade using a 3rd party program such as Sound Forge

Author:  [Gandalf] [ Mon Sep 19, 2005 3:30 pm ]
Post subject: 

Usually, the point of a midi is that it is all computer generated, making editing it quite easy compared to editing wavs, mp3s, etc. Any midi editing program will have a 'volume' feature that you can decrease over time.

Author:  Tony [ Mon Sep 19, 2005 4:06 pm ]
Post subject: 

if you understand the MIDI format, it is possible to open it as text/binary file and edit it with Turing. Though once again, that would be editing the source file, not controlling volume real-time.

Author:  ZeroPaladn [ Wed Sep 21, 2005 9:20 am ]
Post subject: 

nice tutorial (white the rest of you were ranting on about compatiable files, i actually read the tutorial Laughing ). regardless, i though .wma was compatiable with turing 4.0.5

Author:  DIIST [ Wed Dec 14, 2005 10:17 pm ]
Post subject: 

Nice Tutorial! Thought I am wondering, is it possible to seek to a specific time in the music file using Turing! Confused

Author:  Cervantes [ Thu Dec 15, 2005 1:44 pm ]
Post subject: 

thuvs wrote:
Nice Tutorial! Thought I am wondering, is it possible to seek to a specific time in the music file using Turing! Confused

Nooope. (But that doesn't stop you from experimenting. You may find a way: you never know)

Author:  chrispminis [ Thu Dec 15, 2005 10:18 pm ]
Post subject: 

Hmmm... this Sound Forge you speak of... Sounds useful. Where might I acquire this Sound Forge? And would it cost any money? If so, how much and are there any alternatives to Sound Forge that are free to use, but perhaps less powerful, but still useful.

Author:  solblade05 [ Wed Jan 04, 2006 1:41 pm ]
Post subject: 

is it possible to keep the music playing for a certain part of the program, then when the user enters another part of the program i can change the music

Author:  Cervantes [ Wed Jan 04, 2006 3:36 pm ]
Post subject: 

Just use Music.Stop I think it is, and play the next file.

Author:  solblade05 [ Wed Jan 04, 2006 3:52 pm ]
Post subject: 

There isn't that command in turing c 4.04
oh, also can i uses processes over and over again? for the music that i need in the different parts of my program (with the fork)?

Author:  [Gandalf] [ Wed Jan 04, 2006 3:53 pm ]
Post subject: 

Or if the files are the same format, iirc you can just play the next song over the current one without using Music.PlayFileStop().

*edit* Don't fork your music, use Music.PlayFileLoop(). If your version doesn't have Music.PlayFileStop() then just use the method I describe. Just make sure all the music is in the same music format (.mp3, .wav, whatever it is).

Author:  solblade05 [ Wed Jan 04, 2006 3:55 pm ]
Post subject: 

How do i do that?

Author:  [Gandalf] [ Wed Jan 04, 2006 4:21 pm ]
Post subject: 

Ok, assuming you have either Music.PlayFile() or Music.PlayFileLoop(), just make sure that in the folder that you have the source code in has your music, all in the same format (with the same extention). An MP3 has a .mp3 extention, so if you want to use that then make sure all your background music is an MP3.

Then, just have something like this in your program, this being a highly simple example.
code:
Music.PlayFileLoop("BackgroundSongOne.mp3")
put "Press any key to switch songs."
Input.Pause   %In place of this you can have any condition you need
Music.PlayFileLoop("BackgroundSongTwo.mp3")

Author:  solblade05 [ Wed Jan 04, 2006 6:49 pm ]
Post subject: 

Music.PlayFileLoop is not in Turing 4.04
thus i cant use that Sad

is it possible that if i use Music.PlayFileStop, then use another process, and Music.PlayFile?

Author:  stinger4life [ Mon Jan 16, 2006 4:56 pm ]
Post subject: 

in my game i want my bg music to run all the way until they die...i have many sound however so if i use Music.PlayFileStop will it only stop my bg music or even the other sounds?

Author:  siege [ Wed May 31, 2006 1:00 pm ]
Post subject: 

can you play mp3 files? or must it be in wav or midi format?

Author:  Tony [ Wed May 31, 2006 3:13 pm ]
Post subject: 

it's part of the tutorial
Hacker Dan wrote:

Note: playable file types are wav, snd, mod, and mid (mp3 too in newer turings)

Author:  NE0 [ Fri Jun 02, 2006 3:51 pm ]
Post subject: 

i know this might not be the right place to ask but is there a way ( i've already searched ) to load the music files on a loading screen or at another time in my game because i have my music file in a process statement but it has to load the music file before i start to play my game creating atleast a 10 second wait and the music file isnt really that big but im running it of a floopy (i have to because im switching computers alot). if there is a way can you guys help me?

regards NE0

Author:  Delos [ Fri Jun 02, 2006 3:58 pm ]
Post subject: 

I would suspect that this is directly a result of using a floppy. Simply paste its contents onto the harddrive of whatever PC you're at and work from
there. Don't forget to resave after.
If this doesn't work, repost this question in [Turing Help] and attach some code as well so we get a better idea of your situation.
Also, if you're using a process, you might want to look into Music.PlayFileReturn() instead, since that will make your life easier.

Author:  little_guy9 [ Mon Apr 16, 2007 12:21 am ]
Post subject:  Re: [Tutorial]How to use sound and music in Turing

Would there be some coding that could replace Music.PlayFileLoop, for those of us who don't have that version?

Author:  Clayton [ Mon Apr 16, 2007 10:04 am ]
Post subject:  Re: [Tutorial]How to use sound and music in Turing

This sort of question would've been better suited to be posted in [Turing Help]. However, it's here now so I'll answer it.

Seeing as you don't have Music.PlayFileLoop() at your disposal, you simply need to loop within a process and have a (global) variable to keep track of whether or not to keep playing or not. This would be accompanied by a Music.Stop() in your mainline that will trigger your boolean to be false, thus exiting your process.

Author:  MichaelM [ Fri Jan 25, 2008 4:46 pm ]
Post subject:  Re: [Tutorial]How to use sound and music in Turing

I was just reading this topic and I thought this link might be helpful for anyone trying to program some music by frequency:
http://www.phy.mtu.edu/~suits/notefreqs.html
Note you'd have to round the frequencies off.

Author:  faulkner16 [ Thu Feb 21, 2008 6:39 pm ]
Post subject:  RE:[Tutorial]How to use sound and music in Turing

what is the A with the ^ over it?

Author:  harishmabish [ Sat Mar 29, 2008 5:19 pm ]
Post subject:  RE:[Tutorial]How to use sound and music in Turing

do wav and other sound type files work better than mp3? because i encoded in mp3, and the sounds seem delayed, so are other formats faster?

Author:  angelsz [ Tue May 20, 2008 12:10 am ]
Post subject:  Re: [Tutorial]How to use sound and music in Turing

How does Music.Soundoff work ? o.o


if i have ..

Music.PlayFile ("abc.mp3")
code code code
delay (1000)
Music.SoundOff

it continues to play..

Author:  Tony [ Tue May 20, 2008 12:59 am ]
Post subject:  RE:[Tutorial]How to use sound and music in Turing

You should probably notice that none of your "code code code" executes while the sound file is playing. You likely meant to use Music.PlayFileReturn()

Author:  angelsz [ Tue May 20, 2008 7:50 am ]
Post subject:  Re: [Tutorial]How to use sound and music in Turing

oh okay thanks o.o

Author:  mrt2323 [ Wed Apr 01, 2009 11:32 pm ]
Post subject:  Re: [Tutorial]How to use sound and music in Turing

So umm playing mp.3 files in the background? how do u do that? plz and thank you

Author:  Krocker [ Sat Dec 04, 2010 8:32 am ]
Post subject:  RE:[Tutorial]How to use sound and music in Turing

hey guys, i dont get what the fork command is for. whats does it do? Also, how do u stop the music when there is an input.

ex

in name = john
then
% End the music%
blah blah
end if

any one?

Author:  Insectoid [ Sat Dec 04, 2010 6:26 pm ]
Post subject:  RE:[Tutorial]How to use sound and music in Turing

Fork executes a process that runs side-by-side with the rest of your program. More recent versions of Turing have the function Music.PlayFileLoop() built in, so there is no need to use processes or fork() anymore.

Music.PlayFileLoop() will play loop a sound file while allowing the rest of your program to execute. Music.PlayFileStop() will halt the sound.

Author:  Dragon20942 [ Sun Jan 02, 2011 1:23 pm ]
Post subject:  Re: [Tutorial]How to use sound and music in Turing

My music creates a lot of lag in the program. Is there any way to eliminate that? Even something like this:

code:

process moo
loop
Music.PlayFile("something.mid")
end loop
end moo

View.Set ("offscreenonly")
loop
fork moo
drawfillbox (0,0,maxx,maxy,Rand.Int (0,255))
View.Update
end loop


completely wrecks my program.

Author:  Insectoid [ Sun Jan 02, 2011 1:27 pm ]
Post subject:  RE:[Tutorial]How to use sound and music in Turing

You're calling fork in a loop, so every time your program iterates it creates a new instance of moo. With potentially thousands of instances running at a time, this is going to lag.

Try putting the fork call outside the loop.

Author:  Dragon20942 [ Sun Jan 02, 2011 2:07 pm ]
Post subject:  RE:[Tutorial]How to use sound and music in Turing

Ok thanks. O_O

Author:  SirOwnYourFace [ Tue Jan 18, 2011 7:40 pm ]
Post subject:  Re: [Tutorial]How to use sound and music in Turing

Some mp3s wont play on turing D:..

Author:  Tony [ Tue Jan 18, 2011 8:06 pm ]
Post subject:  RE:[Tutorial]How to use sound and music in Turing

It could be encoded in a way that Turing doesn't understand. It could potentially help to re-encode the file with different settings, or export it into another supported format.

Author:  Feltie [ Sat Jun 04, 2011 2:07 pm ]
Post subject:  RE:[Tutorial]How to use sound and music in Turing

It wont work when i try to play the music straight from my turing folder Sad

Author:  Feltie [ Sat Jun 04, 2011 2:51 pm ]
Post subject:  RE:[Tutorial]How to use sound and music in Turing

im not sure which folder to put the file in. do i put it in turing 4.4.1, Support or Examples?

Author:  Tony [ Sat Jun 04, 2011 3:01 pm ]
Post subject:  RE:[Tutorial]How to use sound and music in Turing

You put it in the same folder as the source-code file that you are working with.

Author:  Raknarg [ Sat Jun 04, 2011 4:29 pm ]
Post subject:  RE:[Tutorial]How to use sound and music in Turing

Sometimes it could just be the length of the song. I'm not quite sure if this is right, but i've found that with songs that are over 4 minutes, a lot of them don't work.

Author:  danthedevil3 [ Fri Sep 13, 2013 8:28 pm ]
Post subject: 

JayLo @ Thu Jun 19, 2003 5:58 pm wrote:
rawr. cdplayer.
code:
import GUI
var cdWindow : int := Window.Open ("graphics:900;300")
var cdBack : int := Pic.FileNew ("cdBack.jpg")
var streamNumber : int
var fileName : string
var maxTracks : int := 0
var cdDrive : string
var track : int := 1
var currentTrack : string := "cd:" + intstr (track)
var fontID : int := Font.New ("verdana:10")
var playPic : int := Pic.FileNew ("play.jpg")
var stopPic : int := Pic.FileNew ("stop.jpg")
var nextPic : int := Pic.FileNew ("next.jpg")
var prevPic : int := Pic.FileNew ("previous.jpg")
var playButton : int
var stopButton : int
var nextTrack : int
var previousTrack : int
var quitButton : int
var playing : boolean := false
var cdDriveField, cdDriveLabel : int
var trackID : int := Font.New ("verdana:30")

process playTrack
    Music.PlayFile (currentTrack)
end playTrack

procedure playMusic
    playing := true
    fork playTrack
end playMusic

procedure stopMusic
    Music.PlayFileStop
    playing := false
end stopMusic

procedure next
    Music.PlayFileStop
    if track = maxTracks then
        track := maxTracks
        Draw.FillBox (340, maxy div 2 - 30, 340 + Font.Width ("Track: " + intstr (track), trackID), maxy div 2 + 40, darkgrey)
        Font.Draw ("Track: " + intstr (track), 340, maxy div 2 - 30, trackID, 43)
    else
        track += 1
        Draw.FillBox (340, maxy div 2 - 30, 340 + Font.Width ("Track: " + intstr (track), trackID), maxy div 2 + 40, darkgrey)
        Font.Draw ("Track: " + intstr (track), 340, maxy div 2 - 30, trackID, 43)
    end if
    currentTrack := "cd:" + intstr (track)
    if playing then
        fork playTrack
    else
    end if
end next

procedure previous
    Music.PlayFileStop
    if track = 1 then
        track := 1
        Draw.FillBox (340, maxy div 2 - 30, 340 + Font.Width ("Track: " + intstr (track), trackID), maxy div 2 + 40, darkgrey)
        Font.Draw ("Track: " + intstr (track), 340, maxy div 2 - 30, trackID, 43)
    else
        track -= 1
        Draw.FillBox (340, maxy div 2 - 30, 340 + Font.Width ("Track: " + intstr (track), trackID), maxy div 2 + 40, darkgrey)
        Font.Draw ("Track: " + intstr (track), 340, maxy div 2 - 30, trackID, 43)
        currentTrack := "cd:" + intstr (track)
    end if
    if playing then
        fork playTrack
    else
    end if
end previous

procedure closeWindow
    quit
end closeWindow

procedure error (text : string)
    GUI.Dispose (cdDriveField)
    GUI.Dispose (cdDriveLabel)
    var key : string (1)
    Font.Draw (text, 340, maxy div 2, trackID, 43)
    Font.Draw ("Press Any Key", 360, maxy div 2 - 30, fontID, 43)
    getch (key)
    quit
end error

procedure inputText (text : string)
    if text = "c" or text = "C" then
        error ("That's not the cd drive.")
    else
        var dirCheck : int
        dirCheck := Dir.Open (text + ":\\")
        if dirCheck = 0 then
            error ("Drive Does Not Exist")
            closeWindow
        else
            streamNumber := Dir.Open (text + ":")
            fileName := Dir.Get (streamNumber)
            if index (fileName (* -4 .. *), ".cda") not= 0 then
                loop
                    fileName := Dir.Get (streamNumber)
                    exit when fileName = ""
                    maxTracks += 1
                end loop
                Dir.Close (streamNumber)
                GUI.Dispose (cdDriveField)
                GUI.Dispose (cdDriveLabel)
                Pic.Draw (cdBack, 0, 0, picMerge)
                Draw.FillBox (340, maxy div 2 - 30, 340 + Font.Width ("Track: " + intstr (track), trackID), maxy div 2 + 40, darkgrey)
                Font.Draw ("Track: " + intstr (track), 340, maxy div 2 - 30, trackID, 43)
                playButton := GUI.CreatePictureButtonFull (120, 100, playPic, playMusic, 0, 0, "'", true)
                stopButton := GUI.CreatePictureButtonFull (600, 100, stopPic, stopMusic, 0, 0, "'", true)
                nextTrack := GUI.CreatePictureButtonFull (120, 50, nextPic, next, 0, 0, "/", true)
                previousTrack := GUI.CreatePictureButtonFull (600, 50, prevPic, previous, 0, 0, "/", true)
                quitButton := GUI.CreateButtonFull (360, 10, 0, "Quit", closeWindow, 0, "/", true)
            else
                error ("Invalid CD in Drive " + text + ":\\.")
            end if
            Dir.Close (dirCheck)
        end if
    end if
end inputText

procedure startScreen
    Pic.Draw (cdBack, 0, 0, picMerge)
    cdDriveField := GUI.CreateTextFieldFull (maxx div 2 + 100, 230, 100, "", inputText, GUI.INDENT, fontID, GUI.ANY)
    cdDriveLabel := GUI.CreateLabelFull (maxx div 2, 230, "CD Drive: ", 0, 0, GUI.LEFT, fontID)
end startScreen
startScreen
loop
    exit when GUI.ProcessEvent
end loop


I get an error of, Illegal picture ID number '0'. (Probable cause: picture was not successfully created.).

Can someone tell me what is going on? I'm new to this on my first computer science classes in high school.

Author:  Insectoid [ Fri Sep 13, 2013 8:35 pm ]
Post subject:  RE:[Tutorial]How to use sound and music in Turing

That means the image wasn't loaded. The most likely cause is that you do not have a copy of the image.


: