seqfile.c++
7.21 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//====================================================================
// seqfile.c++
//
// Synopsis:
// Implementation for sequence file object
//
// Author(s)
// Steve Shepard
//
// Copyright 1993, Silicon Graphics, Inc.
// All Rights Reserved.
//
// This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
// the contents of this file may not be disclosed to third parties, copied or
// duplicated in any form, in whole or in part, without the prior written
// permission of Silicon Graphics, Inc.
//
// RESTRICTED RIGHTS LEGEND:
// Use, duplication or disclosure by the Government is subject to restrictions
// as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
// and Computer Software clause at DFARS 252.227-7013, and/or in similar or
// successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
// rights reserved under the Copyright Laws of the United States.
//====================================================================
#include "seqfile.h"
SeqFile::SeqFile(int div)
{
file = 0;
division = div;
eventList.PointToFirst(curIter);
}
SeqFile::~SeqFile()
{
if (file)
fclose(file);
}
int SeqFile::Open(const char *name, const char *rw)
{
file = fopen(name, rw);
if (!file)
return -1;
}
void SeqFile::Close()
{
if (file) {
fclose(file);
file = 0;
}
}
void SeqFile::AddEvent(Event *event)
{
ListIter iter = 0;
ListIter thisIter;
Event *thisEvent;
Time thisTime;
Time eventTime;
//--------------------------------------------------
// filter out the events we don't want
//--------------------------------------------------
char type = event->GetType();
if (type == 0xff) {
if (event->GetSubtype() != 0x51)
return; // only want set tempo events
} else if ((type & 0xf0) == 0xf0) {
return; // don't want sysex events
}
//--------------------------------------------------
// insert in the event list.
//--------------------------------------------------
eventList.PointToFirst( thisIter );
eventList.PointToFirst( iter );
thisEvent = eventList.GetIteratorValue(curIter);
if (thisEvent) {
thisEvent->GetTime(thisTime);
event->GetTime(eventTime);
if (eventTime > thisTime) {
thisIter = curIter;
}
}
while((thisEvent = eventList.GetIteratorValue( thisIter )) != NULL) {
if (testEvent(event, thisEvent))
break;
iter = thisIter;
eventList.PointToNext( thisIter );
}
eventList.AppendData(iter, event);
curIter = iter;
}
//------------------------------------------------------------
// Brute force sort. Returns true if event comes before
// beforeEvent
//------------------------------------------------------------
int SeqFile::testEvent(Event *event, Event *thisEvent)
{
Time thisTime;
Time eventTime;
event->GetTime(eventTime);
thisEvent->GetTime(thisTime);
// first check event times
if (eventTime < thisTime) {
return 1;
} else if (eventTime == thisTime) {
char eventType = event->GetType();
char thisType = thisEvent->GetType();
char eventStatus = (eventType >> 4) & 0xf;
char thisStatus = (thisType >> 4) & 0xf;
if (eventStatus > thisStatus) {
return 1;
} else if (eventStatus == thisStatus) { /* same status */
char eventChannel = eventType & 0x0f;
char thisChannel = eventType & 0x0f;
if (eventChannel < thisChannel) {
return 1;
}
}
}
return 0;
}
int SeqFile::RemoveEvent(Event *event)
{
ListIter iter;
if (eventList.FindByKey(iter, event)) {
eventList.RemoveData(iter);
return 0;
} else {
return -1;
}
}
void SeqFile::Print()
{
ListIter iter;
Event *event;
printf("Sequence file\n");
for (eventList.PointToFirst( iter );
(event = eventList.GetIteratorValue( iter )) != NULL;
eventList.PointToNext( iter )) {
event->Print();
}
}
void SeqFile::Write()
{
ListIter iter;
Event *event;
Time lastTime(0, division);
Time thisTime;
Time delta;
char lastStatus = 0;
int rv;
float qnpt;
qnpt = 1.0/(float)division;
printf("division = %d\n", division);
printf("qnpt = %f\n", qnpt);
rv = fwrite(&qnpt, sizeof(qnpt), 1, file);
assert(rv == 1);
for (eventList.PointToFirst( iter );
(event = eventList.GetIteratorValue( iter )) != NULL;
eventList.PointToNext( iter )) {
//------------------------------------------------------------
// write out delta ticks
//------------------------------------------------------------
event->GetTime(thisTime);
delta = thisTime - lastTime;
short ts = delta.ticks;
int cnt = fwrite ((void *)&ts, sizeof(short), 1, file);
assert(cnt == 1);
lastTime = thisTime;
//------------------------------------------------------------
// write out event
//------------------------------------------------------------
char type = event->GetType();
char subtype = event->GetSubtype();
if ((type == 0xff) && (subtype == 0x51)) { // tempo event
rv = putc(0xff, file);
assert(rv != EOF);
rv = putc(0x51, file);
assert(rv != EOF);
rv = putc(0x03, file);
assert(rv != EOF);
long tempo = ((MSetTempo *)event)->GetTempo();
unsigned char b1 = (unsigned char)(tempo >> 16) & 0xff;
unsigned char b2 = (unsigned char)(tempo >> 8) & 0xff;
unsigned char b3 = (unsigned char)(tempo >> 0) & 0xff;
rv =putc(b1, file);
assert(rv != EOF);
putc(b2, file);
assert(rv != EOF);
putc(b3, file);
assert(rv != EOF);
lastStatus = 0;
} else if ((type & 0xf0) != 0xf0) { // MIDI event
if (type != lastStatus) { // running status
rv = putc(type, file);
assert(rv != EOF);
}
char byte1 = ((MIDIEvent *)event)->GetByte1();
char byte2 = ((MIDIEvent *)event)->GetByte2();
rv = putc(byte1, file);
assert(rv != EOF);
if (((type & 0xf0) != 0xc0) && ((type& 0xf0) != 0xd0)) {
rv = putc(byte2, file);
assert(rv != EOF);
}
lastStatus = type;
} else {
printf("Warning: bogus event in seqence file\n");
}
}
//------------------------------------------------------------
// append End of Track
//------------------------------------------------------------
printf("writing End of Track\n");
putc(0, file); // timestamp
putc(0, file);
putc(0xff, file); // ### dummy for now, should use real event
putc(0x2f, file); // ### and actually put it in the list
putc(0, file);
}