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
|
/* 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$
*
*/
/* OK... 'realtime' may be a little too euphemistic, as this one just
** prays for some reasonable amount of soft real-time, but it's close
** enough, I guess. */
#include "sci/tools.h"
#include "sci/sfx/player/realtime.h"
#include "sci/sfx/sequencer.h"
#include "sci/sfx/iterator.h"
#include "sci/sfx/core.h"
#include "common/system.h"
namespace Sci {
static sfx_sequencer_t *seq;
/* Playing mechanism */
static inline int delta_time(const uint32 comp, const uint32 base) {
return long(comp) - long(base);
}
static SongIterator *play_it = NULL;
static uint32 play_last_time;
static uint32 play_pause_started; /* Beginning of the last pause */
static uint32 play_pause_counter; /* Last point in time to mark a
** play position augmentation */
static int play_paused = 0;
static int play_it_done = 0;
static int play_writeahead = 0;
static int play_moredelay = 0;
static void play_song(SongIterator *it, uint32 *wakeup_time, int writeahead_time) {
unsigned char buf[8];
int result;
if (play_paused) {
uint32 ct = g_system->getMillis();
*wakeup_time += delta_time(play_pause_counter, ct);
play_pause_counter = ct;
} else
/* Not paused: */
while (play_it && delta_time(*wakeup_time, g_system->getMillis())
< writeahead_time) {
int delay;
switch ((delay = songit_next(&(play_it),
&(buf[0]), &result,
IT_READER_MASK_ALL
| IT_READER_MAY_FREE
| IT_READER_MAY_CLEAN))) {
case SI_FINISHED:
play_it_done = 1;
return;
case SI_IGNORE:
case SI_LOOP:
case SI_RELATIVE_CUE:
case SI_ABSOLUTE_CUE:
break;
case SI_PCM:
sfx_play_iterator_pcm(play_it, 0);
break;
case 0:
seq->event(buf[0], result - 1, buf + 1);
break;
default:
play_moredelay = delay - 1;
*wakeup_time += delay * 1000 / SFX_TICKS_PER_SEC;
if (seq->delay)
seq->delay(delay);
}
}
}
static void rt_tell_synth(int buf_nr, byte *buf) {
seq->event(buf[0], buf_nr - 1, buf + 1);
}
static void rt_timer_callback(void) {
if (play_it && !play_it_done) {
if (!play_moredelay) {
int delta = delta_time(play_last_time, g_system->getMillis());
if (delta < 0) {
play_writeahead -= (int)((double)delta * 1.2); /* Adjust upwards */
} else if (delta > 15) {
play_writeahead -= 3; /* Adjust downwards */
}
} else
--play_moredelay;
if (play_writeahead < seq->min_write_ahead_ms)
play_writeahead = seq->min_write_ahead_ms;
play_song(play_it, &play_last_time, play_writeahead);
}
}
static Resource *find_patch(ResourceManager *resmgr, const char *seq_name, int patchfile) {
Resource *res = NULL;
if (patchfile != SFX_SEQ_PATCHFILE_NONE) {
res = resmgr->findResource(kResourceTypePatch, patchfile, 0);
if (!res) {
warning("[SFX] " __FILE__": patch.%03d requested by sequencer (%s), but not found",
patchfile, seq_name);
}
}
return res;
}
/* API implementation */
static Common::Error rt_set_option(char *name, char *value) {
return Common::kUnknownError;
}
static Common::Error rt_init(ResourceManager *resmgr, int expected_latency) {
Resource *res = NULL, *res2 = NULL;
void *seq_dev = NULL;
seq = sfx_find_sequencer(NULL);
if (!seq) {
warning("[SFX] " __FILE__": Could not find sequencer");
return Common::kUnknownError;
}
sfx_player_realtime.polyphony = seq->polyphony;
res = find_patch(resmgr, seq->name, seq->patchfile);
res2 = find_patch(resmgr, seq->name, seq->patchfile2);
if (seq->device)
seq_dev = sfx_find_device(seq->device, NULL);
if (seq->open(res ? res->size : 0,
res ? res->data : NULL,
res2 ? res2->size : 0,
res2 ? res2->data : NULL,
seq_dev)) {
warning("[SFX] " __FILE__": Sequencer failed to initialize");
return Common::kUnknownError;
}
play_writeahead = expected_latency;
if (play_writeahead < seq->min_write_ahead_ms)
play_writeahead = seq->min_write_ahead_ms;
play_writeahead *= 1; /* milliseconds */
if (seq->reset_timer)
seq->reset_timer(0);
return Common::kNoError;
}
static Common::Error rt_add_iterator(SongIterator *it, uint32 start_time) {
if (seq->reset_timer) /* Restart timer counting if possible */
seq->reset_timer(start_time);
SIMSG_SEND(it, SIMSG_SET_PLAYMASK(seq->playmask));
SIMSG_SEND(it, SIMSG_SET_RHYTHM(seq->play_rhythm));
play_last_time = start_time;
play_it = sfx_iterator_combine(play_it, it);
play_it_done = 0;
play_moredelay = 0;
return Common::kNoError;
}
static Common::Error rt_fade_out(void) {
warning(__FILE__": Attempt to fade out- not implemented yet");
return Common::kUnknownError;
}
static Common::Error rt_stop(void) {
SongIterator *it = play_it;
play_it = NULL;
delete it;
if (seq && seq->allstop)
seq->allstop();
return Common::kNoError;
}
static Common::Error rt_send_iterator_message(const SongIterator::Message &msg) {
if (!play_it)
return Common::kUnknownError;
songit_handle_message(&play_it, msg);
return Common::kNoError;
}
static Common::Error rt_pause(void) {
play_pause_started = g_system->getMillis();
// Also, indicate that we haven't modified the time counter yet
play_pause_counter = play_pause_started;
play_paused = 1;
if (!seq->allstop) {
sciprintf("[SFX] Cannot suspend sequencer, sound will continue for a bit\n");
return Common::kNoError;
} else
return seq->allstop();
}
static Common::Error rt_resume(void) {
play_paused = 0;
return Common::kNoError;
}
static Common::Error rt_exit(void) {
if (seq->close()) {
warning("[SFX] Sequencer reported error on close");
return Common::kUnknownError;
}
return Common::kNoError;
}
sfx_player_t sfx_player_realtime = {
"realtime",
"0.1",
&rt_set_option,
&rt_init,
&rt_add_iterator,
&rt_fade_out,
&rt_stop,
&rt_send_iterator_message,
&rt_pause,
&rt_resume,
&rt_exit,
&rt_timer_callback,
&rt_tell_synth,
0 /* polyphony */
};
} // End of namespace Sci
|