Debian Codegraph @

src/core/midiio_rtmidi.h

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: midiio_rtmidi.h
28 // desc: midi io header
29 //
30 // author: Ge Wang ([email protected] | [email protected])
31 // Ananya Misra ([email protected])
32 // date: Summer 2005
33 //-----------------------------------------------------------------------------
34 #ifndef __MIDI_IO_H__
35 #define __MIDI_IO_H__
36
37 #include "chuck_def.h"
38 #ifndef __DISABLE_MIDI__
39 #include "rtmidi.h"
40 #endif
41 #include "util_buffers.h"
42
43
44
45
46 //-----------------------------------------------------------------------------
47 // stuff
48 //-----------------------------------------------------------------------------
49 #define MIDI_NOTEON 0x90
50 #define MIDI_NOTEOFF 0x80
51 #define MIDI_POLYPRESS 0xa0
52 #define MIDI_CTRLCHANGE 0xb0
53 #define MIDI_PROGCHANGE 0xc0
54 #define MIDI_CHANPRESS 0xd0
55 #define MIDI_PITCHBEND 0xe0
56 #define MIDI_ALLNOTESOFF 0x7b
57
58
59
60
61 //-----------------------------------------------------------------------------
62 // definitions
63 //-----------------------------------------------------------------------------
64 union MidiMsg
65 {
66 t_CKBYTE data[4];
67 // t_CKUINT dw;
68 };
69
70
71
72
73 // forward reference
74 class RtMidiOut;
75 class RtMidiIn;
76
77
78
79
80 //-----------------------------------------------------------------------------
81 // name: class MidiOut
82 // desc: midi out
83 //-----------------------------------------------------------------------------
84 class MidiOut
85 {
86 public:
87 MidiOut();
88 ~MidiOut();
89
90 public:
91 t_CKBOOL open( t_CKUINT device_num = 0 );
92 t_CKBOOL open( const std::string & name );
93 t_CKBOOL close();
94 t_CKBOOL good() { return m_valid; }
95 t_CKINT num() { return m_valid ? (t_CKINT)m_device_num : -1; }
96
97 public:
98 void set_suppress( t_CKBOOL print_or_not )
99 { m_suppress_output = print_or_not; }
100 t_CKBOOL get_suppress()
101 { return m_suppress_output; }
102
103 public:
104 t_CKUINT send( t_CKBYTE status );
105 t_CKUINT send( t_CKBYTE status, t_CKBYTE data1 );
106 t_CKUINT send( t_CKBYTE status, t_CKBYTE data1, t_CKBYTE data2 );
107 t_CKUINT send( const MidiMsg * msg );
108
109 public:
110 t_CKUINT noteon( t_CKUINT channel, t_CKUINT note, t_CKUINT velocity );
111 t_CKUINT noteoff( t_CKUINT channel, t_CKUINT note, t_CKUINT velocity );
112 t_CKUINT polypress( t_CKUINT channel, t_CKUINT note, t_CKUINT pressure );
113 t_CKUINT ctrlchange( t_CKUINT channel, t_CKUINT ctrl_num, t_CKUINT ctrl_val );
114 t_CKUINT progchange( t_CKUINT channel, t_CKUINT patch );
115 t_CKUINT chanpress( t_CKUINT channel, t_CKUINT pressure );
116 t_CKUINT pitchbendFine( t_CKUINT channel, t_CKUINT lsb, t_CKUINT msb );
117 t_CKUINT pitchbend( t_CKUINT channel, t_CKUINT bend_val );
118 t_CKUINT allnotesoff( t_CKUINT channel );
119
120 public:
121 RtMidiOut * mout;
122 std::vector<unsigned char> m_msg;
123 t_CKUINT m_device_num;
124 t_CKBOOL m_valid;
125 t_CKBOOL m_suppress_output;
126 };
127
128
129
130
131 //-----------------------------------------------------------------------------
132 // name: class MidiIn
133 // desc: midi
134 //-----------------------------------------------------------------------------
135 class MidiIn : public Chuck_Event
136 {
137 public:
138 MidiIn();
139 ~MidiIn();
140
141 public:
142 t_CKBOOL open( Chuck_VM * vm, t_CKUINT device_num = 0 );
143 t_CKBOOL open( Chuck_VM * vm, const std::string & name );
144 t_CKBOOL close();
145 t_CKBOOL good() { return m_valid; }
146 t_CKINT num() { return m_valid ? (t_CKINT)m_device_num : -1; }
147
148 public:
149 void set_suppress( t_CKBOOL print_or_not )
150 { m_suppress_output = print_or_not; }
151 t_CKBOOL get_suppress()
152 { return m_suppress_output; }
153
154 public:
155 t_CKBOOL empty();
156 t_CKUINT recv( MidiMsg * msg );
157
158 public:
159 CBufferAdvance * m_buffer;
160 t_CKUINT m_read_index;
161 RtMidiIn * min;
162 t_CKBOOL m_valid;
163 t_CKUINT m_device_num;
164 Chuck_Object * SELF;
165 t_CKBOOL m_suppress_output;
166 };
167
168
169 void probeMidiIn();
170 void probeMidiOut();
171
172
173
174
175 //-----------------------------------------------------------------------------
176 // name: class MidiInManager
177 // desc: singleton manager
178 //-----------------------------------------------------------------------------
179 class MidiInManager
180 {
181 public:
182 static t_CKBOOL open( MidiIn * min, Chuck_VM * vm, t_CKINT device_num );
183 static t_CKBOOL open( MidiIn * min, Chuck_VM * vm, const std::string & name );
184 static t_CKBOOL close( MidiIn * min );
185 static void cleanup_buffer( Chuck_VM * vm );
186
187 static void cb_midi_input( double deltatime, std::vector<unsigned char> * msg,
188 void *userData );
189 protected:
190 MidiInManager();
191 ~MidiInManager();
192
193 static t_CKBOOL add_vm( Chuck_VM * vm, t_CKINT device_num, t_CKBOOL suppress_output );
194
195 static std::vector<RtMidiIn *> the_mins;
196 static std::vector< std::map< Chuck_VM *, CBufferAdvance * > > the_bufs;
197
198 public:
199 static std::map< Chuck_VM *, CBufferSimple * > m_event_buffers;
200 };
201
202
203
204
205 //-----------------------------------------------------------------------------
206 // name: class MidiOutManager
207 // desc: singleton manager
208 //-----------------------------------------------------------------------------
209 class MidiOutManager
210 {
211 public:
212 static t_CKBOOL open( MidiOut * mout, t_CKINT device_num );
213 static t_CKBOOL open( MidiOut * mout, const std::string & name );
214 static t_CKBOOL close( MidiOut * mout );
215
216 protected:
217 MidiOutManager();
218 ~MidiOutManager();
219
220 static std::vector<RtMidiOut *> the_mouts;
221 };
222
223
224
225
226 //-----------------------------------------------------------------------------
227 // name: class MidiRW
228 // desc: reads and writes midi messages from file
229 //-----------------------------------------------------------------------------
230 class MidiRW
231 {
232 public:
233 MidiRW();
234 ~MidiRW();
235
236 public:
237 t_CKBOOL open( const char * filename );
238 t_CKBOOL close();
239
240 public:
241 t_CKBOOL read( MidiMsg * msg, t_CKTIME * time );
242 t_CKBOOL write( MidiMsg * msg, t_CKTIME * time );
243
244 protected:
245 FILE * file;
246 };
247
248 // closes all MidiRW file handles
249 t_CKBOOL midirw_detach( );
250
251
252
253
254 //-----------------------------------------------------------------------------
255 // name: class MidiMsgOut
256 // desc: reads midi messages from file
257 //-----------------------------------------------------------------------------
258 class MidiMsgOut
259 {
260 public:
261 MidiMsgOut();
262 ~MidiMsgOut();
263
264 public:
265 t_CKBOOL open( const char * filename );
266 t_CKBOOL close();
267
268 public:
269 t_CKBOOL write( MidiMsg * msg, t_CKTIME * time );
270
271 protected:
272 FILE * file;
273 };
274
275
276
277
278 //-----------------------------------------------------------------------------
279 // name: class MidiMsgIn
280 // desc: reads midi messages from file
281 //-----------------------------------------------------------------------------
282 class MidiMsgIn
283 {
284 public:
285 MidiMsgIn();
286 ~MidiMsgIn();
287
288 public:
289 t_CKBOOL open( const char * filename );
290 t_CKBOOL close();
291
292 public:
293 t_CKBOOL read( MidiMsg * msg, t_CKTIME * time );
294
295 protected:
296 FILE * file;
297 };
298
299
300
301
302 //-----------------------------------------------------------------------------
303 // name: adapted from MCD/y-score-reader.h
304 // desc: midi score reader/data structures
305 //
306 // authors: Ge Wang ([email protected])
307 // Tom Lieber
308 //
309 // date: Winter 2010
310 // version: 1.1
311 //-----------------------------------------------------------------------------
312 #include <vector>
313 #include <deque>
314 #include <string>
315 #include <map>
316
317
318
319
320 //-----------------------------------------------------------------------------
321 // name: struct MidiNoteEvent
322 // desc: a MIDI note event, parsed for start and end time, with simultaneous
323 // notes combined into the 'simultaneous' linked list
324 //-----------------------------------------------------------------------------
325 struct MidiNoteEvent
326 {
327 // data
328 unsigned short data1;
329 unsigned short data2;
330 unsigned short data3;
331 // time data
332 double time;
333 double untilNext;
334 // link to simultaneous events
335 MidiNoteEvent * simultaneous;
336 // link to parent (could be self)
337 MidiNoteEvent * parent;
338
339 // more data
340 double startTime;
341 double endTime;
342 bool shouldBend;
343 int bendAmount;
344 int programChange;
345 bool phrasemark;
346
347 // constructor
348 MidiNoteEvent() : data1(0), data2(0), data3(0), time(0.0), untilNext(0.0),
349 simultaneous(NULL), parent(NULL), startTime(0), endTime(0), shouldBend(false),
350 bendAmount(0), programChange(0), phrasemark(false) { }
351
352 // type of message
353 int type() const { return data1 & 0x70; }
354 // channel
355 int getChannel() const { return data1 & 0x0f; }
356 };
357
358
359
360
361 //-----------------------------------------------------------------------------
362 // name: struct MidiLyricEvent
363 // desc: a MIDI lyric event, with "end" time given by the time of the next
364 // event in the list
365 //-----------------------------------------------------------------------------
366 struct MidiLyricEvent
367 {
368 std::string lyric;
369 // time data
370 double time;
371 double endTime;
372
373 // constructor
374 MidiLyricEvent() : time(0), endTime(0) { }
375 };
376
377
378
379
380 // forward references
381 namespace stk { class MidiFileIn; }
382
383
384
385
386 //-----------------------------------------------------------------------------
387 // name: class YScoreReader()
388 // desc: loads a MIDI score
389 //-----------------------------------------------------------------------------
390 class MidiScoreReader
391 {
392 public:
393 MidiScoreReader();
394 ~MidiScoreReader();
395
396 public:
397 bool load( const char * filename, float velScale = 0.0f );
398 void cleanup();
399
400 public:
401 // rewind to beginning
402 void rewind();
403 // get current top event (NO simultaneous)
404 const MidiNoteEvent * getTopEvent( t_CKINT track, t_CKINT offset = 0 );
405 // get current event (YES simultaneous) + move to next
406 const MidiNoteEvent * scanEvent( t_CKINT track, bool & isNewSet );
407 // advance to next event
408 bool seek( t_CKINT track, t_CKINT numToAdvance = 1 );
409 // advance to next note on
410 bool seekToNoteOn( t_CKINT track );
411 // get notes in a time window
412 void getEvents( t_CKINT track, double startTime, double endTime, std::vector<const MidiNoteEvent *> & result,
413 bool includeSimultaneous = false );
414 // isDone
415 bool isDone( t_CKINT track, double currTime );
416
417
418 public:
419 // get number of tracks
420 t_CKINT getNumTracks() const;
421 // get number of events per track
422 t_CKINT getNumEvents( t_CKINT track ) const;
423 // get number of tracks with more than 0 events
424 t_CKINT getNumTracksNonZero() const;
425 // get a vector of tracks indices that have more than 0 events
426 const std::vector<t_CKINT> & getTracksNonZero() const;
427 // get BPM
428 double getBPM() const;
429 // get an entire track's note events
430 const std::vector<MidiNoteEvent *> & getNoteEvents( t_CKINT track );
431 // get an entire track's lyric events
432 const std::vector<MidiLyricEvent *> & getLyricEvents( t_CKINT track );
433 // get count
434 t_CKINT getCount( t_CKINT track, const std::string & key );
435 // get lowest note for a track
436 t_CKINT getLowestNote( t_CKINT track, const MidiNoteEvent *start );
437 // get highest note for a track
438 t_CKINT getHighestNote( t_CKINT track, const MidiNoteEvent *start );
439
440 public: // TODO: these may as well be a separate object
441 // push a event to remember
442 void enqueue( const MidiNoteEvent * event );
443 // get the front event
444 const MidiNoteEvent * front() const;
445 // dequeue the event
446 void dequeue();
447 // push to front
448 void enqueue_front( const MidiNoteEvent * event );
449
450 public: // track naming
451 // sets track name
452 void setTrackName( t_CKINT track, const std::string & name );
453 // gets track name ("" if not present)
454 std::string getTrackName( t_CKINT track );
455 // gets the track with the given name (-1 if not present)
456 t_CKINT getTrackForName( const std::string & name );
457
458 protected: // for use while loading a track
459 // load a track
460 bool loadTrack( t_CKINT track, std::vector<MidiNoteEvent *> & data, std::vector<MidiLyricEvent *> & lyricData );
461 // apply control to existing event
462 bool applyControl( t_CKINT track, t_CKINT data2, t_CKINT data3, MidiNoteEvent * e );
463 // increment count for the given key (used for counting the number of notes, glissandi, etc)
464 void incrementCount( t_CKINT track, const std::string & key );
465 // handle note events, managing m_activeNotes and setting the event's endTime
466 void handleNoteOn( MidiNoteEvent * e, t_CKINT note );
467 void handleNoteOff( t_CKINT note, double time );
468
469 protected:
470 // midi
471 stk::MidiFileIn * m_midiFile;
472 // vector of tracks of events
473 std::vector< std::vector<MidiNoteEvent *> > m_events;
474 // vector of tracks of events
475 std::vector< std::vector<MidiLyricEvent *> > m_lyricEvents;
476 // vector of indices
477 std::vector<t_CKUINT> m_indices;
478 // stack of events
479 std::deque<const MidiNoteEvent *> m_queue;
480 // vector of event
481 std::vector<const MidiNoteEvent *> m_result;
482 // search pointer
483 std::vector<t_CKUINT> m_searchIndices;
484 // count map
485 std::vector< std::map< std::string, t_CKINT > > m_countMaps;
486 // HACK: scale the velocity (0-1)
487 float m_velocity_scale;
488 // track names
489 std::map<std::string, t_CKINT> m_nameToTrack;
490 std::map<t_CKINT, std::string> m_trackToName;
491 // number of non-zero tracks
492 t_CKINT m_numNonZeroTracks;
493 // vector of indices of non-zero tracks
494 std::vector<t_CKINT> m_nonZeroTrackIndices;
495
496 private: // used during track loading
497 // active notes, map from MIDI note to event
498 std::map<t_CKINT, MidiNoteEvent *> m_activeNotes;
499 };
500
501
502
503
504 #endif