Control MIDI in VB...

Posted on 1997-08-12
Medium Priority
Last Modified: 2012-06-27
Dear all,

  I'd like to know how can I write a program in VB to control the MIDI. The meaning of controlling the MIDI includes playing a particular note using a particular instrument.

  And I wonder if I need to use API calls...

  Thanks a lot !

Question by:pigeon081297
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Accepted Solution

mcoop earned 200 total points
ID: 1430375
These will be useful...

Declare Function midiOutOpen Lib "winmm.dll" (HMidi As Long, ByVal uDeviceID As Long, ByVal dwCallback As Long, ByVal dwInstance As Long, ByVal dwFlags As Long) As Long
Declare Function midiOutShortMsg Lib "winmm.dll" (ByVal HMidi As Long, ByVal dwMsg As Long) As Long
Declare Function midiOutClose Lib "winmm.dll" (ByVal HMidi As Long) As Long

Something like...

(inside your initialisation code...)

  '  Open the MIDI Mapper Device 0
  OK = midiOutOpen(HMidi, MIDI_MAPPER, 0, 0, 0)

  '  Check for an error in opening the device
  If OK <> 0 Then
      MsgBox "The MIDI device could not be opened (" & OK & ")"
  End If

(This was used for a specific application, so you will need to fiddle with the bits you send out - depending on the device youa are talking to)

Private Sub SendMIDI(value As Integer)


Dim Throw As Long
Static message As Long

  message = (value * &H10000) + (CONTROL_GROUP * &H100&) + &HB0 + MIDI_CHANNEL
  Throw = midiOutShortMsg(HMidi, message)

  ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Const VELOCITY = 127
  message = (VELOCITY * &H10000) + (value * &H100&) + &H90 + MIDI_CHANNEL
  Throw = midiOutShortMsg(HMidi, message)

End Sub

(and finally...)

  Dim OK As Long
  '  Close the MIDI Device
  OK = midiOutClose(HMidi)
  '  Check for an error in opening the device
  If OK <> 0 Then
      MsgBox "The MIDI device could not be closed (" & OK & ")"
  End If

Good luck...

Author Comment

ID: 1430376
Well, I've got your idea, and I've tried your code.

I think it has successfully open the MIDI port, but I'm not sure how can I play a particular note using a particular instrument. Can you give me an example, say playing the Middle C using the Grand Piano ?

And also, when I tried to close the MIDI port using your code, it said it can't. The code is 5, and then I can't open the MIDI port anymore, with error code 4...


Author Comment

ID: 1430377
BTW, what should be the value of HMidi ? How can I know the address of an HMIDIOUT handle ?


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 1430378
Ok, I've fixed the problem of opening and closing the port. Now I don't have any error message in during the opening and closing. But still I can't figure out how can I play a note...

Expert Comment

ID: 1430379
Hi again...

This routine was written to control a M-O Disk recorder playing audio effects into a TV show.  The specifics of the message have been hard coded for that device.

First item to note is tht HMidi should be global to all the functions.  the 'open' call will set that global valuem and the other calls will reference it.

Second is that you only need ONE of the  midiOutShortMsg() calls to play a note.  the values (&H90 and &HB0) were offsets into a command table of the M-O box.

To select the voice that you want to use will require that you read up a bit more onm the MIDI-MAPPER, and the MIDI-CHANNEL numbers that you are using.

The original base for my code came from "Visual Basic 4 - API "How-To"" from Waite Group Press.  A very useful book.


Author Comment

ID: 1430380
Good... but really I need an example, can you make one for me ? Really thanks.

One more thing, is the VELOCITY the frequency of the note ? Or what is that ? And I've inserted the MIDI_MAPPER constant from the API viewer, should I do this ?

Thanks a lot !

Expert Comment

ID: 1430381
VELOCITY is normally assigned to the volume of the note played.  the 'value' field that I pass is the 'note'.

Send me your email address, and I can send you a whole VBP/FRM/BAS set to demo the goodies.


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question