The PLAYMIDI command is a bit more complicated than the PLAYWAVE command. Unlike the PLAYWAVE command, the programmer using PLAYMIDI must keep track of the playmidi progress. (1) Whether a midi file is currently playing (2) Whether the midi file has finished playing To keep track, you must assign variables. Such variables are commonly referred to as flags. The programmer must also determine the length of the midi file. Fortunately, this value is extracted by Just BASIC as part of the PLAYMIDI command. The directions from the Help File
Quote:
Description:
This plays a *.MIDI sound from a file on disk as specified in filename. The length variable will hold the length of the MIDI file (not in seconds). You can only play one file at a time. Periodically, you will need to use the MIDIPOS( ) function to see if you've reached the end of the music:
Finally, use the STOPMIDI command to close the music file before you can play a different one.
Usage:
'the playmidi command returns the length of the midi in
' the variable howLong
playmidi "c:\somedir\mymusic.midi", howLong
timer 1000, [checkPlay]
wait
[checkPlay]
if howLong = midipos( ) then [musicEnded]
wait
[musicEnded]
stopmidi
timer 0
wait
Just BASIC includes the midi file theme.mid in its MEDIA sub folder. This article assumes you are running the demo codes from your Just BASIC directory. By doing this, DefaultDir$ can find the theme.mid in the MEDIA sub folder. If you are using a midi file located in the same folder as the demo, change any instance of
Playmidi DefaultDir$;”\MEDIA\theme.mid”
to
Playmidi “theme.mid”
First, the code to begin playing the midi
Playmidi DefaultDir$;”\MEDIA\theme.mid”, midiLength
Print “It is important to press Enter BEFORE the music stops.”
Input “Press Enter at any time to stop the midi: “;any$
Stopmidi
End
Unfortunately, there is no automatic way of knowing whether the midi file has finished playing. And, unlike the PLAYWAVE “” command, if a STOPMIDI command is issued when no midi was playing, your program will crash. Run this Code
Playwave “”
Print “This works fine even if no wav file was being played.”
Print “But….”
STOPMIDI
End
A flag variable is required to keep track of whether a midi file is being played or not. In this demo, we’ll use midiFlag as that flag variable.
midiFlag = 0 ‘No midi file being played
midiFlat = 1 ‘yes a midi file is being played
But, how do we keep track of whether the midi file is finished or not? The PLAYMIDI command gets the length of that midi file and stores it in the variable you assign. In this demo, that variable is midiLength.
Playmidi DefaultDir$;”\MEDIA\theme.mid”, midiLength
Print “midiLength = “;midiLength
Stopmidi
End
If you’re using the theme.mid file, you might find that midiLength = 627. Knowing the full length of the file is only useful if the current position of the midi file is known. That position is made known by the command MIDIPOS(). Run this code to see MIDIPOS() in action. Note also the use of the midiFlag variable to indicate whether a midi file is being played or not.
Playmidi DefaultDir$;”\MEDIA\theme.mid”, midiLength
midiFlag = 1
For i = 1 to 300
Print “midiLength = “;midiLength
Print “midiPosition = “;Midipos()
Next i
Stopmidi
midiFlag = 0
End
That’s very nice, but how can we periodically check the position of the midi file being played and still continue on with the program. In this case, we set up a timer. A timer is kind of an alarm clock that’s set not for a particular time, but for a particular time span. Every time that time span has been reached, the timer executes the assigned block of code, then sits off to the side and waits for that time span to be reached again. In this next example, a timer will be used to check the position of the midi file every 1000 milliseconds (1 second). While that’s happening, the program goes on as usual.
Playmidi DefaultDir$;”\MEDIA\theme.mid”, midiLength
midiFlag = 1
Timer 1000, [checkPlay] ‘Every 1000 milliseconds, check the midi position
[doSomethingHere]
If midiFlag = 1 Then
msg$ = “Do you want to stop the music yet?”
Else
msg$ = “Press Enter to Quit”
End If
If midiFlag = 0 Then
Confirm msg$;any$ ‘Confirm is used here only to prevent the BEEP from Notice
End
End If
Confirm msg$;yn$
If midiFlag = 1 Then
If yn$ = “yes” Then
Timer 0 ‘Turn off the timer
Stopmidi ‘Stop the midi file
midiFlag = 0 ‘Set the midiFlag to 0 to indicate midi file is no longer being played
End If
End If
Goto [doSomethingHere]
[checkPlay]
If Midipos() = midiLength Then ‘If the position is at the total length then
Stopmidi ‘Stop playing the midi
midiFlag = 0 ‘Set the midiFlag to 0 to indicate midi file is no longer being played
Timer 0 ‘Stop the timer
End If
Wait
One last word of caution. When you end your program, don’t expect the midi that’s playing to be stopped automatically (as is the case with PLAYWAVE). If your midi file has not reached the end of the file and you haven’t issued a STOPMIDI command, your music will continue even after the program has ENDed. Running the same or another program will encounter the Midi File already playing error message when you try to issue the PLAYMIDI command again. This will happen even if the music has finished playing and you no longer hear the music. So, you must use STOPMIDI before ENDING your program, but ONLY IF the STOPMIDI command was not issued prior. This is another place the midiFlag variable becomes crucial. I almost always issue a Timer 0 command at the end of any program using a timer, whether the program has shut the timer off or not.
[quit]
Timer 0
If midiFlag = 1 Then
Stopmidi
End If
Close #main
End
Now, let’s use these commands
‘ These are a list of commands only
‘ Don’t try to RUN this block of code
Playmidi DefaultDir$;”\MEDIA\theme.mid”, midiLength
midiFlag = 1
Timer 1000, [checkMidi]
If Midipos() = midiLength
Stopmidi
midiFlag = 0
Timer 0
in a GUI environment. Download the file JBMidiPlayer.zip at http://jbusers.com/phpBB/viewtopic.php?t=31. Nothing fancy, just a simple demo showing how to play and CONTROL those midi files.
By the way, although you can’t play multiple wav files or multiple midi files simultaneously, you can PLAYWAVE a wav file even while a midi file is playing. In this way, you don’t have to interrupt background music to hear those blood curdling sound effects. Time to adjust those volume controls.
Edit: Keep in mind Alyce's advice regarding relative paths in her reply to the PLAYWAVE tutorial at http://justbasic.conforums.com/index.cgi?board=tutorial&action=display&num=1109364735.