2009-02-17 15:07:44 +00:00
|
|
|
|
/* 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$
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
/* Song iterators */
|
|
|
|
|
|
2009-02-21 14:11:41 +00:00
|
|
|
|
#include "common/util.h"
|
2009-03-01 06:02:17 +00:00
|
|
|
|
#include "sci/sfx/iterator_internal.h"
|
|
|
|
|
#include "sci/sfx/player.h"
|
2009-02-21 18:28:38 +00:00
|
|
|
|
#include "sci/tools.h"
|
2009-02-24 05:39:10 +00:00
|
|
|
|
#include "sci/sci_memory.h"
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-04 07:31:31 +00:00
|
|
|
|
#include "sound/audiostream.h"
|
|
|
|
|
#include "sound/mixer.h"
|
|
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
|
namespace Sci {
|
|
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
|
static const int MIDI_cmdlen[16] = {0, 0, 0, 0, 0, 0, 0, 0,
|
2009-02-15 22:34:41 +00:00
|
|
|
|
2, 2, 2, 2, 1, 1, 2, 0
|
|
|
|
|
};
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
/*#define DEBUG_DECODING*/
|
|
|
|
|
/*#define DEBUG_VERBOSE*/
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
static void print_tabs_id(int nr, songit_id_t id) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
while (nr-- > 0)
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "\t");
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "[%08lx] ", id);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
static byte *sci_memchr(byte *data, int c, int n) {
|
|
|
|
|
while (n && *data != c) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
++data;
|
|
|
|
|
--n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (n)
|
|
|
|
|
return data;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
BaseSongIterator::BaseSongIterator(byte *data, uint size, songit_id_t id) {
|
|
|
|
|
ID = id;
|
|
|
|
|
|
|
|
|
|
_data = (byte *)sci_refcount_memdup(data, size);
|
|
|
|
|
_size = size;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 17:53:11 +00:00
|
|
|
|
BaseSongIterator::~BaseSongIterator() {
|
|
|
|
|
#ifdef DEBUG_VERBOSE
|
2009-03-07 06:56:39 +00:00
|
|
|
|
fprintf(stderr, "** FREEING it %p: data at %p\n", this, _data);
|
2009-03-06 17:53:11 +00:00
|
|
|
|
#endif
|
2009-03-07 06:56:39 +00:00
|
|
|
|
if (_data)
|
|
|
|
|
sci_refcount_decref(_data);
|
|
|
|
|
_data = NULL;
|
2009-03-06 17:53:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
|
/************************************/
|
|
|
|
|
/*-- SCI0 iterator implementation --*/
|
|
|
|
|
/************************************/
|
|
|
|
|
|
|
|
|
|
#define SCI0_MIDI_OFFSET 33
|
|
|
|
|
#define SCI0_END_OF_SONG 0xfc /* proprietary MIDI command */
|
|
|
|
|
|
|
|
|
|
#define SCI0_PCM_SAMPLE_RATE_OFFSET 0x0e
|
|
|
|
|
#define SCI0_PCM_SIZE_OFFSET 0x20
|
|
|
|
|
#define SCI0_PCM_DATA_OFFSET 0x2c
|
|
|
|
|
|
|
|
|
|
#define CHECK_FOR_END_ABSOLUTE(offset) \
|
2009-03-06 07:25:25 +00:00
|
|
|
|
if (offset > self->_size) { \
|
|
|
|
|
warning(SIPFX "Reached end of song without terminator (%x/%x) at %d!", offset, self->_size, __LINE__); \
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return SI_FINISHED; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define CHECK_FOR_END(offset_augment) \
|
|
|
|
|
if ((channel->offset + (offset_augment)) > channel->end) { \
|
|
|
|
|
channel->state = SI_STATE_FINISHED; \
|
2009-02-28 13:41:43 +00:00
|
|
|
|
warning(SIPFX "Reached end of track %d without terminator (%x+%x/%x) at %d!", channel->id, channel->offset, offset_augment, channel->end, __LINE__); \
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return SI_FINISHED; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
static int _parse_ticks(byte *data, int *offset_p, int size) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int ticks = 0;
|
|
|
|
|
int tempticks;
|
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
tempticks = data[offset++];
|
2009-02-15 22:34:41 +00:00
|
|
|
|
ticks += (tempticks == SCI_MIDI_TIME_EXPANSION_PREFIX) ?
|
|
|
|
|
SCI_MIDI_TIME_EXPANSION_LENGTH : tempticks;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
} while (tempticks == SCI_MIDI_TIME_EXPANSION_PREFIX
|
2009-02-15 22:34:41 +00:00
|
|
|
|
&& offset < size);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
if (offset_p)
|
|
|
|
|
*offset_p = offset;
|
|
|
|
|
|
|
|
|
|
return ticks;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
static int _sci0_get_pcm_data(Sci0SongIterator *self, sfx_pcm_config_t *format, int *xoffset, uint *xsize);
|
|
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
#define PARSE_FLAG_LOOPS_UNLIMITED (1 << 0) /* Unlimited # of loops? */
|
|
|
|
|
#define PARSE_FLAG_PARAMETRIC_CUE (1 << 1) /* Assume that cues take an additional "cue value" argument */
|
|
|
|
|
/* This implements a difference between SCI0 and SCI1 cues. */
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
void SongIteratorChannel::resetSynthChannels() {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
byte buf[5];
|
|
|
|
|
tell_synth_func *tell = sfx_get_player_tell_func();
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
for (int i = 0; i < MIDI_CHANNELS; i++) {
|
|
|
|
|
if (saw_notes & (1 << i)) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
buf[0] = 0xe0 | i; /* Pitch bend */
|
|
|
|
|
buf[1] = 0x80; /* Wheel center */
|
|
|
|
|
buf[2] = 0x40;
|
|
|
|
|
if (tell)
|
|
|
|
|
tell(3, buf);
|
|
|
|
|
/* TODO: Reset other controls? */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
static int _parse_sci_midi_command(BaseSongIterator *self, byte *buf,
|
2009-03-06 07:25:06 +00:00
|
|
|
|
int *result, SongIteratorChannel *channel, int flags) {
|
2009-03-07 06:56:39 +00:00
|
|
|
|
byte cmd;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int paramsleft;
|
|
|
|
|
int midi_op;
|
|
|
|
|
int midi_channel;
|
|
|
|
|
|
|
|
|
|
channel->state = SI_STATE_DELTA_TIME;
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
cmd = self->_data[channel->offset++];
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
if (!(cmd & 0x80)) {
|
|
|
|
|
/* 'Running status' mode */
|
|
|
|
|
channel->offset--;
|
|
|
|
|
cmd = channel->last_cmd;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-15 22:34:41 +00:00
|
|
|
|
if (cmd == 0xfe) {
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "song iterator subsystem: Corrupted sound resource detected.\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return SI_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
midi_op = cmd >> 4;
|
|
|
|
|
midi_channel = cmd & 0xf;
|
|
|
|
|
paramsleft = MIDI_cmdlen[midi_op];
|
|
|
|
|
channel->saw_notes |= 1 << midi_channel;
|
|
|
|
|
|
|
|
|
|
#if 0
|
2009-02-15 22:34:41 +00:00
|
|
|
|
if (1) {
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "[IT]: off=%x, cmd=%02x, takes %d args ",
|
2009-02-15 22:34:41 +00:00
|
|
|
|
channel->offset - 1, cmd, paramsleft);
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "[%02x %02x <%02x> %02x %02x %02x]\n",
|
2009-03-07 06:56:39 +00:00
|
|
|
|
self->_data[channel->offset-3],
|
|
|
|
|
self->_data[channel->offset-2],
|
|
|
|
|
self->_data[channel->offset-1],
|
|
|
|
|
self->_data[channel->offset],
|
|
|
|
|
self->_data[channel->offset+1],
|
|
|
|
|
self->_data[channel->offset+2]);
|
2009-02-15 22:34:41 +00:00
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
buf[0] = cmd;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CHECK_FOR_END(paramsleft);
|
2009-03-07 06:56:39 +00:00
|
|
|
|
memcpy(buf + 1, self->_data + channel->offset, paramsleft);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
*result = 1 + paramsleft;
|
|
|
|
|
|
|
|
|
|
channel->offset += paramsleft;
|
|
|
|
|
|
|
|
|
|
channel->last_cmd = cmd;
|
|
|
|
|
|
|
|
|
|
/* Are we supposed to play this channel? */
|
|
|
|
|
if (
|
|
|
|
|
/* First, exclude "global" properties-- such as cues-- from consideration */
|
2009-02-15 22:34:41 +00:00
|
|
|
|
(midi_op < 0xf
|
|
|
|
|
&& !(cmd == SCI_MIDI_SET_SIGNAL)
|
|
|
|
|
&& !(SCI_MIDI_CONTROLLER(cmd)
|
|
|
|
|
&& buf[1] == SCI_MIDI_CUMULATIVE_CUE))
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
/* Next, check if the channel is allowed */
|
2009-02-15 22:34:41 +00:00
|
|
|
|
&& (!((1 << midi_channel) & channel->playmask)))
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return /* Execute next command */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
self->nextCommand(buf, result);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (cmd == SCI_MIDI_EOT) {
|
|
|
|
|
/* End of track? */
|
2009-03-07 06:56:39 +00:00
|
|
|
|
channel->resetSynthChannels();
|
2009-02-21 14:11:41 +00:00
|
|
|
|
/* fprintf(stderr, "eot; loops = %d, notesplayed=%d\n", self->loops, channel->notes_played);*/
|
2009-02-15 06:10:59 +00:00
|
|
|
|
if (self->loops > 1 /* && channel->notes_played*/) {
|
|
|
|
|
/* If allowed, decrement the number of loops */
|
|
|
|
|
if (!(flags & PARSE_FLAG_LOOPS_UNLIMITED))
|
|
|
|
|
*result = --self->loops;
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_DECODING
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "%s L%d: (%p):%d Looping ", __FILE__, __LINE__, self, channel->id);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
if (flags & PARSE_FLAG_LOOPS_UNLIMITED)
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "(indef.)");
|
2009-02-15 06:10:59 +00:00
|
|
|
|
else
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "(%d)", self->loops);
|
|
|
|
|
fprintf(stderr, " %x -> %x\n",
|
2009-02-15 22:34:41 +00:00
|
|
|
|
channel->offset, channel->loop_offset);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#endif
|
|
|
|
|
channel->offset = channel->loop_offset;
|
|
|
|
|
channel->notes_played = 0;
|
|
|
|
|
channel->state = SI_STATE_DELTA_TIME;
|
|
|
|
|
channel->total_timepos = channel->loop_timepos;
|
|
|
|
|
channel->last_cmd = 0xfe;
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "Looping song iterator %08lx.\n", self->ID);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return SI_LOOP;
|
|
|
|
|
} else {
|
|
|
|
|
channel->state = SI_STATE_FINISHED;
|
|
|
|
|
#ifdef DEBUG_DECODING
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "%s L%d: (%p):%d EOT because"
|
2009-02-15 22:34:41 +00:00
|
|
|
|
" %d notes, %d loops\n",
|
|
|
|
|
__FILE__, __LINE__, self, channel->id,
|
|
|
|
|
channel->notes_played, self->loops);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#endif
|
|
|
|
|
return SI_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (cmd == SCI_MIDI_SET_SIGNAL) {
|
|
|
|
|
if (buf[1] == SCI_MIDI_SET_SIGNAL_LOOP) {
|
|
|
|
|
channel->loop_offset = channel->offset;
|
|
|
|
|
channel->loop_timepos = channel->total_timepos;
|
|
|
|
|
|
|
|
|
|
return /* Execute next command */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
self->nextCommand(buf, result);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
} else {
|
|
|
|
|
/* Used to be conditional <= 127 */
|
|
|
|
|
*result = buf[1]; /* Absolute cue */
|
|
|
|
|
return SI_ABSOLUTE_CUE;
|
|
|
|
|
}
|
|
|
|
|
} else if (SCI_MIDI_CONTROLLER(cmd)) {
|
|
|
|
|
switch (buf[1]) {
|
|
|
|
|
|
|
|
|
|
case SCI_MIDI_CUMULATIVE_CUE:
|
|
|
|
|
if (flags & PARSE_FLAG_PARAMETRIC_CUE)
|
|
|
|
|
self->ccc += buf[2];
|
|
|
|
|
else { /* No parameter to CC */
|
|
|
|
|
self->ccc++;
|
2009-02-15 22:34:41 +00:00
|
|
|
|
/* channel->offset--; */
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
*result = self->ccc;
|
|
|
|
|
return SI_RELATIVE_CUE;
|
|
|
|
|
|
|
|
|
|
case SCI_MIDI_RESET_ON_SUSPEND:
|
|
|
|
|
self->resetflag = buf[2];
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SCI_MIDI_SET_POLYPHONY:
|
|
|
|
|
self->polyphony[midi_channel] = buf[2];
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
{
|
2009-02-15 22:34:41 +00:00
|
|
|
|
int i;
|
|
|
|
|
int voices = 0;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
for (i = 0; i < ((Sci1SongIterator *) self)->_numChannels; i++) {
|
2009-02-15 22:34:41 +00:00
|
|
|
|
voices += self->polyphony[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sciprintf("SET_POLYPHONY(%d, %d) for a total of %d voices\n", midi_channel, buf[2], voices);
|
|
|
|
|
sciprintf("[iterator-1] DEBUG: Polyphony = [ ");
|
2009-03-06 07:25:48 +00:00
|
|
|
|
for (i = 0; i < ((Sci1SongIterator *) self)->_numChannels; i++)
|
2009-02-15 22:34:41 +00:00
|
|
|
|
sciprintf("%d ", self->polyphony[i]);
|
|
|
|
|
sciprintf("]\n");
|
|
|
|
|
sciprintf("[iterator-1] DEBUG: Importance = [ ");
|
2009-03-06 07:25:48 +00:00
|
|
|
|
for (i = 0; i < ((Sci1SongIterator *) self)->_numChannels; i++)
|
2009-02-15 22:34:41 +00:00
|
|
|
|
sciprintf("%d ", self->importance[i]);
|
|
|
|
|
sciprintf("]\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SCI_MIDI_SET_REVERB:
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SCI_MIDI_CHANNEL_MUTE:
|
|
|
|
|
sciprintf("CHANNEL_MUTE(%d, %d)\n", midi_channel, buf[2]);
|
|
|
|
|
break;
|
|
|
|
|
|
2009-02-15 22:34:41 +00:00
|
|
|
|
case SCI_MIDI_HOLD: {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
// Safe cast: This controller is only used in SCI1
|
2009-03-07 06:56:39 +00:00
|
|
|
|
Sci1SongIterator *self1 = (Sci1SongIterator *)self;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
if (buf[2] == self1->_hold) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
channel->offset = channel->initial_offset;
|
|
|
|
|
channel->notes_played = 0;
|
|
|
|
|
channel->state = SI_STATE_COMMAND;
|
|
|
|
|
channel->total_timepos = 0;
|
2009-02-15 22:34:41 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
self1->_numLoopedChannels = self1->active_channels - 1;
|
2009-02-15 22:34:41 +00:00
|
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return SI_LOOP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 0x04: /* UNKNOWN NYI (happens in LSL2 gameshow) */
|
|
|
|
|
case 0x46: /* UNKNOWN NYI (happens in LSL3 binoculars) */
|
|
|
|
|
case 0x61: /* UNKNOWN NYI (special for adlib? Iceman) */
|
|
|
|
|
case 0x73: /* UNKNOWN NYI (happens in Hoyle) */
|
|
|
|
|
case 0xd1: /* UNKNOWN NYI (happens in KQ4 when riding the unicorn) */
|
|
|
|
|
return /* Execute next command */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
self->nextCommand(buf, result);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
case 0x01: /* modulation */
|
|
|
|
|
case 0x07: /* volume */
|
|
|
|
|
case 0x0a: /* panpot */
|
|
|
|
|
case 0x0b: /* expression */
|
|
|
|
|
case 0x40: /* hold */
|
|
|
|
|
case 0x79: /* reset all */
|
|
|
|
|
/* No special treatment neccessary */
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
if ((cmd & 0xf0) == 0x90) /* note on? */
|
|
|
|
|
channel->notes_played++;
|
|
|
|
|
|
|
|
|
|
/* Process as normal MIDI operation */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
static int _sci_midi_process_state(BaseSongIterator *self, byte *buf, int *result,
|
2009-03-06 07:25:06 +00:00
|
|
|
|
SongIteratorChannel *channel, int flags) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
CHECK_FOR_END(0);
|
|
|
|
|
|
|
|
|
|
switch (channel->state) {
|
|
|
|
|
|
|
|
|
|
case SI_STATE_PCM: {
|
2009-03-07 06:56:39 +00:00
|
|
|
|
if (*(self->_data + channel->offset) == 0
|
|
|
|
|
&& *(self->_data + channel->offset + 1) == SCI_MIDI_EOT)
|
2009-02-15 06:10:59 +00:00
|
|
|
|
/* Fake one extra tick to trick the interpreter into not killing the song iterator right away */
|
|
|
|
|
channel->state = SI_STATE_PCM_MAGIC_DELTA;
|
|
|
|
|
else
|
|
|
|
|
channel->state = SI_STATE_DELTA_TIME;
|
|
|
|
|
return SI_PCM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SI_STATE_PCM_MAGIC_DELTA: {
|
|
|
|
|
sfx_pcm_config_t format;
|
|
|
|
|
int offset;
|
2009-03-06 07:25:37 +00:00
|
|
|
|
uint size;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int delay;
|
2009-03-06 07:25:06 +00:00
|
|
|
|
if (_sci0_get_pcm_data((Sci0SongIterator *) self, &format, &offset, &size))
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return SI_FINISHED; /* 'tis broken */
|
|
|
|
|
channel->state = SI_STATE_FINISHED;
|
|
|
|
|
delay = (size * 50 + format.rate - 1) / format.rate; /* number of ticks to completion*/
|
|
|
|
|
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "delaying %d ticks\n", delay);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return delay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SI_STATE_UNINITIALISED:
|
2009-02-28 13:41:43 +00:00
|
|
|
|
warning(SIPFX "Attempt to read command from uninitialized iterator!");
|
2009-03-06 07:25:48 +00:00
|
|
|
|
self->init();
|
|
|
|
|
return self->nextCommand(buf, result);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
case SI_STATE_FINISHED:
|
|
|
|
|
return SI_FINISHED;
|
|
|
|
|
|
|
|
|
|
case SI_STATE_DELTA_TIME: {
|
|
|
|
|
int offset;
|
2009-03-07 06:56:39 +00:00
|
|
|
|
int ticks = _parse_ticks(self->_data + channel->offset,
|
2009-02-15 22:34:41 +00:00
|
|
|
|
&offset,
|
2009-03-06 07:25:25 +00:00
|
|
|
|
self->_size - channel->offset);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
channel->offset += offset;
|
|
|
|
|
channel->delay += ticks;
|
|
|
|
|
channel->timepos_increment = ticks;
|
|
|
|
|
|
|
|
|
|
CHECK_FOR_END(0);
|
|
|
|
|
|
|
|
|
|
channel->state = SI_STATE_COMMAND;
|
|
|
|
|
|
|
|
|
|
if (ticks)
|
|
|
|
|
return ticks;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-15 22:34:41 +00:00
|
|
|
|
/* continute otherwise... */
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
case SI_STATE_COMMAND: {
|
|
|
|
|
int retval;
|
|
|
|
|
channel->total_timepos += channel->timepos_increment;
|
|
|
|
|
channel->timepos_increment = 0;
|
|
|
|
|
|
|
|
|
|
retval = _parse_sci_midi_command(self, buf, result,
|
2009-02-15 22:34:41 +00:00
|
|
|
|
channel, flags);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
if (retval == SI_FINISHED) {
|
|
|
|
|
if (self->active_channels)
|
|
|
|
|
--(self->active_channels);
|
|
|
|
|
#ifdef DEBUG_DECODING
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "%s L%d: (%p):%d Finished channel, %d channels left\n",
|
2009-02-15 22:34:41 +00:00
|
|
|
|
__FILE__, __LINE__, self, channel->id,
|
|
|
|
|
self->active_channels);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#endif
|
|
|
|
|
/* If we still have channels left... */
|
|
|
|
|
if (self->active_channels) {
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return self->nextCommand(buf, result);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Otherwise, we have reached the end */
|
|
|
|
|
self->loops = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
2009-02-28 13:41:43 +00:00
|
|
|
|
warning(SIPFX "Invalid iterator state %d!", channel->state);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
BREAKPOINT();
|
|
|
|
|
return SI_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
static int _sci_midi_process(BaseSongIterator *self, byte *buf, int *result,
|
2009-03-06 07:25:06 +00:00
|
|
|
|
SongIteratorChannel *channel, int flags) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return _sci_midi_process_state(self, buf, result,
|
2009-02-15 22:34:41 +00:00
|
|
|
|
channel,
|
|
|
|
|
flags);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
int Sci0SongIterator::nextCommand(byte *buf, int *result) {
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return _sci_midi_process(this, buf, result,
|
|
|
|
|
&channel,
|
2009-02-15 22:34:41 +00:00
|
|
|
|
PARSE_FLAG_PARAMETRIC_CUE);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
static int _sci0_header_magic_p(byte *data, int offset, int size) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
if (offset + 0x10 > size)
|
|
|
|
|
return 0;
|
2009-03-07 06:56:39 +00:00
|
|
|
|
return (data[offset] == 0x1a)
|
2009-02-15 22:34:41 +00:00
|
|
|
|
&& (data[offset + 1] == 0x00)
|
|
|
|
|
&& (data[offset + 2] == 0x01)
|
|
|
|
|
&& (data[offset + 3] == 0x00);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-03-06 07:25:06 +00:00
|
|
|
|
static int _sci0_get_pcm_data(Sci0SongIterator *self,
|
2009-03-06 07:25:37 +00:00
|
|
|
|
sfx_pcm_config_t *format, int *xoffset, uint *xsize) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int tries = 2;
|
|
|
|
|
int found_it = 0;
|
2009-03-07 06:56:39 +00:00
|
|
|
|
byte *pcm_data;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int size;
|
2009-03-06 07:25:37 +00:00
|
|
|
|
uint offset = SCI0_MIDI_OFFSET;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
if (self->_data[0] != 2)
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return 1;
|
|
|
|
|
/* No such luck */
|
|
|
|
|
|
2009-03-06 07:25:25 +00:00
|
|
|
|
while ((tries--) && (offset < self->_size) && (!found_it)) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
/* Search through the garbage manually */
|
2009-03-07 06:56:39 +00:00
|
|
|
|
byte *fc = sci_memchr(self->_data + offset,
|
2009-02-15 22:34:41 +00:00
|
|
|
|
SCI0_END_OF_SONG,
|
2009-03-06 07:25:25 +00:00
|
|
|
|
self->_size - offset);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
if (!fc) {
|
2009-02-28 13:41:43 +00:00
|
|
|
|
warning(SIPFX "Playing unterminated song!");
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* add one to move it past the END_OF_SONG marker */
|
2009-03-07 06:56:39 +00:00
|
|
|
|
offset = fc - self->_data + 1;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
if (_sci0_header_magic_p(self->_data, offset, self->_size))
|
2009-02-15 06:10:59 +00:00
|
|
|
|
found_it = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!found_it) {
|
2009-02-28 13:41:43 +00:00
|
|
|
|
warning(SIPFX "Song indicates presence of PCM, but"
|
|
|
|
|
" none found (finally at offset %04x)", offset);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
pcm_data = self->_data + offset;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-07 06:57:17 +00:00
|
|
|
|
size = READ_LE_UINT16(pcm_data + SCI0_PCM_SIZE_OFFSET);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
/* Two of the format parameters are fixed by design: */
|
|
|
|
|
format->format = SFX_PCM_FORMAT_U8;
|
|
|
|
|
format->stereo = SFX_PCM_MONO;
|
2009-03-07 06:57:17 +00:00
|
|
|
|
format->rate = READ_LE_UINT16(pcm_data + SCI0_PCM_SAMPLE_RATE_OFFSET);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:25 +00:00
|
|
|
|
if (offset + SCI0_PCM_DATA_OFFSET + size != self->_size) {
|
|
|
|
|
int d = offset + SCI0_PCM_DATA_OFFSET + size - self->_size;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-02-28 13:41:43 +00:00
|
|
|
|
warning(SIPFX "PCM advertizes %d bytes of data, but %d"
|
|
|
|
|
" bytes are trailing in the resource!",
|
2009-03-06 07:25:25 +00:00
|
|
|
|
size, self->_size - (offset + SCI0_PCM_DATA_OFFSET));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
if (d > 0)
|
|
|
|
|
size -= d; /* Fix this */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*xoffset = offset;
|
|
|
|
|
*xsize = size;
|
|
|
|
|
|
|
|
|
|
return 0;
|
2009-02-15 22:34:41 +00:00
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-04 07:31:31 +00:00
|
|
|
|
static Audio::AudioStream *makeStream(byte *data, int size, sfx_pcm_config_t conf) {
|
|
|
|
|
printf("Playing PCM data of size %d, rate %d\n", size, conf.rate);
|
|
|
|
|
|
|
|
|
|
// Duplicate the data
|
|
|
|
|
byte *sound = (byte *)malloc(size);
|
|
|
|
|
memcpy(sound, data + SCI0_PCM_DATA_OFFSET, size);
|
|
|
|
|
|
|
|
|
|
// Convert stream format flags
|
|
|
|
|
int flags = Audio::Mixer::FLAG_AUTOFREE;
|
|
|
|
|
if (conf.format == SFX_PCM_FORMAT_U8)
|
|
|
|
|
flags |= Audio::Mixer::FLAG_UNSIGNED;
|
|
|
|
|
else if (conf.format == SFX_PCM_FORMAT_S16_NATIVE) {
|
|
|
|
|
flags |= Audio::Mixer::FLAG_16BITS;
|
|
|
|
|
#ifndef SCUMM_BIG_ENDIAN
|
|
|
|
|
flags |= Audio::Mixer::FLAG_LITTLE_ENDIAN;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
if (conf.stereo)
|
|
|
|
|
flags |= Audio::Mixer::FLAG_STEREO;
|
|
|
|
|
|
|
|
|
|
return Audio::makeLinearInputStream(sound, size, conf.rate, flags, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
Audio::AudioStream *Sci0SongIterator::getAudioStream() {
|
2009-03-04 07:31:31 +00:00
|
|
|
|
sfx_pcm_config_t conf;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int offset;
|
2009-03-06 07:25:37 +00:00
|
|
|
|
uint size;
|
2009-03-06 07:25:25 +00:00
|
|
|
|
if (_sci0_get_pcm_data(this, &conf, &offset, &size))
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
|
2009-03-06 07:25:25 +00:00
|
|
|
|
channel.state = SI_STATE_FINISHED; /* Don't play both PCM and music */
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
return makeStream(_data + offset + SCI0_PCM_DATA_OFFSET, size, conf);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
SongIterator *Sci0SongIterator::handleMessage(SongIteratorMessage msg) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
if (msg.recipient == _SIMSG_BASE) {
|
|
|
|
|
switch (msg.type) {
|
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_PRINT:
|
2009-03-06 07:25:48 +00:00
|
|
|
|
print_tabs_id(msg.args[0].i, ID);
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "SCI0: dev=%d, active-chan=%d, size=%d, loops=%d\n",
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_deviceId, active_channels, _size, loops);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_SET_LOOPS:
|
2009-03-06 07:25:48 +00:00
|
|
|
|
loops = msg.args[0].i;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_CLONE: {
|
2009-03-07 06:56:39 +00:00
|
|
|
|
BaseSongIterator *mem = new Sci0SongIterator(*this);
|
|
|
|
|
sci_refcount_incref(mem->_data);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#ifdef DEBUG_VERBOSE
|
2009-03-07 06:56:39 +00:00
|
|
|
|
fprintf(stderr, "** CLONE INCREF for new %p from %p at %p\n", mem, this, mem->_data);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#endif
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return mem; /* Assume caller has another copy of this */
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_STOP: {
|
|
|
|
|
songit_id_t sought_id = msg.ID;
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (sought_id == ID)
|
|
|
|
|
channel.state = SI_STATE_FINISHED;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_SET_PLAYMASK: {
|
|
|
|
|
int i;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_deviceId = msg.args[0].i;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
/* Set all but the rhytm channel mask bits */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
channel.playmask &= ~(1 << MIDI_RHYTHM_CHANNEL);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < MIDI_CHANNELS; i++)
|
2009-03-07 06:56:39 +00:00
|
|
|
|
if (_data[2 + (i << 1)] & _deviceId
|
2009-02-15 22:34:41 +00:00
|
|
|
|
&& i != MIDI_RHYTHM_CHANNEL)
|
2009-03-06 07:25:48 +00:00
|
|
|
|
channel.playmask |= (1 << i);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
2009-02-15 22:34:41 +00:00
|
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_SET_RHYTHM:
|
2009-03-06 07:25:48 +00:00
|
|
|
|
channel.playmask &= ~(1 << MIDI_RHYTHM_CHANNEL);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
if (msg.args[0].i)
|
2009-03-06 07:25:48 +00:00
|
|
|
|
channel.playmask |= (1 << MIDI_RHYTHM_CHANNEL);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2009-02-15 22:34:41 +00:00
|
|
|
|
case _SIMSG_BASEMSG_SET_FADE: {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
fade_params_t *fp = (fade_params_t *) msg.args[0].p;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
fade.action = fp->action;
|
|
|
|
|
fade.final_volume = fp->final_volume;
|
|
|
|
|
fade.ticks_per_step = fp->ticks_per_step;
|
|
|
|
|
fade.step_size = fp->step_size;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return this;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
int Sci0SongIterator::getTimepos() {
|
|
|
|
|
return channel.total_timepos;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
static void _base_init_channel(SongIteratorChannel *channel, int id, int offset, int end) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
channel->playmask = PLAYMASK_NONE; /* Disable all channels */
|
|
|
|
|
channel->id = id;
|
|
|
|
|
channel->notes_played = 0;
|
|
|
|
|
channel->state = SI_STATE_DELTA_TIME;
|
|
|
|
|
channel->loop_timepos = 0;
|
|
|
|
|
channel->total_timepos = 0;
|
|
|
|
|
channel->timepos_increment = 0;
|
|
|
|
|
channel->delay = 0; /* Only used for more than one channel */
|
|
|
|
|
channel->last_cmd = 0xfe;
|
|
|
|
|
|
|
|
|
|
channel->offset
|
2009-02-15 22:34:41 +00:00
|
|
|
|
= channel->loop_offset
|
|
|
|
|
= channel->initial_offset
|
|
|
|
|
= offset;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
channel->end = end;
|
|
|
|
|
channel->saw_notes = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
Sci0SongIterator::Sci0SongIterator(byte *data, uint size, songit_id_t id)
|
|
|
|
|
: BaseSongIterator(data, size, id) {
|
2009-03-06 17:53:11 +00:00
|
|
|
|
channel_mask = 0xffff; // Allocate all channels by default
|
|
|
|
|
channel.state = SI_STATE_UNINITIALISED;
|
2009-03-07 06:56:39 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < MIDI_CHANNELS; i++)
|
|
|
|
|
polyphony[i] = data[1 + (i << 1)];
|
|
|
|
|
|
|
|
|
|
init();
|
2009-03-06 17:53:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
void Sci0SongIterator::init() {
|
|
|
|
|
fade.action = FADE_ACTION_NONE;
|
|
|
|
|
resetflag = 0;
|
|
|
|
|
loops = 0;
|
|
|
|
|
priority = 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
ccc = 0; /* Reset cumulative cue counter */
|
|
|
|
|
active_channels = 1;
|
|
|
|
|
_base_init_channel(&channel, 0, SCI0_MIDI_OFFSET, _size);
|
2009-03-07 06:56:39 +00:00
|
|
|
|
channel.resetSynthChannels();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
if (_data[0] == 2) /* Do we have an embedded PCM? */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
channel.state = SI_STATE_PCM;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***************************/
|
|
|
|
|
/*-- SCI1 song iterators --*/
|
|
|
|
|
/***************************/
|
|
|
|
|
|
|
|
|
|
#define SCI01_INVALID_DEVICE 0xff
|
|
|
|
|
|
2009-02-28 06:48:53 +00:00
|
|
|
|
/* Second index determines whether PCM output is supported */
|
2009-03-07 06:56:39 +00:00
|
|
|
|
static const int sci0_to_sci1_device_map[][2] = {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
{0x06, 0x0c}, /* MT-32 */
|
|
|
|
|
{0xff, 0xff}, /* YM FB-01 */
|
|
|
|
|
{0x00, 0x00}, /* CMS/Game Blaster-- we assume OPL/2 here... */
|
|
|
|
|
{0xff, 0xff}, /* Casio MT540/CT460 */
|
|
|
|
|
{0x13, 0x13}, /* Tandy 3-voice */
|
|
|
|
|
{0x12, 0x12}, /* PC speaker */
|
|
|
|
|
{0xff, 0xff},
|
|
|
|
|
{0xff, 0xff},
|
|
|
|
|
}; /* Maps bit number to device ID */
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
#define SONGDATA(x) self->_data[offset + (x)]
|
|
|
|
|
#define SCI1_CHANDATA(off) self->_data[channel->offset + (off)]
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:06 +00:00
|
|
|
|
static int _sci1_sample_init(Sci1SongIterator *self, int offset) {
|
2009-03-07 06:57:17 +00:00
|
|
|
|
Sci1Sample sample;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int rate;
|
|
|
|
|
int length;
|
|
|
|
|
int begin;
|
|
|
|
|
int end;
|
|
|
|
|
|
2009-03-06 07:25:37 +00:00
|
|
|
|
CHECK_FOR_END_ABSOLUTE((uint)offset + 10);
|
2009-03-07 06:56:39 +00:00
|
|
|
|
if (self->_data[offset + 1] != 0)
|
2009-02-15 06:10:59 +00:00
|
|
|
|
sciprintf("[iterator-1] In sample at offset 0x04x: Byte #1 is %02x instead of zero\n",
|
2009-03-07 06:56:39 +00:00
|
|
|
|
self->_data[offset + 1]);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-07 16:14:20 +00:00
|
|
|
|
rate = (int16)READ_LE_UINT16(self->_data + offset + 2);
|
2009-03-07 06:57:17 +00:00
|
|
|
|
length = READ_LE_UINT16(self->_data + offset + 4);
|
2009-03-07 16:14:20 +00:00
|
|
|
|
begin = (int16)READ_LE_UINT16(self->_data + offset + 6);
|
|
|
|
|
end = (int16)READ_LE_UINT16(self->_data + offset + 8);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:37 +00:00
|
|
|
|
CHECK_FOR_END_ABSOLUTE((uint)(offset + 10 + length));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-07 06:57:17 +00:00
|
|
|
|
sample.delta = begin;
|
|
|
|
|
sample.size = length;
|
|
|
|
|
sample._data = self->_data + offset + 10;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_VERBOSE
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "[SAMPLE] %x/%x/%x/%x l=%x\n",
|
2009-03-06 07:25:25 +00:00
|
|
|
|
offset + 10, begin, end, self->_size, length);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2009-03-07 06:57:17 +00:00
|
|
|
|
sample.format.format = SFX_PCM_FORMAT_U8;
|
|
|
|
|
sample.format.stereo = SFX_PCM_MONO;
|
|
|
|
|
sample.format.rate = rate;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-07 06:57:17 +00:00
|
|
|
|
sample.announced = false;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
/* Perform insertion sort */
|
2009-03-07 06:57:17 +00:00
|
|
|
|
Common::List<Sci1Sample>::iterator seeker = self->_samples.begin();
|
|
|
|
|
while (seeker != self->_samples.end() && seeker->delta < begin)
|
|
|
|
|
++seeker;
|
|
|
|
|
self->_samples.insert(seeker, sample);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
return 0; /* Everything's fine */
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:06 +00:00
|
|
|
|
static int _sci1_song_init(Sci1SongIterator *self) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int last_time;
|
2009-03-06 07:25:37 +00:00
|
|
|
|
uint offset = 0;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
self->_numChannels = 0;
|
2009-03-07 06:57:17 +00:00
|
|
|
|
self->_samples.clear();
|
2009-03-06 07:25:48 +00:00
|
|
|
|
// self->_deviceId = 0x0c;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-02-15 22:34:41 +00:00
|
|
|
|
if (SONGDATA(0) == 0xf0) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
self->priority = SONGDATA(1);
|
|
|
|
|
|
|
|
|
|
offset += 8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (SONGDATA(0) != 0xff
|
2009-03-06 07:25:48 +00:00
|
|
|
|
&& SONGDATA(0) != self->_deviceId) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
offset++;
|
|
|
|
|
CHECK_FOR_END_ABSOLUTE(offset + 1);
|
|
|
|
|
while (SONGDATA(0) != 0xff) {
|
|
|
|
|
CHECK_FOR_END_ABSOLUTE(offset + 7);
|
|
|
|
|
offset += 6;
|
|
|
|
|
}
|
|
|
|
|
offset++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SONGDATA(0) == 0xff) {
|
|
|
|
|
sciprintf("[iterator-1] Song does not support"
|
2009-02-15 22:34:41 +00:00
|
|
|
|
" hardware 0x%02x\n",
|
2009-03-06 07:25:48 +00:00
|
|
|
|
self->_deviceId);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
offset++;
|
|
|
|
|
|
|
|
|
|
while (SONGDATA(0) != 0xff) { /* End of list? */
|
2009-03-06 07:25:37 +00:00
|
|
|
|
uint track_offset;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int end;
|
|
|
|
|
offset += 2;
|
|
|
|
|
|
|
|
|
|
CHECK_FOR_END_ABSOLUTE(offset + 4);
|
|
|
|
|
|
2009-03-07 06:57:17 +00:00
|
|
|
|
track_offset = READ_LE_UINT16(self->_data + offset);
|
|
|
|
|
end = READ_LE_UINT16(self->_data + offset + 2);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
CHECK_FOR_END_ABSOLUTE(track_offset - 1);
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
if (self->_data[track_offset] == 0xfe) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
if (_sci1_sample_init(self, track_offset))
|
|
|
|
|
return 1; /* Error */
|
|
|
|
|
} else {
|
|
|
|
|
/* Regular MIDI channel */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (self->_numChannels >= MIDI_CHANNELS) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
sciprintf("[iterator-1] Warning: Song has more than %d channels, cutting them off\n",
|
2009-02-15 22:34:41 +00:00
|
|
|
|
MIDI_CHANNELS);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
break; /* Scan for remaining samples */
|
|
|
|
|
} else {
|
2009-03-07 06:56:39 +00:00
|
|
|
|
int channel_nr = self->_data[track_offset] & 0xf;
|
2009-03-06 07:25:06 +00:00
|
|
|
|
SongIteratorChannel *channel =
|
2009-03-06 07:25:48 +00:00
|
|
|
|
&(self->_channels[self->_numChannels++]);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
if (self->_data[track_offset] & 0xf0)
|
2009-02-15 22:34:41 +00:00
|
|
|
|
printf("Channel %d has mapping bits %02x\n",
|
2009-03-07 06:56:39 +00:00
|
|
|
|
channel_nr, self->_data[track_offset] & 0xf0);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
_base_init_channel(channel,
|
2009-02-15 22:34:41 +00:00
|
|
|
|
channel_nr,
|
|
|
|
|
/* Skip over header bytes: */
|
|
|
|
|
track_offset + 2,
|
|
|
|
|
track_offset + end);
|
2009-03-07 06:56:39 +00:00
|
|
|
|
channel->resetSynthChannels();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
self->polyphony[self->_numChannels - 1]
|
2009-02-15 22:34:41 +00:00
|
|
|
|
= SCI1_CHANDATA(-1);
|
2009-03-06 07:25:48 +00:00
|
|
|
|
self->importance[self->_numChannels - 1]
|
|
|
|
|
= self->polyphony[self->_numChannels - 1] >> 4;
|
|
|
|
|
self->polyphony[self->_numChannels - 1] &= 15;
|
2009-02-15 22:34:41 +00:00
|
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
|
channel->playmask = ~0; /* Enable all */
|
|
|
|
|
self->channel_mask |= (1 << channel_nr);
|
|
|
|
|
|
|
|
|
|
CHECK_FOR_END_ABSOLUTE(offset + end);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
offset += 4;
|
|
|
|
|
CHECK_FOR_END_ABSOLUTE(offset);
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:57:17 +00:00
|
|
|
|
/* Now ensure that sam<61>le deltas are relative to the previous sample */
|
2009-02-15 06:10:59 +00:00
|
|
|
|
last_time = 0;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
self->active_channels = self->_numChannels;
|
|
|
|
|
self->_numLoopedChannels = 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-07 06:57:17 +00:00
|
|
|
|
for (Common::List<Sci1Sample>::iterator seeker = self->_samples.begin();
|
|
|
|
|
seeker != self->_samples.end(); ++seeker) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int prev_last_time = last_time;
|
|
|
|
|
sciprintf("[iterator-1] Detected sample: %d Hz, %d bytes at time %d\n",
|
2009-02-15 22:34:41 +00:00
|
|
|
|
seeker->format.rate, seeker->size, seeker->delta);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
last_time = seeker->delta;
|
|
|
|
|
seeker->delta -= prev_last_time;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0; /* Success */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#undef SONGDATA
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
static int _sci1_get_smallest_delta(Sci1SongIterator *self) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int i, d = -1;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
for (i = 0; i < self->_numChannels; i++)
|
|
|
|
|
if (self->_channels[i].state == SI_STATE_COMMAND
|
|
|
|
|
&& (d == -1 || self->_channels[i].delay < d))
|
|
|
|
|
d = self->_channels[i].delay;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-07 06:57:17 +00:00
|
|
|
|
if (!self->_samples.empty() && self->_samples.begin()->delta < d)
|
|
|
|
|
return self->_samples.begin()->delta;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
else
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
static void _sci1_update_delta(Sci1SongIterator *self, int delta) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int i;
|
|
|
|
|
|
2009-03-07 06:57:17 +00:00
|
|
|
|
if (!self->_samples.empty())
|
|
|
|
|
self->_samples.begin()->delta -= delta;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
for (i = 0; i < self->_numChannels; i++)
|
|
|
|
|
if (self->_channels[i].state == SI_STATE_COMMAND)
|
|
|
|
|
self->_channels[i].delay -= delta;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
static int _sci1_no_delta_time(Sci1SongIterator *self) { /* Checks that none of the channels is waiting for its delta to be read */
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int i;
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
for (i = 0; i < self->_numChannels; i++)
|
|
|
|
|
if (self->_channels[i].state == SI_STATE_DELTA_TIME)
|
2009-02-15 22:34:41 +00:00
|
|
|
|
return 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-15 14:26:33 +00:00
|
|
|
|
#if 0
|
|
|
|
|
// Unreferenced - removed
|
2009-03-06 07:25:06 +00:00
|
|
|
|
static void _sci1_dump_state(Sci1SongIterator *self) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
sciprintf("-- [%p] ------------------------\n", self);
|
2009-03-06 07:25:48 +00:00
|
|
|
|
for (i = 0; i < self->_numChannels; i++) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int j;
|
|
|
|
|
sciprintf("%d(s%02d): d-%d:\t(%x/%x) ",
|
2009-03-06 07:25:48 +00:00
|
|
|
|
self->_channels[i].id,
|
|
|
|
|
self->_channels[i].state,
|
|
|
|
|
self->_channels[i].delay,
|
|
|
|
|
self->_channels[i].offset,
|
|
|
|
|
self->_channels[i].end);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
for (j = -3; j < 9; j++) {
|
|
|
|
|
if (j == 0)
|
|
|
|
|
sciprintf(">");
|
|
|
|
|
else
|
|
|
|
|
sciprintf(" ");
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
sciprintf("%02x", self->_data[self->_channels[i].offset + j]);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
if (j == 0)
|
|
|
|
|
sciprintf("<");
|
|
|
|
|
else
|
|
|
|
|
sciprintf(" ");
|
|
|
|
|
}
|
|
|
|
|
sciprintf("\n");
|
|
|
|
|
}
|
2009-03-07 06:57:17 +00:00
|
|
|
|
if (!self->_samples.empty()) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
sciprintf("\t[sample %d]\n",
|
2009-03-07 06:57:17 +00:00
|
|
|
|
self->_samples.begin()->delta);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
sciprintf("------------------------------------------\n");
|
|
|
|
|
}
|
2009-02-15 14:26:33 +00:00
|
|
|
|
#endif
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
#define COMMAND_INDEX_NONE -1
|
|
|
|
|
#define COMMAND_INDEX_PCM -2
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
static int _sci1_command_index(Sci1SongIterator *self) {
|
2009-02-21 16:20:53 +00:00
|
|
|
|
/* Determine the channel # of the next active event, or -1 */
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int i;
|
|
|
|
|
int base_delay = 0x7ffffff;
|
|
|
|
|
int best_chan = COMMAND_INDEX_NONE;
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
for (i = 0; i < self->_numChannels; i++)
|
|
|
|
|
if ((self->_channels[i].state != SI_STATE_PENDING)
|
|
|
|
|
&& (self->_channels[i].state != SI_STATE_FINISHED)) {
|
2009-02-15 22:34:41 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if ((self->_channels[i].state == SI_STATE_DELTA_TIME)
|
|
|
|
|
&& (self->_channels[i].delay == 0))
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return i;
|
|
|
|
|
/* First, read all unknown delta times */
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (self->_channels[i].delay < base_delay) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
best_chan = i;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
base_delay = self->_channels[i].delay;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:57:17 +00:00
|
|
|
|
if (!self->_samples.empty() && base_delay >= self->_samples.begin()->delta)
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return COMMAND_INDEX_PCM;
|
|
|
|
|
|
|
|
|
|
return best_chan;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
Audio::AudioStream *Sci1SongIterator::getAudioStream() {
|
2009-03-07 06:57:17 +00:00
|
|
|
|
Common::List<Sci1Sample>::iterator sample = _samples.begin();
|
|
|
|
|
if (sample != _samples.end() && sample->delta <= 0) {
|
2009-03-07 06:56:39 +00:00
|
|
|
|
Audio::AudioStream *feed = makeStream(sample->_data, sample->size, sample->format);
|
2009-03-07 06:57:17 +00:00
|
|
|
|
_samples.erase(sample);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
return feed;
|
|
|
|
|
} else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
int Sci1SongIterator::nextCommand(byte *buf, int *result) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int retval = -42; /* Shouldn't happen, but gcc doesn't agree */
|
|
|
|
|
int chan;
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (!_initialised) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
sciprintf("[iterator-1] DEBUG: Initialising for %d\n",
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_deviceId);
|
2009-03-07 06:56:39 +00:00
|
|
|
|
_initialised = true;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (_sci1_song_init(this))
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return SI_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (_delayRemaining) {
|
|
|
|
|
int delay = _delayRemaining;
|
|
|
|
|
_delayRemaining = 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return delay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
do {
|
2009-03-06 07:25:48 +00:00
|
|
|
|
chan = _sci1_command_index(this);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
if (chan == COMMAND_INDEX_NONE) {
|
|
|
|
|
return SI_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (chan == COMMAND_INDEX_PCM) {
|
|
|
|
|
|
2009-03-07 06:57:17 +00:00
|
|
|
|
if (_samples.begin()->announced) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
/* Already announced; let's discard it */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
Audio::AudioStream *feed = getAudioStream();
|
2009-03-04 07:31:31 +00:00
|
|
|
|
delete feed;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
} else {
|
2009-03-07 06:57:17 +00:00
|
|
|
|
int delay = _samples.begin()->delta;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
if (delay) {
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_sci1_update_delta(this, delay);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return delay;
|
|
|
|
|
}
|
|
|
|
|
/* otherwise we're touching a PCM */
|
2009-03-07 06:57:17 +00:00
|
|
|
|
_samples.begin()->announced = true;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return SI_PCM;
|
|
|
|
|
}
|
|
|
|
|
} else { /* Not a PCM */
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
retval = _sci_midi_process(this,
|
2009-02-15 22:34:41 +00:00
|
|
|
|
buf, result,
|
2009-03-06 07:25:48 +00:00
|
|
|
|
&(_channels[chan]),
|
2009-02-15 22:34:41 +00:00
|
|
|
|
PARSE_FLAG_LOOPS_UNLIMITED);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-02-15 22:34:41 +00:00
|
|
|
|
if (retval == SI_LOOP) {
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_numLoopedChannels++;
|
|
|
|
|
_channels[chan].state = SI_STATE_PENDING;
|
|
|
|
|
_channels[chan].delay = 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (_numLoopedChannels == active_channels) {
|
2009-02-15 22:34:41 +00:00
|
|
|
|
int i;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-02-15 22:34:41 +00:00
|
|
|
|
/* Everyone's ready: Let's loop */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
for (i = 0; i < _numChannels; i++)
|
|
|
|
|
if (_channels[i].state == SI_STATE_PENDING)
|
|
|
|
|
_channels[i].state = SI_STATE_DELTA_TIME;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_numLoopedChannels = 0;
|
2009-02-15 22:34:41 +00:00
|
|
|
|
return SI_LOOP;
|
|
|
|
|
}
|
|
|
|
|
} else if (retval == SI_FINISHED) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#ifdef DEBUG
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "FINISHED some channel\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#endif
|
2009-02-15 22:34:41 +00:00
|
|
|
|
} else if (retval > 0) {
|
|
|
|
|
int sd ;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
sd = _sci1_get_smallest_delta(this);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (_sci1_no_delta_time(this) && sd) {
|
2009-02-15 22:34:41 +00:00
|
|
|
|
/* No other channel is ready */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_sci1_update_delta(this, sd);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-02-15 22:34:41 +00:00
|
|
|
|
/* Only from here do we return delta times */
|
|
|
|
|
return sd;
|
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} /* Not a PCM */
|
|
|
|
|
|
|
|
|
|
} while (retval > 0); /* All delays must be processed separately */
|
|
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
SongIterator *Sci1SongIterator::handleMessage(SongIteratorMessage msg) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
if (msg.recipient == _SIMSG_BASE) { /* May extend this in the future */
|
|
|
|
|
switch (msg.type) {
|
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_PRINT: {
|
|
|
|
|
int playmask = 0;
|
|
|
|
|
int i;
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
for (i = 0; i < _numChannels; i++)
|
|
|
|
|
playmask |= _channels[i].playmask;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
print_tabs_id(msg.args[0].i, ID);
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "SCI1: chan-nr=%d, playmask=%04x\n",
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_numChannels, playmask);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
2009-02-15 22:34:41 +00:00
|
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_CLONE: {
|
2009-03-07 06:56:39 +00:00
|
|
|
|
Sci1SongIterator *mem = new Sci1SongIterator(*this);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int delta = msg.args[0].i; /* Delay until next step */
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
sci_refcount_incref(mem->_data);
|
2009-03-06 07:25:48 +00:00
|
|
|
|
mem->_delayRemaining += delta;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return mem; /* Assume caller has another copy of this */
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_STOP: {
|
|
|
|
|
songit_id_t sought_id = msg.ID;
|
|
|
|
|
int i;
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (sought_id == ID) {
|
|
|
|
|
ID = 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
for (i = 0; i < _numChannels; i++)
|
|
|
|
|
_channels[i].state = SI_STATE_FINISHED;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-15 22:34:41 +00:00
|
|
|
|
case _SIMSG_BASEMSG_SET_PLAYMASK:
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (msg.ID == ID) {
|
|
|
|
|
channel_mask = 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_deviceId
|
2009-02-15 06:10:59 +00:00
|
|
|
|
= sci0_to_sci1_device_map
|
2009-02-15 22:34:41 +00:00
|
|
|
|
[sci_ffs(msg.args[0].i & 0xff) - 1]
|
|
|
|
|
[sfx_pcm_available()]
|
|
|
|
|
;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (_deviceId == 0xff) {
|
2009-02-15 22:34:41 +00:00
|
|
|
|
sciprintf("[iterator-1] Warning: Device %d(%d) not supported",
|
|
|
|
|
msg.args[0].i & 0xff, sfx_pcm_available());
|
|
|
|
|
}
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (_initialised) {
|
2009-02-15 22:34:41 +00:00
|
|
|
|
int i;
|
|
|
|
|
int toffset = -1;
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
for (i = 0; i < _numChannels; i++)
|
|
|
|
|
if (_channels[i].state != SI_STATE_FINISHED
|
|
|
|
|
&& _channels[i].total_timepos > toffset) {
|
|
|
|
|
toffset = _channels[i].total_timepos
|
|
|
|
|
+ _channels[i].timepos_increment
|
|
|
|
|
- _channels[i].delay;
|
2009-02-15 22:34:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Find an active channel so that we can
|
|
|
|
|
** get the correct time offset */
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_sci1_song_init(this);
|
2009-02-15 22:34:41 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
toffset -= _delayRemaining;
|
|
|
|
|
_delayRemaining = 0;
|
2009-02-15 22:34:41 +00:00
|
|
|
|
|
|
|
|
|
if (toffset > 0)
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return new_fast_forward_iterator(this, toffset);
|
2009-02-15 22:34:41 +00:00
|
|
|
|
} else {
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_sci1_song_init(this);
|
2009-03-07 06:56:39 +00:00
|
|
|
|
_initialised = true;
|
2009-02-15 22:34:41 +00:00
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-02-15 22:34:41 +00:00
|
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_SET_LOOPS:
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (msg.ID == ID)
|
|
|
|
|
loops = (msg.args[0].i > 32767) ? 99 : 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
/* 99 is arbitrary, but we can't use '1' because of
|
|
|
|
|
** the way we're testing in the decoding section. */
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_SET_HOLD:
|
2009-03-07 06:56:39 +00:00
|
|
|
|
_hold = msg.args[0].i;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
break;
|
|
|
|
|
case _SIMSG_BASEMSG_SET_RHYTHM:
|
|
|
|
|
/* Ignore */
|
|
|
|
|
break;
|
|
|
|
|
|
2009-02-15 22:34:41 +00:00
|
|
|
|
case _SIMSG_BASEMSG_SET_FADE: {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
fade_params_t *fp = (fade_params_t *) msg.args[0].p;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
fade.action = fp->action;
|
|
|
|
|
fade.final_volume = fp->final_volume;
|
|
|
|
|
fade.ticks_per_step = fp->ticks_per_step;
|
|
|
|
|
fade.step_size = fp->step_size;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
2009-02-28 13:41:43 +00:00
|
|
|
|
warning(SIPFX "Unsupported command %d to SCI1 iterator", msg.type);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return this;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
Sci1SongIterator::Sci1SongIterator(byte *data, uint size, songit_id_t id)
|
|
|
|
|
: BaseSongIterator(data, size, id) {
|
2009-03-06 17:53:11 +00:00
|
|
|
|
channel_mask = 0; // Defer channel allocation
|
2009-03-07 06:56:39 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < MIDI_CHANNELS; i++)
|
|
|
|
|
polyphony[i] = 0; // Unknown
|
|
|
|
|
|
|
|
|
|
init();
|
2009-03-06 17:53:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
void Sci1SongIterator::init() {
|
|
|
|
|
fade.action = FADE_ACTION_NONE;
|
|
|
|
|
resetflag = 0;
|
|
|
|
|
loops = 0;
|
|
|
|
|
priority = 0;
|
|
|
|
|
|
|
|
|
|
ccc = 127;
|
|
|
|
|
_deviceId = 0x00; // Default to Sound Blaster/Adlib for purposes of cue computation
|
|
|
|
|
_numChannels = 0;
|
2009-03-07 06:56:39 +00:00
|
|
|
|
_initialised = false;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_delayRemaining = 0;
|
|
|
|
|
loops = 0;
|
2009-03-07 06:56:39 +00:00
|
|
|
|
_hold = 0;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
memset(polyphony, 0, sizeof(polyphony));
|
|
|
|
|
memset(importance, 0, sizeof(importance));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 17:53:11 +00:00
|
|
|
|
Sci1SongIterator::~Sci1SongIterator() {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
int Sci1SongIterator::getTimepos() {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int max = 0;
|
|
|
|
|
int i;
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
for (i = 0; i < _numChannels; i++)
|
|
|
|
|
if (_channels[i].total_timepos > max)
|
|
|
|
|
max = _channels[i].total_timepos;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
return max;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
/**
|
|
|
|
|
* A song iterator with the purpose of sending notes-off channel commands.
|
|
|
|
|
*/
|
2009-03-06 07:25:48 +00:00
|
|
|
|
class CleanupSongIterator : public SongIterator {
|
|
|
|
|
public:
|
|
|
|
|
CleanupSongIterator(uint channels) {
|
|
|
|
|
channel_mask = channels;
|
|
|
|
|
ID = 17;
|
|
|
|
|
flags = 0;
|
|
|
|
|
}
|
2009-03-07 06:56:39 +00:00
|
|
|
|
|
|
|
|
|
SongIterator *makeClone() { return 0; }
|
2009-03-06 07:25:48 +00:00
|
|
|
|
int nextCommand(byte *buf, int *result);
|
|
|
|
|
Audio::AudioStream *getAudioStream() { return NULL; }
|
|
|
|
|
SongIterator *handleMessage(SongIteratorMessage msg);
|
|
|
|
|
int getTimepos() { return 0; }
|
|
|
|
|
};
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
SongIterator *CleanupSongIterator::handleMessage(SongIteratorMessage msg) {
|
|
|
|
|
if (msg.recipient == _SIMSG_BASEMSG_PRINT && msg.type == _SIMSG_BASEMSG_PRINT) {
|
|
|
|
|
print_tabs_id(msg.args[0].i, ID);
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "CLEANUP\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
int CleanupSongIterator::nextCommand(byte *buf, int *result) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
/* Task: Return channel-notes-off for each channel */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (channel_mask) {
|
|
|
|
|
int bs = sci_ffs(channel_mask) - 1;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
channel_mask &= ~(1 << bs);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
buf[0] = 0xb0 | bs; /* Controller */
|
|
|
|
|
buf[1] = SCI_MIDI_CHANNEL_NOTES_OFF;
|
|
|
|
|
buf[2] = 0; /* Hmm... */
|
|
|
|
|
*result = 3;
|
|
|
|
|
return 0;
|
|
|
|
|
} else
|
|
|
|
|
return SI_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**********************************/
|
|
|
|
|
/*-- Fast-forward song iterator --*/
|
|
|
|
|
/**********************************/
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
int FastForwardSongIterator::nextCommand(byte *buf, int *result) {
|
2009-03-07 06:56:39 +00:00
|
|
|
|
if (_delta <= 0)
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return SI_MORPH; /* Did our duty */
|
|
|
|
|
|
|
|
|
|
while (1) {
|
2009-03-07 06:56:39 +00:00
|
|
|
|
int rv = _delegate->nextCommand(buf, result);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
if (rv > 0) {
|
|
|
|
|
/* Subtract from the delta we want to wait */
|
2009-03-07 06:56:39 +00:00
|
|
|
|
_delta -= rv;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
/* Done */
|
2009-03-07 06:56:39 +00:00
|
|
|
|
if (_delta < 0)
|
|
|
|
|
return -_delta;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rv <= 0)
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
Audio::AudioStream *FastForwardSongIterator::getAudioStream() {
|
2009-03-07 06:56:39 +00:00
|
|
|
|
return _delegate->getAudioStream();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
SongIterator *FastForwardSongIterator::handleMessage(SongIteratorMessage msg) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
if (msg.recipient == _SIMSG_PLASTICWRAP)
|
|
|
|
|
switch (msg.type) {
|
|
|
|
|
|
|
|
|
|
case _SIMSG_PLASTICWRAP_ACK_MORPH:
|
2009-03-07 06:56:39 +00:00
|
|
|
|
if (_delta <= 0) {
|
|
|
|
|
SongIterator *it = _delegate;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
delete this;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return it;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
BREAKPOINT();
|
|
|
|
|
}
|
|
|
|
|
else if (msg.recipient == _SIMSG_BASE) {
|
|
|
|
|
switch (msg.type) {
|
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_CLONE: {
|
2009-03-07 06:56:39 +00:00
|
|
|
|
FastForwardSongIterator *clone = new FastForwardSongIterator(*this);
|
|
|
|
|
songit_handle_message(&clone->_delegate, msg);
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return clone;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_PRINT:
|
2009-03-06 07:25:48 +00:00
|
|
|
|
print_tabs_id(msg.args[0].i, ID);
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "PLASTICWRAP:\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
|
msg.args[0].i++;
|
2009-03-07 06:56:39 +00:00
|
|
|
|
songit_handle_message(&_delegate, msg);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2009-03-07 06:56:39 +00:00
|
|
|
|
songit_handle_message(&_delegate, msg);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
} else
|
2009-03-07 06:56:39 +00:00
|
|
|
|
songit_handle_message(&_delegate, msg);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
int FastForwardSongIterator::getTimepos() {
|
2009-03-07 06:56:39 +00:00
|
|
|
|
return _delegate->getTimepos();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FastForwardSongIterator::FastForwardSongIterator(SongIterator *capsit, int delta)
|
|
|
|
|
: _delegate(capsit), _delta(delta) {
|
|
|
|
|
|
|
|
|
|
channel_mask = capsit->channel_mask;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:06 +00:00
|
|
|
|
SongIterator *new_fast_forward_iterator(SongIterator *capsit, int delta) {
|
2009-03-07 06:56:39 +00:00
|
|
|
|
if (capsit == NULL)
|
2009-02-15 22:34:41 +00:00
|
|
|
|
return NULL;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
FastForwardSongIterator *it = new FastForwardSongIterator();
|
|
|
|
|
return it;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/********************/
|
|
|
|
|
/*-- Tee iterator --*/
|
|
|
|
|
/********************/
|
|
|
|
|
|
|
|
|
|
|
2009-03-06 17:39:15 +00:00
|
|
|
|
static void song_iterator_add_death_listener(SongIterator *it, TeeSongIterator *client) {
|
|
|
|
|
for (int i = 0; i < SONGIT_MAX_LISTENERS; ++i) {
|
|
|
|
|
if (it->_deathListeners[i] == 0) {
|
|
|
|
|
it->_deathListeners[i] = client;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
error("FATAL: Too many death listeners for song iterator");
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 17:39:46 +00:00
|
|
|
|
static void song_iterator_remove_death_listener(SongIterator *it, TeeSongIterator *client) {
|
|
|
|
|
for (int i = 0; i < SONGIT_MAX_LISTENERS; ++i) {
|
|
|
|
|
if (it->_deathListeners[i] == client) {
|
|
|
|
|
it->_deathListeners[i] = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void songit_tee_death_notification(TeeSongIterator *self, SongIterator *corpse) {
|
2009-03-06 17:39:15 +00:00
|
|
|
|
if (corpse == self->_children[TEE_LEFT].it) {
|
|
|
|
|
self->_status &= ~TEE_LEFT_ACTIVE;
|
|
|
|
|
self->_children[TEE_LEFT].it = NULL;
|
|
|
|
|
} else if (corpse == self->_children[TEE_RIGHT].it) {
|
|
|
|
|
self->_status &= ~TEE_RIGHT_ACTIVE;
|
|
|
|
|
self->_children[TEE_RIGHT].it = NULL;
|
|
|
|
|
} else {
|
|
|
|
|
BREAKPOINT();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 17:39:46 +00:00
|
|
|
|
TeeSongIterator::~TeeSongIterator() {
|
|
|
|
|
// When we die, remove any listeners from our children
|
|
|
|
|
if (_children[TEE_LEFT].it) {
|
|
|
|
|
song_iterator_remove_death_listener(_children[TEE_LEFT].it, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_children[TEE_RIGHT].it) {
|
|
|
|
|
song_iterator_remove_death_listener(_children[TEE_RIGHT].it, this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 17:39:15 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
int TeeSongIterator::nextCommand(byte *buf, int *result) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
static int ready_masks[2] = {TEE_LEFT_READY, TEE_RIGHT_READY};
|
|
|
|
|
static int active_masks[2] = {TEE_LEFT_ACTIVE, TEE_RIGHT_ACTIVE};
|
|
|
|
|
static int pcm_masks[2] = {TEE_LEFT_PCM, TEE_RIGHT_PCM};
|
|
|
|
|
int i;
|
|
|
|
|
int retid;
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_TEE_ITERATOR
|
2009-03-06 07:25:48 +00:00
|
|
|
|
fprintf(stderr, "[Tee] %02x\n", _status);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (!(_status & (TEE_LEFT_ACTIVE | TEE_RIGHT_ACTIVE)))
|
2009-02-15 06:10:59 +00:00
|
|
|
|
/* None is active? */
|
|
|
|
|
return SI_FINISHED;
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (morph_deferred == TEE_MORPH_READY)
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return SI_MORPH;
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if ((_status & (TEE_LEFT_ACTIVE | TEE_RIGHT_ACTIVE))
|
2009-02-15 22:34:41 +00:00
|
|
|
|
!= (TEE_LEFT_ACTIVE | TEE_RIGHT_ACTIVE)) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
/* Not all are is active? */
|
2009-02-15 14:26:33 +00:00
|
|
|
|
int which = 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#ifdef DEBUG_TEE_ITERATOR
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "\tRequesting transformation...\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#endif
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (_status & TEE_LEFT_ACTIVE)
|
2009-02-15 06:10:59 +00:00
|
|
|
|
which = TEE_LEFT;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
else if (_status & TEE_RIGHT_ACTIVE)
|
2009-02-15 06:10:59 +00:00
|
|
|
|
which = TEE_RIGHT;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
memcpy(buf, _children[which].buf, MAX_BUF_SIZE);
|
|
|
|
|
*result = _children[which].result;
|
|
|
|
|
morph_deferred = TEE_MORPH_READY;
|
|
|
|
|
return _children[which].retval;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* First, check for unreported PCMs */
|
|
|
|
|
for (i = TEE_LEFT; i <= TEE_RIGHT; i++)
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if ((_status & (ready_masks[i] | pcm_masks[i]))
|
2009-02-15 22:34:41 +00:00
|
|
|
|
== (ready_masks[i] | pcm_masks[i])) {
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_status &= ~ready_masks[i];
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return SI_PCM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = TEE_LEFT; i <= TEE_RIGHT; i++)
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (!(_status & ready_masks[i])) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
/* Buffers aren't ready yet */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_children[i].retval =
|
|
|
|
|
songit_next(&(_children[i].it),
|
|
|
|
|
_children[i].buf,
|
|
|
|
|
&(_children[i].result),
|
2009-02-15 22:34:41 +00:00
|
|
|
|
IT_READER_MASK_ALL
|
|
|
|
|
| IT_READER_MAY_FREE
|
|
|
|
|
| IT_READER_MAY_CLEAN);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_status |= ready_masks[i];
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#ifdef DEBUG_TEE_ITERATOR
|
2009-03-06 07:25:48 +00:00
|
|
|
|
fprintf(stderr, "\t Must check %d: %d\n", i, _children[i].retval);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (_children[i].retval == SI_ABSOLUTE_CUE ||
|
|
|
|
|
_children[i].retval == SI_RELATIVE_CUE)
|
|
|
|
|
return _children[i].retval;
|
|
|
|
|
if (_children[i].retval == SI_FINISHED) {
|
|
|
|
|
_status &= ~active_masks[i];
|
2009-02-15 06:10:59 +00:00
|
|
|
|
/* Recurse to complete */
|
|
|
|
|
#ifdef DEBUG_TEE_ITERATOR
|
2009-03-06 07:25:48 +00:00
|
|
|
|
fprintf(stderr, "\t Child %d signalled completion, recursing w/ status %02x\n", i, _status);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#endif
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return nextCommand(buf, result);
|
|
|
|
|
} else if (_children[i].retval == SI_PCM) {
|
|
|
|
|
_status |= pcm_masks[i];
|
|
|
|
|
_status &= ~ready_masks[i];
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return SI_PCM;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* We've already handled PCM, MORPH and FINISHED, CUEs & LOOP remain */
|
|
|
|
|
|
|
|
|
|
retid = TEE_LEFT;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if ((_children[TEE_LEFT].retval > 0)
|
2009-02-15 22:34:41 +00:00
|
|
|
|
/* Asked to delay */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
&& (_children[TEE_RIGHT].retval <= _children[TEE_LEFT].retval))
|
2009-02-15 06:10:59 +00:00
|
|
|
|
/* Is not delaying or not delaying as much */
|
|
|
|
|
retid = TEE_RIGHT;
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_TEE_ITERATOR
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "\tl:%d / r:%d / chose %d\n",
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_children[TEE_LEFT].retval, _children[TEE_RIGHT].retval, retid);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
#endif
|
|
|
|
|
#if 0
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (_children[retid].retval == 0) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
/* Perform remapping, if neccessary */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
byte *buf = _children[retid].buf;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
if (*buf != SCI_MIDI_SET_SIGNAL
|
2009-02-15 22:34:41 +00:00
|
|
|
|
&& *buf < 0xf0) { /* Not a generic command */
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int chan = *buf & 0xf;
|
|
|
|
|
int op = *buf & 0xf0;
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
chan = _children[retid].channel_remap[chan];
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
*buf = chan | op;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Adjust delta times */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (_children[retid].retval > 0
|
|
|
|
|
&& _children[1-retid].retval > 0) {
|
|
|
|
|
if (_children[1-retid].retval
|
|
|
|
|
== _children[retid].retval)
|
|
|
|
|
/* If both _children wait the same amount of time,
|
2009-02-15 06:10:59 +00:00
|
|
|
|
** we have to re-fetch commands from both */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_status &= ~ready_masks[1-retid];
|
2009-02-15 06:10:59 +00:00
|
|
|
|
else
|
|
|
|
|
/* If they don't, we can/must re-use the other
|
|
|
|
|
** child's delay time */
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_children[1-retid].retval
|
|
|
|
|
-= _children[retid].retval;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
_status &= ~ready_masks[retid];
|
|
|
|
|
memcpy(buf, _children[retid].buf, MAX_BUF_SIZE);
|
|
|
|
|
*result = _children[retid].result;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return _children[retid].retval;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
Audio::AudioStream *TeeSongIterator::getAudioStream() {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
static int pcm_masks[2] = {TEE_LEFT_PCM, TEE_RIGHT_PCM};
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = TEE_LEFT; i <= TEE_RIGHT; i++)
|
2009-03-06 07:25:25 +00:00
|
|
|
|
if (_status & pcm_masks[i]) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:25 +00:00
|
|
|
|
_status &= ~pcm_masks[i];
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return _children[i].it->getAudioStream();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:25 +00:00
|
|
|
|
return NULL; // No iterator
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
SongIterator *TeeSongIterator::handleMessage(SongIteratorMessage msg) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
if (msg.recipient == _SIMSG_BASE) {
|
|
|
|
|
switch (msg.type) {
|
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_PRINT:
|
2009-03-06 07:25:48 +00:00
|
|
|
|
print_tabs_id(msg.args[0].i, ID);
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "TEE:\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
|
msg.args[0].i++;
|
|
|
|
|
break; /* And continue with our children */
|
|
|
|
|
|
|
|
|
|
case _SIMSG_BASEMSG_CLONE: {
|
2009-03-07 06:56:39 +00:00
|
|
|
|
TeeSongIterator *newit = new TeeSongIterator(*this);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (newit->_children[TEE_LEFT].it)
|
|
|
|
|
newit->_children[TEE_LEFT].it =
|
|
|
|
|
songit_clone(newit->_children[TEE_LEFT].it, msg.args[0].i);
|
|
|
|
|
if (newit->_children[TEE_RIGHT].it)
|
|
|
|
|
newit->_children[TEE_RIGHT].it =
|
|
|
|
|
songit_clone(newit->_children[TEE_RIGHT].it, msg.args[0].i);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return newit;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (msg.recipient == _SIMSG_PLASTICWRAP) {
|
2009-03-06 07:25:06 +00:00
|
|
|
|
SongIterator *old_it;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
switch (msg.type) {
|
|
|
|
|
|
|
|
|
|
case _SIMSG_PLASTICWRAP_ACK_MORPH:
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (!(_status & (TEE_LEFT_ACTIVE | TEE_RIGHT_ACTIVE))) {
|
2009-03-06 17:53:11 +00:00
|
|
|
|
delete this;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return NULL;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
} else if (!(_status & TEE_LEFT_ACTIVE)) {
|
2009-03-06 17:53:11 +00:00
|
|
|
|
delete _children[TEE_LEFT].it;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
old_it = _children[TEE_RIGHT].it;
|
|
|
|
|
delete this;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return old_it;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
} else if (!(_status & TEE_RIGHT_ACTIVE)) {
|
2009-03-06 17:53:11 +00:00
|
|
|
|
delete _children[TEE_RIGHT].it;
|
2009-03-06 07:25:48 +00:00
|
|
|
|
old_it = _children[TEE_LEFT].it;
|
|
|
|
|
delete this;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return old_it;
|
|
|
|
|
} else {
|
|
|
|
|
sciprintf("[tee-iterator] WARNING:"
|
2009-02-15 22:34:41 +00:00
|
|
|
|
" Morphing without need\n");
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return this;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
BREAKPOINT();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
if (_children[TEE_LEFT].it)
|
|
|
|
|
songit_handle_message(&(_children[TEE_LEFT].it), msg);
|
|
|
|
|
if (_children[TEE_RIGHT].it)
|
|
|
|
|
songit_handle_message(&(_children[TEE_RIGHT].it), msg);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
void TeeSongIterator::init() {
|
|
|
|
|
_status = TEE_LEFT_ACTIVE | TEE_RIGHT_ACTIVE;
|
|
|
|
|
_children[TEE_LEFT].it->init();
|
|
|
|
|
_children[TEE_RIGHT].it->init();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 17:39:46 +00:00
|
|
|
|
SongIterator *songit_new_tee(SongIterator *left, SongIterator *right) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int i;
|
|
|
|
|
int firstfree = 1; /* First free channel */
|
|
|
|
|
int incomplete_map = 0;
|
2009-03-06 07:25:06 +00:00
|
|
|
|
TeeSongIterator *it = new TeeSongIterator();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
it->morph_deferred = TEE_MORPH_NONE;
|
2009-03-06 07:25:25 +00:00
|
|
|
|
it->_status = TEE_LEFT_ACTIVE | TEE_RIGHT_ACTIVE;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
it->_children[TEE_LEFT].it = left;
|
|
|
|
|
it->_children[TEE_RIGHT].it = right;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
/* By default, don't remap */
|
|
|
|
|
for (i = 0; i < 16; i++)
|
2009-03-06 07:25:48 +00:00
|
|
|
|
it->_children[TEE_LEFT].channel_remap[i]
|
|
|
|
|
= it->_children[TEE_RIGHT].channel_remap[i] = i;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
/* Default to lhs channels */
|
|
|
|
|
it->channel_mask = left->channel_mask;
|
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
|
if (it->channel_mask & (1 << i) & right->channel_mask
|
2009-02-15 22:34:41 +00:00
|
|
|
|
&& (i != MIDI_RHYTHM_CHANNEL) /* Share rhythm */) { /*conflict*/
|
2009-02-15 06:10:59 +00:00
|
|
|
|
while ((firstfree == MIDI_RHYTHM_CHANNEL)
|
2009-02-15 22:34:41 +00:00
|
|
|
|
/* Either if it's the rhythm channel or if it's taken */
|
|
|
|
|
|| (firstfree < MIDI_CHANNELS
|
|
|
|
|
&& ((1 << firstfree) & it->channel_mask)))
|
2009-02-15 06:10:59 +00:00
|
|
|
|
++firstfree;
|
|
|
|
|
|
|
|
|
|
if (firstfree == MIDI_CHANNELS) {
|
|
|
|
|
incomplete_map = 1;
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "[songit-tee <%08lx,%08lx>] "
|
2009-02-15 22:34:41 +00:00
|
|
|
|
"Could not remap right channel #%d:"
|
|
|
|
|
" Out of channels\n",
|
|
|
|
|
left->ID, right->ID, i);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
} else {
|
2009-03-06 07:25:48 +00:00
|
|
|
|
it->_children[TEE_RIGHT].channel_remap[i]
|
2009-02-15 22:34:41 +00:00
|
|
|
|
= firstfree;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
it->channel_mask |= (1 << firstfree);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#ifdef DEBUG_TEE_ITERATOR
|
|
|
|
|
if (incomplete_map) {
|
|
|
|
|
int c;
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "[songit-tee <%08lx,%08lx>] Channels:"
|
2009-02-15 22:34:41 +00:00
|
|
|
|
" %04x <- %04x | %04x\n",
|
|
|
|
|
left->ID, right->ID,
|
|
|
|
|
it->channel_mask,
|
|
|
|
|
left->channel_mask, right->channel_mask);
|
|
|
|
|
for (c = 0 ; c < 2; c++)
|
|
|
|
|
for (i = 0 ; i < 16; i++)
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, " map [%d][%d] -> %d\n",
|
2009-03-06 07:25:48 +00:00
|
|
|
|
c, i, it->_children[c].channel_remap[i]);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2009-03-06 17:39:46 +00:00
|
|
|
|
song_iterator_add_death_listener(left, it);
|
|
|
|
|
song_iterator_add_death_listener(right, it);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return it;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************/
|
|
|
|
|
/*-- General purpose functionality --*/
|
|
|
|
|
/*************************************/
|
|
|
|
|
|
2009-03-07 06:56:39 +00:00
|
|
|
|
int songit_next(SongIterator **it, byte *buf, int *result, int mask) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
|
|
if (!*it)
|
|
|
|
|
return SI_FINISHED;
|
|
|
|
|
|
|
|
|
|
do {
|
2009-03-06 07:25:48 +00:00
|
|
|
|
retval = (*it)->nextCommand(buf, result);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
if (retval == SI_MORPH) {
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, " Morphing %p (stored at %p)\n", (void *)*it, (void *)it);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
if (!SIMSG_SEND((*it), SIMSG_ACK_MORPH)) {
|
|
|
|
|
BREAKPOINT();
|
2009-02-21 14:11:41 +00:00
|
|
|
|
} else fprintf(stderr, "SI_MORPH successful\n");
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (retval == SI_FINISHED)
|
2009-02-21 14:11:41 +00:00
|
|
|
|
fprintf(stderr, "[song-iterator] Song finished. mask = %04x, cm=%04x\n",
|
2009-02-15 22:34:41 +00:00
|
|
|
|
mask, (*it)->channel_mask);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
if (retval == SI_FINISHED
|
2009-02-15 22:34:41 +00:00
|
|
|
|
&& (mask & IT_READER_MAY_CLEAN)
|
|
|
|
|
&& (*it)->channel_mask) { /* This last test will fail
|
2009-02-15 06:10:59 +00:00
|
|
|
|
** with a terminated
|
|
|
|
|
** cleanup iterator */
|
|
|
|
|
int channel_mask = (*it)->channel_mask;
|
|
|
|
|
|
|
|
|
|
if (mask & IT_READER_MAY_FREE)
|
2009-03-06 17:53:11 +00:00
|
|
|
|
delete *it;
|
2009-03-07 06:56:39 +00:00
|
|
|
|
*it = new CleanupSongIterator(channel_mask);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
retval = -9999; /* Continue */
|
|
|
|
|
}
|
2009-02-15 22:34:41 +00:00
|
|
|
|
} while (!( /* Until one of the following holds */
|
|
|
|
|
(retval > 0 && (mask & IT_READER_MASK_DELAY))
|
|
|
|
|
|| (retval == 0 && (mask & IT_READER_MASK_MIDI))
|
|
|
|
|
|| (retval == SI_LOOP && (mask & IT_READER_MASK_LOOP))
|
|
|
|
|
|| (retval == SI_ABSOLUTE_CUE &&
|
|
|
|
|
(mask & IT_READER_MASK_CUE))
|
|
|
|
|
|| (retval == SI_RELATIVE_CUE &&
|
|
|
|
|
(mask & IT_READER_MASK_CUE))
|
|
|
|
|
|| (retval == SI_PCM && (mask & IT_READER_MASK_PCM))
|
|
|
|
|
|| (retval == SI_FINISHED)
|
|
|
|
|
));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
if (retval == SI_FINISHED
|
2009-02-15 22:34:41 +00:00
|
|
|
|
&& (mask & IT_READER_MAY_FREE)) {
|
2009-03-06 17:53:11 +00:00
|
|
|
|
delete *it;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
*it = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 17:39:15 +00:00
|
|
|
|
SongIterator::SongIterator() {
|
|
|
|
|
ID = 0;
|
|
|
|
|
channel_mask = 0;
|
|
|
|
|
fade.action = FADE_ACTION_NONE;
|
|
|
|
|
flags = 0;
|
|
|
|
|
priority = 0;
|
|
|
|
|
memset(_deathListeners, 0, sizeof(_deathListeners));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SongIterator::~SongIterator() {
|
2009-03-06 17:39:46 +00:00
|
|
|
|
for (int i = 0; i < SONGIT_MAX_LISTENERS; ++i)
|
|
|
|
|
if (_deathListeners[i])
|
|
|
|
|
songit_tee_death_notification(_deathListeners[i], this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SongIterator *songit_new(byte *data, uint size, int type, songit_id_t id) {
|
2009-03-06 07:25:06 +00:00
|
|
|
|
BaseSongIterator *it;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
if (!data || size < 22) {
|
2009-02-28 13:41:43 +00:00
|
|
|
|
warning(SIPFX "Attempt to instantiate song iterator for null song data");
|
2009-02-15 06:10:59 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (type) {
|
2009-02-15 22:34:41 +00:00
|
|
|
|
case SCI_SONG_ITERATOR_TYPE_SCI0:
|
2009-03-07 06:56:39 +00:00
|
|
|
|
it = new Sci0SongIterator(data, size, id);
|
2009-02-15 22:34:41 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SCI_SONG_ITERATOR_TYPE_SCI1:
|
2009-03-07 06:56:39 +00:00
|
|
|
|
it = new Sci1SongIterator(data, size, id);
|
2009-02-15 22:34:41 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
/**-- Invalid/unsupported sound resources --**/
|
2009-03-07 06:56:39 +00:00
|
|
|
|
warning(SIPFX "Attempt to instantiate invalid/unknown song iterator type %d", type);
|
2009-02-15 22:34:41 +00:00
|
|
|
|
return NULL;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
return it;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:37 +00:00
|
|
|
|
SongIteratorMessage::SongIteratorMessage() {
|
|
|
|
|
ID = 0;
|
|
|
|
|
recipient = 0;
|
|
|
|
|
type = 0;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:37 +00:00
|
|
|
|
SongIteratorMessage::SongIteratorMessage(songit_id_t id, int r, int t, int a1, int a2) {
|
|
|
|
|
ID = id;
|
|
|
|
|
recipient = r;
|
|
|
|
|
type = t;
|
|
|
|
|
args[0].i = a1;
|
|
|
|
|
args[1].i = a2;
|
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:37 +00:00
|
|
|
|
SongIteratorMessage::SongIteratorMessage(songit_id_t id, int r, int t, void *a1, int a2) {
|
|
|
|
|
ID = id;
|
|
|
|
|
recipient = r;
|
|
|
|
|
type = t;
|
|
|
|
|
args[0].p = a1;
|
|
|
|
|
args[1].i = a2;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:06 +00:00
|
|
|
|
int songit_handle_message(SongIterator **it_reg_p, SongIteratorMessage msg) {
|
|
|
|
|
SongIterator *it = *it_reg_p;
|
|
|
|
|
SongIterator *newit;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
2009-03-06 07:25:48 +00:00
|
|
|
|
newit = it->handleMessage(msg);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
|
|
if (!newit)
|
|
|
|
|
return 0; /* Couldn't handle */
|
|
|
|
|
|
|
|
|
|
*it_reg_p = newit; /* Might have self-morphed */
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:06 +00:00
|
|
|
|
SongIterator *songit_clone(SongIterator *it, int delta) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
SIMSG_SEND(it, SIMSG_CLONE(delta));
|
2009-03-06 17:39:15 +00:00
|
|
|
|
memset(it->_deathListeners, 0, sizeof(it->_deathListeners));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
it->flags |= SONGIT_FLAG_CLONE;
|
|
|
|
|
return it;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 07:25:06 +00:00
|
|
|
|
SongIterator *sfx_iterator_combine(SongIterator *it1, SongIterator *it2) {
|
2009-02-15 06:10:59 +00:00
|
|
|
|
if (it1 == NULL)
|
|
|
|
|
return it2;
|
|
|
|
|
if (it2 == NULL)
|
|
|
|
|
return it1;
|
|
|
|
|
|
|
|
|
|
/* Both are non-NULL: */
|
2009-03-06 17:39:46 +00:00
|
|
|
|
return songit_new_tee(it1, it2); /* 'may destroy' */
|
2009-02-15 06:10:59 +00:00
|
|
|
|
}
|
2009-02-21 10:23:36 +00:00
|
|
|
|
|
|
|
|
|
} // End of namespace Sci
|