*** empty log message ***
[chuck-blob.git] / v2 / midiio_rtmidi.h
blob3f37cbc210d7c492bdf2eb85fb8aa978cfeab162
1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 U.S.A.
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: midiio_rtmidi.h
27 // desc: midi io header
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 // Ananya Misra (amisra@cs.princeton.edu)
32 // date: Summer 2005
33 //-----------------------------------------------------------------------------
34 #ifndef __MIDI_IO_H__
35 #define __MIDI_IO_H__
37 #include "chuck_def.h"
38 #include "rtmidi.h"
39 #include "util_buffers.h"
44 //-----------------------------------------------------------------------------
45 // stuff
46 //-----------------------------------------------------------------------------
47 #define MIDI_NOTEON 0x90
48 #define MIDI_NOTEOFF 0x80
49 #define MIDI_POLYPRESS 0xa0
50 #define MIDI_CTRLCHANGE 0xb0
51 #define MIDI_PROGCHANGE 0xc0
52 #define MIDI_CHANPRESS 0xd0
53 #define MIDI_PITCHBEND 0xe0
54 #define MIDI_ALLNOTESOFF 0x7b
59 //-----------------------------------------------------------------------------
60 // definitions
61 //-----------------------------------------------------------------------------
62 union MidiMsg
64 t_CKBYTE data[4];
65 // t_CKUINT dw;
71 //-----------------------------------------------------------------------------
72 // name: class MidiOut
73 // desc: midi out
74 //-----------------------------------------------------------------------------
75 class MidiOut
77 public:
78 MidiOut();
79 ~MidiOut();
81 public:
82 t_CKBOOL open( t_CKUINT device_num = 0 );
83 t_CKBOOL close();
84 t_CKBOOL good() { return m_valid; }
85 t_CKINT num() { return m_valid ? (t_CKINT)m_device_num : -1; }
87 public:
88 void set_suppress( t_CKBOOL print_or_not )
89 { m_suppress_output = print_or_not; }
90 t_CKBOOL get_suppress()
91 { return m_suppress_output; }
93 public:
94 t_CKUINT send( t_CKBYTE status );
95 t_CKUINT send( t_CKBYTE status, t_CKBYTE data1 );
96 t_CKUINT send( t_CKBYTE status, t_CKBYTE data1, t_CKBYTE data2 );
97 t_CKUINT send( const MidiMsg * msg );
99 public:
100 t_CKUINT noteon( t_CKUINT channel, t_CKUINT note, t_CKUINT velocity );
101 t_CKUINT noteoff( t_CKUINT channel, t_CKUINT note, t_CKUINT velocity );
102 t_CKUINT polypress( t_CKUINT channel, t_CKUINT note, t_CKUINT pressure );
103 t_CKUINT ctrlchange( t_CKUINT channel, t_CKUINT ctrl_num, t_CKUINT ctrl_val );
104 t_CKUINT progchange( t_CKUINT channel, t_CKUINT patch );
105 t_CKUINT chanpress( t_CKUINT channel, t_CKUINT pressure );
106 t_CKUINT pitchbend( t_CKUINT channel, t_CKUINT bend_val );
107 t_CKUINT allnotesoff( t_CKUINT channel );
109 public:
110 RtMidiOut * mout;
111 std::vector<unsigned char> m_msg;
112 t_CKUINT m_device_num;
113 t_CKBOOL m_valid;
114 t_CKBOOL m_suppress_output;
120 //-----------------------------------------------------------------------------
121 // name: class MidiIn
122 // desc: midi
123 //-----------------------------------------------------------------------------
124 class MidiIn : public Chuck_Event
126 public:
127 MidiIn();
128 ~MidiIn();
130 public:
131 t_CKBOOL open( t_CKUINT device_num = 0 );
132 t_CKBOOL close();
133 t_CKBOOL good() { return m_valid; }
134 t_CKINT num() { return m_valid ? (t_CKINT)m_device_num : -1; }
136 public:
137 void set_suppress( t_CKBOOL print_or_not )
138 { m_suppress_output = print_or_not; }
139 t_CKBOOL get_suppress()
140 { return m_suppress_output; }
142 public:
143 t_CKBOOL empty();
144 t_CKUINT recv( MidiMsg * msg );
146 public:
147 CBufferAdvance * m_buffer;
148 t_CKUINT m_read_index;
149 RtMidiIn * min;
150 t_CKBOOL m_valid;
151 t_CKUINT m_device_num;
152 Chuck_Object * SELF;
153 t_CKBOOL m_suppress_output;
157 void probeMidiIn();
158 void probeMidiOut();
161 class MidiInManager
163 public:
164 static t_CKBOOL open( MidiIn * min, t_CKINT device_num );
165 static t_CKBOOL close( MidiIn * min );
167 static void cb_midi_input( double deltatime, std::vector<unsigned char> * msg,
168 void *userData );
169 protected:
170 MidiInManager();
171 ~MidiInManager();
173 static std::vector<RtMidiIn *> the_mins;
174 static std::vector<CBufferAdvance *> the_bufs;
178 class MidiOutManager
180 public:
181 static t_CKBOOL open( MidiOut * mout, t_CKINT device_num );
182 static t_CKBOOL close( MidiOut * mout );
184 protected:
185 MidiOutManager();
186 ~MidiOutManager();
188 static std::vector<RtMidiOut *> the_mouts;
192 //-----------------------------------------------------------------------------
193 // name: class MidiRW
194 // desc: reads and writes midi messages from file
195 //-----------------------------------------------------------------------------
196 class MidiRW
198 public:
199 MidiRW();
200 ~MidiRW();
202 public:
203 t_CKBOOL open( const char * filename );
204 t_CKBOOL close();
206 public:
207 t_CKBOOL read( MidiMsg * msg, t_CKTIME * time );
208 t_CKBOOL write( MidiMsg * msg, t_CKTIME * time );
210 protected:
211 FILE * file;
214 // closes all MidiRW file handles
215 t_CKBOOL midirw_detach( );
218 //-----------------------------------------------------------------------------
219 // name: class MidiMsgOut
220 // desc: reads midi messages from file
221 //-----------------------------------------------------------------------------
222 class MidiMsgOut
224 public:
225 MidiMsgOut();
226 ~MidiMsgOut();
228 public:
229 t_CKBOOL open( const char * filename );
230 t_CKBOOL close();
232 public:
233 t_CKBOOL write( MidiMsg * msg, t_CKTIME * time );
235 protected:
236 FILE * file;
240 //-----------------------------------------------------------------------------
241 // name: class MidiMsgIn
242 // desc: reads midi messages from file
243 //-----------------------------------------------------------------------------
244 class MidiMsgIn
246 public:
247 MidiMsgIn();
248 ~MidiMsgIn();
250 public:
251 t_CKBOOL open( const char * filename );
252 t_CKBOOL close();
254 public:
255 t_CKBOOL read( MidiMsg * msg, t_CKTIME * time );
257 protected:
258 FILE * file;
262 #endif