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

Username:   Password: 
 RegisterRegister   
 [Tutorial]How to use sound and music in Turing
Index -> Programming, Turing -> Turing Tutorials
Goto page 1, 2, 3, 4, 5  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Dan




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
cutecotton




PostPosted: Mon Dec 23, 2002 1:03 am   Post subject: (No 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!
Danger Zone




PostPosted: Thu Jun 05, 2003 9:55 am   Post subject: (No 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.
Tony




PostPosted: Thu Jun 05, 2003 12:58 pm   Post subject: (No subject)

how old is your "older" version?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
PaddyLong




PostPosted: Thu Jun 05, 2003 3:33 pm   Post subject: (No subject)

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




PostPosted: Thu Jun 05, 2003 4:52 pm   Post subject: (No subject)

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




PostPosted: Thu Jun 05, 2003 8:58 pm   Post subject: (No 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
Danger Zone




PostPosted: Fri Jun 06, 2003 9:01 am   Post subject: (No subject)

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




PostPosted: Thu Jun 19, 2003 11:46 am   Post subject: (No subject)

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




PostPosted: Thu Jun 19, 2003 11:49 am   Post subject: (No 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.
Asok




PostPosted: Thu Jun 19, 2003 3:37 pm   Post subject: (No subject)

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




PostPosted: Thu Jun 19, 2003 5:58 pm   Post subject: (No 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
HyperFlexed




PostPosted: Sat Nov 06, 2004 4:26 pm   Post subject: (No subject)

Jay Lo....

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

I got that error when I tried your code.
atrain




PostPosted: Sun Jan 30, 2005 2:00 pm   Post subject: (No 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...
atrain




PostPosted: Sun Jan 30, 2005 2:02 pm   Post subject: (No subject)

nevermind:

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

fork DoMusic
code:
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 5  [ 69 Posts ]
Goto page 1, 2, 3, 4, 5  Next
Jump to:   


Style:  
Search: