Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 669
  • Last Modified:

Control MIDI in VB...

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
pigeon081297
Asked:
pigeon081297
  • 4
  • 3
1 Solution
 
mcoopCommented:
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
 
pigeon081297Author Commented:
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
 
pigeon081297Author Commented:
BTW, what should be the value of HMidi ? How can I know the address of an HMIDIOUT handle ?


0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
pigeon081297Author Commented:
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
 
mcoopCommented:
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
 
pigeon081297Author Commented:
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
 
mcoopCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now