Code: Select all
EnableExplicit
;GUI
Global Window_Main, Combo_IN, Combo_Out, Text_in, Text_Out
Define event
Structure PortName
ShowName.s
Name.s
EndStructure
Global NewList OutPort.PortName(), NewList InPort.PortName()
;-ImportC
ImportC "/System/Library/Frameworks/CoreMIDI.framework/CoreMIDI"
; Strings
CFSTR(cStr.p-ascii) As "___CFStringMakeConstantString"
; Objects
MIDIObjectGetStringProperty(obj, propertyID, *str)
MIDIObjectGetIntegerProperty(obj, propertyID, *outValue )
;Device
MIDIDeviceGetEntity(device, entityIndex0)
MIDIEntityGetNumberOfSources(entity)
MIDIEntityGetNumberOfDestinations(entity)
MIDIEntityGetSource(entity, sourceIndex0)
MIDIDeviceGetNumberOfEntities(device)
MIDIGetDevice(deviceIndex0)
MIDIGetNumberOfDevices()
MIDIEntityGetDestination(entity, destIndex0)
EndImport
Procedure.s GetName(object)
Protected name, _kMIDIPropertyName = CFSTR("name")
If MIDIObjectGetStringProperty(object, _kMIDIPropertyName, @name)
Debug "Error"
ProcedureReturn ""
EndIf
ProcedureReturn PeekS(CocoaMessage(0, name, "UTF8String"), -1, #PB_UTF8)
EndProcedure
Procedure FindPorts()
Protected PortName.s, P, entityCount, E, entity, sourceCount, destCount
Protected deviceCount, device, DeviceName.s, D, isOffline.a, source, dest
Protected _kMIDIPropertyOffline = CFSTR("offline")
deviceCount = MIDIGetNumberOfDevices()
For D = 0 To deviceCount -1
device = MIDIGetDevice(D)
DeviceName = GetName(device) + " "
isOffline = #False
MIDIObjectGetIntegerProperty(device, _kMIDIPropertyOffline, @isOffline)
If isOffline = #True
DeviceName = Chr(126) +DeviceName
Debug DeviceName + " isOffline"
EndIf
entityCount = MIDIDeviceGetNumberOfEntities(device)
For E = 0 To entityCount -1
entity = MIDIDeviceGetEntity(device, E)
sourceCount = MIDIEntityGetNumberOfSources(entity)
For P = 0 To sourceCount-1
source = MIDIEntityGetSource(entity, P)
PortName = GetName(source)
AddElement(InPort())
InPort()\ShowName = DeviceName + PortName
InPort()\Name = PortName
Next
destCount = MIDIEntityGetNumberOfDestinations(entity)
For P = 0 To destCount -1
dest = MIDIEntityGetDestination(entity, P)
PortName = GetName(dest)
AddElement(outPort())
OutPort()\ShowName = DeviceName + PortName
OutPort()\Name = PortName
Next
Next
Debug "------Next Device"
Next
;Fill Gadget
For P =0 To ListSize(InPort()) -1
SelectElement(InPort(), P)
AddGadgetItem(Combo_IN, -1, InPort()\ShowName)
Next
SetGadgetState(Combo_IN, 0)
For P =0 To ListSize(OutPort()) -1
SelectElement(OutPort(), P)
AddGadgetItem(Combo_Out, -1, OutPort()\ShowName)
Next
SetGadgetState(Combo_Out, 0)
EndProcedure
Procedure Window_Main_Events(event)
Select event
Case #PB_Event_CloseWindow
ProcedureReturn #False
Case #PB_Event_Gadget
Select EventGadget()
Case Combo_IN
SelectElement(InPort(), GetGadgetState(Combo_IN))
Debug InPort()\Name
Case Combo_Out
SelectElement(OutPort(), GetGadgetState(Combo_Out))
Debug OutPort()\Name
EndSelect
EndSelect
ProcedureReturn #True
EndProcedure
Window_Main = OpenWindow(#PB_Any, 0, 0, 470, 130, "MIDI I/O", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
Combo_IN = ComboBoxGadget(#PB_Any, 50, 60, 160, 20)
Text_in = TextGadget(#PB_Any, 50, 40, 110, 20, "MIDI IN")
Combo_Out = ComboBoxGadget(#PB_Any, 250, 60, 160, 20)
Text_Out = TextGadget(#PB_Any, 250, 40, 110, 20, "MIDI Out")
FindPorts()
Repeat
event = WaitWindowEvent()
Until Window_Main_Events(event) = #False
End