Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add native MIDI support for Mac OSX via Core MIDI API #395

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

HunterZ
Copy link
Contributor

@HunterZ HunterZ commented Aug 8, 2022

A couple of years ago I contributed native MIDI support for Windows and Linux, but was not able to do so for Mac OSX because I lacked a workable development environment. This has been remedied, and I was able to implement and test Core MIDI support in an OSX Mojave VM talking to a real Roland SC-88 via a USB-MIDI interface.

As with Windows, support for this is automatically enabled in OSX builds because it's an OS-provided functionality.

File change notes:

  • CMakeLists.txt:
    • link required Core API libraries on OSX
  • src/MusicSrc/MusicDevice.c:
    • include relevant headers for native MIDI support on OSX
    • add Core MIDI handles to native MIDI device struct on OSX
    • change Windows NativeMidiSendMessage() to take a pointer for outHandle so that calling code can stay common between that and OSX
    • add device name lookup utilities from rtmidi library
    • implement Core MIDI based NativeMidiSendMessage() function
    • implement Core MIDI device init/destroy and device query logic
    • update NativeMidiSendMessage() callers to pass outHandle as pointer for Windows and OSX

File change notes:
- CMakeLists.txt:
  - link required Core API libraries on OSX
- src/MusicSrc/MusicDevice.c:
  - include relevant headers for native MIDI support on OSX
  - add Core MIDI handles to native MIDI device struct on OSX
  - change Windows NativeMidiSendMessage() to take a pointer for outHandle so that calling code can stay common between that and OSX
  - add device name lookup utilities from rtmidi library
  - implement Core MIDI based NativeMidiSendMessage() function
  - implement Core MIDI device init/destroy and device query logic
  - update NativeMidiSendMessage() callers to pass outHandle as pointer for Windows and OSX
Copy link

@MaddTheSane MaddTheSane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some CoreFoundation memory changes that I'm not sure about (CFRelease on functions that are "Get" and not "Copy" or "Create") and some places where the usage of the CFSTR() macro might make more sense.

}

// some MIDI devices have a leading space in endpoint name. trim
CFStringRef space = CFStringCreateWithCString(NULL, " ", kCFStringEncodingUTF8);

This comment was marked as resolved.

if (str)
{
CFStringAppend(result, str);
CFRelease(str);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see anything in MIDIObjectGetStringProperty that suggests that the property should be released, so I'm assuming it follows the "Get" rule.
In other words, it shouldn't be released.

if (CFStringGetLength(result) == 0 )
{
CFRelease(result);
return str;

This comment was marked as resolved.

#elif defined(__APPLE__)
// create Core MIDI client
if (ndev->midiClient) WARN("NativeMidiInit(): midiClient != 0");
const CFStringRef clientNameRef = CFStringCreateWithCString(NULL, "systemshockClient", kCFStringEncodingASCII);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another use of CFSTR()

const CFStringRef clientNameRef = CFSTR("systemshockClient");
const OSStatus clientResult = MIDIClientCreate(clientNameRef, NULL, NULL, &(ndev->midiClient));
//CFRelease(clientNameRef); // Has no effect on CFStrings created with CFSTR

}
// create output port
if (ndev->outHandle.outputPort) WARN("NativeMidiInit(): outputPort != 0");
const CFStringRef portNameRef = CFStringCreateWithCString(NULL, "systemshockPort", kCFStringEncodingASCII);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same CFSTR() comment.

Fix possible memory management errors.
Rename some functions to better match CoreFoundation naming conventions (this is not needed, just makes me feel better).
Replace unneeded calls to CFStringCreateWithCString with just the CFSTR macro.
@MaddTheSane
Copy link

You can see what I meant with my own pull request to your pull request: HunterZ#2

@HunterZ
Copy link
Contributor Author

HunterZ commented Apr 19, 2023

You can see what I meant with my own pull request to your pull request: HunterZ#2

Thanks. I don't know much about coremidi and was just going off of what examples I could find, so I'm happy to merge in your suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants