Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. MIDI and USB Keyboard!

MIDI and USB Keyboard!

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
46 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    The keys i say are the keys from my piano MIDI keyboard (hardware piano keyboard)!... Not from PC´s keyboard!!! This is all the code i have now....plus the clsMIDI module:

    Imports System.Runtime.InteropServices

    Public Class Form1
    Dim m As New clsMIDI
    Dim hMidiIn As Integer

    Private Sub Form1\_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FillInstrumentCombo()
    
        If midiInGetNumDevs() = 0 Then
            MsgBox("No MIDI devices connected!")
        End If
    
        Dim InCaps As New MIDIINCAPS
        Dim DevCnt As Integer
    
        For DevCnt = 0 To (midiInGetNumDevs - 1)
            midiInGetDevCaps(DevCnt, InCaps, Len(InCaps))
            ComboBox1.Items.Add(InCaps.szPname)
        Next DevCnt
    
    
    End Sub
    
    Declare Function midiInOpen Lib "winmm.dll" (ByRef lphMidiIn As Integer,
        ByVal uDeviceID As Integer,  ByVal dwCallback As MidiDelegate,
        ByVal dwInstance As Integer, ByVal dwFlags As Integer) As Integer
    Public Delegate Sub MidiDelegate(ByVal MidiInHandle As Int32, ByVal wMsg As Int32, ByVal Instance As Int32, ByVal wParam As Int32, ByVal lParam As Int32)
    Public ptrCallback As New MidiDelegate(AddressOf MidiInProc)
    
    Public Delegate Sub DisplayDataDelegate(wParam)
    
    Private Sub ComboBox1\_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        midiInStop(hMidiIn)
        midiInReset(hMidiIn)
        midiInClose(hMidiIn)
        Dim DeviceID As Integer = ComboBox1.SelectedIndex
        midiInOpen(hMidiIn, DeviceID, ptrCallback, 0, CALLBACK\_FUNCTION Or MIDI\_IO\_STATUS)
        midiInStart(hMidiIn)
        Dim duration = CInt(cboduration.Text)
        m.NoteDuration = duration
    End Sub
    
    
    
    Function MidiInProc(ByVal MidiInHandle As Int32, ByVal wMsg As Int32, ByVal Instance As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Integer
        TextBox1.Invoke(New DisplayDataDelegate(AddressOf DisplayData), New Object() {wParam})
        Dim DataByte3 = (wParam And &HFF00) >> 8
        m.PlayMIDINote(DataByte3, 127)
    End Function
    
    Private Sub DisplayData(wParam)
        Dim StatusByte As Byte
        Dim DataByte1 As Byte
        Dim DataByte2 As Byte
        StatusByte = (wParam And &HFF)
        DataByte1 = (wParam And &HFF00) >> 8
        DataByte2 = (wParam And &HFF0000) >> 16
        TextBox1.AppendText(String.Format("{0:X2} {1:X2} {2:X2}{3}", StatusByte, DataByte1, D
    
    S Offline
    S Offline
    Sascha Lefevre
    wrote on last edited by
    #30

    Alienoiz wrote:

    The keys i say are the keys from my piano MIDI keyboard (hardware piano keyboard)!... Not from PC´s keyboard!!!

    Alright :laugh: Please post the contents of your TextBox1 after pressing and releasing a single key.

    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

    1 Reply Last reply
    0
    • L Lost User

      The keys i say are the keys from my piano MIDI keyboard (hardware piano keyboard)!... Not from PC´s keyboard!!! This is all the code i have now....plus the clsMIDI module:

      Imports System.Runtime.InteropServices

      Public Class Form1
      Dim m As New clsMIDI
      Dim hMidiIn As Integer

      Private Sub Form1\_Load(sender As Object, e As EventArgs) Handles MyBase.Load
          FillInstrumentCombo()
      
          If midiInGetNumDevs() = 0 Then
              MsgBox("No MIDI devices connected!")
          End If
      
          Dim InCaps As New MIDIINCAPS
          Dim DevCnt As Integer
      
          For DevCnt = 0 To (midiInGetNumDevs - 1)
              midiInGetDevCaps(DevCnt, InCaps, Len(InCaps))
              ComboBox1.Items.Add(InCaps.szPname)
          Next DevCnt
      
      
      End Sub
      
      Declare Function midiInOpen Lib "winmm.dll" (ByRef lphMidiIn As Integer,
          ByVal uDeviceID As Integer,  ByVal dwCallback As MidiDelegate,
          ByVal dwInstance As Integer, ByVal dwFlags As Integer) As Integer
      Public Delegate Sub MidiDelegate(ByVal MidiInHandle As Int32, ByVal wMsg As Int32, ByVal Instance As Int32, ByVal wParam As Int32, ByVal lParam As Int32)
      Public ptrCallback As New MidiDelegate(AddressOf MidiInProc)
      
      Public Delegate Sub DisplayDataDelegate(wParam)
      
      Private Sub ComboBox1\_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
          midiInStop(hMidiIn)
          midiInReset(hMidiIn)
          midiInClose(hMidiIn)
          Dim DeviceID As Integer = ComboBox1.SelectedIndex
          midiInOpen(hMidiIn, DeviceID, ptrCallback, 0, CALLBACK\_FUNCTION Or MIDI\_IO\_STATUS)
          midiInStart(hMidiIn)
          Dim duration = CInt(cboduration.Text)
          m.NoteDuration = duration
      End Sub
      
      
      
      Function MidiInProc(ByVal MidiInHandle As Int32, ByVal wMsg As Int32, ByVal Instance As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Integer
          TextBox1.Invoke(New DisplayDataDelegate(AddressOf DisplayData), New Object() {wParam})
          Dim DataByte3 = (wParam And &HFF00) >> 8
          m.PlayMIDINote(DataByte3, 127)
      End Function
      
      Private Sub DisplayData(wParam)
          Dim StatusByte As Byte
          Dim DataByte1 As Byte
          Dim DataByte2 As Byte
          StatusByte = (wParam And &HFF)
          DataByte1 = (wParam And &HFF00) >> 8
          DataByte2 = (wParam And &HFF0000) >> 16
          TextBox1.AppendText(String.Format("{0:X2} {1:X2} {2:X2}{3}", StatusByte, DataByte1, D
      
      S Offline
      S Offline
      Sascha Lefevre
      wrote on last edited by
      #31

      No - not for a single key but for two different keys please, in sequence.

      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

      L 1 Reply Last reply
      0
      • S Sascha Lefevre

        No - not for a single key but for two different keys please, in sequence.

        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #32

        i am not understanding : (((( can you set an example?... i guess the textbox1 is not affecting the MIDI playing!!! What is making the play its the "m.playMIDINote" function!!! The textbox1 is just for showing the data that is being sent from the MIDI Keyboard into the PC!

        S 1 Reply Last reply
        0
        • L Lost User

          i am not understanding : (((( can you set an example?... i guess the textbox1 is not affecting the MIDI playing!!! What is making the play its the "m.playMIDINote" function!!! The textbox1 is just for showing the data that is being sent from the MIDI Keyboard into the PC!

          S Offline
          S Offline
          Sascha Lefevre
          wrote on last edited by
          #33

          Alienoiz wrote:

          The textbox1 is just for showing the data that is being sent from the MIDI Keyboard into the PC!

          Yes and I want to see that

          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

          L 1 Reply Last reply
          0
          • S Sascha Lefevre

            Alienoiz wrote:

            The textbox1 is just for showing the data that is being sent from the MIDI Keyboard into the PC!

            Yes and I want to see that

            If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #34

            ouch..i am sorry..i understood something else.. here...00 00 00 appears when i select the MIDI keyboard from combobox1..next is the data from 3 followed keys: 00 00 00 90 30 36 80 30 00 90 31 38 80 31 00 90 32 22 80 32 00

            S 1 Reply Last reply
            0
            • L Lost User

              ouch..i am sorry..i understood something else.. here...00 00 00 appears when i select the MIDI keyboard from combobox1..next is the data from 3 followed keys: 00 00 00 90 30 36 80 30 00 90 31 38 80 31 00 90 32 22 80 32 00

              S Offline
              S Offline
              Sascha Lefevre
              wrote on last edited by
              #35

              OK - try this:

              Function MidiInProc(ByVal MidiInHandle As Int32, ByVal wMsg As Int32, ByVal Instance As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Integer
                  Dim Status As Byte
                  Dim Note As Byte
                  Dim Velocity As Byte
                  TextBox1.Invoke(New DisplayDataDelegate(AddressOf DisplayData), New Object() {wParam})
                  Status = (wParam And &HFF)
                  If Status = 90 Then
                      Note = (wParam And &HFF00) >> 8
                      Velocity = (wParam And &HFF0000) >> 16
                      m.PlayMIDINote(Note, Velocity)
                  End If
              End Function
              

              If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

              L 1 Reply Last reply
              0
              • S Sascha Lefevre

                OK - try this:

                Function MidiInProc(ByVal MidiInHandle As Int32, ByVal wMsg As Int32, ByVal Instance As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Integer
                    Dim Status As Byte
                    Dim Note As Byte
                    Dim Velocity As Byte
                    TextBox1.Invoke(New DisplayDataDelegate(AddressOf DisplayData), New Object() {wParam})
                    Status = (wParam And &HFF)
                    If Status = 90 Then
                        Note = (wParam And &HFF00) >> 8
                        Velocity = (wParam And &HFF0000) >> 16
                        m.PlayMIDINote(Note, Velocity)
                    End If
                End Function
                

                If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #36

                i understand the code..yes..but it is not working! : (

                S 1 Reply Last reply
                0
                • L Lost User

                  i understand the code..yes..but it is not working! : (

                  S Offline
                  S Offline
                  Sascha Lefevre
                  wrote on last edited by
                  #37

                  You need to go into more detail with problem descriptions. "Not working" is not helping me understand. Does it not do anything any more or still the same problem as before or what?

                  If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                  L 2 Replies Last reply
                  0
                  • S Sascha Lefevre

                    You need to go into more detail with problem descriptions. "Not working" is not helping me understand. Does it not do anything any more or still the same problem as before or what?

                    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #38

                    sorry..everything works except the sound ... no sound!!

                    1 Reply Last reply
                    0
                    • S Sascha Lefevre

                      You need to go into more detail with problem descriptions. "Not working" is not helping me understand. Does it not do anything any more or still the same problem as before or what?

                      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #39

                      if i set it like this:

                      If Status <> 90 Then

                      it seems to work...the 90/80 might not be the "correct" value!

                      S 1 Reply Last reply
                      0
                      • L Lost User

                        if i set it like this:

                        If Status <> 90 Then

                        it seems to work...the 90/80 might not be the "correct" value!

                        S Offline
                        S Offline
                        Sascha Lefevre
                        wrote on last edited by
                        #40

                        I missed that you formatted the output as hexadecimal. Change that line to:

                        If Status = 144 Then

                        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                        L 3 Replies Last reply
                        0
                        • S Sascha Lefevre

                          I missed that you formatted the output as hexadecimal. Change that line to:

                          If Status = 144 Then

                          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #41

                          OOOOOOOOKKKKKKK..seems good..A LOT OF THANKS!

                          1 Reply Last reply
                          0
                          • S Sascha Lefevre

                            I missed that you formatted the output as hexadecimal. Change that line to:

                            If Status = 144 Then

                            If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #42

                            i am encountering this issue..i do not know where to put the

                                Dim duration = CInt(cboduration.Text)
                                m.NoteDuration = duration
                            

                            code.... i tried several ways, but when i load the app, it doesnt updades..only after a manual update it plays ok! Basically when i load the app..the duration is infinite!!

                            S 1 Reply Last reply
                            0
                            • S Sascha Lefevre

                              I missed that you formatted the output as hexadecimal. Change that line to:

                              If Status = 144 Then

                              If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #43

                              It looks like something gets stuck..i overpassed the problem by adding this:

                              m.PlayMIDINote(0, 0)
                              m.STOPMIDINote(0)

                              to the form´s load event!!

                              1 Reply Last reply
                              0
                              • L Lost User

                                i am encountering this issue..i do not know where to put the

                                    Dim duration = CInt(cboduration.Text)
                                    m.NoteDuration = duration
                                

                                code.... i tried several ways, but when i load the app, it doesnt updades..only after a manual update it plays ok! Basically when i load the app..the duration is infinite!!

                                S Offline
                                S Offline
                                Sascha Lefevre
                                wrote on last edited by
                                #44

                                I have an idea how to fix that but I'm leaving for a vacation trip until Monday. I'll get back to you then.

                                If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                L 1 Reply Last reply
                                0
                                • S Sascha Lefevre

                                  I have an idea how to fix that but I'm leaving for a vacation trip until Monday. I'll get back to you then.

                                  If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #45

                                  OK..thanks..later then!

                                  1 Reply Last reply
                                  0
                                  • L Lost User

                                    Hi..where can i learn to play MIDI sounds through an USB MIDI controller?! I have a MIDI instruments example where i can play sounds with the PC´s keyboard...but i would like to play them from my MIDI controller..although i am totally blind on that part! I googled but i did not found something realy clear about it..specialy for VB! Thanks for any Support! Duarte

                                    J Offline
                                    J Offline
                                    Jaroslav Mitrovic
                                    wrote on last edited by
                                    #46

                                    Hi. (Uppsssie just missed the there where more Replies, is just saw only that about the First Error Message :rolleyes: ) I did alot of VB.NET and VB6 MIDI Stuff. That´s why I started to learn Programing. To be honest, I used like You a Library, Mostly MidiOx. (Oldie, but goldie...) It is always the same things that are nessesary for Midi Handling. First check if the Midi Device(s) are Present. Then Open them and Store it in a Instance (Variable). Then use the Subparts (Properties) like Channel number (MIDI-Channel) etc. Then handle (Midi is mostly about Timings) the Midi Events. But if You have a USB Midi Controller, You have to make Sure it has a Tone Generator. Like the Korg NanoPads, they can only be used as Input Devices. Maybe thats the cause for the Error-Messages. If that´s the Case, use Your Computer and find a Software Synthesizer (Or some Tone Generator, Sampler etc.) and connect them inside the Computer. Of course Virtual Plugs (And there is no WLAN-Cable :laugh: ) to connect the MIDI Input (Controller) with the Generator (You can make your own Sampler in VB A Soundboard for example). Or use a DAW ( 'Digital Audio Workstation' Application like: Cubase, Reason, Logik Fruity-Loops to name some of them, and all are Commercial I can´t remeber some Freeware atm.) Please consider to use a MIDI analyzer Software like MIDIOX to see how MIDI DATA is transported and Connected Interchanged etc.) For the VB Programming, it is very easy to Programm basic Midi Handling. Lik I sad: First get the Inputs and Outputs of the Midi-Devices in a List. Like Jazz.GetDevices() Then find the Proper Device, maybe with a Index or by a Name, whats the most convinitent way to do it. Then Get the Input of Your USB-Controller. Test it with MidiOX it has all Information allready awailable to see what is named what etc. Then Connect the Midi-Messages of the Inputs and send them to the Tone generator Input. So You have to Reroute the Midi Flow from the Controller to the Tonegenerator. Wow very Complicated explained. Let me break it Down. Your VB-App will have to have Inputs and Outputs. The USB-Controller will be a Input in Your App. You have to use then the Output of your App to create some Noise (A Symphony for Example). But if the Output should be a Soundboard that will be Playing MP3/or Wav Files on a Keyboardpress (Pianokeyboard) You can ommit the Output and just play the Files with the Windows Soundsystem, when the NoteOn Message is recieved. When Jazz Library

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups