我正在使用sgm studio 12光混合桌,我买了一根MIDI转USB线,以便将信号传送到我的电脑。我尝试使用MIDI.NET
来获取信号但我似乎找不到任何关于如何执行此操作的代码,除了C#
代码是
void PrintCC(ControlChangeMessage msg) {
Console.WriteLine("Device: " + msg.Device.Name);
Console.WriteLine("Channel: " + msg.Channel.ToString());
Console.WriteLine("Control: " + msg.Control.ToString());
Console.WriteLine("Value: " + msg.Value);
}
...
InputDevice inputDevice = InputDevice.InstalledDevices[0];
inputDevice.Open();
inputDevice.ControlChange += new InputDevice.ControlChangeHandler(PrintCC);
inputDevice.StartReceiving(null);
Console.ReadKey();
现在,有没有人知道如何在Visual Basic中执行此操作?
由于
答案 0 :(得分:0)
您应该能够在VB.NET中使用相同的MIDI.NET库 - VB.NET和C#(大多数)功能兼容,但具有不同的语法。在VB.NET项目中包含对Midi.dll的引用,并将语法示例更改为使用VB.NET语法而不是C#。在查看MIDI.NET网站后,似乎其他用户正在这样做。
这converter可能有所帮助 - 但它并非万无一失。首先,我会转换此example project。我会使用Visual Studio创建一个新的VB.NET项目,并添加与示例项目中的每个.cs文件同名的空.vb文件。然后我会通过转换器清洗每个文件的C#,并将结果粘贴到文件中。这里的曲线非常陡峭。
或者学习C#。 C#和VB.NET真的都是关于相同的底层概念。他们看起来不一样。
答案 1 :(得分:0)
这是一个非常条纹化的例子,说明我如何在VB.NET中使用MIDI-dot-NET。您需要添加对MIDI-dot-NET项目(如果您正在使用源代码)或项目中的DLL的引用。
''Modify the Item index as necessary:
Dim inputDevice as Midi.InputDevice = Midi.InputDevice.InstalledDevices.Item(0)
Dim outputDevice as Midi.InputDevice = Midi.OutputDevice.InstalledDevices.Item(0)
Sub ConnectDevices()
Try
With inputDevice
.Open()
.StartReceiving(Nothing)
AddHandler .NoteOn, AddressOf NoteOn
AddHandler .NoteOff, AddressOf NoteOff
AddHandler .ControlChange, AddressOf ControlChange
End With
Catch ex As Exception
''Hitting this block is likely because something is already connected
'' to the device at the Item index, try again as necessary
End Try
Try
outputDevice.Open()
Catch ex As Exception
''Again, probably already opened
End Try
End Sub
Sub DisconnectDevices()
If inputDevice.IsReceiving Then inputDevice.StopReceiving()
If inputDevice.IsOpen Then
inDev.Close()
RemoveHandler inputDevice.NoteOn, AddressOf NoteOn
RemoveHandler inputDevice.NoteOff, AddressOf NoteOff
RemoveHandler inputDevice.ControlChange, AddressOf ControlChange
''Or you could do something like this:
inputDevice.RemoveAllEventHandlers()
End If
If outputDevice.IsOpen Then outputDevice.Close()
End Sub
Sub NoteOn(ByVal msg As Midi.NoteOnMessage)
''Received a NoteOn (9X)
Dim channel As Byte = CByte(msg.Channel)
Dim pitch As Byte = CByte(msg.Pitch)
Dim velocity As Byte = CByte(msg.Velocity)
End Sub
Sub NoteOff(ByVal msg As Midi.NoteOffMessage)
''Received a NoteOff (8X)
Dim channel As Byte = CByte(msg.Channel)
Dim pitch As Byte = CByte(msg.Pitch)
Dim velocity As Byte = CByte(msg.Velocity)
End Sub
Sub ControlChange(ByVal msg as Midi.ControlChangeMessage)
''Received a Control Change (BX)
Dim channel As Byte = CByte(msg.Channel)
Dim control As Byte = CByte(msg.Control)
Dim value As Byte = CByte(msg.Value)
End Sub
调整三个事件子,以处理所有传入的MIDI信息。发送MIDI也是一件非常简单的事情,您只需要将您拥有的任何字节或整数值转换为库的类型(但是,我不确定它为什么使用整数而不是字节)。
Dim channel As Midi.Channel = CType(0, Midi.Channel)
Dim pitch As Midi.Pitch = CType(1, Midi.Pitch)
Dim velocity As Integer = 127
Dim control As Midi.Control = CType(1, Midi.Control)
Dim value As Integer = 127
outputDevice.SendNoteOn(channel, pitch, velocity)
outputDevice.SendNoteOff(channel, pitch, velocity)
outputDevice.SendControlChange(channel, control, value)
更新:以下是您问题中代码的直接转换:
Private Sub PrintCC(ByVal msg As ControlChangeMessage)
Console.WriteLine("Device: " & msg.Device.Name)
Console.WriteLine("Channel: " & msg.Channel.ToString())
Console.WriteLine("Control: " & msg.Control.ToString())
Console.WriteLine("Value: " & msg.Value.ToString())
End Sub
Dim inputDevice As InputDevice = InputDevice.InstalledDevices(0)
inputDevice.Open()
AddHandler inputDevice.ControlChange, AddressOf PrintCC
inputDevice.StartReceiving(Nothing)
'Console.ReadKey() ''This isn't related to MIDI.Net
最后Console.ReadKey()
行不会影响MIDI.Net;我假设它是一个示例控制台应用程序的一部分。如果您直接翻译其中一个示例,则可能需要它,但如果您正在构建WinForms应用程序,则可能不需要。请记住在关闭/处理代码中的某处实现适当的RemoveHandler inputDevice.ControlChange, AddressOf PrintCC
。