Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 119 additions & 10 deletions CoreMIDI4J/Native/CoreMidi4J/CoreMidiInputPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
/*
* The native callback that is called when a MIDI message is received
*
* @param packets A list of MIDI packets that have been received.
* @param readProcRefCon The refCon passed to MIDIInputPortCreate
* @param srcConnRefCon The refCon passed to MIDIPortConnectSource
* @param packets A list of MIDI packets that have been received.
* @param callbackParameters Context of the JVM
*
*/

void MIDIInput(const MIDIPacketList *packets, void *readProcRefCon, void *srcConnRefCon) {
void sendMidiToCallback(const MIDIPacketList *packets, const MIDI_CALLBACK_PARAMETERS *callbackParameters) {

static mach_timebase_info_data_t sTimebaseInfo; // Will hold conversion factor for timestamps
JNIEnv *env;
Expand All @@ -39,13 +37,10 @@ void MIDIInput(const MIDIPacketList *packets, void *readProcRefCon, void *srcCon
// uninitialised because it makes no sense to have a zero
// denominator in a fraction.
if ( sTimebaseInfo.denom == 0 ) {

(void) mach_timebase_info(&sTimebaseInfo);

}

// Cast the supplied reference to the correct data type
MIDI_CALLBACK_PARAMETERS *callbackParameters = (MIDI_CALLBACK_PARAMETERS *) srcConnRefCon;
}

// Get a JNIEnv reference from the cached JVM
int getEnvStat = callbackParameters->jvm->GetEnv((void **) &env, NULL);
Expand Down Expand Up @@ -114,6 +109,33 @@ void MIDIInput(const MIDIPacketList *packets, void *readProcRefCon, void *srcCon

}

/*
* The native callback that is called when a MIDI message is received
*
* @param packets A list of MIDI packets that have been received.
* @param readProcRefCon The refCon passed to MIDIInputPortCreate
* @param srcConnRefCon The refCon passed to MIDIPortConnectSource
*
*/
void MIDIInput(const MIDIPacketList *packets, void *readProcRefCon, void *srcConnRefCon) {
MIDI_CALLBACK_PARAMETERS *callbackParameters = (MIDI_CALLBACK_PARAMETERS *) srcConnRefCon;
sendMidiToCallback(packets, callbackParameters);
}

/*
* The native callback that is called when a MIDI message is received for a virtual device
*
* @param packets A list of MIDI packets that have been received.
* @param readProcRefCon The readProcRefCon passed to MIDIDestinationCreate
* @param srcConnRefCon The srcConnRefCon (always NULL)
*
*/
void MIDIInputVirtual(const MIDIPacketList *packets, void *readProcRefCon, void *srcConnRefCon) {
MIDI_CALLBACK_PARAMETERS *callbackParameters = (MIDI_CALLBACK_PARAMETERS *) readProcRefCon;
sendMidiToCallback(packets, callbackParameters);
}


/////////////////////////////////////////////////////////
// Native functions for CoreMidiInputPort
/////////////////////////////////////////////////////////
Expand Down Expand Up @@ -207,6 +229,7 @@ JNIEXPORT jlong JNICALL Java_uk_co_xfactorylibrarians_coremidi4j_CoreMidiInputPo
// Get the endpoint reference from the info object
int sourceEndPointReference = env->GetIntField(info, env->GetFieldID(env->GetObjectClass(info),"endPointReference","I"));

std::cout << "midiPortConnectSource" << std::endl;
// Connect the input port to the source endpoint.
status = MIDIPortConnectSource(inputPortReference, sourceEndPointReference, callbackParameters);

Expand Down Expand Up @@ -268,3 +291,89 @@ JNIEXPORT void JNICALL Java_uk_co_xfactorylibrarians_coremidi4j_CoreMidiInputPor
}

}

/*
* Creates the virtual MIDI Input Port
*
* Class: com_coremidi4j_CoreMidiInputPort
* Method: createVirtualPort
* Signature: (ILjava/lang/String;)Iuk/co/xfactorylibrarians/coremidi4j/CoreMidiSource;)V
*
* @param env The JNI environment
* @param obj The reference to the java object instance that called this native method
* @param clientReference The MIDI Client used to create the port
* @param portName The name of the input port
* @param sourceDevice The reference of the source device
*
* @return A reference to the created virtual input port
*
* @throws CoreMidiException if the virtual input port cannot be created
*
*/

JNIEXPORT jint JNICALL Java_uk_co_xfactorylibrarians_coremidi4j_CoreMidiInputPort_createVirtualPort(JNIEnv *env, jobject obj, jint clientReference, jstring portName, jobject sourceDevice) {

OSStatus status;

// Allocate memory for the callback parameters
MIDI_CALLBACK_PARAMETERS *callbackParameters = (MIDI_CALLBACK_PARAMETERS *) malloc(sizeof(MIDI_CALLBACK_PARAMETERS));

// Throw exception if memory allocation failed
if ( callbackParameters == NULL ) {

ThrowException(env,CFSTR("MIDIPortCreateVirtual"),-1);

}

// Cache the information needed for the callback, noting that we obtain a l=global reference for the CoreMidiInputPortObject
callbackParameters->object = env->NewGlobalRef(sourceDevice);
callbackParameters->methodID = env->GetMethodID(env->GetObjectClass(sourceDevice), "messageCallback", "(JI[B)V");
jint result = env->GetJavaVM(&callbackParameters->jvm);

// Ensure that the last call succeeded
assert (result == JNI_OK);

MIDIPortRef portRef;
// Create a CFStringRef from the portName jstring
const char *portNameString = env->GetStringUTFChars(portName,0);
CFStringRef cfPortName = CFStringCreateWithCString(NULL,portNameString,kCFStringEncodingMacRoman);

status = MIDIDestinationCreate( clientReference, cfPortName, MIDIInputVirtual, callbackParameters, &portRef );

if ( status != 0) {

ThrowException(env,CFSTR("MIDIPortCreateVirtual"),status);

}

return portRef;

}

/*
* Disposes a virtual source or destination your client created.
*
* Class: com_coremidi4j_CoreMidiInputPort
* Method: disposeVirtualPort
* Signature: (IL
*
* @param env The JNI environment
* @param obj The reference to the java object instance that called this native method
* @param inputPortReference The reference of the input point that we wish to dispose
*
* @throws CoreMidiException if the input port cannot be disposed
*
*/
JNIEXPORT void JNICALL Java_uk_co_xfactorylibrarians_coremidi4j_CoreMidiInputPort_disposeVirtualPort(JNIEnv *env, jobject obj, jint inputPortReference) {

OSStatus status;

status = MIDIEndpointDispose( inputPortReference );

if ( status != 0) {

ThrowException(env,CFSTR("MIDIPortDisposeVirtual"),status);

}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
* Company: x.factory Librarians
*
* @author Derek Cook
*
*
* CoreMIDI4J is an open source Service Provider Interface for supporting external MIDI devices on MAC OS X
*
*
* CREDITS - This library uses principles established by OSXMIDI4J, but converted so it operates at the JNI level with no additional libraries required
*
*
*/

package uk.co.xfactorylibrarians.coremidi4j;
Expand All @@ -26,11 +26,11 @@ public class CoreMidiClient {

/**
* Constructor for class
*
* @param name The name of the client
*
*
* @param name The name of the client
*
* @throws CoreMidiException if the client cannot be initialized
*
*
*/

public CoreMidiClient(String name) throws CoreMidiException {
Expand All @@ -41,30 +41,32 @@ public CoreMidiClient(String name) throws CoreMidiException {

/**
* Creates a new CoreMidiInputPort
*
*
* @param name The name of the port
*
*
* @param createVirtual If true, create a virtual input port
*
* @return A new CoreMidiInputPort
*
*
* @throws CoreMidiException if the port cannot be created
*
*
*/

public CoreMidiInputPort inputPortCreate(final String name) throws CoreMidiException {
public CoreMidiInputPort inputPortCreate(final String name, final boolean createVirtual) throws CoreMidiException {

return new CoreMidiInputPort(midiClientReference,name);
return new CoreMidiInputPort(midiClientReference,name,createVirtual);

}

/**
* Creates a new CoreMidiOutputPort
*
*
* @param name The name of the port
*
*
* @return A new CoreMidiOutputPort
*
*
* @throws CoreMidiException if the port cannot be created
*
*
*/

public CoreMidiOutputPort outputPortCreate(final String name) throws CoreMidiException {
Expand All @@ -75,9 +77,9 @@ public CoreMidiOutputPort outputPortCreate(final String name) throws CoreMidiExc

/**
* The message callback for receiving notifications about changes in the MIDI environment from the JNI code
*
*
* @throws CoreMidiException if a problem occurs passing along the notification
*
*
*/

public void notifyCallback() throws CoreMidiException {
Expand Down Expand Up @@ -115,11 +117,11 @@ public void notifyCallback() throws CoreMidiException {

/**
* Creates the MIDI Client
*
*
* @param clientName The name of the client
*
*
* @return A reference to the MIDI client
*
*
* @throws CoreMidiException if the client cannot be created
*
*/
Expand All @@ -128,11 +130,11 @@ public void notifyCallback() throws CoreMidiException {

/**
* Disposes of a CoreMIDI Client
*
*
* @param clientReference The reference of the client to dispose of
*
*
* @throws CoreMidiException if there is a problem disposing of the client
*
*
*/

private native void disposeClient(int clientReference) throws CoreMidiException;
Expand Down
Loading