Control MIDI in VB...

Posted on 1997-08-12
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 50 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 ?

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb6 connector to SQL Server 2 42
MS Access 03, TransferText, decimal places 8 86
pop out of webbrowser1 control vba6 5 44
Automatic Email Reminder 4 77
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

730 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