1 |
kbinani |
6 |
/* |
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 |
kbinani |
18 |
#if JAVA |
15 |
|
|
package org.kbinani.media; |
16 |
|
|
|
17 |
|
|
public class MidiInDevice{ |
18 |
|
|
|
19 |
|
|
} |
20 |
|
|
#else |
21 |
kbinani |
6 |
using System; |
22 |
kbinani |
18 |
using System.Collections.Generic; |
23 |
kbinani |
6 |
using System.Runtime.InteropServices; |
24 |
|
|
using bocoree; |
25 |
|
|
|
26 |
|
|
namespace Boare.Lib.Media { |
27 |
|
|
|
28 |
kbinani |
18 |
public delegate void MidiReceivedEventHandler( double time, byte[] data ); |
29 |
kbinani |
6 |
|
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 |
kbinani |
18 |
win32.midiInOpen( ref m_hmidiin, port_number, m_delegate_pointer, 0, win32.CALLBACK_FUNCTION ); |
45 |
kbinani |
6 |
} |
46 |
|
|
|
47 |
|
|
public void Start() { |
48 |
|
|
if ( m_hmidiin > 0 ) { |
49 |
|
|
try { |
50 |
kbinani |
18 |
win32.midiInStart( m_hmidiin ); |
51 |
kbinani |
6 |
} 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 |
kbinani |
18 |
win32.midiInReset( m_hmidiin ); |
62 |
kbinani |
6 |
} 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 |
kbinani |
18 |
win32.midiInClose( m_hmidiin ); |
73 |
kbinani |
6 |
} 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 |
kbinani |
18 |
int i = (int)win32.midiInGetNumDevs(); |
88 |
kbinani |
6 |
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 |
kbinani |
18 |
num = win32.midiInGetNumDevs(); |
101 |
kbinani |
6 |
} catch { |
102 |
|
|
num = 0; |
103 |
|
|
} |
104 |
|
|
for ( uint i = 0; i < num; i++ ) { |
105 |
|
|
MIDIINCAPS m = new MIDIINCAPS(); |
106 |
kbinani |
18 |
uint r = win32.midiInGetDevCaps( i, ref m, (uint)Marshal.SizeOf( m ) ); |
107 |
kbinani |
6 |
ret.Add( m ); |
108 |
|
|
} |
109 |
|
|
return ret.ToArray(); |
110 |
|
|
} |
111 |
|
|
|
112 |
kbinani |
8 |
public void MidiInProc( uint hMidiIn, uint wMsg, int dwInstance, int dwParam1, int dwParam2 ) { |
113 |
kbinani |
6 |
try { |
114 |
|
|
switch ( wMsg ) { |
115 |
kbinani |
18 |
case win32.MM_MIM_OPEN: |
116 |
kbinani |
6 |
return; |
117 |
kbinani |
18 |
case win32.MM_MIM_CLOSE: |
118 |
kbinani |
6 |
return; |
119 |
kbinani |
18 |
case win32.MM_MIM_DATA: |
120 |
kbinani |
6 |
int receive = dwParam1; |
121 |
kbinani |
18 |
double now = PortUtil.getCurrentTime(); |
122 |
kbinani |
6 |
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 |
kbinani |
18 |
case win32.MM_MIM_LONGDATA: |
144 |
kbinani |
6 |
return; |
145 |
kbinani |
18 |
case win32.MM_MIM_ERROR: |
146 |
kbinani |
6 |
return; |
147 |
kbinani |
18 |
case win32.MM_MIM_LONGERROR: |
148 |
kbinani |
6 |
return; |
149 |
|
|
} |
150 |
|
|
} catch ( Exception ex ) { |
151 |
|
|
debug.push_log( "MidiInDevice.MidiInProc" ); |
152 |
|
|
debug.push_log( " ex=" + ex ); |
153 |
|
|
} |
154 |
|
|
} |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
} |
158 |
kbinani |
18 |
#endif |