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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#include "iterator.h"
#include "iterator_internal.h"
#include <stdarg.h>
#include <stdio.h>
using namespace Sci;
#define ASSERT_S(x) if (!(x)) { error("Failed assertion in L%d: " #x, __LINE__); return; }
#define ASSERT(x) ASSERT_S(x)
/* Tests the song iterators */
int errors = 0;
void error(char *fmt, ...) {
va_list ap;
fprintf(stderr, "[ERROR] ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
++errors;
}
/* The simple iterator will finish after a fixed amount of time. Before that,
** it emits (absolute) cues in ascending order. */
struct simple_iterator : public SongIterator {
int lifetime_remaining;
char *cues;
int cue_counter;
int cue_progress;
int cues_nr;
};
int simple_it_next(SongIterator *_self, unsigned char *buf, int *result) {
simple_iterator *self = (simple_iterator *)_self;
if (self->lifetime_remaining == -1) {
error("Song iterator called post mortem");
return SI_FINISHED;
}
if (self->lifetime_remaining) {
if (self->cue_counter < self->cues_nr) {
int time_to_cue = self->cues[self->cue_counter];
if (self->cue_progress == time_to_cue) {
++self->cue_counter;
self->cue_progress = 0;
*result = self->cue_counter;
return SI_ABSOLUTE_CUE;
} else {
int retval = time_to_cue - self->cue_progress;
self->cue_progress = time_to_cue;
if (retval > self->lifetime_remaining) {
retval = self->lifetime_remaining;
self->lifetime_remaining = 0;
self->cue_progress = retval;
return retval;
}
self->lifetime_remaining -= retval;
return retval;
}
} else {
int retval = self->lifetime_remaining;
self->lifetime_remaining = 0;
return retval;
}
} else {
self->lifetime_remaining = -1;
return SI_FINISHED;
}
}
Audio::AudioStream *simple_it_pcm_feed(SongIterator *_self) {
error("No PCM feed");
return NULL;
}
void simple_it_init(SongIterator *_self) {
}
SongIterator *simple_it_handle_message(SongIterator *_self, SongIterator::Message msg) {
return NULL;
}
void simple_it_cleanup(SongIterator *_self) {
}
/* Initialises the simple iterator.
** Parameters: (int) delay: Number of ticks until the iterator finishes
** (int *) cues: An array of cue delays (cue values are [1,2...])
** (int) cues_nr: Number of cues in ``cues''
** The first cue is emitted after cues[0] ticks, and it is 1. After cues[1] additional ticks
** the next cue is emitted, and so on. */
SongIterator *setup_simple_iterator(int delay, char *cues, int cues_nr) {
simple_iterator.lifetime_remaining = delay;
simple_iterator.cues = cues;
simple_iterator.cue_counter = 0;
simple_iterator.cues_nr = cues_nr;
simple_iterator.cue_progress = 0;
simple_iterator.ID = 42;
simple_iterator.channel_mask = 0x004f;
simple_iterator.flags = 0;
simple_iterator.priority = 1;
simple_iterator.death_listeners_nr = 0;
simple_iterator.cleanup = simple_it_cleanup;
simple_iterator.init = simple_it_init;
simple_iterator.handle_message = simple_it_handle_message;
simple_iterator.get_pcm_feed = simple_it_pcm_feed;
simple_iterator.next = simple_it_next;
return (SongIterator *) &simple_iterator;
}
#define ASSERT_SIT ASSERT(it == simple_it)
#define ASSERT_FFIT ASSERT(it == ff_it)
#define ASSERT_NEXT(n) ASSERT(songit_next(&it, data, &result, IT_READER_MASK_ALL) == n)
#define ASSERT_RESULT(n) ASSERT(result == n)
#define ASSERT_CUE(n) ASSERT_NEXT(SI_ABSOLUTE_CUE); ASSERT_RESULT(n)
void test_simple_it() {
SongIterator *it;
SongIterator *simple_it = (SongIterator *) & simple_iterator;
unsigned char data[4];
int result;
puts("[TEST] simple iterator (test artifact)");
it = setup_simple_iterator(42, NULL, 0);
ASSERT_SIT;
ASSERT_NEXT(42);
ASSERT_SIT;
ASSERT_NEXT(SI_FINISHED);
ASSERT_SIT;
it = setup_simple_iterator(42, "\003\004", 2);
ASSERT_SIT;
ASSERT_NEXT(3);
ASSERT_CUE(1);
ASSERT_SIT;
ASSERT_NEXT(4);
ASSERT_CUE(2);
ASSERT_SIT;
// warning("XXX => %d", songit_next(&it, data, &result, IT_READER_MASK_ALL));
ASSERT_NEXT(35);
ASSERT_NEXT(SI_FINISHED);
ASSERT_SIT;
puts("[TEST] Test OK.");
}
void test_fastforward() {
SongIterator *it;
SongIterator *simple_it = (SongIterator *) & simple_iterator;
SongIterator *ff_it;
unsigned char data[4];
int result;
puts("[TEST] fast-forward iterator");
it = setup_simple_iterator(42, NULL, 0);
ff_it = it = new_fast_forward_iterator(it, 0);
ASSERT_FFIT;
ASSERT_NEXT(42);
ASSERT_SIT; /* Must have morphed back */
ASSERT_NEXT(SI_FINISHED);
ASSERT_SIT;
it = setup_simple_iterator(42, NULL, 0);
ff_it = it = new_fast_forward_iterator(it, 1);
ASSERT_FFIT;
ASSERT_NEXT(41);
/* May or may not have morphed back here */
ASSERT_NEXT(SI_FINISHED);
ASSERT_SIT;
it = setup_simple_iterator(42, NULL, 0);
ff_it = it = new_fast_forward_iterator(it, 41);
ASSERT_FFIT;
ASSERT_NEXT(1);
/* May or may not have morphed back here */
ASSERT_NEXT(SI_FINISHED);
ASSERT_SIT;
it = setup_simple_iterator(42, NULL, 0);
ff_it = it = new_fast_forward_iterator(it, 42);
ASSERT_NEXT(SI_FINISHED);
/* May or may not have morphed back here */
it = setup_simple_iterator(42, NULL, 0);
ff_it = it = new_fast_forward_iterator(it, 10000);
ASSERT_NEXT(SI_FINISHED);
/* May or may not have morphed back here */
it = setup_simple_iterator(42, "\003\004", 2);
ff_it = it = new_fast_forward_iterator(it, 2);
ASSERT_FFIT;
ASSERT_NEXT(1);
ASSERT_CUE(1);
ASSERT_SIT;
ASSERT_NEXT(4);
ASSERT_CUE(2);
ASSERT_SIT;
ASSERT_NEXT(35);
ASSERT_NEXT(SI_FINISHED);
ASSERT_SIT;
it = setup_simple_iterator(42, "\003\004", 2);
ff_it = it = new_fast_forward_iterator(it, 5);
ASSERT_FFIT;
ASSERT_CUE(1);
ASSERT_FFIT;
ASSERT_NEXT(2);
ASSERT_CUE(2);
ASSERT_SIT;
ASSERT_NEXT(35);
ASSERT_NEXT(SI_FINISHED);
ASSERT_SIT;
it = setup_simple_iterator(42, "\003\004", 2);
ff_it = it = new_fast_forward_iterator(it, 41);
ASSERT_FFIT;
ASSERT_CUE(1);
ASSERT_FFIT;
ASSERT_CUE(2);
ASSERT_FFIT;
ASSERT_NEXT(1);
ASSERT_NEXT(SI_FINISHED);
ASSERT_SIT;
puts("[TEST] Test OK.");
}
#define SIMPLE_SONG_SIZE 50
static unsigned char simple_song[SIMPLE_SONG_SIZE] = {
0x00, /* Regular song */
/* Only use channel 0 for all devices */
0x02, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* Song begins here */
42, 0x90, 60, 0x7f, /* Play C after 42 ticks */
02, 64, 0x42, /* Play E after 2 more ticks, using running status mode */
0xf8, 10, 0x80, 60, 0x02, /* Stop C after 250 ticks */
0, 64, 0x00, /* Stop E immediately */
00, 0xfc /* Stop song */
};
#define ASSERT_MIDI3(cmd, arg0, arg1) \
ASSERT(data[0] == cmd); \
ASSERT(data[1] == arg0); \
ASSERT(data[2] == arg1);
void test_iterator_sci0() {
SongIterator *it = songit_new(simple_song, SIMPLE_SONG_SIZE, SCI_SONG_ITERATOR_TYPE_SCI0, 0l);
unsigned char data[4];
int result;
SIMSG_SEND(it, SIMSG_SET_PLAYMASK(0x0001)); /* Initialise song, enabling channel 0 */
puts("[TEST] SCI0-style song");
ASSERT_NEXT(42);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x90, 60, 0x7f);
ASSERT_NEXT(2);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x90, 64, 0x42);
ASSERT_NEXT(250);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x80, 60, 0x02);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x80, 64, 0x00);
ASSERT_NEXT(SI_FINISHED);
puts("[TEST] Test OK.");
}
void test_iterator_sci0_loop() {
SongIterator *it = songit_new(simple_song, SIMPLE_SONG_SIZE, SCI_SONG_ITERATOR_TYPE_SCI0, 0l);
unsigned char data[4];
int result;
SIMSG_SEND(it, SIMSG_SET_PLAYMASK(0x0001)); /* Initialise song, enabling channel 0 */
SIMSG_SEND(it, SIMSG_SET_LOOPS(2)); /* Loop one additional time */
puts("[TEST] SCI0-style song with looping");
ASSERT_NEXT(42);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x90, 60, 0x7f);
ASSERT_NEXT(2);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x90, 64, 0x42);
ASSERT_NEXT(250);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x80, 60, 0x02);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x80, 64, 0x00);
ASSERT_NEXT(SI_LOOP);
ASSERT_NEXT(42);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x90, 60, 0x7f);
ASSERT_NEXT(2);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x90, 64, 0x42);
ASSERT_NEXT(250);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x80, 60, 0x02);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x80, 64, 0x00);
ASSERT_NEXT(SI_FINISHED);
puts("[TEST] Test OK.");
}
#define LOOP_SONG_SIZE 54
unsigned char loop_song[LOOP_SONG_SIZE] = {
0x00, /* Regular song song */
/* Only use channel 0 for all devices */
0x02, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* Song begins here */
42, 0x90, 60, 0x7f, /* Play C after 42 ticks */
13, 0x80, 60, 0x00, /* Stop C after 13 ticks */
00, 0xCF, 0x7f, /* Set loop point */
02, 0x90, 64, 0x42, /* Play E after 2 more ticks, using running status mode */
03, 0x80, 64, 0x00, /* Stop E after 3 ticks */
00, 0xfc /* Stop song/loop */
};
void test_iterator_sci0_mark_loop() {
SongIterator *it = songit_new(loop_song, LOOP_SONG_SIZE, SCI_SONG_ITERATOR_TYPE_SCI0, 0l);
unsigned char data[4];
int result;
SIMSG_SEND(it, SIMSG_SET_PLAYMASK(0x0001)); /* Initialise song, enabling channel 0 */
SIMSG_SEND(it, SIMSG_SET_LOOPS(3)); /* Loop once more */
puts("[TEST] SCI0-style song with loop mark, looping");
ASSERT_NEXT(42);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x90, 60, 0x7f);
ASSERT_NEXT(13);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x80, 60, 0x00);
/* Loop point here: we don't observe that in the iterator interface yet, though */
ASSERT_NEXT(2);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x90, 64, 0x42);
ASSERT_NEXT(3);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x80, 64, 0x00);
/* Now we loop back to the loop pont */
ASSERT_NEXT(SI_LOOP);
ASSERT_NEXT(2);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x90, 64, 0x42);
ASSERT_NEXT(3);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x80, 64, 0x00);
/* ...and one final time */
ASSERT_NEXT(SI_LOOP);
ASSERT_NEXT(2);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x90, 64, 0x42);
ASSERT_NEXT(3);
ASSERT_NEXT(0);
ASSERT_MIDI3(0x80, 64, 0x00);
ASSERT_NEXT(SI_FINISHED);
puts("[TEST] Test OK.");
}
int main(int argc, char **argv) {
test_simple_it();
test_fastforward();
test_iterator_sci0();
test_iterator_sci0_loop();
test_iterator_sci0_mark_loop();
if (errors != 0)
warning("[ERROR] %d errors total", errors);
return (errors != 0);
}
|