00001
00017 #include "CoreMidiOutputPort.h"
00018
00020
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 JNIEXPORT jint JNICALL Java_uk_co_xfactorylibrarians_coremidi4j_CoreMidiOutputPort_createOutputPort(JNIEnv *env, jobject obj, jint clientReference, jstring portName) {
00042
00043 MIDIPortRef outputPort;
00044 OSStatus status;
00045
00046
00047 const char *portNameString = env->GetStringUTFChars(portName,0);
00048 CFStringRef cfPortName = CFStringCreateWithCString(NULL,portNameString,kCFStringEncodingMacRoman);
00049
00050
00051 status = MIDIOutputPortCreate(clientReference, cfPortName, &outputPort);
00052
00053
00054 env->ReleaseStringUTFChars(portName, portNameString);
00055
00056
00057 if ( status != 0) {
00058
00059 ThrowException(env,CFSTR("MIDIOutputPortCreate"),status);
00060
00061 }
00062
00063
00064 return outputPort;
00065
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 JNIEXPORT void JNICALL Java_uk_co_xfactorylibrarians_coremidi4j_CoreMidiOutputPort_sendMidiMessage(JNIEnv *env, jobject obj, jint outputPortReference, jint endPointReference, jobject midiMessage, jlong timestamp) {
00087
00088 static mach_timebase_info_data_t sTimebaseInfo;
00089
00090
00091
00092
00093
00094 if ( sTimebaseInfo.denom == 0 ) {
00095
00096 (void) mach_timebase_info(&sTimebaseInfo);
00097
00098 }
00099
00100 OSStatus status = 0;
00101
00102 int messageLength;
00103 int bufferLength;
00104 signed char *messageData;
00105 jobject mvdata;
00106
00107
00108 jclass mmClass = env->FindClass("javax/sound/midi/MidiMessage");
00109
00110
00111 messageLength = env->GetIntField(midiMessage, env->GetFieldID(mmClass,"length","I"));
00112
00113
00114 bufferLength = 1000 + messageLength;
00115
00116
00117 mvdata = env->GetObjectField(midiMessage, env->GetFieldID(mmClass,"data","[B"));
00118 jbyteArray *array = reinterpret_cast<jbyteArray*>(&mvdata);
00119 messageData = env->GetByteArrayElements(*array, NULL);
00120
00121
00122 char *buffer = (char *) malloc(bufferLength);
00123
00124
00125 uint64_t coreTimestamp = (timestamp == 0) ? 0 : ((timestamp * sTimebaseInfo.denom) / sTimebaseInfo.numer) * 1000;
00126
00127
00128 if ( buffer != NULL ) {
00129
00130 MIDIPacketList *packets = (MIDIPacketList *)buffer;
00131
00132 MIDIPacket *packet = MIDIPacketListInit(packets);
00133 MIDIPacketListAdd(packets, bufferLength, packet, coreTimestamp, messageLength, (Byte *) messageData);
00134
00135 status = MIDISend(outputPortReference, endPointReference, packets);
00136
00137 free(buffer);
00138
00139 } else {
00140
00141 ThrowException(env,CFSTR("MIDISend - Memory Allocation Fail"),-1);
00142
00143 }
00144
00145
00146 env->ReleaseByteArrayElements(*array, messageData, 0);
00147
00148
00149 if ( status != 0) {
00150
00151 ThrowException(env,CFSTR("MIDISend"),status);
00152
00153 }
00154
00155 }