Turing Logo  

Designed for computer science instruction, Turing is simply the easiest, most fun, and most effective way of teaching programming concepts.


Quick Links
Home page of Holt Software Associates  | Home page of the Turing Programming Language, the fastest way to teach programming concepts  | Home page of Holt Software's Java products  | Home page of Ready to Program with Java(tm) Technology, a Java development environment designed for education  | Information about Holt Software's courses for teachers  | Information about how to contact Holt Software  | Information about how students can purchase Holt Software's books and software  | Information about how schools and bookstores can purchase Holt Software's books and software

Playing Music under Turing 4.0

[Turing 4.0 Only]


This document contains information about how to play music under Turing 4.0.

Turing has had the ability to play music and sounds from way back. Classic Turing for DOS and older versions of OOT used the built in speaker to produce sounds. This allowed music to be played on any IBM PC, but limited what could be done. As well, later versions of Microsoft Windows denied applications direct access to the hardware stopping the Music.Play, Music.Sound, play and sound procedures from working at all. As well, Music.PlayFile was not well supported.

Turing 4.0 increases the support for sound under Windows 95/98/Me/NT/2000/XP. Music.Play and Music.Sound are now supported, and Music.PlayFile allows the playing of WAVE files (.wav), MIDI files (.mid) and music CDs! On systems with an up-to-date Media player, Turing programs can also play MP3 files.

The Music.Play and Music.Sound procedures operate as before. However, they now use the Windows sound system. This means that systems without sound cards will not make any sound.

The Music.PlayFile plays WAVE and MIDI files. To play these files, specify the name of the file (ending in ".wav" or ".mid") as the parameter to the procedure call. To play CD music, you must have a music CD (or a CD with audio tracks) in the CD player. Then you can issue the command

             Music.PlayFile ("cd")
  
To play a single track of the CD, you can use:

             Music.PlayFile ("cd:x")
  
where x is the track number from 1 on up.

If for some reason, Turing is unable to play the file (because it couldn't find it or there was no CD in the drive), Error.Last will return a non-zero value. You can then output the value of Error.LastMsg for a more detailed explanation.


Technical Details

When playing music, it's often desirable to be able to play two or more sounds at once (such as background music and context specific sounds). This can be done in Turing, but there are limitations.

Windows has the capability of playing three types of sound: WAVE sound, MIDI sound and CD sound. While it can't play two of the same kind of sound simultaneously, it can play one of each at the same time.

This means that if you make certain that your background music is a MIDI file, then you can play WAVE sound effects over top of it.

Another important point is that in Turing, all the Music.Play, Music.Sound and Music.PlayFile commands block. In other words, they don't return until the entire Music command finishes. This means that in order to use Music properly, you need to introduce a little bit of concurrency. Concurrency is simply the idea of having multiple processes executing simultaneously. While this can be quite complicated, we'll be using it very simply. First, a very simple example:

        process PlayBackgroundMusic
            loop
                Music.PlayFile ("mybackground.mid")
             end loop
        end PlayBackgroundMusic

        process PlaySoundEffect
            Music.PlayFile ("souneffect.wav")
        end PlaySoundEffect

        fork PlayBackgroundMusic  % Will play the background music forever.

        loop
            ... My interesting program here ...

            % I want to play my sound effect.
            fork PlaySoundEffect

            ... More interesting program here ...
        end loop
  
The fork keyword tells Turing to start the process, but also continue executing the main program. A process is the same as a procedure, but can execute concurrently with the main program (and any other processes).

In our example, the main program executes while PlayBackgroundMusic is looping forever. Whenever we want to play a sound effect, we use fork PlaySoundEffect. This plays the sound effect in the background along with our music.

This program has one slight problem. It continues forever, even when you exit the main loop. (i.e. you have to click the Stop button or press Ctrl+C). To modify the program slightly to allow the program to gracefully exit, you need to provide some method of exiting PlayBackgroundMusic.

Here's a slightly more complicated version.

        var finished : boolean := false

        process PlayBackgroundMusic
            loop
                Music.PlayFile ("mybackground.mid")
                exit when finished
             end loop
        end PlayBackgroundMusic

        process PlaySoundEffect
            Music.PlayFile ("souneffect.wav")
        end PlaySoundEffect

        fork PlayBackgroundMusic  % Will play the background music forever.

        loop
            ... My interesting program here ...

            % I want to play my sound effect.
            fork PlaySoundEffect

            ... More interesting program here ...
        end loop

        % Stop the music playing.
        finished := true
        Music.PlayFileStop
  
The last two statements cause the finished flag to be set to true and then to stop playing the background music immediately (without it, the program would finish only when the MIDI file completed playing).

If you try and play two MIDI files at the same time, the second call will terminate immediately setting Error.Last. If you try to play two WAVE files at the same time, the first sound is terminated and the second sound starts playing. This is, in general, the right behaviour for sound effects. In other words, try an use MIDI files for the background music and the WAVE files for the sound effects.

Another point, Music.Play is implemented using MIDI. This means that you can't play MIDI files and Music.Play at the same time. Likewise, Music.Sound uses the WAVE file playing mechanism to generate tones of arbitrary frequency. Thus you can't play WAVE files and use Music.Sound at the same time,

Lastly, because Turing has to generate the complete wave form for the Music.Sound command, it's slightly slower than before. Because of this, there is a noticeable pause (perhaps 1/50 of a second) between calls to Music.Sound. You can hear the problem with the following program.

        for i : 200 .. 2000
            sound (i, 100)
        end for
  
We apologize for this, and are working to improve performance.

Feedback

As always, we are very interested in getting feedback on both bugs and requested features. If you are having a problem, or there is some feature you want to see added to Turing, please send me (Tom West) e-mail at west@hsa.on.ca.
[ Turing Home ] * [ Top of Page ] * [ Feedback ]