1 |
/* |
2 |
* MidiInDevice.cs |
3 |
* Copyright (c) 2009 kbinani |
4 |
* |
5 |
* This file is part of Boare.Lib.Media. |
6 |
* |
7 |
* Boare.Lib.Media is free software; you can redistribute it and/or |
8 |
* modify it under the terms of the BSD License. |
9 |
* |
10 |
* Boare.Lib.Media is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
13 |
*/ |
14 |
#if JAVA |
15 |
package org.kbinani.media; |
16 |
|
17 |
public class MidiInDevice{ |
18 |
|
19 |
} |
20 |
#else |
21 |
using System; |
22 |
using System.Collections.Generic; |
23 |
using System.Runtime.InteropServices; |
24 |
using bocoree; |
25 |
|
26 |
namespace Boare.Lib.Media { |
27 |
|
28 |
public delegate void MidiReceivedEventHandler( double time, byte[] data ); |
29 |
|
30 |
public class MidiInDevice : IDisposable { |
31 |
delegate void MidiInProcDelegate( uint hMidiIn, uint wMsg, int dwInstance, int dwParam1, int dwParam2 ); |
32 |
|
33 |
private volatile MidiInProcDelegate m_delegate; |
34 |
private IntPtr m_delegate_pointer; |
35 |
private uint m_hmidiin = 0; |
36 |
private int m_port_number; |
37 |
|
38 |
public event MidiReceivedEventHandler MidiReceived; |
39 |
|
40 |
public MidiInDevice( int port_number ) { |
41 |
m_port_number = port_number; |
42 |
m_delegate = new MidiInProcDelegate( MidiInProc ); |
43 |
m_delegate_pointer = Marshal.GetFunctionPointerForDelegate( m_delegate ); |
44 |
win32.midiInOpen( ref m_hmidiin, port_number, m_delegate_pointer, 0, win32.CALLBACK_FUNCTION ); |
45 |
} |
46 |
|
47 |
public void Start() { |
48 |
if ( m_hmidiin > 0 ) { |
49 |
try { |
50 |
win32.midiInStart( m_hmidiin ); |
51 |
} catch ( Exception ex ) { |
52 |
debug.push_log( "MidiInDevice.Start" ); |
53 |
debug.push_log( " ex=" + ex ); |
54 |
} |
55 |
} |
56 |
} |
57 |
|
58 |
public void Stop() { |
59 |
if ( m_hmidiin > 0 ) { |
60 |
try { |
61 |
win32.midiInReset( m_hmidiin ); |
62 |
} catch ( Exception ex ) { |
63 |
debug.push_log( "MidiInDevice.Stop" ); |
64 |
debug.push_log( " ex=" + ex ); |
65 |
} |
66 |
} |
67 |
} |
68 |
|
69 |
public void Close() { |
70 |
if ( m_hmidiin > 0 ) { |
71 |
try { |
72 |
win32.midiInClose( m_hmidiin ); |
73 |
} catch ( Exception ex ) { |
74 |
debug.push_log( "MidiInDevice.Close" ); |
75 |
debug.push_log( " ex=" + ex ); |
76 |
} |
77 |
} |
78 |
m_hmidiin = 0; |
79 |
} |
80 |
|
81 |
public void Dispose() { |
82 |
Close(); |
83 |
} |
84 |
|
85 |
public static int GetNumDevs() { |
86 |
try { |
87 |
int i = (int)win32.midiInGetNumDevs(); |
88 |
return i; |
89 |
} catch ( Exception ex ) { |
90 |
debug.push_log( "MidiInDevice.GetNumDevs" ); |
91 |
debug.push_log( " ex=" + ex ); |
92 |
} |
93 |
return 0; |
94 |
} |
95 |
|
96 |
public static MIDIINCAPS[] GetMidiInDevices() { |
97 |
List<MIDIINCAPS> ret = new List<MIDIINCAPS>(); |
98 |
uint num = 0; |
99 |
try { |
100 |
num = win32.midiInGetNumDevs(); |
101 |
} catch { |
102 |
num = 0; |
103 |
} |
104 |
for ( uint i = 0; i < num; i++ ) { |
105 |
MIDIINCAPS m = new MIDIINCAPS(); |
106 |
uint r = win32.midiInGetDevCaps( i, ref m, (uint)Marshal.SizeOf( m ) ); |
107 |
ret.Add( m ); |
108 |
} |
109 |
return ret.ToArray(); |
110 |
} |
111 |
|
112 |
public void MidiInProc( uint hMidiIn, uint wMsg, int dwInstance, int dwParam1, int dwParam2 ) { |
113 |
try { |
114 |
switch ( wMsg ) { |
115 |
case win32.MM_MIM_OPEN: |
116 |
return; |
117 |
case win32.MM_MIM_CLOSE: |
118 |
return; |
119 |
case win32.MM_MIM_DATA: |
120 |
int receive = dwParam1; |
121 |
double now = PortUtil.getCurrentTime(); |
122 |
switch ( receive & 0xF0 ) { |
123 |
case 0x80: |
124 |
case 0x90: |
125 |
case 0xa0: |
126 |
case 0xb0: |
127 |
case 0xe0: |
128 |
if ( MidiReceived != null ) { |
129 |
MidiReceived( now, new byte[] { (byte)(receive & 0xff), |
130 |
(byte)((receive & 0xffff) >> 8), |
131 |
(byte)((receive & ((2 << 24) - 1)) >> 16) } ); |
132 |
} |
133 |
break; |
134 |
case 0xc0: |
135 |
case 0xd0: |
136 |
if ( MidiReceived != null ) { |
137 |
MidiReceived( now, new byte[] { (byte)( receive & 0xff ), |
138 |
(byte)((receive & 0xffff) >> 8) } ); |
139 |
} |
140 |
break; |
141 |
} |
142 |
return; |
143 |
case win32.MM_MIM_LONGDATA: |
144 |
return; |
145 |
case win32.MM_MIM_ERROR: |
146 |
return; |
147 |
case win32.MM_MIM_LONGERROR: |
148 |
return; |
149 |
} |
150 |
} catch ( Exception ex ) { |
151 |
debug.push_log( "MidiInDevice.MidiInProc" ); |
152 |
debug.push_log( " ex=" + ex ); |
153 |
} |
154 |
} |
155 |
} |
156 |
|
157 |
} |
158 |
#endif |