Menu

MIDI CC messages

Oyvind
2023-11-19
2023-11-24
  • Oyvind

    Oyvind - 2023-11-19

    I'm trying to find out how to send CC messages to MIDI devices from Excel VBA, and came accross this tool. It is really impressive, but too big for me.
    Can anyone help me to get the essence of how to send only CC messages?

    I'm trying to control an Behringer X32.
    From Excel, I want to mute/unmute every channel, and adjust fader on every channel.
    These commands use 0 to 31 to address the channel, and 0 to 127 to set the desired value on the mixer.

    I would really appreciate if somone could explain to me how to build the message with simple values.

    Thanks in advance.

     
  • Rui Seixas Monteiro

    Hi, you can just set 16 CC lines with the desired CC control number.

     
  • Oyvind

    Oyvind - 2023-11-24

    I finnaly found a solution. There are very little information about this, so I asked ChatGPT.
    The answer had to be adjusted and trouble shooted, but it lead me to a working code.

    Here is a code that works in Windows 10 64-bit, Excel 365. It might need to be adjusted to work on other computers.

    Option Explicit
    
    Declare Function midiOutShortMsg Lib "winmm.dll" (ByVal hMidiOut As Long, ByVal dwMsg As Long) As Long
    Declare Function midiOutOpen Lib "winmm.dll" (lphMidiOut As Long, ByVal uDeviceID As Long, ByVal dwCallback As Long, ByVal dwInstance As Long, ByVal dwFlags As Long) As Long
    Declare Function midiOutClose Lib "winmm.dll" (ByVal hMidiOut As Long) As Long
    Declare Function midiOutGetNumDevs Lib "winmm.dll" () As Long
    Declare Function midiOutGetDevCaps Lib "winmm.dll" Alias "midiOutGetDevCapsA" (ByVal uDeviceID As Long, lpMidiOutCaps As Any, ByVal cbMidiOutCaps As Long) As Long
    
    Public Const MIM_OPEN As Long = &H3C1
    Public Const MIM_CLOSE As Long = &H3C2
    Public Const MIM_DATA As Long = &H3C3
    
    Public Device As Long
    
    
    ' Define the MIDIOUTCAPS structure
    Type midiOutCaps
        wMid As Integer
        wPid As Integer
        vDriverVersion As Long
        szPname As String * 32
        dwSupport As Long
    End Type
    

    The next part is the subroutine for sending the MIDI message:

    Sub SendMIDIControlChange(DeviceIndex As Long, Channel As Integer, ccNumber As Integer, ccValue As Integer)
        '
        ' For Behringer X32, the following info is valid:
        ' Channel 1 is used for faders
        ' Channel 2 is used for mutes
        ' Channel 3 is used for pans/balance
        '
        ' ccNumber is used to identify the actual channel on the mixer
        '   -  0 to 31: Ch 01 to Ch 32
        '   - 32 to 39: Aux In 1 to 6
        '
        ' ccValue is used to set the desired value from 0 to 127 (value 0 for unmute, 127 for mute)
        '
        '
    
        Dim hMidiOut As Long
        Dim dwMsg As Long
    
        ' Check if the specified device index is valid
        If DeviceIndex < 0 Or DeviceIndex >= midiOutGetNumDevs Then
            MsgBox "Invalid MIDI device index."
            Exit Sub
        End If
    
        ' Open MIDI output device
        If midiOutOpen(hMidiOut, DeviceIndex, 0, 0, 0) <> 0 Then
            MsgBox "Error opening MIDI output device."
            Exit Sub
        End If
    
        ' Construct MIDI CC message
        dwMsg = &HB0 + (Channel - 1) + (&H100 * ccNumber) + (&H10000 * ccValue)
    
        ' Send MIDI CC message
        midiOutShortMsg hMidiOut, dwMsg
    
    
        ' Close MIDI output device
        midiOutClose hMidiOut
    End Sub
    

    And finally, here's a subroutine to select which mixer channels to mute. This routine calls up the SendMIDIControlChange routine.

    Sub TestSendMIDIControlChange()
        ' Specify the MIDI device index for your Behringer X32 mixer
        Dim DeviceIndex As Long
        Dim Ch As Integer
    
    
        DeviceIndex = 5 ' MIDI device 6 corresponds to index 5
    
        ' Send a MIDI CC message to mute/unmute all channels (value 127 for mute, 0 for unmute)
        For Ch = 0 To 31
            SendMIDIControlChange DeviceIndex, 2, Ch, 127 'Comment to make inactive
            'SendMIDIControlChange DeviceIndex, 2, Ch, 0 'Uncomment to make active
        Next Ch
    
        'Modify this code to adopt to your own needs.
        'This could be connected to a worksheet whith a table showing which
        'channels are to be muted or unmuted in several steps.
    
    
    End Sub
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.