Hi folks, this is what I want to do and I don't know how. I want to play a midi file in VB and in real time alter the voices of the channels whilst it is playing dependent upon user inputs - these will be determined by inputs from pressure pads via parrallel port. The parallel port code I have sorted it's the midi control I don't know how to do. Any advice tips or code guys?
cheers james :eek:

11 Years
Discussion Span
Last Post by techniner

Play a MIDI file
If you want to play a MIDI file from VB you have to use MCI functions. The main MCI function is mciSendString, that sends command strings to the system and execute them:

Declare Function mciSendString Lib "winmm" Alias "mciSendStringA" (ByVal _
    lpstrCommand As String, ByVal lpstrReturnString As String, _
    ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

The first argument is the command string, lpstrReturnString receives return information (if needed), uReturnLength indicates the size, in characters, of lpstrReturnString, and hwndCallback is used for system notification. Remember that every command string described in this tip must be sent to the system with:

CommandString = "Your MCI command here"
RetVal = mciSendString(CommandString, vbNullString, 0, 0)

First you must open the MIDI device with

CommandString = "Open " + FileName + " type sequencer alias MidiFile"

Notice that FileName must be in the short format 8.3 characters; lpstrReturnString is null because you don't need any return information. "MidiFile" is an arbitrary name that you'll use in the next command strings as an alias of the filename. Finally you can play the MIDI file:

CommandString = "Play MidiFile wait"

The execution is synchronous, but you can play the file asynchronously and receive notifications with

CommandString = "Play MidiFile notify"
RetVal = mciSendString(CommandString, vbNullString, 0, MyForm.hWnd)

where MyForm is the form you are using. The system will send to your form a MM_MCINOTIFY message when the playback is finished. To intercept this message you have to create a custom window procedure and use subclassing.

By default playback begins at the current position and stops at the end of the content, but you can play only a part of the file with this statement

CommandString = "Play MidiFile from X to Y"

where X and Y are two different positions. It's often useful to specify X and Y in milliseconds, so before Play command you must set the time format with this command:

CommandString = "Set MidiFile time format milliseconds"

With asynchronous executions, you can pause / resume and stop playback with ' pause playback CommandString = "Pause MidiFile" ' resume a paused playback CommandString = "Resume MidiFile" ' stop playback CommandString = "Stop MidiFile" When you have finished to use the MIDI device, remember to close it with

CommandString = "Close MidiFile"

The above is a pretty good post. I've used similar code in my universal player (mp3's, Wav's, and Midi's) and it seems to work great for me.... nice post techniner


Thank you for the info it has been very useful. However, it doesn't answer my question and I was wondering if anyone could help me.
Having set a midi file playing I want to change the voice of an instrument on the fly from user input e.g. take an example of a symphony one selection by the user the violins carry on playing but start to sound like bagpipes. The music cariies on just the voice of the line of music chosen is changed. Does anyone know how I can do this?
Many thanks for the help so far. I hope someone can help.
james :?:


You need to handle the creation of the midi file then in aseprate program.

Example use a midi keyboard and automate a sound on it.

Then change the tone and automate another sound.

Like you want to design something to make music then make the different tones.

Then on "some event" call to it and generate a midi file absed on the criteria.

Pretty straight forward but you need to develop the code to CREAT a midi file ... meaning you need to create a keyboard or instrument FIRST.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.