Solved

Control MIDI in VB...

Posted on 1997-08-12
7
518 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
Comment Utility
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
Comment Utility
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
Comment Utility
BTW, what should be the value of HMidi ? How can I know the address of an HMIDIOUT handle ?


0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:pigeon081297
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now