| 1 | /* |
|---|
| 2 | * libjdkmidi-2004 C++ Class Library for MIDI |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2004 J.D. Koftinoff Software, Ltd. |
|---|
| 5 | * www.jdkoftinoff.com |
|---|
| 6 | * jeffk@jdkoftinoff.com |
|---|
| 7 | * |
|---|
| 8 | * *** RELEASED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) April 27, 2004 *** |
|---|
| 9 | * |
|---|
| 10 | * This program is free software; you can redistribute it and/or modify |
|---|
| 11 | * it under the terms of the GNU General Public License as published by |
|---|
| 12 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 13 | * (at your option) any later version. |
|---|
| 14 | * |
|---|
| 15 | * This program is distributed in the hope that it will be useful, |
|---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | * GNU General Public License for more details. |
|---|
| 19 | * |
|---|
| 20 | * You should have received a copy of the GNU General Public License |
|---|
| 21 | * along with this program; if not, write to the Free Software |
|---|
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 23 | */ |
|---|
| 24 | #ifndef JDKMIDI_SEQUENCER_H |
|---|
| 25 | #define JDKMIDI_SEQUENCER_H |
|---|
| 26 | |
|---|
| 27 | #include "jdkmidi/track.h" |
|---|
| 28 | #include "jdkmidi/multitrack.h" |
|---|
| 29 | #include "jdkmidi/tempo.h" |
|---|
| 30 | #include "jdkmidi/matrix.h" |
|---|
| 31 | #include "jdkmidi/process.h" |
|---|
| 32 | |
|---|
| 33 | namespace jdkmidi |
|---|
| 34 | { |
|---|
| 35 | |
|---|
| 36 | class MIDISequencerGUIEvent; |
|---|
| 37 | class MIDISequencerGUIEventNotifier; |
|---|
| 38 | class MIDISequencerTrackState; |
|---|
| 39 | class MIDISequencer; |
|---|
| 40 | |
|---|
| 41 | class MIDISequencerGUIEvent |
|---|
| 42 | { |
|---|
| 43 | public: |
|---|
| 44 | MIDISequencerGUIEvent() |
|---|
| 45 | { |
|---|
| 46 | bits=0; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | MIDISequencerGUIEvent ( unsigned long bits_ ) |
|---|
| 50 | : bits ( bits_ ) |
|---|
| 51 | { |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | MIDISequencerGUIEvent ( const MIDISequencerGUIEvent &e ) |
|---|
| 55 | : bits ( e.bits ) |
|---|
| 56 | { |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | MIDISequencerGUIEvent ( int group, int subgroup, int item=0 ) |
|---|
| 60 | { |
|---|
| 61 | bits = ( ( group&0xff ) <<24 ) |
|---|
| 62 | | ( ( subgroup&0xfff ) <<12 ) |
|---|
| 63 | | ( ( item&0xfff ) <<0 ); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | operator unsigned long () const |
|---|
| 67 | { |
|---|
| 68 | return bits; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | void SetEvent ( int group, int subgroup = 0, int item = 0 ) |
|---|
| 72 | { |
|---|
| 73 | bits = ( ( group&0xff ) <<24 ) |
|---|
| 74 | | ( ( subgroup&0xfff ) <<12 ) |
|---|
| 75 | | ( ( item&0xfff ) <<0 ); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | int GetEventGroup() const |
|---|
| 79 | { |
|---|
| 80 | return ( int ) ( ( bits>>24 ) &0xff ); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | int GetEventSubGroup() const |
|---|
| 84 | { |
|---|
| 85 | return ( int ) ( ( bits>>12 ) &0xfff ); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | int GetEventItem() const |
|---|
| 89 | { |
|---|
| 90 | return ( int ) ( ( bits>>0 ) &0xfff ); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | // main groups |
|---|
| 94 | enum |
|---|
| 95 | { |
|---|
| 96 | GROUP_ALL = 0, |
|---|
| 97 | GROUP_CONDUCTOR, |
|---|
| 98 | GROUP_TRANSPORT, |
|---|
| 99 | GROUP_TRACK |
|---|
| 100 | }; |
|---|
| 101 | |
|---|
| 102 | // items in conductor group |
|---|
| 103 | enum |
|---|
| 104 | { |
|---|
| 105 | GROUP_CONDUCTOR_ALL = 0, |
|---|
| 106 | GROUP_CONDUCTOR_TEMPO, |
|---|
| 107 | GROUP_CONDUCTOR_TIMESIG |
|---|
| 108 | }; |
|---|
| 109 | |
|---|
| 110 | // items in transport group |
|---|
| 111 | enum |
|---|
| 112 | { |
|---|
| 113 | GROUP_TRANSPORT_ALL = 0, |
|---|
| 114 | GROUP_TRANSPORT_MODE, |
|---|
| 115 | GROUP_TRANSPORT_MEASURE, |
|---|
| 116 | GROUP_TRANSPORT_BEAT, |
|---|
| 117 | GROUP_TRANSPORT_ENDOFSONG |
|---|
| 118 | }; |
|---|
| 119 | |
|---|
| 120 | // items in TRACK group |
|---|
| 121 | enum |
|---|
| 122 | { |
|---|
| 123 | GROUP_TRACK_ALL = 0, |
|---|
| 124 | GROUP_TRACK_NAME, |
|---|
| 125 | GROUP_TRACK_PG, |
|---|
| 126 | GROUP_TRACK_NOTE, |
|---|
| 127 | GROUP_TRACK_VOLUME |
|---|
| 128 | }; |
|---|
| 129 | |
|---|
| 130 | private: |
|---|
| 131 | unsigned long bits; |
|---|
| 132 | }; |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | class MIDISequencerGUIEventNotifier |
|---|
| 136 | { |
|---|
| 137 | public: |
|---|
| 138 | MIDISequencerGUIEventNotifier(); |
|---|
| 139 | |
|---|
| 140 | virtual ~MIDISequencerGUIEventNotifier(); |
|---|
| 141 | |
|---|
| 142 | virtual void Notify ( const MIDISequencer *seq, MIDISequencerGUIEvent e ) = 0; |
|---|
| 143 | virtual bool GetEnable() const = 0; |
|---|
| 144 | virtual void SetEnable ( bool f ) = 0; |
|---|
| 145 | }; |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | class MIDISequencerGUIEventNotifierText : |
|---|
| 149 | public MIDISequencerGUIEventNotifier |
|---|
| 150 | { |
|---|
| 151 | public: |
|---|
| 152 | MIDISequencerGUIEventNotifierText ( FILE *f ); |
|---|
| 153 | |
|---|
| 154 | virtual ~MIDISequencerGUIEventNotifierText(); |
|---|
| 155 | |
|---|
| 156 | virtual void Notify ( const MIDISequencer *seq, MIDISequencerGUIEvent e ); |
|---|
| 157 | virtual bool GetEnable() const; |
|---|
| 158 | virtual void SetEnable ( bool f ); |
|---|
| 159 | |
|---|
| 160 | private: |
|---|
| 161 | |
|---|
| 162 | FILE *f; |
|---|
| 163 | bool en; |
|---|
| 164 | }; |
|---|
| 165 | |
|---|
| 166 | class MIDISequencerTrackNotifier : public MIDIProcessor |
|---|
| 167 | { |
|---|
| 168 | public: |
|---|
| 169 | MIDISequencerTrackNotifier ( |
|---|
| 170 | MIDISequencer *seq_, |
|---|
| 171 | int trk, |
|---|
| 172 | MIDISequencerGUIEventNotifier *n |
|---|
| 173 | ); |
|---|
| 174 | |
|---|
| 175 | virtual ~MIDISequencerTrackNotifier(); |
|---|
| 176 | |
|---|
| 177 | void SetNotifier ( |
|---|
| 178 | MIDISequencer *seq_, |
|---|
| 179 | int trk, |
|---|
| 180 | MIDISequencerGUIEventNotifier *n |
|---|
| 181 | ) |
|---|
| 182 | { |
|---|
| 183 | seq = seq_; |
|---|
| 184 | track_num = trk; |
|---|
| 185 | notifier=n; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | void Notify ( int item ); |
|---|
| 190 | void NotifyConductor ( int item ); |
|---|
| 191 | |
|---|
| 192 | private: |
|---|
| 193 | MIDISequencer *seq; |
|---|
| 194 | int track_num; |
|---|
| 195 | MIDISequencerGUIEventNotifier *notifier; |
|---|
| 196 | }; |
|---|
| 197 | |
|---|
| 198 | class MIDISequencerTrackProcessor : public MIDIProcessor |
|---|
| 199 | { |
|---|
| 200 | public: |
|---|
| 201 | MIDISequencerTrackProcessor(); |
|---|
| 202 | virtual ~MIDISequencerTrackProcessor(); |
|---|
| 203 | |
|---|
| 204 | virtual void Reset(); |
|---|
| 205 | virtual bool Process ( MIDITimedBigMessage *msg ); |
|---|
| 206 | |
|---|
| 207 | bool mute; // track is muted |
|---|
| 208 | bool solo; // track is solod |
|---|
| 209 | int velocity_scale; // current velocity scale value for note ons, 100=normal |
|---|
| 210 | int rechannel; // rechannelization value, or -1 for none |
|---|
| 211 | int transpose; // amount to transpose note values |
|---|
| 212 | |
|---|
| 213 | MIDIProcessor *extra_proc; // extra midi processing for this track |
|---|
| 214 | }; |
|---|
| 215 | |
|---|
| 216 | class MIDISequencerTrackState : public MIDISequencerTrackNotifier |
|---|
| 217 | { |
|---|
| 218 | public: |
|---|
| 219 | |
|---|
| 220 | MIDISequencerTrackState ( |
|---|
| 221 | MIDISequencer *seq_, |
|---|
| 222 | int trk, |
|---|
| 223 | MIDISequencerGUIEventNotifier *n |
|---|
| 224 | ); |
|---|
| 225 | virtual ~MIDISequencerTrackState(); |
|---|
| 226 | |
|---|
| 227 | virtual void GoToZero(); |
|---|
| 228 | virtual void Reset(); |
|---|
| 229 | virtual bool Process ( MIDITimedBigMessage *msg ); |
|---|
| 230 | |
|---|
| 231 | float tempobpm; // current tempo in beats per minute |
|---|
| 232 | int pg; // current program change, or -1 |
|---|
| 233 | int volume; // current volume controller value |
|---|
| 234 | int timesig_numerator; // numerator of current time signature |
|---|
| 235 | int timesig_denominator; // denominator of current time signature |
|---|
| 236 | int bender_value; // last seen bender value |
|---|
| 237 | char track_name[256]; // track name |
|---|
| 238 | bool got_good_track_name; // true if we dont want to use generic text events for track name |
|---|
| 239 | |
|---|
| 240 | bool notes_are_on; // true if there are any notes currently on |
|---|
| 241 | MIDIMatrix note_matrix; // to keep track of all notes on |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | }; |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | class MIDISequencerState |
|---|
| 248 | { |
|---|
| 249 | public: |
|---|
| 250 | MIDISequencerState ( MIDISequencer *s, |
|---|
| 251 | MIDIMultiTrack *multitrack_, |
|---|
| 252 | MIDISequencerGUIEventNotifier *n ); |
|---|
| 253 | |
|---|
| 254 | MIDISequencerState ( const MIDISequencerState &s ); |
|---|
| 255 | ~MIDISequencerState(); |
|---|
| 256 | |
|---|
| 257 | const MIDISequencerState & operator = ( const MIDISequencerState &s ); |
|---|
| 258 | |
|---|
| 259 | MIDISequencerGUIEventNotifier *notifier; |
|---|
| 260 | MIDIMultiTrack *multitrack; |
|---|
| 261 | int num_tracks; |
|---|
| 262 | |
|---|
| 263 | MIDISequencerTrackState *track_state[64]; |
|---|
| 264 | MIDIMultiTrackIterator iterator; |
|---|
| 265 | MIDIClockTime cur_clock; |
|---|
| 266 | float cur_time_ms; |
|---|
| 267 | int cur_beat; |
|---|
| 268 | int cur_measure; |
|---|
| 269 | MIDIClockTime next_beat_time; |
|---|
| 270 | }; |
|---|
| 271 | |
|---|
| 272 | class MIDISequencer |
|---|
| 273 | { |
|---|
| 274 | public: |
|---|
| 275 | |
|---|
| 276 | MIDISequencer ( |
|---|
| 277 | MIDIMultiTrack *m, |
|---|
| 278 | MIDISequencerGUIEventNotifier *n=0 |
|---|
| 279 | ); |
|---|
| 280 | |
|---|
| 281 | virtual ~MIDISequencer(); |
|---|
| 282 | |
|---|
| 283 | void ResetTrack ( int trk ); |
|---|
| 284 | void ResetAllTracks(); |
|---|
| 285 | |
|---|
| 286 | MIDIClockTime GetCurrentMIDIClockTime() const; |
|---|
| 287 | double GetCurrentTimeInMs() const; |
|---|
| 288 | int GetCurrentBeat() const; |
|---|
| 289 | int GetCurrentMeasure() const; |
|---|
| 290 | |
|---|
| 291 | double GetCurrentTempoScale() const; |
|---|
| 292 | double GetCurrentTempo() const; |
|---|
| 293 | |
|---|
| 294 | MIDISequencerState *GetState(); |
|---|
| 295 | const MIDISequencerState *GetState() const; |
|---|
| 296 | |
|---|
| 297 | void SetState ( MIDISequencerState * ); |
|---|
| 298 | |
|---|
| 299 | MIDISequencerTrackState * GetTrackState ( int trk ); |
|---|
| 300 | const MIDISequencerTrackState * GetTrackState ( int trk ) const; |
|---|
| 301 | |
|---|
| 302 | MIDISequencerTrackProcessor * GetTrackProcessor ( int trk ); |
|---|
| 303 | const MIDISequencerTrackProcessor * GetTrackProcessor ( int trk ) const; |
|---|
| 304 | |
|---|
| 305 | int GetNumTracks() const |
|---|
| 306 | { |
|---|
| 307 | return state.num_tracks; |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | bool GetSoloMode() const; |
|---|
| 311 | |
|---|
| 312 | void SetCurrentTempoScale ( float scale ); |
|---|
| 313 | void SetSoloMode ( bool m, int trk=-1 ); |
|---|
| 314 | |
|---|
| 315 | void GoToZero(); |
|---|
| 316 | bool GoToTime ( MIDIClockTime time_clk ); |
|---|
| 317 | bool GoToTimeMs ( float time_ms ); |
|---|
| 318 | bool GoToMeasure ( int measure, int beat=0 ); |
|---|
| 319 | |
|---|
| 320 | bool GetNextEventTimeMs ( float *t ); |
|---|
| 321 | bool GetNextEventTime ( MIDIClockTime *t ); |
|---|
| 322 | bool GetNextEvent ( int *tracknum, MIDITimedBigMessage *msg ); |
|---|
| 323 | |
|---|
| 324 | void ScanEventsAtThisTime(); |
|---|
| 325 | |
|---|
| 326 | protected: |
|---|
| 327 | |
|---|
| 328 | MIDITimedBigMessage beat_marker_msg; |
|---|
| 329 | |
|---|
| 330 | bool solo_mode; |
|---|
| 331 | int tempo_scale; |
|---|
| 332 | |
|---|
| 333 | int num_tracks; |
|---|
| 334 | MIDISequencerTrackProcessor *track_processors[64]; |
|---|
| 335 | |
|---|
| 336 | MIDISequencerState state; |
|---|
| 337 | |
|---|
| 338 | } ; |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | #endif |
|---|