~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar-oss/jni/org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem.c

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <string.h>
 
3
#include <unistd.h>
 
4
#include <stdlib.h>
 
5
#include <fcntl.h>
 
6
#include <sys/soundcard.h>
 
7
#include "org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem.h"
 
8
 
 
9
typedef struct {
 
10
        int fd;
 
11
        int port;
 
12
} handle_t;
 
13
 
 
14
JNIEXPORT jlong JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_malloc(JNIEnv* env, jobject obj)
 
15
{
 
16
        jlong ptr = 0;
 
17
        
 
18
        handle_t *handle = (handle_t *) malloc( sizeof(handle_t) );
 
19
        handle->fd = -1;
 
20
        handle->port = -1;
 
21
        
 
22
        memcpy(&ptr, &handle, sizeof( handle ));
 
23
        
 
24
        return ptr;
 
25
}
 
26
 
 
27
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_free(JNIEnv* env, jobject obj, jlong ptr)
 
28
{
 
29
        handle_t *handle = NULL;
 
30
        memcpy(&handle, &ptr, sizeof(handle));
 
31
        if(handle != NULL){
 
32
                free( handle );
 
33
        }
 
34
}
 
35
 
 
36
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_open(JNIEnv* env, jobject obj, jlong ptr, jstring str)
 
37
{
 
38
        handle_t *handle = NULL;
 
39
        memcpy(&handle, &ptr, sizeof(handle));
 
40
        if(handle != NULL && handle->fd == -1)
 
41
        {
 
42
                const char *device = (*env)->GetStringUTFChars(env, str, 0);
 
43
                
 
44
                handle->port = -1;
 
45
                if (( handle->fd = open (device, O_WRONLY)) == -1)
 
46
                {
 
47
                        handle = NULL;
 
48
                        perror (device);
 
49
                }
 
50
                
 
51
                (*env)->ReleaseStringUTFChars(env, str, device);
 
52
        }
 
53
}
 
54
 
 
55
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_close(JNIEnv* env, jobject obj, jlong ptr)
 
56
{
 
57
        handle_t *handle = NULL;
 
58
        memcpy(&handle, &ptr, sizeof(handle));
 
59
        if(handle != NULL && handle->fd >= 0)
 
60
        {
 
61
                close(handle->fd);
 
62
                handle->fd = -1;
 
63
                handle->port = -1;
 
64
        }
 
65
}
 
66
 
 
67
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_findPorts(JNIEnv* env, jobject obj, jlong ptr)
 
68
{       
 
69
        handle_t *handle = NULL;
 
70
        memcpy(&handle, &ptr, sizeof(handle));
 
71
        if(handle != NULL && handle->fd >= 0)
 
72
        {
 
73
                int i;
 
74
                int count;
 
75
                struct midi_info info;
 
76
                ioctl(handle->fd, SNDCTL_SEQ_NRMIDIS, &count);
 
77
                for ( i = 0; i < count ; i++ )
 
78
                {
 
79
                        info.device = i;
 
80
                        ioctl(handle->fd, SNDCTL_MIDI_INFO, &info);
 
81
                        
 
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");
 
87
                        if (mid != 0){
 
88
                                (*env)->CallVoidMethod(env, obj, mid,name,device);
 
89
                        }
 
90
                }
 
91
        }
 
92
}
 
93
 
 
94
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_openPort(JNIEnv* env, jobject obj, jlong ptr, jint device)
 
95
{
 
96
        handle_t *handle = NULL;
 
97
        memcpy(&handle, &ptr, sizeof(handle));
 
98
        if(handle != NULL)
 
99
        {
 
100
                handle->port = device;
 
101
        }
 
102
}
 
103
 
 
104
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_closePort(JNIEnv* env, jobject obj, jlong ptr)
 
105
{
 
106
        handle_t *handle = NULL;
 
107
        memcpy(&handle, &ptr, sizeof(handle));
 
108
        if(handle != NULL)
 
109
        {
 
110
                handle->port = -1;
 
111
        }
 
112
}
 
113
 
 
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)
 
115
{
 
116
        handle_t *handle = NULL;
 
117
        memcpy(&handle, &ptr, sizeof(handle));
 
118
        if(handle != NULL && handle->fd >= 0 && handle->port >= 0)
 
119
        {
 
120
                unsigned char packet[4] = {SEQ_MIDIPUTC, 0, handle->port, 1};
 
121
                packet[1] = (0x90 | channel);
 
122
                write(handle->fd, packet, sizeof(packet));
 
123
                packet[1] = note;
 
124
                write(handle->fd, packet, sizeof(packet));
 
125
                packet[1] = velocity;
 
126
                write(handle->fd, packet, sizeof(packet));
 
127
        }
 
128
}
 
129
 
 
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)
 
131
{
 
132
        handle_t *handle = NULL;
 
133
        memcpy(&handle, &ptr, sizeof(handle));
 
134
        if(handle != NULL && handle->fd >= 0 && handle->port >= 0)
 
135
        {
 
136
                unsigned char packet[4] = {SEQ_MIDIPUTC, 0, handle->port, 0};
 
137
                packet[1] = (0x80 | channel);
 
138
                write(handle->fd, packet, sizeof(packet));
 
139
                packet[1] = note;
 
140
                write(handle->fd, packet, sizeof(packet));
 
141
                packet[1] = velocity;
 
142
                write(handle->fd, packet, sizeof(packet));
 
143
        }
 
144
}
 
145
 
 
146
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_programChange(JNIEnv* env, jobject ojb, jlong ptr, jint channel, jint program)
 
147
{
 
148
        handle_t *handle = NULL;
 
149
        memcpy(&handle, &ptr, sizeof(handle));
 
150
        if(handle != NULL && handle->fd >= 0 && handle->port >= 0)
 
151
        {
 
152
                unsigned char packet[4] = {SEQ_MIDIPUTC, 0, handle->port, 0};
 
153
                packet[1] = (0xC0 | channel);
 
154
                write(handle->fd, packet, sizeof(packet));
 
155
                packet[1] = program;
 
156
                write(handle->fd, packet, sizeof(packet));
 
157
        }
 
158
}
 
159
 
 
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)
 
161
{
 
162
        handle_t *handle = NULL;
 
163
        memcpy(&handle, &ptr, sizeof(handle));
 
164
        if(handle != NULL && handle->fd >= 0 && handle->port >= 0)
 
165
        {
 
166
                unsigned char packet[4] = {SEQ_MIDIPUTC, 0, handle->port, 0};
 
167
                packet[1] = (0xB0 | channel);
 
168
                write(handle->fd, packet, sizeof(packet));
 
169
                packet[1] = control;
 
170
                write(handle->fd, packet, sizeof(packet));
 
171
                packet[1] = value;
 
172
                write(handle->fd, packet, sizeof(packet));
 
173
        }
 
174
}
 
175
 
 
176
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_oss_MidiSystem_pitchBend(JNIEnv* env, jobject ojb, jlong ptr, jint channel, jint value)
 
177
{
 
178
        handle_t *handle = NULL;
 
179
        memcpy(&handle, &ptr, sizeof(handle));
 
180
        if(handle != NULL && handle->fd >= 0 && handle->port >= 0)
 
181
        {
 
182
                unsigned char packet[4] = {SEQ_MIDIPUTC, 0, handle->port, 0};
 
183
                packet[1] = (0xE0 | channel);
 
184
                write(handle->fd, packet, sizeof(packet));
 
185
                packet[1] = 0;
 
186
                write(handle->fd, packet, sizeof(packet));
 
187
                packet[1] = value;
 
188
                write(handle->fd, packet, sizeof(packet));
 
189
        }
 
190
}