Click here to Skip to main content
15,887,812 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: MIDI and USB Keyboard! Pin
User 98970746-Dec-17 6:45
User 98970746-Dec-17 6:45 
GeneralRe: MIDI and USB Keyboard! Pin
User 98970749-Dec-17 6:01
User 98970749-Dec-17 6:01 
GeneralRe: MIDI and USB Keyboard! Pin
Sascha Lefèvre9-Dec-17 6:49
professionalSascha Lefèvre9-Dec-17 6:49 
GeneralRe: MIDI and USB Keyboard! Pin
User 98970749-Dec-17 7:31
User 98970749-Dec-17 7:31 
GeneralRe: MIDI and USB Keyboard! Pin
User 989707410-Dec-17 4:39
User 989707410-Dec-17 4:39 
GeneralRe: MIDI and USB Keyboard! Pin
User 989707411-Dec-17 0:56
User 989707411-Dec-17 0:56 
GeneralRe: MIDI and USB Keyboard! Pin
Sascha Lefèvre11-Dec-17 4:40
professionalSascha Lefèvre11-Dec-17 4:40 
GeneralRe: MIDI and USB Keyboard! Pin
User 989707411-Dec-17 4:48
User 989707411-Dec-17 4:48 
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:

VB
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, <MarshalAs(UnmanagedType.FunctionPtr)> 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, DataByte2, vbCrLf))
    End Sub

    Private Sub FillInstrumentCombo()
        For i = 0 To 121
            cboinstruments.Items.Add(Instrument.GMInstrumentNames(i))
        Next
        cboinstruments.SelectedIndex = 0
    End Sub

    Private Sub cboInstruments_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboinstruments.SelectedIndexChanged
        m.CurrentInstrument = cboinstruments.Text
    End Sub

    Private Sub hsbVolume_ValueChanged(sender As Object, e As EventArgs) Handles hsbvolume.ValueChanged
        m.Volume = hsbvolume.Value
    End Sub

    Private Sub cboduration_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboduration.SelectedIndexChanged
        Dim duration = CInt(cboduration.Text)
        m.NoteDuration = duration
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        midiInStop(hMidiIn)
        midiInReset(hMidiIn)
        midiInClose(hMidiIn)
        m.STOPAllMIDINotes()
        End
    End Sub

    Private Sub Button2_MouseDown(sender As Object, e As MouseEventArgs) Handles Button2.MouseDown
        m.PlayMIDINote(60, 127)
    End Sub

    Private Sub Button2_MouseUp(sender As Object, e As MouseEventArgs) Handles Button2.MouseUp
        m.STOPMIDINote(60)
    End Sub
End Class


When i press the MIDI keyboard keys it plays..but when i unpress it it plays again...i would like it to play only when i press!
This is the code i am using for it to play (check underlined):

VB
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


if i leave it like this it plays when i press and when i unpress the MIDI keyboard keys...but if i set it like this:

VB
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) >> 8
    m.PlayMIDINote(DataByte3, 127)
End Function


It only plays when i unpress...

I would like it to play only when i press the MIDI keyboard keys!

Thanks....

modified 7-Jan-19 21:02pm.

GeneralRe: MIDI and USB Keyboard! Pin
Sascha Lefèvre11-Dec-17 4:56
professionalSascha Lefèvre11-Dec-17 4:56 
GeneralRe: MIDI and USB Keyboard! Pin
Sascha Lefèvre11-Dec-17 4:58
professionalSascha Lefèvre11-Dec-17 4:58 
GeneralRe: MIDI and USB Keyboard! Pin
User 989707411-Dec-17 5:04
User 989707411-Dec-17 5:04 
GeneralRe: MIDI and USB Keyboard! Pin
Sascha Lefèvre11-Dec-17 5:09
professionalSascha Lefèvre11-Dec-17 5:09 
GeneralRe: MIDI and USB Keyboard! Pin
User 989707411-Dec-17 5:18
User 989707411-Dec-17 5:18 
GeneralRe: MIDI and USB Keyboard! Pin
Sascha Lefèvre11-Dec-17 5:39
professionalSascha Lefèvre11-Dec-17 5:39 
GeneralRe: MIDI and USB Keyboard! Pin
User 989707411-Dec-17 5:55
User 989707411-Dec-17 5:55 
GeneralRe: MIDI and USB Keyboard! Pin
Sascha Lefèvre11-Dec-17 6:01
professionalSascha Lefèvre11-Dec-17 6:01 
GeneralRe: MIDI and USB Keyboard! Pin
User 989707411-Dec-17 6:05
User 989707411-Dec-17 6:05 
GeneralRe: MIDI and USB Keyboard! Pin
User 989707411-Dec-17 6:12
User 989707411-Dec-17 6:12 
GeneralRe: MIDI and USB Keyboard! Pin
Sascha Lefèvre11-Dec-17 6:20
professionalSascha Lefèvre11-Dec-17 6:20 
GeneralRe: MIDI and USB Keyboard! Pin
User 989707411-Dec-17 6:22
User 989707411-Dec-17 6:22 
GeneralRe: MIDI and USB Keyboard! Pin
User 989707411-Dec-17 8:33
User 989707411-Dec-17 8:33 
GeneralRe: MIDI and USB Keyboard! Pin
Sascha Lefèvre13-Dec-17 8:08
professionalSascha Lefèvre13-Dec-17 8:08 
GeneralRe: MIDI and USB Keyboard! Pin
User 989707413-Dec-17 8:42
User 989707413-Dec-17 8:42 
GeneralRe: MIDI and USB Keyboard! Pin
User 989707411-Dec-17 9:41
User 989707411-Dec-17 9:41 
GeneralRe: MIDI and USB Keyboard! Pin
User 989707412-Nov-17 8:30
User 989707412-Nov-17 8:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.