Results 1 to 11 of 11

Thread: Midi input

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2009
    Posts
    64

    Midi input

    Hi everyone

    I have a vb6 application, that I want to be able to control by receiving midi comands

    Such that when the users external usb midi divice sends a midi message, my program will repond

    So I am looking for sample code that will simply capture any incoming midi message, I can then use that message ( depending on the message ) to do some simple commands within my program.

    So for example:- I envisage the user will go to a setup page within my program to set the midi input device, the user will then ( in the case of a musical keyboard ) press a note key, when my program receives the note message, the user will be asked what function ( within my program ) he wants that key to perfom. ( I can then store this info so the user does not have to repeate the setup each time he runs my program )

    So I am looking for a nice simple, midi in monitor code

    Any Idea's

    PS I have the mabry "midiio2k.ocx" installed, but cannot figure out how to use the midi in control

    Thanks in advance

    David
    Last edited by musicscroller; May 11th, 2014 at 05:34 PM.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Midi input

    Have you checked out the following link.
    http://www.codeguru.com/vb/gen/vb_mu...d-Midi-FAQ.htm
    It says it covers input and output and Midi through. Its in VB5 but should load without problem in VB6.
    Don't have time to evaluate it myself.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2009
    Posts
    64

    Re: Midi input

    Hi Passel

    Thanks for your suggestion, but I already tried that link.
    I downloaded the code, it loads up ok into VB6
    But when you run it, it stops vb6 from running, and the only way out is CTR-ALT-DEL


    Thanks anyway

    david

  4. #4
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Midi input

    check this out:
    http://www.activevb.de/cgi-bin/uploa...oad.pl?id=3072

    its more than 10 years old but i think i still can figure out what i did back then and help if you run into troubles.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2009
    Posts
    64

    Re: Midi input

    Hi All

    digitalShaman's program works fine, but it is not quite exactly what I need.

    My own program is very time sensative and uses loads of timers, so I want to avoid adding anything with more timers as this could ffect the timing within my own program.

    DigitalShaman's program seems to have a timer that is continually running to check for incoming midi messages, I really need something where an event is triggered, maybe a MidiInEvent when a midi message arrives.

    Thanks to all for your help

    regards

    david

  6. #6
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Midi input

    just answered your PM but it does not show up in my sent items folder? anyhow, this might be interesting for others as well so here we go again:

    There is a callback procedure that is called whenever new midi data comes in: modMidiFunctions.MidiInProc but MSDN says:
    ' Applications should not call any system-defined functions from inside a callback
    ' function, except for EnterCriticalSection, LeaveCriticalSection, midiOutLongMsg,
    ' midiOutShortMsg, OutputDebugString, PostMessage, PostThreadMessage, SetEvent,
    ' timeGetSystemTime, timeGetTime, timeKillEvent, and timeSetEvent.

    that means that only very basic things should be done within this callback otherwise your app will crash and be aware that in some cases it only crashes when compiled to .exe.
    Therefore to separate things i went for simple stuff in the callback and periodic processing of messages in a timer event. you could improve this by using PostMessage in the callback, subclass your main window and process the message there. this will eliminate the timer intervall but will still be a few ms after the data actually arrived. Depending on other stuff that is happening the few ms may still get longer though...

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2009
    Posts
    64

    Re: Midi input

    Thanks digtalshaman

    I sort of see what you mean, but am a little out of my depth.
    I know how to disable the timer in your program, but do not understand how to catch the callback "modMidiFunctions.MidiInProc" event.

    All I actually want to do is capture what musical keyboard key has been pressed ( not interested in velocity or when the key is released ), and it does not matter if there is a short delay in recoginising that a key has been pressed.

    In it's basic form, image my program displays written music, I want the user to be able to simply press the far right white key on his musical keyboard ( or I will actually get a little fancy, and let the user set which musical keyboard key ), and my program will display the next page of the written music.

    So, is it a simple line of code to use the "modMidiFunctions.MidiInProc" ?

    Thanks so much for your help

    Regards

    david

  8. #8
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Midi input

    So, is it a simple line of code to use the "modMidiFunctions.MidiInProc" ?
    i suggest you postmessage a WM_KEYDOWN to a hidden textbox in the MidiInProc. This allows you to use the keydown event of the textbox without messing around with subclassing your application window.
    or you just set a flag variable and stay with one of your already existing timers to check and reset this flag.

    what you describe does not sound that time critical so i think you can as well just stay with the timer.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2009
    Posts
    64

    Re: Midi input

    Hi Digitalshaman

    the senario I gave was a simple one, my program actually scrolls the music up the screen, synchronized to backing tracks, so there is actually much more "timing" going on that realised.

    Where my knowledge fails me, is knowing how to detect when a message arrives.

    As you say I can put a line in one of my existing timers to check for a message, but I don't understand what the line of code would be to check the "MidiInProc" to see if there is a message.

    Where your program is continually checking and reading and displaying any messages ( this all takes time ), I want to simply check if there is a message, and if not my program can continue with what it is doing, but if there is a message then my program will undertake a certain action, depending on the message.

    Maybe you could guide me as to check the "MidiInProc" to see if there is a message.

    Hope that all makes sense

    Thanks again for everything

    regards

    dave

  10. #10
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Midi input

    ok. the code in MidiInProc is called by the system everytime some midi data is received (or other action happens like device open aso).
    The function gets some parameters passed in by windows:
    Code:
    Public Sub MidiInProc(ByVal hmIN As Long, ByVal wMsg As Long, ByVal dwInstance As Long, ByVal dwParam1 As Long, ByVal dwParam2 As Long)
    wMsg will tell you what had happened, this is why there is the select case in the example code:
    Code:
      Select Case wMsg
        Case MM_MIM_OPEN    'Device geƶffnet Message
        Case MM_MIM_CLOSE   'Device geschlossen Message
        Case MM_MIM_DATA    'normale Midi Daten sind gekommen
    ....
        Case MM_MIM_LONGDATA    'Lange Midi Daten sind angekommen.
    ....
        Case MM_MIM_ERROR
        Case MM_MIM_LONGERROR
        Case Else
      End Select
    you are interested in wMsg = MM_MIM_DATA. this happens when a key is pressed on your keyboard. you wont need MM_MIM_LONGDATA as this occurs only when midi system exclusive messages are received.

    so you need to react on MM_MIM_DATA. In this case dwParam1 will contain the 3 bytes of the midi message:
    Code:
            'Bytewerte aus dwParam1 ausmustern:
            Byte1 = Hex(dwParam1 And &H800000FF)
            Byte2 = Hex((dwParam1 And &H8000FF00) / &H100)
            Byte3 = Hex((dwParam1 And &H80FF0000) / &H10000)
    (you wont need the 'HEX' command for your application)
    these bytes should be different for every key that is pressed so these identify the key. now you cant do much with these bytes within MidiInProc because if you violate the restrictions mentioned in MSDN your app will crash. so you may declare Byte1 to Byte3 as public in some module and store the values there. within your timer proc you can now check if they are set and if so do something:
    Code:
    if (Byte1 OR Byte2 OR Byte3)<>0 then
      'a key was pressed: do your work
      ...
      'and finally clear bytes
      Byte1=0
      Byte2=0
      Byte3=0
    end if
    i would not worry too much about timing unless it prooves to be a problem. any latency in visual activity should not be an issue up to several 100ms. latency is mainly a problem if you want audio like samples beeing synchronized.

  11. #11
    New Member
    Join Date
    Aug 2019
    Posts
    1

    Re: Midi input

    Quote Originally Posted by digitalShaman View Post
    check this out:
    http://www.activevb.de/cgi-bin/uploa...oad.pl?id=3072

    its more than 10 years old but i think i still can figure out what i did back then and help if you run into troubles.
    Thank you digitalShaman,
    Your midi monitor code example helped me realize my 25 year dream.

    I had always wanted to find a way to reprogram a keyboard.
    Although there are 12 keys in an octave, depending on the scale, we would use only 7 keys out of that.
    My goal was to program the WHITE keys to those 7 notes and avoid the need for BLACK keys altogether.
    For a learner, this would reduce the complexity immensely.

    I had no knowledge of MIDI. But using your code example, I achieved this goal.
    Thank you very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width