6
#include <sys/soundcard.h>
7
#include "org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem.h"
14
JNIEXPORT jlong JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_malloc(JNIEnv* env, jobject obj)
18
handle_t *handle = (handle_t *) malloc( sizeof(handle_t) );
22
memcpy(&ptr, &handle, sizeof( handle ));
27
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_free(JNIEnv* env, jobject obj, jlong ptr)
29
handle_t *handle = NULL;
30
memcpy(&handle, &ptr, sizeof(handle));
36
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_open(JNIEnv* env, jobject obj, jlong ptr, jstring str)
38
handle_t *handle = NULL;
39
memcpy(&handle, &ptr, sizeof(handle));
40
if(handle != NULL && handle->fd == -1)
42
const char *device = (*env)->GetStringUTFChars(env, str, 0);
45
if (( handle->fd = open (device, O_WRONLY)) == -1)
51
(*env)->ReleaseStringUTFChars(env, str, device);
55
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_close(JNIEnv* env, jobject obj, jlong ptr)
57
handle_t *handle = NULL;
58
memcpy(&handle, &ptr, sizeof(handle));
59
if(handle != NULL && handle->fd >= 0)
67
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_findPorts(JNIEnv* env, jobject obj, jlong ptr)
69
handle_t *handle = NULL;
70
memcpy(&handle, &ptr, sizeof(handle));
71
if(handle != NULL && handle->fd >= 0)
75
struct midi_info info;
76
ioctl(handle->fd, SNDCTL_SEQ_NRMIDIS, &count);
77
for ( i = 0; i < count ; i++ )
80
ioctl(handle->fd, SNDCTL_MIDI_INFO, &info);
82
//Add a new MidiDevice to the java class
83
jint device = info.device;
84
jstring name = (*env)->NewStringUTF(env, info.name);
85
jclass cl = (*env)->GetObjectClass(env, obj);
86
jmethodID mid = (*env)->GetMethodID(env, cl, "addPort", "(Ljava/lang/String;I)V");
88
(*env)->CallVoidMethod(env, obj, mid,name,device);
94
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_openPort(JNIEnv* env, jobject obj, jlong ptr, jint device)
96
handle_t *handle = NULL;
97
memcpy(&handle, &ptr, sizeof(handle));
100
handle->port = device;
104
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_closePort(JNIEnv* env, jobject obj, jlong ptr)
106
handle_t *handle = NULL;
107
memcpy(&handle, &ptr, sizeof(handle));
114
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_noteOn(JNIEnv* env, jobject ojb, jlong ptr, jint channel, jint note, jint velocity)
116
handle_t *handle = NULL;
117
memcpy(&handle, &ptr, sizeof(handle));
118
if(handle != NULL && handle->fd >= 0 && handle->port >= 0)
120
unsigned char packet[4] = {SEQ_MIDIPUTC, 0, handle->port, 1};
121
packet[1] = (0x90 | channel);
122
write(handle->fd, packet, sizeof(packet));
124
write(handle->fd, packet, sizeof(packet));
125
packet[1] = velocity;
126
write(handle->fd, packet, sizeof(packet));
130
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_noteOff(JNIEnv* env, jobject ojb, jlong ptr, jint channel, jint note, jint velocity)
132
handle_t *handle = NULL;
133
memcpy(&handle, &ptr, sizeof(handle));
134
if(handle != NULL && handle->fd >= 0 && handle->port >= 0)
136
unsigned char packet[4] = {SEQ_MIDIPUTC, 0, handle->port, 0};
137
packet[1] = (0x80 | channel);
138
write(handle->fd, packet, sizeof(packet));
140
write(handle->fd, packet, sizeof(packet));
141
packet[1] = velocity;
142
write(handle->fd, packet, sizeof(packet));
146
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_programChange(JNIEnv* env, jobject ojb, jlong ptr, jint channel, jint program)
148
handle_t *handle = NULL;
149
memcpy(&handle, &ptr, sizeof(handle));
150
if(handle != NULL && handle->fd >= 0 && handle->port >= 0)
152
unsigned char packet[4] = {SEQ_MIDIPUTC, 0, handle->port, 0};
153
packet[1] = (0xC0 | channel);
154
write(handle->fd, packet, sizeof(packet));
156
write(handle->fd, packet, sizeof(packet));
160
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_controlChange(JNIEnv* env, jobject ojb, jlong ptr, jint channel, jint control, jint value)
162
handle_t *handle = NULL;
163
memcpy(&handle, &ptr, sizeof(handle));
164
if(handle != NULL && handle->fd >= 0 && handle->port >= 0)
166
unsigned char packet[4] = {SEQ_MIDIPUTC, 0, handle->port, 0};
167
packet[1] = (0xB0 | channel);
168
write(handle->fd, packet, sizeof(packet));
170
write(handle->fd, packet, sizeof(packet));
172
write(handle->fd, packet, sizeof(packet));
176
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_pitchBend(JNIEnv* env, jobject ojb, jlong ptr, jint channel, jint value)
178
handle_t *handle = NULL;
179
memcpy(&handle, &ptr, sizeof(handle));
180
if(handle != NULL && handle->fd >= 0 && handle->port >= 0)
182
unsigned char packet[4] = {SEQ_MIDIPUTC, 0, handle->port, 0};
183
packet[1] = (0xE0 | channel);
184
write(handle->fd, packet, sizeof(packet));
186
write(handle->fd, packet, sizeof(packet));
188
write(handle->fd, packet, sizeof(packet));