Copyright © 2013-2015 MultiMedia Soft

CallbackMidiDevicesEventRawReceived delegate

Previous pageReturn to chapter overviewNext page

Remarks

 

Callback delegate invoked from a secondary thread when a raw MIDI event is received from a MIDI input device.

 

In order to allow the component invoking this delegate at runtime, the container application must invoke the CallbackMidiDevicesEventRawReceivedSet method passing the address of the callback function.

 

For further details about callback delegates see the How to synchronize the container application with the API tutorial.

For further details about the use of MIDI devices see the ClassMidiDevices class section and the How to manage MIDI devices tutorial.

 

 

Syntax

[Visual Basic]

Public Delegate Sub CallbackMidiDevicesEventRawReceived (

nDeviceUniqueId as Int16,

pBuffer as IntPtr,

nBufferLenght as Int32,

nTimeStamp as Int32,

nUserData as Int32

)


 

[C#]

public delegate void CallbackMidiDevicesEventRawReceived (

Int16 nDeviceUniqueId,

IntPtr pBuffer,

Int32 nBufferLenght,

Int32 nTimeStamp,

Int32 nUserData

)


 

[C++]

public delegate void CallbackMidiDevicesEventRawReceived (

Int16 nDeviceUniqueId,

IntPtr pBuffer,

Int32 nBufferLenght,

Int32 nTimeStamp,

Int32 nUserData

)


 

 

Parameter

Description

 

 

nDeviceUniqueId

The unique identifier of the MIDI device returned by a previous call to the MidiDevices.Open method

pBuffer

The memory buffer containing raw data of the MIDI event.

See the code snippets on the bottom of this page in order to see how to access raw data stored inside the buffer through marshaling.

nBufferLenght

Length in bytes of the given buffer

nTimeStamp

Reference that, on return from the method call, will contain the time stamp of the event expressed in milliseconds elapsed from the moment in which the MIDI input device was opened through a call to the MidiDevices.Open method.

nUserData

User instance data

 

 

The code snippets below show how to access raw data stored inside the buffer

 

Visual Basic .NET

 

Imports System.Runtime.InteropServices ' needed for data marshaling

 

Namespace ProfileEditor

 Public Partial Class FormMain

         Inherits Form

         Public Sub New()

                 InitializeComponent()

         End Sub

 

         ' instance the component

         Dim m_djConsole As ActiveDjConsoleNetApi.ActiveDjConsoleNetApi = New ActiveDjConsoleNetApi.ActiveDjConsoleNetApi()

 

         Public m_nIdInputDevice As Short

 

         ' predispose the delegate and the callback

         Private Delegate Sub EventRawReceivedCallbackDelegate(ByVal nDeviceUniqueId As Int16, _

                         ByVal pBuffer As IntPtr, ByVal nBufferLenght As Int32, ByVal nTimeStamp As Int32, ByVal nUserData As Int32)

         Private addrCallbackMidiDevicesEventRawReceived As CallbackMidiDevicesEventRawReceived

 

         ' the subroutine that manages the callback

         Private Sub MidiDevicesEventRawReceivedCallback(ByVal nDeviceUniqueId As Int16, ByVal pBuffer As IntPtr, _

                                         ByVal nBufferLenght As Int32, ByVal nTimeStamp As Int32, ByVal nUserData As Int32)

            ' check if we are being invoked from a secondary thread

             If Me.InvokeRequired Then

                   Me.Invoke(New EventRawReceivedCallbackDelegate(AddressOf MidiDevicesEventRawReceivedCallback), _

                                         nDeviceUniqueId, pBuffer, nBufferLenght, nTimeStamp, nUserData)

                 Return

             End If

 

             Dim strMessage As String = ""

             Dim i As Integer = 0

             Do While i < nBufferLenght

                   ' use marshaling to access raw data in memory directly

                   strMessage &= HexFormatted (Marshal.ReadByte(pBuffer, i), 2) & " "

                   i += 1

             Loop

 

            ' display the string somewhere, for example inside am edit box

             TextRawInput.Text = strMessage

         End Sub

 

         Private Sub FormMain_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

                 ' initialize the component

                 m_djConsole.Init()

         End Sub

 

       Private Sub CommandConnectDevices_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CommandConnectDevices.Click

          ' open selected input device

           Dim nResult As enumDjcErrorCodes = 0

           nResult = m_djConsole.MidiDevices.Open(True, ComboMidiInputs.SelectedIndex, m_nIdInputDevice)

           If nResult <> enumDjcErrorCodes.ERR_DJC_NOERROR Then

              ' predispose the callback that will notify about reception of raw MIDI messages from the MIDI input device

               addrCallbackMidiDevicesEventRawReceived = New CallbackMidiDevicesEventRawReceived(AddressOf MidiDevicesEventRawReceivedCallback)

               m_djConsole.CallbackMidiDevicesEventRawReceivedSet(m_nIdInputDevice, addrCallbackMidiDevicesEventRawReceived, 0)

               Return

           End If

         

           MessageBox.Show("MIDI input device opening failed due to error " & nResult)

       End Sub

 End Class

End Namespace

 

 

Visual C#

 

using System.Runtime.InteropServices; // needed for data marshaling

 

namespace ProfileEditor

{

 public partial class FormMain : Form

 {

         public FormMain()

         {

                 InitializeComponent();

         }

 

         // instance the component

         ActiveDjConsoleNetApi.ActiveDjConsoleNetApi m_djConsole = new ActiveDjConsoleNetApi.ActiveDjConsoleNetApi();

 

         public short m_nIdInputDevice;

 

         // predispose the delegate and the callback

         delegate void EventRawReceivedCallbackDelegate(Int16 nDeviceUniqueId,

                                       IntPtr pBuffer, Int32 nBufferLenght, Int32 nTimeStamp, Int32 nUserData);

         CallbackMidiDevicesEventRawReceived     addrCallbackMidiDevicesEventRawReceived;

 

         void MidiDevicesEventRawReceivedCallback(Int16 nDeviceUniqueId, IntPtr pBuffer, Int32 nBufferLenght, Int32 nTimeStamp, Int32 nUserData)

         {

                 // check if we are being invoked from a secondary thread

                 if (this.InvokeRequired)

                 {

                         this.Invoke(new EventRawReceivedCallbackDelegate(MidiDevicesEventRawReceivedCallback), nDeviceUniqueId,

                                                                     pBuffer, nBufferLenght, nTimeStamp, nUserData);

                         return;

                 }

 

                 string strMessage = "";

                 for (int i = 0; i < nBufferLenght; i++)

                         // use marshaling to access raw data in memory directly and store it inside a string

                         strMessage += HexFormatted (Marshal.ReadByte(pBuffer, i), 2) + " ";

 

                 // display the string somewhere, for example inside am edit box

                 TextRawInput.Text = strMessage;

         }

 

         private void FormMain_Load(object sender, EventArgs e)

         {

                 // initialize the component

                 m_djConsole.Init();

         }

 

         private void CommandConnectDevices_Click(object sender, EventArgs e)

         {

                 // open selected input device

                 enumDjcErrorCodes nResult = 0;

                 nResult = m_djConsole.MidiDevices.Open(true, ComboMidiInputs.SelectedIndex, ref m_nIdInputDevice);

                 if (nResult == enumDjcErrorCodes.ERR_DJC_NOERROR)

                 {

                    // predispose the callback that will notify about reception of raw MIDI messages from the MIDI input device

                     addrCallbackMidiDevicesEventRawReceived = new CallbackMidiDevicesEventRawReceived(MidiDevicesEventRawReceivedCallback);

                     m_djConsole.CallbackMidiDevicesEventRawReceivedSet(m_nIdInputDevice, addrCallbackMidiDevicesEventRawReceived, 0);

                     return;

                 }

                 MessageBox.Show("MIDI input device opening failed due to error " + nResult);

         }

 }

}