diff options
Diffstat (limited to 'engines/sci')
-rw-r--r-- | engines/sci/gfx/operations.cpp | 4 | ||||
-rw-r--r-- | engines/sci/sfx/core.cpp | 91 | ||||
-rw-r--r-- | engines/sci/sfx/mixer/soft.cpp | 65 | ||||
-rw-r--r-- | engines/sci/sfx/player/polled.cpp | 6 | ||||
-rw-r--r-- | engines/sci/sfx/player/realtime.cpp | 65 | ||||
-rw-r--r-- | engines/sci/sfx/seq/gm.cpp | 2 | ||||
-rw-r--r-- | engines/sci/sfx/sequencer.h | 5 | ||||
-rw-r--r-- | engines/sci/sfx/sfx_engine.h | 4 | ||||
-rw-r--r-- | engines/sci/sfx/sfx_player.h | 6 | ||||
-rw-r--r-- | engines/sci/sfx/sfx_songlib.h | 18 | ||||
-rw-r--r-- | engines/sci/sfx/sfx_time.h | 22 | ||||
-rw-r--r-- | engines/sci/sfx/songlib.cpp | 39 | ||||
-rw-r--r-- | engines/sci/sfx/time.cpp | 58 | ||||
-rw-r--r-- | engines/sci/tools.cpp | 48 | ||||
-rw-r--r-- | engines/sci/tools.h | 26 |
15 files changed, 113 insertions, 346 deletions
diff --git a/engines/sci/gfx/operations.cpp b/engines/sci/gfx/operations.cpp index 6250d89660..d4190353a1 100644 --- a/engines/sci/gfx/operations.cpp +++ b/engines/sci/gfx/operations.cpp @@ -1335,10 +1335,6 @@ int gfxop_disable_dirty_frames(gfx_state_t *state) { // Pointer and IO ops -#define SECONDS_OF_DAY (24*60*60) -#define MILLION 1000000 -// Sure, this may seem silly, but it's too easy to miss a zero...) - #define GFXOP_FULL_POINTER_REFRESH if (_gfxop_full_pointer_refresh(state)) { GFXERROR("Failed to do full pointer refresh!\n"); return GFX_ERROR; } static int _gfxop_full_pointer_refresh(gfx_state_t *state) { diff --git a/engines/sci/sfx/core.cpp b/engines/sci/sfx/core.cpp index 540750538f..3ccf38115c 100644 --- a/engines/sci/sfx/core.cpp +++ b/engines/sci/sfx/core.cpp @@ -26,12 +26,16 @@ /* Sound subsystem core: Event handler, sound player dispatching */ #include <stdio.h> + +#include "sci/tools.h" #include "sci/sfx/sfx_timer.h" #include "sci/sfx/sfx_iterator_internal.h" #include "sci/sfx/sfx_player.h" #include "sci/sfx/mixer.h" #include "sci/include/sci_midi.h" + #include "common/mutex.h" +#include "common/system.h" namespace Sci { @@ -50,9 +54,6 @@ extern sfx_pcm_device_t sfx_pcm_driver_scummvm; Common::Mutex* callbackMutex; - -#define MILLION 1000000 - int sfx_pcm_available() { return (pcm_device != NULL); } @@ -76,41 +77,16 @@ int sfx_get_player_polyphony() { return 0; } -static long time_minus(GTimeVal t1, GTimeVal t2) { - return (t1.tv_sec - t2.tv_sec) * MILLION + (t1.tv_usec - t2.tv_usec); -} - -static GTimeVal time_plus(GTimeVal t1, long delta) { - if (delta > 0) - t1.tv_usec += delta % MILLION; - else - t1.tv_usec -= (-delta) % MILLION; - - t1.tv_sec += delta / MILLION; - - if (t1.tv_usec > MILLION) { - t1.tv_sec++; - t1.tv_usec -= MILLION; - } - - return t1; -} - - static void _freeze_time(sfx_state_t *self) { /* Freezes the top song delay time */ - GTimeVal ctime; - long delta; - + uint32 ctime = g_system->getMillis(); song_t *song = self->song; - sci_get_current_time(&ctime); while (song) { - delta = time_minus(song->wakeup_time, ctime); - if (delta < 0) - delta = 0; - - song->delay = delta; + if (ctime > song->wakeup_time) + song->delay = 0; + else + song->delay = song->wakeup_time - ctime; song = song->next_playing; } @@ -162,13 +138,11 @@ static void _dump_songs(sfx_state_t *self) { static void _thaw_time(sfx_state_t *self) { /* inverse of _freeze_time() */ - GTimeVal ctime; + uint32 ctime = g_system->getMillis(); song_t *song = self->song; - sci_get_current_time(&ctime); - while (song) { - song->wakeup_time = time_plus(ctime, song->delay); + song->wakeup_time = ctime + song->delay; song = song->next_playing; } @@ -198,15 +172,11 @@ _sfx_set_song_status(sfx_state_t *self, song_t *song, int status) { case SOUND_STATUS_SUSPENDED: case SOUND_STATUS_WAITING: - if (song->status == SOUND_STATUS_PLAYING) { /* Update delay, set wakeup_time */ - GTimeVal time; - long delta; - sci_get_current_time(&time); - delta = time_minus(time, song->wakeup_time); + uint32 time = g_system->getMillis(); - song->delay -= delta; + song->delay -= long(time) - long(song->wakeup_time); song->wakeup_time = time; } if (status == SOUND_STATUS_SUSPENDED) @@ -217,7 +187,7 @@ _sfx_set_song_status(sfx_state_t *self, song_t *song, int status) { case SOUND_STATUS_PLAYING: if (song->status == SOUND_STATUS_STOPPED) /* Starting anew */ - sci_get_current_time(&song->wakeup_time); + song->wakeup_time = g_system->getMillis(); if (is_playing(self, song)) status = SOUND_STATUS_PLAYING; @@ -239,7 +209,6 @@ static void _update_single_song(sfx_state_t *self) { song_t *newsong = song_lib_find_active(self->songlib); if (newsong != self->song) { - _freeze_time(self); /* Store song delay time */ if (player) @@ -286,8 +255,7 @@ static void _update_single_song(sfx_state_t *self) { song_iterator_t *clonesong = songit_clone(newsong->it, newsong->delay); - player->add_iterator(clonesong, - newsong->wakeup_time); + player->add_iterator(clonesong, newsong->wakeup_time); } } } @@ -301,8 +269,7 @@ static void _update_multi_song(sfx_state_t *self) { song_t not_playing_anymore; /* Dummy object, referenced by ** songs which are no longer ** active. */ - GTimeVal tv; - sci_get_current_time(&tv); + /* _dump_playing_list(self, "before");*/ _freeze_time(self); /* Store song delay time */ @@ -359,7 +326,7 @@ static void _update_multi_song(sfx_state_t *self) { player->add_iterator(songit_clone(newseeker->it, newseeker->delay), - tv); + g_system->getMillis()); } _sfx_set_song_status(self, newseeker, SOUND_STATUS_PLAYING); @@ -535,10 +502,6 @@ void sfx_exit(sfx_state_t *self) { callbackMutex = 0; } -static inline int time_le(GTimeVal a, GTimeVal b) { - return time_minus(a, b) <= 0; -} - void sfx_suspend(sfx_state_t *self, int suspend) { #ifdef DEBUG_SONG_API fprintf(stderr, "[sfx-core] Suspending? = %d\n", suspend); @@ -582,11 +545,9 @@ int sfx_poll(sfx_state_t *self, song_handle_t *handle, int *cue) { } int sfx_poll_specific(sfx_state_t *self, song_handle_t handle, int *cue) { - GTimeVal ctime; + uint32 ctime = g_system->getMillis(); song_t *song = self->song; - sci_get_current_time(&ctime); - while (song && song->handle != handle) song = song->next_playing; @@ -601,16 +562,15 @@ int sfx_poll_specific(sfx_state_t *self, song_handle_t handle, int *cue) { unsigned char buf[8]; int result; - if (!time_le(song->wakeup_time, ctime)) + if (song->wakeup_time > ctime) return 0; /* Patience, young hacker! */ - result = songit_next(&(song->it), buf, cue, - IT_READER_MASK_ALL); + + result = songit_next(&(song->it), buf, cue, IT_READER_MASK_ALL); switch (result) { case SI_FINISHED: - _sfx_set_song_status(self, song, - SOUND_STATUS_STOPPED); + _sfx_set_song_status(self, song, SOUND_STATUS_STOPPED); _update(self); /* ...fall through... */ case SI_LOOP: @@ -634,9 +594,8 @@ int sfx_poll_specific(sfx_state_t *self, song_handle_t handle, int *cue) { default: if (result > 0) - song->wakeup_time = - time_plus(song->wakeup_time, - result * SOUND_TICK); + song->wakeup_time += result * SOUND_TICK; + /* Delay */ break; } @@ -690,7 +649,7 @@ int sfx_add_song(sfx_state_t *self, song_iterator_t *it, int priority, song_hand song->resource_num = number; song->hold = 0; song->loops = 0; - sci_get_current_time(&song->wakeup_time); /* No need to delay */ + song->wakeup_time = g_system->getMillis(); /* No need to delay */ song_lib_add(self->songlib, song); self->song = NULL; /* As above */ _update(self); diff --git a/engines/sci/sfx/mixer/soft.cpp b/engines/sci/sfx/mixer/soft.cpp index ed351be9bb..05812e7aaf 100644 --- a/engines/sci/sfx/mixer/soft.cpp +++ b/engines/sci/sfx/mixer/soft.cpp @@ -24,6 +24,7 @@ */ #include "common/mutex.h" +#include "common/system.h" #include "sci/tools.h" #include "sci/sfx/mixer.h" @@ -31,8 +32,8 @@ namespace Sci { -/* Max. number of microseconds in difference allowed between independent audio streams */ -#define TIMESTAMP_MAX_ALLOWED_DELTA 2000 +/* Max. number of milliseconds in difference allowed between independent audio streams */ +#define TIMESTAMP_MAX_ALLOWED_DELTA 2 /*#define DEBUG 3*/ /* Set DEBUG to one of the following: @@ -42,7 +43,7 @@ namespace Sci { ** >= 3 -- fully detailed input and output analysis (once per frame and feed) */ -/*#define DEBUG 0*/ +//#define DEBUG 1 #define MIN_DELTA_OBSERVATIONS 100 /* Number of times the mixer is called before it starts trying to improve latency */ #define MAX_DELTA_OBSERVATIONS 1000000 /* Number of times the mixer is called before we assume we truly understand timing */ @@ -61,9 +62,9 @@ struct mixer_private { int32 *compbuf_l, *compbuf_r; /* Intermediate buffers for computation */ int lastbuf_len; /* Number of frames stored in the last buffer */ - long skew; /* Millisecond relative to which we compute time. This is the millisecond + uint32 skew; /* Millisecond relative to which we compute time. This is the millisecond ** part of the first time we emitted sound, to simplify some computations. */ - long lsec; /* Last point in time we updated buffers, if any (seconds since the epoch) */ + uint32 lsec; /* Last point in time we updated buffers, if any (seconds since the epoch) */ int played_this_second; /* Number of frames emitted so far in second lsec */ int max_delta; /* maximum observed time delta (using 'frames' as a metric unit) */ @@ -85,11 +86,7 @@ static int mix_init(sfx_pcm_mixer_t *self, sfx_pcm_device_t *device) { P->compbuf_r = (int32*)sci_malloc(sizeof(int32) * device->buf_size); P->played_this_second = 0; P->paused = 0; -#ifdef DEBUG - sciprintf("[soft-mixer] Initialised device %s v%s (%d Hz, %d/%x)\n", - device->name, device->version, - device->conf.rate, device->conf.stereo, device->conf.format); -#endif + return SFX_OK; } @@ -355,12 +352,6 @@ static inline void mix_swap_buffers(sfx_pcm_mixer_t *self) { /* Swap buffers */ P->writebuf = tmp; } - -#define FRAME_OFFSET(usecs) \ - ((usecs >> 7) /* approximate, since uint32 is too small */ \ - * ((long) self->dev->conf.rate)) \ - / (1000000L >> 7) - static inline int mix_compute_buf_len(sfx_pcm_mixer_t *self, int *skip_frames) /* Computes the number of frames we ought to write. It tries to minimise the number, ** in order to reduce latency. */ @@ -369,35 +360,32 @@ static inline int mix_compute_buf_len(sfx_pcm_mixer_t *self, int *skip_frames) { int free_frames; int played_frames = 0; /* since the last call */ - long secs, usecs; + uint32 msecs; int frame_pos; int result_frames; - sci_gettime(&secs, &usecs); + msecs = g_system->getMillis(); if (!P->outbuf) { /* Called for the first time ever? */ - P->skew = usecs; - P->lsec = secs; + P->skew = msecs % 1000; + P->lsec = msecs / 1000; P->max_delta = 0; P->delta_observations = 0; P->played_this_second = 0; *skip_frames = 0; + return self->dev->buf_size; } /* fprintf(stderr, "[%d:%d]S%d ", secs, usecs, P->skew);*/ - if (P->skew > usecs) { - secs--; - usecs += (1000000 - P->skew); - } else - usecs -= P->skew; + msecs -= P->skew; - frame_pos = FRAME_OFFSET(usecs); + frame_pos = (msecs % 1000) * self->dev->conf.rate / 1000; played_frames = frame_pos - P->played_this_second - + ((secs - P->lsec) * self->dev->conf.rate); + + ((msecs / 1000 - P->lsec) * self->dev->conf.rate); /* fprintf(stderr, "%d:%d - %d:%d => %d\n", secs, frame_pos, P->lsec, P->played_this_second, played_frames); @@ -446,8 +434,8 @@ static inline int mix_compute_buf_len(sfx_pcm_mixer_t *self, int *skip_frames) /* recommended_frames = self->dev->buf_size; /\* Initially, keep the buffer full *\/ */ #if (DEBUG >= 1) - sciprintf("[soft-mixer] played since last time: %d, recommended: %d, free: %d\n", - played_frames, recommended_frames, free_frames); + sciprintf("[soft-mixer] played since last time: %d, free: %d\n", + played_frames, free_frames); #endif result_frames = free_frames; @@ -553,7 +541,7 @@ static void mix_compute_input_linear(sfx_pcm_mixer_t *self, int add_result, ) / fs->spd.den; - ts->secs = -1; + ts->msecs = 0; if (frames_nr > fs->buf_size) { fprintf(stderr, "%d (%d*%d + somethign) bytes, but only %d allowed!!!!!\n", @@ -791,16 +779,13 @@ static int mix_process_linear(sfx_pcm_mixer_t *self) { int have_timestamp = 0; sfx_timestamp_t start_timestamp; /* The timestamp at which the first frame will be played */ sfx_timestamp_t min_timestamp; - min_timestamp.secs = 0; + min_timestamp.msecs = 0; sfx_timestamp_t timestamp; if (self->dev->get_output_timestamp) start_timestamp = self->dev->get_output_timestamp(self->dev); - else { - long sec, usec; - sci_gettime(&sec, &usec); - start_timestamp = sfx_new_timestamp(sec, usec, self->dev->conf.rate); - } + else + start_timestamp = sfx_new_timestamp(g_system->getMillis(), self->dev->conf.rate); if ((P->outbuf) && (P->lastbuf_len)) { sfx_timestamp_t ts; @@ -846,9 +831,9 @@ static int mix_process_linear(sfx_pcm_mixer_t *self) { fake_buflen, ×tamp, start_timestamp); - if (timestamp.secs >= 0) { + if (timestamp.msecs > 0) { if (have_timestamp) { - int diff = sfx_timestamp_usecs_diff(min_timestamp, timestamp); + int diff = sfx_timestamp_msecs_diff(min_timestamp, timestamp); if (diff > 0) { /* New earlier timestamp */ timestamp = min_timestamp; @@ -890,7 +875,7 @@ static int mix_process_linear(sfx_pcm_mixer_t *self) { #endif if (timestamp_max_delta > TIMESTAMP_MAX_ALLOWED_DELTA) - sciprintf("[soft-mixer] Warning: Difference in timestamps between audio feeds is %d us\n", timestamp_max_delta); + sciprintf("[soft-mixer] Warning: Difference in timestamps between audio feeds is %d ms\n", timestamp_max_delta); mix_compute_output(self, buflen); P->lastbuf_len = buflen; @@ -899,7 +884,7 @@ static int mix_process_linear(sfx_pcm_mixer_t *self) { mix_swap_buffers(self); if (have_timestamp) P->outbuf_timestamp = sfx_timestamp_add(min_timestamp, - timestamp_max_delta >> 1); + timestamp_max_delta * 500); P->have_outbuf_timestamp = have_timestamp; } diff --git a/engines/sci/sfx/player/polled.cpp b/engines/sci/sfx/player/polled.cpp index 869f7c00b2..339b418a77 100644 --- a/engines/sci/sfx/player/polled.cpp +++ b/engines/sci/sfx/player/polled.cpp @@ -202,7 +202,7 @@ static int pp_init(ResourceManager *resmgr, int expected_latency) { return SFX_OK; } -static int pp_add_iterator(song_iterator_t *it, GTimeVal start_time) { +static int pp_add_iterator(song_iterator_t *it, uint32 start_time) { song_iterator_t *old = play_it; SIMSG_SEND(it, SIMSG_SET_PLAYMASK(seq->playmask)); @@ -218,9 +218,7 @@ static int pp_add_iterator(song_iterator_t *it, GTimeVal start_time) { /* The check must happen HERE, and not at the beginning of the function, to avoid a race condition with the mixer. */ if (old == NULL) { - new_timestamp = sfx_new_timestamp(start_time.tv_sec, - start_time.tv_usec, - seq->pcm_conf.rate); + new_timestamp = sfx_new_timestamp(start_time, seq->pcm_conf.rate); /* ASAP otherwise */ time_counter = 0; new_song = 1; diff --git a/engines/sci/sfx/player/realtime.cpp b/engines/sci/sfx/player/realtime.cpp index ab08d7f44b..84b40ced53 100644 --- a/engines/sci/sfx/player/realtime.cpp +++ b/engines/sci/sfx/player/realtime.cpp @@ -27,9 +27,12 @@ ** prays for some reasonable amount of soft real-time, but it's close ** enough, I guess. */ +#include "sci/tools.h" #include "sci/sfx/sfx_player.h" #include "sci/sfx/sequencer.h" +#include "common/system.h" + namespace Sci { static sfx_sequencer_t *seq; @@ -38,38 +41,14 @@ extern sfx_player_t sfx_player_realtime; /* Playing mechanism */ -static inline GTimeVal -current_time(void) { - GTimeVal tv; - sci_get_current_time(&tv); - return tv; -} - -static inline GTimeVal -add_time_delta(GTimeVal tv, long delta) { - int sec_d; - - tv.tv_usec += delta; - sec_d = tv.tv_usec / 1000000; - tv.tv_usec -= sec_d * 1000000; - - tv.tv_sec += sec_d; - - return tv; -} - -static inline long -delta_time(GTimeVal comp_tv, GTimeVal base) { - GTimeVal tv; - sci_get_current_time(&tv); - return (comp_tv.tv_sec - tv.tv_sec) * 1000000 - + (comp_tv.tv_usec - tv.tv_usec); +static inline int delta_time(const uint32 comp, const uint32 base) { + return long(comp) - long(base); } static song_iterator_t *play_it = NULL; -static GTimeVal play_last_time; -static GTimeVal play_pause_started; /* Beginning of the last pause */ -static GTimeVal play_pause_counter; /* Last point in time to mark a +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; @@ -77,20 +56,19 @@ static int play_writeahead = 0; static int play_moredelay = 0; static void -play_song(song_iterator_t *it, GTimeVal *wakeup_time, int writeahead_time) { +play_song(song_iterator_t *it, uint32 *wakeup_time, int writeahead_time) { unsigned char buf[8]; int result; if (play_paused) { - GTimeVal ct; - sci_get_current_time(&ct); + uint32 ct = g_system->getMillis(); + + *wakeup_time += delta_time(play_pause_counter, ct); - *wakeup_time = - add_time_delta(*wakeup_time, delta_time(play_pause_counter, ct)); play_pause_counter = ct; } else /* Not paused: */ - while (play_it && delta_time(*wakeup_time, current_time()) + while (play_it && delta_time(*wakeup_time, g_system->getMillis()) < writeahead_time) { int delay; @@ -121,7 +99,7 @@ play_song(song_iterator_t *it, GTimeVal *wakeup_time, int writeahead_time) { default: play_moredelay = delay - 1; - *wakeup_time = song_next_wakeup_time(wakeup_time, delay); + *wakeup_time += delay * SOUND_TICK; if (seq->delay) seq->delay(delay); } @@ -137,12 +115,12 @@ static void rt_timer_callback(void) { if (play_it && !play_it_done) { if (!play_moredelay) { - long delta = delta_time(play_last_time, current_time()); + 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 > 15000) { - play_writeahead -= 2500; /* Adjust downwards */ + } else if (delta > 15) { + play_writeahead -= 3; /* Adjust downwards */ } } else --play_moredelay; @@ -179,7 +157,6 @@ static int rt_init(ResourceManager *resmgr, int expected_latency) { resource_t *res = NULL, *res2 = NULL; void *seq_dev = NULL; - GTimeVal foo = {0, 0}; seq = sfx_find_sequencer(NULL); @@ -209,16 +186,16 @@ rt_init(ResourceManager *resmgr, int expected_latency) { if (play_writeahead < seq->min_write_ahead_ms) play_writeahead = seq->min_write_ahead_ms; - play_writeahead *= 1000; /* microseconds */ + play_writeahead *= 1; /* milliseconds */ if (seq->reset_timer) - seq->reset_timer(foo); + seq->reset_timer(0); return SFX_OK; } static int -rt_add_iterator(song_iterator_t *it, GTimeVal start_time) { +rt_add_iterator(song_iterator_t *it, uint32 start_time) { if (seq->reset_timer) /* Restart timer counting if possible */ seq->reset_timer(start_time); @@ -264,7 +241,7 @@ rt_send_iterator_message(song_iterator_message_t msg) { static int rt_pause(void) { - sci_get_current_time(&play_pause_started); + play_pause_started = g_system->getMillis(); /* Also, indicate that we haven't modified the time counter ** yet */ play_pause_counter = play_pause_started; diff --git a/engines/sci/sfx/seq/gm.cpp b/engines/sci/sfx/seq/gm.cpp index ebd0b47bf6..7daa2b9535 100644 --- a/engines/sci/sfx/seq/gm.cpp +++ b/engines/sci/sfx/seq/gm.cpp @@ -78,7 +78,7 @@ midi_gm_delay(int ticks) { } static int -midi_gm_reset_timer(GTimeVal ts) { +midi_gm_reset_timer(uint32 ts) { writer->reset_timer(writer); return SFX_OK; diff --git a/engines/sci/sfx/sequencer.h b/engines/sci/sfx/sequencer.h index 182e2bf771..a2f5fb789c 100644 --- a/engines/sci/sfx/sequencer.h +++ b/engines/sci/sfx/sequencer.h @@ -29,7 +29,6 @@ #include "common/scummsys.h" -#include "sci/tools.h" // For GTimeVal #include "sci/sfx/sfx_core.h" #include "sci/sfx/device.h" @@ -88,10 +87,10 @@ struct sfx_sequencer_t { ** Returns : SFX_OK on success, SFX_ERROR otherwise */ - int (*reset_timer)(GTimeVal ts); + int (*reset_timer)(uint32 ts); /* OPTIONAL -- may be NULL, but highly recommended in combination with delay() */ /* Resets the timer counter associated with the 'delay()' function - ** Parameters: (GTimeVal) ts: Timestamp of the base time + ** Parameters: (uint32) ts: Timestamp of the base time ** Returns : SFX_OK on success, SFX_ERROR otherwise */ diff --git a/engines/sci/sfx/sfx_engine.h b/engines/sci/sfx/sfx_engine.h index 059cf370c7..642c84c068 100644 --- a/engines/sci/sfx/sfx_engine.h +++ b/engines/sci/sfx/sfx_engine.h @@ -34,8 +34,8 @@ namespace Sci { -#define SOUND_TICK 1000000 / 60 -/* Approximately 16666 microseconds */ +#define SOUND_TICK 1000 / 60 +/* Approximately 17 milliseconds */ #define SFX_STATE_FLAG_MULTIPLAY (1 << 0) /* More than one song playable diff --git a/engines/sci/sfx/sfx_player.h b/engines/sci/sfx/sfx_player.h index 68a915c3a0..8718622428 100644 --- a/engines/sci/sfx/sfx_player.h +++ b/engines/sci/sfx/sfx_player.h @@ -32,6 +32,8 @@ #include "sci/sfx/sfx_iterator.h" #include "sci/include/sciresource.h" +#include "common/scummsys.h" + namespace Sci { typedef void tell_synth_func(int buf_nr, byte *buf); @@ -55,10 +57,10 @@ struct sfx_player_t { ** Returns : (int) SFX_OK on success, SFX_ERROR on failure */ - int (*add_iterator)(song_iterator_t *it, GTimeVal start_time); + int (*add_iterator)(song_iterator_t *it, uint32 start_time); /* Adds an iterator to the song player ** Parameters: (songx_iterator_t *) it: The iterator to play - ** (GTimeVal) start_time: The time to assume as the + ** (uint32) start_time: The time to assume as the ** time the first MIDI command executes at ** Returns : (int) SFX_OK on success, SFX_ERROR on failure ** The iterator should not be cloned (to avoid memory leaks) and diff --git a/engines/sci/sfx/sfx_songlib.h b/engines/sci/sfx/sfx_songlib.h index f390b3828d..bf8dc8b5a9 100644 --- a/engines/sci/sfx/sfx_songlib.h +++ b/engines/sci/sfx/sfx_songlib.h @@ -30,7 +30,6 @@ #include "common/scummsys.h" -#include "sci/tools.h" // For GTimeVal #include "sci/sfx/sfx_iterator.h" namespace Sci { @@ -66,7 +65,7 @@ struct song_t { song_iterator_t *it; long delay; /* Delay before accessing the iterator, in microseconds */ - GTimeVal wakeup_time; /* Used by the sound core: + uint32 wakeup_time; /* Used by the sound core: ** Playing -> time at which 'delay' has elapsed ** Suspended/Waiting -> stopping time */ @@ -161,21 +160,6 @@ int song_lib_count(songlib_t songlib); ** Returns : (int) The number of songs */ -GTimeVal song_sleep_time(GTimeVal *lastslept, long ticks); -/* Caluculates the amount of seconds and microseconds to sleep. -** Parameters: (GTimeVal *) lastslept: The time to start counting on -** (long) ticks: Number of ticks to sleep -** Returns : (GTimeVal) The amount of time to sleep -*/ - -GTimeVal song_next_wakeup_time(GTimeVal *lastslept, long ticks); -/* Calculates the time at which "ticks" have passed, counting from "lastslept". -** Parameters: (GTimeVal *) lastslept: The base to start counting on -** (long) ticks: Number of ticks to count -** Returns : (GTimeVal) A structure describing the time at which the -** specified number of ticks has passed -*/ - void song_lib_set_restore_behavior(songlib_t songlib, song_handle_t handle, RESTORE_BEHAVIOR action); /* Determines what should be done with the song "handle" when diff --git a/engines/sci/sfx/sfx_time.h b/engines/sci/sfx/sfx_time.h index 512dc70cd1..2a9be71ced 100644 --- a/engines/sci/sfx/sfx_time.h +++ b/engines/sci/sfx/sfx_time.h @@ -26,20 +26,21 @@ #ifndef SCI_SFX_SFX_TIME_H #define SCI_SFX_SFX_TIME_H +#include "common/scummsys.h" + namespace Sci { struct sfx_timestamp_t { - long secs; - long usecs; + uint32 msecs; int frame_rate; int frame_offset; - /* Total time: secs + usecs + frame_offset/frame_rate */ + /* Total time: msecs + frame_offset/frame_rate */ }; -sfx_timestamp_t sfx_new_timestamp(long secs, long usecs, int frame_rate); +sfx_timestamp_t sfx_new_timestamp(const uint32 msecs, const int frame_rate); /* Creates a new mutable timestamp -** Parameters: (long x long) (secs, usecs): Initial timestamp +** Parameters: (uint32) msecs: Initial timestamp ** (int) frame_rate: Frame rate, for increasing the time stamp */ @@ -66,18 +67,17 @@ int sfx_timestamp_frame_diff(sfx_timestamp_t a, sfx_timestamp_t b); ** Returns : (int) a-b */ -long sfx_timestamp_usecs_diff(sfx_timestamp_t a, sfx_timestamp_t b); -/* Computes the difference (# of microseconds) between two timestamps +int sfx_timestamp_msecs_diff(sfx_timestamp_t a, sfx_timestamp_t b); +/* Computes the difference (# of milliseconds) between two timestamps ** Parameters: (sfx_timestamp) a: See below ** (sfx_timestamp) b: See below -** Returns : (long) a-b +** Returns : (int) a-b */ -void sfx_timestamp_gettime(sfx_timestamp_t *timestamp, long *secs, long *usecs); +void sfx_timestamp_gettime(sfx_timestamp_t *timestamp, uint32 *msecs); /* Determines the time described by a given timestamp ** Parameters: (sfx_timestamp_t *) timestamp: Timestamp to read from -** Returns : (int * x int *) (secs, usecs): Seconds and microseconds since -** the epoch described there +** Returns : (uint32 *) msecs: Milliseconds since startup */ } // End of namespace Sci diff --git a/engines/sci/sfx/songlib.cpp b/engines/sci/sfx/songlib.cpp index 5a53affe18..6b2703b95b 100644 --- a/engines/sci/sfx/songlib.cpp +++ b/engines/sci/sfx/songlib.cpp @@ -24,6 +24,8 @@ */ #include <stdio.h> + +#include "sci/tools.h" #include "sci/sfx/sfx_engine.h" #include "sci/sci_memory.h" @@ -31,43 +33,6 @@ namespace Sci { #define debug_stream stderr -GTimeVal song_sleep_time(GTimeVal *lastslept, long ticks) { - GTimeVal tv; - long timetosleep; - long timeslept; /* Time already slept */ - timetosleep = ticks * SOUND_TICK; /* Time to sleep in us */ - - sci_get_current_time(&tv); - timeslept = 1000000 * (tv.tv_sec - lastslept->tv_sec) + - tv.tv_usec - lastslept->tv_usec; - - timetosleep -= timeslept; - - if (timetosleep < 0) - timetosleep = 0; - - tv.tv_sec = timetosleep / 1000000; - tv.tv_usec = timetosleep % 1000000; - - return tv; -} - - -GTimeVal song_next_wakeup_time(GTimeVal *lastslept, long ticks) { - GTimeVal retval; - - retval.tv_sec = lastslept->tv_sec + (ticks / 60); - retval.tv_usec = lastslept->tv_usec + ((ticks % 60) * SOUND_TICK); - - if (retval.tv_usec >= 1000000) { - retval.tv_usec -= 1000000; - ++retval.tv_sec; - } - - return retval; -} - - song_t *song_new(song_handle_t handle, song_iterator_t *it, int priority) { song_t *retval; retval = (song_t*) sci_malloc(sizeof(song_t)); diff --git a/engines/sci/sfx/time.cpp b/engines/sci/sfx/time.cpp index 720c233968..b765a56685 100644 --- a/engines/sci/sfx/time.cpp +++ b/engines/sci/sfx/time.cpp @@ -28,10 +28,9 @@ namespace Sci { -sfx_timestamp_t sfx_new_timestamp(long secs, long usecs, int frame_rate) { +sfx_timestamp_t sfx_new_timestamp(const uint32 msecs, const int frame_rate) { sfx_timestamp_t r; - r.secs = secs; - r.usecs = usecs; + r.msecs = msecs; r.frame_rate = frame_rate; r.frame_offset = 0; @@ -45,73 +44,50 @@ sfx_timestamp_t sfx_timestamp_add(sfx_timestamp_t timestamp, int frames) { int secsub = 1 + (-timestamp.frame_offset / timestamp.frame_rate); timestamp.frame_offset += timestamp.frame_rate * secsub; - timestamp.secs -= secsub; + timestamp.msecs -= secsub * 1000; } - timestamp.secs += (timestamp.frame_offset / timestamp.frame_rate); + timestamp.msecs += (timestamp.frame_offset / timestamp.frame_rate) * 1000; timestamp.frame_offset %= timestamp.frame_rate; return timestamp; } int sfx_timestamp_frame_diff(sfx_timestamp_t a, sfx_timestamp_t b) { - long usecdelta = 0; + int msecdelta = 0; if (a.frame_rate != b.frame_rate) { fprintf(stderr, "Fatal: The semantics of subtracting two timestamps with a different base from each other is not defined!\n"); BREAKPOINT(); } - if (a.usecs != b.usecs) { -#if (SIZEOF_LONG >= 8) - usecdelta = (a.usecs * a.frame_rate) / 1000000 - - (b.usecs * b.frame_rate) / 1000000; -#else - usecdelta = ((a.usecs / 1000) * a.frame_rate) / 1000 - - ((b.usecs / 1000) * b.frame_rate) / 1000; -#endif - } + if (a.msecs != b.msecs) + msecdelta = (long(a.msecs) - long(b.msecs)) * a.frame_rate / 1000; - return usecdelta - + (a.secs - b.secs) * a.frame_rate - + a.frame_offset - b.frame_offset; + return msecdelta + a.frame_offset - b.frame_offset; } -long sfx_timestamp_usecs_diff(sfx_timestamp_t t1, sfx_timestamp_t t2) { - long secs1, secs2; - long usecs1, usecs2; +int sfx_timestamp_msecs_diff(sfx_timestamp_t t1, sfx_timestamp_t t2) { + uint32 msecs1, msecs2; - sfx_timestamp_gettime(&t1, &secs1, &usecs1); - sfx_timestamp_gettime(&t2, &secs2, &usecs2); + sfx_timestamp_gettime(&t1, &msecs1); + sfx_timestamp_gettime(&t2, &msecs2); - return (usecs1 - usecs2) + ((secs1 - secs2) * 1000000); + return long(msecs1) - long(msecs2); } sfx_timestamp_t sfx_timestamp_renormalise(sfx_timestamp_t timestamp, int new_freq) { sfx_timestamp_t r; - sfx_timestamp_gettime(×tamp, &r.secs, &r.usecs); + sfx_timestamp_gettime(×tamp, &r.msecs); r.frame_rate = new_freq; r.frame_offset = 0; return r; } -void sfx_timestamp_gettime(sfx_timestamp_t *timestamp, long *secs, long *usecs) { - long ust = timestamp->usecs; - /* On 64 bit machines, we can do an accurate computation */ -#if (SIZEOF_LONG >= 8) - ust += (timestamp->frame_offset * 1000000l) / (timestamp->frame_rate); -#else - ust += (timestamp->frame_offset * 1000l) / (timestamp->frame_rate / 1000l); -#endif - - if (ust > 1000000) { - ust -= 1000000; - *secs = timestamp->secs + 1; - } else - *secs = timestamp->secs; - - *usecs = ust; +void sfx_timestamp_gettime(sfx_timestamp_t *timestamp, uint32 *msecs) { + *msecs = timestamp->msecs + + timestamp->frame_offset * 1000l / timestamp->frame_rate; } } // End of namespace Sci diff --git a/engines/sci/tools.cpp b/engines/sci/tools.cpp index 757190e277..58c86cd73a 100644 --- a/engines/sci/tools.cpp +++ b/engines/sci/tools.cpp @@ -29,7 +29,6 @@ # include <windows.h> # include <errno.h> # include <mmsystem.h> -# include <sys/timeb.h> # include <sys/types.h> # include <sys/stat.h> # include <direct.h> @@ -44,10 +43,6 @@ namespace Sci { -#ifndef _MSC_VER -# include <sys/time.h> -#endif - int script_debug_flag = 0; // Defaulting to running mode int sci_debug_flags = 0; // Special flags @@ -132,47 +127,4 @@ void _SCIGNUkdebug(const char *funcname, EngineState *s, const char *file, int l } } - -#ifndef _MSC_VER -void sci_gettime(long *seconds, long *useconds) { - struct timeval tv; - - assert(!gettimeofday(&tv, NULL)); - *seconds = tv.tv_sec; - *useconds = tv.tv_usec; -} -#elif defined (WIN32) - -/*WARNING(Incorrect)*/ -/* Warning: This function only retrieves the amount of mseconds since the start of -** the Win32 kernel; it does /not/ provide the number of seconds since the epoch! -** There are no known cases where this causes problems, though. */ -void sci_gettime(long *seconds, long *useconds) { - DWORD tm; - - if (TIMERR_NOERROR != timeBeginPeriod(1)) { - fprintf(stderr, "timeBeginPeriod(1) failed in sci_gettime\n"); - } - - tm = timeGetTime(); - - if (TIMERR_NOERROR != timeEndPeriod(1)) { - fprintf(stderr, "timeEndPeriod(1) failed in sci_gettime\n"); - } - - *seconds = tm / 1000; - *useconds = (tm % 1000) * 1000; -} -#else -# error "You need to provide a microsecond resolution sci_gettime implementation for your platform!" -#endif - - -void sci_get_current_time(GTimeVal *val) { - long foo, bar; - sci_gettime(&foo, &bar); - val->tv_sec = foo; - val->tv_usec = bar; -} - } // End of namespace Sci diff --git a/engines/sci/tools.h b/engines/sci/tools.h index 4bc0330bd2..dc5a1a33c5 100644 --- a/engines/sci/tools.h +++ b/engines/sci/tools.h @@ -31,40 +31,14 @@ namespace Sci { - -struct GTimeVal { - long tv_sec; - long tv_usec; -}; - - - /**** FUNCTION DECLARATIONS ****/ #define getInt16 (int16)READ_LE_UINT16 #define getUInt16 READ_LE_UINT16 #define putInt16 WRITE_LE_UINT16 - /* --- */ -void sci_gettime(long *seconds, long *useconds); -/* Calculates the current time in seconds and microseconds -** Parameters: (long *) seconds: Pointer to the variable the seconds part of the -** current time will be stored in -** (long *) useconds: Pointer to the variable the microseconds part -** of the current time will be stored in -** Returns : (void) -** The resulting values must be relative to an arbitrary fixed point in time -** (typically 01/01/1970 on *NIX systems). -*/ - -void sci_get_current_time(GTimeVal *val); -/* GTimeVal version of sci_gettime() -** Parameters: (GTimeVal *) val: Pointer to the structure the values will be stored in -** Returns : (void) -*/ - int sciprintf(const char *fmt, ...) GCC_PRINTF(1, 2); #define gfxprintf sciprintf /* Prints a string to the console stack |