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

« back to all changes in this revision

Viewing changes to TuxGuitar-winmm/jni/org_herac_tuxguitar_player_impl_midiport_winmm_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 <stdlib.h>
 
3
#include <string.h>
 
4
#include <windows.h>
 
5
#include <mmsystem.h>
 
6
#include "org_herac_tuxguitar_player_impl_midiport_winmm_MidiSystem.h"
 
7
 
 
8
typedef struct handle{
 
9
        HMIDIOUT* out;
 
10
}midi_handle_t;
 
11
 
 
12
JNIEXPORT jlong JNICALL Java_org_herac_tuxguitar_player_impl_midiport_winmm_MidiSystem_malloc(JNIEnv* env, jobject obj)
 
13
{
 
14
        jlong ptr = 0;
 
15
        
 
16
        midi_handle_t *handle = (midi_handle_t *) malloc( sizeof(midi_handle_t) );
 
17
        handle->out = NULL;
 
18
        
 
19
        memcpy(&ptr, &handle, sizeof( handle ));
 
20
        
 
21
        return ptr;
 
22
}
 
23
 
 
24
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_winmm_MidiSystem_free(JNIEnv* env, jobject obj, jlong ptr)
 
25
{
 
26
        midi_handle_t *handle = NULL;
 
27
        memcpy(&handle, &ptr, sizeof(handle));
 
28
        if(handle != NULL){
 
29
                free( handle );
 
30
        }
 
31
}
 
32
 
 
33
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_winmm_MidiSystem_findPorts(JNIEnv* env, jobject obj, jlong ptr)
 
34
{
 
35
        midi_handle_t *handle = NULL;
 
36
        memcpy(&handle, &ptr, sizeof(handle));
 
37
        if(handle != NULL){
 
38
                MIDIOUTCAPS moc;
 
39
                UINT count, i;
 
40
                count = midiOutGetNumDevs();
 
41
                for (i = 0; i < count; i++){
 
42
                        if (midiOutGetDevCaps(i, &moc, sizeof(MIDIOUTCAPS)) == MMSYSERR_NOERROR){
 
43
                                int srcLen = strlen( moc.szPname );
 
44
                                int dstLen = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)moc.szPname, srcLen, NULL, 0 );
 
45
                                wchar_t* dstBuffer = malloc( (dstLen * 2) + 1 );
 
46
                                MultiByteToWideChar( CP_ACP, 0, (LPCSTR)moc.szPname, srcLen,(LPWSTR)dstBuffer, dstLen );
 
47
                                
 
48
                                //Add a new MidiDevice to the java class
 
49
                                jint device  = i;
 
50
                                jstring name =  (*env)->NewString( env, (jchar*)dstBuffer, dstLen );
 
51
                                jclass cl = (*env)->GetObjectClass(env, obj);
 
52
                                jmethodID mid = (*env)->GetMethodID(env, cl, "addPort", "(Ljava/lang/String;I)V");
 
53
                                if (mid != 0){
 
54
                                        (*env)->CallVoidMethod(env, obj, mid, name, device);
 
55
                                }
 
56
                                free (dstBuffer);
 
57
                        }
 
58
                }
 
59
        }
 
60
}
 
61
 
 
62
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_winmm_MidiSystem_openPort(JNIEnv* env, jobject obj, jlong ptr, jint device)
 
63
{
 
64
        midi_handle_t *handle = NULL;
 
65
        memcpy(&handle, &ptr, sizeof(handle));
 
66
        if(handle != NULL && handle->out == NULL){
 
67
                handle->out = (HMIDIOUT *)malloc( sizeof(HMIDIOUT) );
 
68
                if ( midiOutOpen(handle->out, (UINT)device, 0, 0, CALLBACK_WINDOW) != MMSYSERR_NOERROR ){
 
69
                        free( handle->out );
 
70
                        handle->out = NULL;
 
71
                }
 
72
        }
 
73
}
 
74
 
 
75
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_winmm_MidiSystem_closePort(JNIEnv* env, jobject obj, jlong ptr)
 
76
{
 
77
        midi_handle_t *handle = NULL;
 
78
        memcpy(&handle, &ptr, sizeof(handle));
 
79
        if(handle != NULL && handle->out != NULL){
 
80
                midiOutClose(*handle->out);
 
81
                free( handle->out );
 
82
                handle->out = NULL;
 
83
        }
 
84
}
 
85
 
 
86
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_winmm_MidiSystem_noteOn(JNIEnv* env, jobject ojb, jlong ptr, jint channel, jint note, jint velocity)
 
87
{
 
88
        midi_handle_t *handle = NULL;
 
89
        memcpy(&handle, &ptr, sizeof(handle));
 
90
        if(handle != NULL && handle->out != NULL){
 
91
                midiOutShortMsg(*handle->out, ( ( 0x90 | channel ) | ( note << 8) | ( velocity << 16) ) );
 
92
        }
 
93
}
 
94
 
 
95
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_winmm_MidiSystem_noteOff(JNIEnv* env, jobject ojb, jlong ptr, jint channel, jint note, jint velocity)
 
96
{
 
97
        midi_handle_t *handle = NULL;
 
98
        memcpy(&handle, &ptr, sizeof(handle));
 
99
        if(handle != NULL && handle->out != NULL){
 
100
                midiOutShortMsg(*handle->out, ( ( 0x80 | channel ) | ( note << 8) | ( velocity << 16) ) );
 
101
        }
 
102
}
 
103
 
 
104
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_winmm_MidiSystem_programChange(JNIEnv* env, jobject ojb, jlong ptr, jint channel, jint program)
 
105
{
 
106
        midi_handle_t *handle = NULL;
 
107
        memcpy(&handle, &ptr, sizeof(handle));
 
108
        if(handle != NULL && handle->out != NULL){
 
109
                midiOutShortMsg(*handle->out, ( ( 0xC0 | channel ) | ( program << 8) ) );
 
110
        }
 
111
}
 
112
 
 
113
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_winmm_MidiSystem_controlChange(JNIEnv* env, jobject ojb, jlong ptr, jint channel, jint control, jint value)
 
114
{
 
115
        midi_handle_t *handle = NULL;
 
116
        memcpy(&handle, &ptr, sizeof(handle));
 
117
        if(handle != NULL && handle->out != NULL){
 
118
                midiOutShortMsg(*handle->out, ( ( 0xB0 | channel ) | ( control << 8) | ( value << 16) ) );
 
119
        }
 
120
}
 
121
 
 
122
JNIEXPORT void JNICALL Java_org_herac_tuxguitar_player_impl_midiport_winmm_MidiSystem_pitchBend(JNIEnv* env, jobject ojb, jlong ptr, jint channel, jint value)
 
123
{
 
124
        midi_handle_t *handle = NULL;
 
125
        memcpy(&handle, &ptr, sizeof(handle));
 
126
        if(handle != NULL && handle->out != NULL){
 
127
                midiOutShortMsg(*handle->out, ( ( 0xE0 | channel ) | ( (value * 128 * 2) << 8 ) ) );
 
128
        }
 
129
}