| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 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 |
|
| 63 |
|
| 64 |
union MidiMsg |
| 65 |
{ |
| 66 |
t_CKBYTE data[4]; |
| 67 |
|
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
class RtMidiOut; |
| 75 |
class RtMidiIn; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 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 |
|
| 133 |
|
| 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 |
|
| 177 |
|
| 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 |
|
| 207 |
|
| 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 |
|
| 228 |
|
| 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 |
|
| 249 |
t_CKBOOL midirw_detach( ); |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 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 |
|
| 280 |
|
| 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 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
#include <vector> |
| 313 |
#include <deque> |
| 314 |
#include <string> |
| 315 |
#include <map> |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
struct MidiNoteEvent |
| 326 |
{ |
| 327 |
|
| 328 |
unsigned short data1; |
| 329 |
unsigned short data2; |
| 330 |
unsigned short data3; |
| 331 |
|
| 332 |
double time; |
| 333 |
double untilNext; |
| 334 |
|
| 335 |
MidiNoteEvent * simultaneous; |
| 336 |
|
| 337 |
MidiNoteEvent * parent; |
| 338 |
|
| 339 |
|
| 340 |
double startTime; |
| 341 |
double endTime; |
| 342 |
bool shouldBend; |
| 343 |
int bendAmount; |
| 344 |
int programChange; |
| 345 |
bool phrasemark; |
| 346 |
|
| 347 |
|
| 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 |
|
| 353 |
int type() const { return data1 & 0x70; } |
| 354 |
|
| 355 |
int getChannel() const { return data1 & 0x0f; } |
| 356 |
}; |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
struct MidiLyricEvent |
| 367 |
{ |
| 368 |
std::string lyric; |
| 369 |
|
| 370 |
double time; |
| 371 |
double endTime; |
| 372 |
|
| 373 |
|
| 374 |
MidiLyricEvent() : time(0), endTime(0) { } |
| 375 |
}; |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
namespace stk { class MidiFileIn; } |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 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 |
|
| 402 |
void rewind(); |
| 403 |
|
| 404 |
const MidiNoteEvent * getTopEvent( t_CKINT track, t_CKINT offset = 0 ); |
| 405 |
|
| 406 |
const MidiNoteEvent * scanEvent( t_CKINT track, bool & isNewSet ); |
| 407 |
|
| 408 |
bool seek( t_CKINT track, t_CKINT numToAdvance = 1 ); |
| 409 |
|
| 410 |
bool seekToNoteOn( t_CKINT track ); |
| 411 |
|
| 412 |
void getEvents( t_CKINT track, double startTime, double endTime, std::vector<const MidiNoteEvent *> & result, |
| 413 |
bool includeSimultaneous = false ); |
| 414 |
|
| 415 |
bool isDone( t_CKINT track, double currTime ); |
| 416 |
|
| 417 |
|
| 418 |
public: |
| 419 |
|
| 420 |
t_CKINT getNumTracks() const; |
| 421 |
|
| 422 |
t_CKINT getNumEvents( t_CKINT track ) const; |
| 423 |
|
| 424 |
t_CKINT getNumTracksNonZero() const; |
| 425 |
|
| 426 |
const std::vector<t_CKINT> & getTracksNonZero() const; |
| 427 |
|
| 428 |
double getBPM() const; |
| 429 |
|
| 430 |
const std::vector<MidiNoteEvent *> & getNoteEvents( t_CKINT track ); |
| 431 |
|
| 432 |
const std::vector<MidiLyricEvent *> & getLyricEvents( t_CKINT track ); |
| 433 |
|
| 434 |
t_CKINT getCount( t_CKINT track, const std::string & key ); |
| 435 |
|
| 436 |
t_CKINT getLowestNote( t_CKINT track, const MidiNoteEvent *start ); |
| 437 |
|
| 438 |
t_CKINT getHighestNote( t_CKINT track, const MidiNoteEvent *start ); |
| 439 |
|
| 440 |
public: |
| 441 |
|
| 442 |
void enqueue( const MidiNoteEvent * event ); |
| 443 |
|
| 444 |
const MidiNoteEvent * front() const; |
| 445 |
|
| 446 |
void dequeue(); |
| 447 |
|
| 448 |
void enqueue_front( const MidiNoteEvent * event ); |
| 449 |
|
| 450 |
public: |
| 451 |
|
| 452 |
void setTrackName( t_CKINT track, const std::string & name ); |
| 453 |
|
| 454 |
std::string getTrackName( t_CKINT track ); |
| 455 |
|
| 456 |
t_CKINT getTrackForName( const std::string & name ); |
| 457 |
|
| 458 |
protected: |
| 459 |
|
| 460 |
bool loadTrack( t_CKINT track, std::vector<MidiNoteEvent *> & data, std::vector<MidiLyricEvent *> & lyricData ); |
| 461 |
|
| 462 |
bool applyControl( t_CKINT track, t_CKINT data2, t_CKINT data3, MidiNoteEvent * e ); |
| 463 |
|
| 464 |
void incrementCount( t_CKINT track, const std::string & key ); |
| 465 |
|
| 466 |
void handleNoteOn( MidiNoteEvent * e, t_CKINT note ); |
| 467 |
void handleNoteOff( t_CKINT note, double time ); |
| 468 |
|
| 469 |
protected: |
| 470 |
|
| 471 |
stk::MidiFileIn * m_midiFile; |
| 472 |
|
| 473 |
std::vector< std::vector<MidiNoteEvent *> > m_events; |
| 474 |
|
| 475 |
std::vector< std::vector<MidiLyricEvent *> > m_lyricEvents; |
| 476 |
|
| 477 |
std::vector<t_CKUINT> m_indices; |
| 478 |
|
| 479 |
std::deque<const MidiNoteEvent *> m_queue; |
| 480 |
|
| 481 |
std::vector<const MidiNoteEvent *> m_result; |
| 482 |
|
| 483 |
std::vector<t_CKUINT> m_searchIndices; |
| 484 |
|
| 485 |
std::vector< std::map< std::string, t_CKINT > > m_countMaps; |
| 486 |
|
| 487 |
float m_velocity_scale; |
| 488 |
|
| 489 |
std::map<std::string, t_CKINT> m_nameToTrack; |
| 490 |
std::map<t_CKINT, std::string> m_trackToName; |
| 491 |
|
| 492 |
t_CKINT m_numNonZeroTracks; |
| 493 |
|
| 494 |
std::vector<t_CKINT> m_nonZeroTrackIndices; |
| 495 |
|
| 496 |
private: |
| 497 |
|
| 498 |
std::map<t_CKINT, MidiNoteEvent *> m_activeNotes; |
| 499 |
}; |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
#endif |