summaryrefslogtreecommitdiffstats
path: root/libkmid/track.h
blob: 073fc9825588c52b0c4edb075c1540968194b153 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*  track.h  - class track, which has a midi file track and its events
    This file is part of LibKMid 0.9.5
    Copyright (C) 1997,98,99,2000  Antonio Larrosa Jimenez
    LibKMid's homepage : http://www.arrakis.es/~rlarrosa/libkmid.html

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.
 
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.                                                  

    Send comments and bug fixes to Antonio Larrosa <larrosa@kde.org>

***************************************************************************/
#ifndef _TRACK_H
#define _TRACK_H

#include <stdio.h>
#include <libkmid/dattypes.h>

/**
 * An structure that represents a MIDI event.
 *
 * @short Represents a MIDI event
 * @version 0.9.5 17/01/2000
 * @author Antonio Larrosa Jimenez <larrosa@kde.org>
 */ 
struct MidiEvent
{
  /**
   * MIDI Command
   * 
   * Caution, if a command doesn't use a variable, it may contain garbage.
   */
  uchar	command;

  /**
   * Channel
   */
  uchar   chn;

  /**
   * Note
   */
  uchar   note;

  /**
   * Velocity
   */
  uchar   vel;

  /**
   * Patch (if command was a change patch command)
   */
  uchar   patch;

  /**
   * Patch (if command was a controller command)
   */
  uchar   ctl;

  /**
   * Data 1
   */
  uchar   d1;

  /**
   * Data 2
   */
  uchar   d2;

  /**
   * Data 3
   */
  uchar   d3;

  /**
   * Data 4
   */
  uchar   d4;

  /**
   * Data 5
   */
  uchar   d5;

  /**
   * Data 6
   */
  uchar   d6;

  /**
   * Length of the generic data variable
   */
  ulong	length;

  /**
   * The data for commands like text, sysex, etc.
   */
  uchar  *data;

};

/**
 * Stores a MIDI track. This can be thought of as a list of MIDI events.
 *
 * The data types used to store the track is similar to how events are
 * stored on a MIDI file, but used in a way that allows for faster parses.
 * 
 * This class is used on MidiPlayer::loadSong() to load the song and
 * later play it with MidiPlayer::play().
 *
 * @short Stores a MIDI track with a simple API
 * @version 0.9.5 17/01/2000
 * @author Antonio Larrosa Jimenez <larrosa@kde.org>
 */
class MidiTrack
{
  private:
    class MidiTrackPrivate;
    MidiTrackPrivate *d;

    int	id;

    ulong size;
    uchar *data;
    uchar *ptrdata;

    bool  note[16][128]; // Notes that are set on or off by this track
    ulong current_ticks; // Total number of ticks since beginning of song
    ulong delta_ticks;   // Delta ticks from previous event to next event
    ulong wait_ticks;    // Wait ticks from previous event in other track
    // to next event in this track

    ulong currentpos; // Some songs don't have a endoftrack event, so
    // we have to see when currentpos > size
    int	endoftrack;

    ulong readVariableLengthValue(void);

    uchar lastcommand;  // This is to run light without overbyte :-)


    double current_time; // in ms.
    double time_at_previous_tempochange;  // in ms.
    double ticks_from_previous_tempochange; // in ticks	
    //	double	time_to_next_event;  // in ms.
    double	time_at_next_event;  // in ms.
    int	tPCN;
    ulong	tempo;

    int	power2to(int i);

  public:
    /**
     * Constructor. 
     * @param file the file to read the track from. It should be ready at the
     * start of a track. MidiTrack reads just that track and the file is left at
     * the end of this track).
     * @param tpcn the ticks per cuarter note used in this file.
     * @param Id the ID for this track.
     */ 
    MidiTrack(FILE *file,int tpcn,int Id);

    /**
     * Destructor
     */
    ~MidiTrack();

    /**
     * Makes the iterator advance the given number of ticks.
     *
     * @return 0 if OK, and 1 if you didn't handle this track well and you
     * forgot to take an event (thing that will never happen if you use
     * MidiPlayer::play() ).
     */ 
    int	ticksPassed (ulong ticks); 

    /**
     * Makes the iterator advance the given number of milliseconds.
     *
     * @return 0 if OK, and 1 if you didn't handle this track well and you
     * forgot to take an event (thing that will never happen if you use
     * MidiPlayer::play() ).
     */
    int	msPassed    (ulong ms);

    /**
     * Returns the current millisecond which the iterator is at.
     */
    int	currentMs   (double ms);

    /**
     * Returns the number of ticks left for the next event.
     */
    ulong	waitTicks   (void) { return wait_ticks; }

    //	ulong   waitMs (void) {return time_to_next_event;};

    /**
     * Returns the absolute number of milliseconds of the next event.
     */ 
    double   absMsOfNextEvent (void) { return time_at_next_event; }

    /**
     * Change the tempo of the song.
     */
    void 	changeTempo(ulong t);

    /**
     * Reads the event at the iterator position, and puts it on the structure
     * pointed to by @p ev.
     */
    void	readEvent(MidiEvent *ev);

    /**
     * Initializes the iterator.
     */
    void	init(void);

    /**
     * Clears the internal variables.
     */
    void	clear(void);

};

#endif