Solved

Control MIDI in VB...

Posted on 1997-08-12
7
543 Views
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 !

Pigeon.
0
Comment
Question by:pigeon081297
  • 4
  • 3
7 Comments
 
LVL 4

Accepted Solution

by:
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

Then...
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)

Const MIDI_CHANNEL = 0
Const CONTROL_GROUP = 85

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...
MC
0
 

Author Comment

by:pigeon081297
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...


0
 

Author Comment

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


0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:pigeon081297
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...
0
 
LVL 4

Expert Comment

by:mcoop
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.

Regards
MC
0
 

Author Comment

by:pigeon081297
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 !
0
 
LVL 4

Expert Comment

by:mcoop
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.

Regards
mcoop@pop.jaring.my
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

832 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