From 4fdceafc5277a316f0267ac3549a1a6c9fe07eb2 Mon Sep 17 00:00:00 2001 From: ghaushe Date: Thu, 30 Jan 2003 04:28:59 +0000 Subject: [PATCH] * fix o-scope problems with ff/frev and the streaming code git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@604 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/src/sc2code/libs/sound/mixsdl/sound.c | 2 +- sc2/src/sc2code/libs/sound/mixsdl/sound.h | 4 ++- sc2/src/sc2code/libs/sound/mixsdl/stream.c | 25 ++++++++++++------- .../sc2code/libs/sound/mixsdl/trackplayer.c | 20 +++++++-------- sc2/src/sc2code/libs/sound/openal/sound.c | 2 +- sc2/src/sc2code/libs/sound/openal/sound.h | 4 ++- sc2/src/sc2code/libs/sound/openal/stream.c | 23 +++++++++++------ .../sc2code/libs/sound/openal/trackplayer.c | 18 ++++++------- 8 files changed, 57 insertions(+), 41 deletions(-) diff --git a/sc2/src/sc2code/libs/sound/mixsdl/sound.c b/sc2/src/sc2code/libs/sound/mixsdl/sound.c index d09d6395f..0e7994e80 100644 --- a/sc2/src/sc2code/libs/sound/mixsdl/sound.c +++ b/sc2/src/sc2code/libs/sound/mixsdl/sound.c @@ -93,7 +93,7 @@ TFB_InitSound (int driver, int flags) soundSource[i].sbuffer = NULL; soundSource[i].sbuf_start = 0; soundSource[i].sbuf_size = 0; - soundSource[i].total_decoded = 0; + soundSource[i].sbuf_offset = 0; } SetSFXVolume (sfxVolumeScale); diff --git a/sc2/src/sc2code/libs/sound/mixsdl/sound.h b/sc2/src/sc2code/libs/sound/mixsdl/sound.h index 54ffe2685..3b92dcbb2 100644 --- a/sc2/src/sc2code/libs/sound/mixsdl/sound.h +++ b/sc2/src/sc2code/libs/sound/mixsdl/sound.h @@ -31,6 +31,7 @@ #define MUSIC_SOURCE (LAST_SFX_SOURCE + 1) #define SPEECH_SOURCE (MUSIC_SOURCE + 1) #define NUM_SOUNDSOURCES (SPEECH_SOURCE + 1) +#define PAD_SCOPE_BYTES 8096 typedef struct { @@ -78,7 +79,8 @@ typedef struct tfb_soundsource void *sbuffer; uint32 sbuf_start; uint32 sbuf_size; - uint32 total_decoded; + uint32 sbuf_offset; + uint32 sbuf_lasttime; } TFB_SoundSource; diff --git a/sc2/src/sc2code/libs/sound/mixsdl/stream.c b/sc2/src/sc2code/libs/sound/mixsdl/stream.c index 2ac93f46e..7e8b35565 100644 --- a/sc2/src/sc2code/libs/sound/mixsdl/stream.c +++ b/sc2/src/sc2code/libs/sound/mixsdl/stream.c @@ -53,7 +53,8 @@ PlayStream (TFB_SoundSample *sample, uint32 source, bool looping, bool scope, bo if (scope) { - soundSource[source].sbuffer = NULL; + pos = PAD_SCOPE_BYTES; + soundSource[source].sbuffer = HMalloc (pos); } if (sample->read_chain_ptr && sample->read_chain_ptr->tag.type) @@ -79,11 +80,8 @@ PlayStream (TFB_SoundSample *sample, uint32 source, bool looping, bool scope, bo if (scope) { UBYTE *p; - if (! soundSource[source].sbuffer) - soundSource[source].sbuffer = HMalloc (decoded_bytes); - else - soundSource[source].sbuffer = HRealloc (soundSource[source].sbuffer, - pos + decoded_bytes); + soundSource[source].sbuffer = HRealloc (soundSource[source].sbuffer, + pos + decoded_bytes); p = (UBYTE *)soundSource[source].sbuffer; memcpy (&p[pos], sample->decoder->buffer, decoded_bytes); pos += decoded_bytes; @@ -112,6 +110,8 @@ PlayStream (TFB_SoundSample *sample, uint32 source, bool looping, bool scope, bo for ( ; i num_buffers; ++i) sample->buffer_tag[i] = 0; soundSource[source].sbuf_size = pos; + soundSource[source].sbuf_start = pos - PAD_SCOPE_BYTES; + soundSource[source].sbuf_lasttime = GetTimeCounter (); soundSource[source].start_time = (sint32)GetTimeCounter () - offset; soundSource[source].stream_should_be_playing = TRUE; mixSDL_SourcePlay (soundSource[source].handle); @@ -134,7 +134,7 @@ StopStream (uint32 source) } soundSource[source].sbuf_start = 0; soundSource[source].sbuf_size = 0; - soundSource[source].total_decoded = 0; + soundSource[source].sbuf_offset = 0; mixSDL_SourceStop (soundSource[source].handle); mixSDL_GetSourcei (soundSource[source].handle, MIX_BUFFERS_PROCESSED, &processed); @@ -268,8 +268,15 @@ StreamDecoderTaskFunc (void *data) } } } - - soundSource[i].total_decoded += soundSource[i].sample->decoder->buffer_size; + { + uint32 buf_size; + soundSource[i].sbuf_lasttime = GetTimeCounter (); + mixSDL_GetBufferi(buffer, MIX_SIZE, &buf_size); + soundSource[i].sbuf_offset += buf_size; + if (soundSource[i].sbuf_offset > soundSource[i].sbuf_size) + soundSource[i].sbuf_offset -= soundSource[i].sbuf_size; + } + //soundSource[i].total_decoded += soundSource[i].sample->decoder->buffer_size; if (soundSource[i].sample->decoder->error) { diff --git a/sc2/src/sc2code/libs/sound/mixsdl/trackplayer.c b/sc2/src/sc2code/libs/sound/mixsdl/trackplayer.c index b0518c2ff..aa2c3d859 100644 --- a/sc2/src/sc2code/libs/sound/mixsdl/trackplayer.c +++ b/sc2/src/sc2code/libs/sound/mixsdl/trackplayer.c @@ -544,12 +544,12 @@ GetSoundData (void *data) PlayingStream (SPEECH_SOURCE) && soundSource[SPEECH_SOURCE].sbuffer && soundSource[SPEECH_SOURCE].sbuf_size > 0) { - float played_time = (GetTimeCounter () - soundSource[SPEECH_SOURCE].start_time) / + float played_time = (GetTimeCounter () - soundSource[SPEECH_SOURCE].sbuf_lasttime) / (float)ONE_SECOND; - int played_data = (int) (played_time * (float)soundSource[SPEECH_SOURCE]. + long delta = (int) (played_time * (float)soundSource[SPEECH_SOURCE]. sample->decoder->frequency * 2.0f); - int delta = played_data - soundSource[SPEECH_SOURCE].total_decoded; - unsigned int pos, i; + unsigned long pos; + int i; UBYTE *scopedata = (UBYTE *) data; UBYTE *sbuffer = soundSource[SPEECH_SOURCE].sbuffer; @@ -569,7 +569,7 @@ GetSoundData (void *data) //fprintf (stderr, "played_data %d total_decoded %d delta %d\n", played_data, soundSource[SPEECH_SOURCE].total_decoded, delta); - pos = soundSource[SPEECH_SOURCE].sbuf_start + delta; + pos = soundSource[SPEECH_SOURCE].sbuf_offset + delta; if (pos % 2 == 1) pos++; @@ -612,12 +612,12 @@ GetSoundData (void *data) PlayingStream (MUSIC_SOURCE) && soundSource[MUSIC_SOURCE].sbuffer && soundSource[MUSIC_SOURCE].sbuf_size > 0) { - float played_time = (GetTimeCounter () - soundSource[MUSIC_SOURCE].start_time) / + float played_time = (GetTimeCounter () - soundSource[MUSIC_SOURCE].sbuf_lasttime) / (float)ONE_SECOND; - int played_data = (int) (played_time * (float)soundSource[MUSIC_SOURCE]. + int delta = (int) (played_time * (float)soundSource[MUSIC_SOURCE]. sample->decoder->frequency * 4.0f); - int delta = played_data - soundSource[MUSIC_SOURCE].total_decoded; - unsigned int pos, i, step; + unsigned long pos; + int i, step; UBYTE *scopedata = (UBYTE *) data; UBYTE *sbuffer = soundSource[MUSIC_SOURCE].sbuffer; @@ -641,7 +641,7 @@ GetSoundData (void *data) //fprintf (stderr, "played_data %d total_decoded %d delta %d\n", played_data, soundSource[MUSIC_SOURCE].total_decoded, delta); - pos = soundSource[MUSIC_SOURCE].sbuf_start + delta; + pos = soundSource[SPEECH_SOURCE].sbuf_offset + delta; if (pos % 2 == 1) pos++; diff --git a/sc2/src/sc2code/libs/sound/openal/sound.c b/sc2/src/sc2code/libs/sound/openal/sound.c index 4869aa54c..51ea782e7 100644 --- a/sc2/src/sc2code/libs/sound/openal/sound.c +++ b/sc2/src/sc2code/libs/sound/openal/sound.c @@ -96,7 +96,7 @@ TFB_InitSound (int driver, int flags) soundSource[i].sbuffer = NULL; soundSource[i].sbuf_start = 0; soundSource[i].sbuf_size = 0; - soundSource[i].total_decoded = 0; + soundSource[i].sbuf_offset = 0; } SetSFXVolume (sfxVolumeScale); diff --git a/sc2/src/sc2code/libs/sound/openal/sound.h b/sc2/src/sc2code/libs/sound/openal/sound.h index 60c431462..b304479b7 100644 --- a/sc2/src/sc2code/libs/sound/openal/sound.h +++ b/sc2/src/sc2code/libs/sound/openal/sound.h @@ -40,6 +40,7 @@ #define MUSIC_SOURCE (LAST_SFX_SOURCE + 1) #define SPEECH_SOURCE (MUSIC_SOURCE + 1) #define NUM_SOUNDSOURCES (SPEECH_SOURCE + 1) +#define PAD_SCOPE_BYTES 8096 typedef struct { @@ -88,7 +89,8 @@ typedef struct tfb_soundsource void *sbuffer; ALuint sbuf_start; ALuint sbuf_size; - ALuint total_decoded; + ALuint sbuf_offset; + ALuint sbuf_lasttime; } TFB_SoundSource; diff --git a/sc2/src/sc2code/libs/sound/openal/stream.c b/sc2/src/sc2code/libs/sound/openal/stream.c index b0a110c07..e0c52ce2f 100644 --- a/sc2/src/sc2code/libs/sound/openal/stream.c +++ b/sc2/src/sc2code/libs/sound/openal/stream.c @@ -56,7 +56,8 @@ PlayStream (TFB_SoundSample *sample, ALuint source, ALboolean looping, if (scope) { - soundSource[source].sbuffer = NULL; + pos = PAD_SCOPE_BYTES; + soundSource[source].sbuffer = HMalloc (pos); } if (sample->read_chain_ptr && sample->read_chain_ptr->tag.type) @@ -98,11 +99,8 @@ PlayStream (TFB_SoundSample *sample, ALuint source, ALboolean looping, if (scope) { UBYTE *p; - if (! soundSource[source].sbuffer) - soundSource[source].sbuffer = HMalloc (decoded_bytes); - else - soundSource[source].sbuffer = HRealloc (soundSource[source].sbuffer, - pos + decoded_bytes); + soundSource[source].sbuffer = HRealloc (soundSource[source].sbuffer, + pos + decoded_bytes); p = (UBYTE *)soundSource[source].sbuffer; memcpy (&p[pos], sample->decoder->buffer, decoded_bytes); pos += decoded_bytes; @@ -131,6 +129,8 @@ PlayStream (TFB_SoundSample *sample, ALuint source, ALboolean looping, for ( ; i num_buffers; ++i) sample->buffer_tag[i] = 0; soundSource[source].sbuf_size = pos; + soundSource[source].sbuf_start = pos - PAD_SCOPE_BYTES; + soundSource[source].sbuf_lasttime = GetTimeCounter (); soundSource[source].start_time = (ALint)GetTimeCounter () - offset; soundSource[source].stream_should_be_playing = TRUE; alSourcePlay (soundSource[source].handle); @@ -153,7 +153,7 @@ StopStream (ALuint source) } soundSource[source].sbuf_start = 0; soundSource[source].sbuf_size = 0; - soundSource[source].total_decoded = 0; + soundSource[source].sbuf_offset = 0; alSourceStop (soundSource[source].handle); alGetSourcei (soundSource[source].handle, AL_BUFFERS_PROCESSED, &processed); @@ -288,7 +288,14 @@ StreamDecoderTaskFunc (void *data) } } - soundSource[i].total_decoded += soundSource[i].sample->decoder->buffer_size; + { + ALuint buf_size; + soundSource[i].sbuf_lasttime = GetTimeCounter (); + alGetBufferi(buffer, AL_SIZE, &buf_size); + soundSource[i].sbuf_offset += buf_size; + if (soundSource[i].sbuf_offset > soundSource[i].sbuf_size) + soundSource[i].sbuf_offset -= soundSource[i].sbuf_size; + } if (soundSource[i].sample->decoder->error) { diff --git a/sc2/src/sc2code/libs/sound/openal/trackplayer.c b/sc2/src/sc2code/libs/sound/openal/trackplayer.c index 230d76123..9e1985837 100644 --- a/sc2/src/sc2code/libs/sound/openal/trackplayer.c +++ b/sc2/src/sc2code/libs/sound/openal/trackplayer.c @@ -544,12 +544,11 @@ GetSoundData (void *data) PlayingStream (SPEECH_SOURCE) && soundSource[SPEECH_SOURCE].sbuffer && soundSource[SPEECH_SOURCE].sbuf_size > 0) { - float played_time = (GetTimeCounter () - soundSource[SPEECH_SOURCE].start_time) / + float played_time = (GetTimeCounter () - soundSource[SPEECH_SOURCE].sbuf_lasttime) / (float)ONE_SECOND; - int played_data = (int) (played_time * (float)soundSource[SPEECH_SOURCE]. + int delta = (int) (played_time * (float)soundSource[SPEECH_SOURCE]. sample->decoder->frequency * 2.0f); - int delta = played_data - soundSource[SPEECH_SOURCE].total_decoded; - unsigned int pos, i; + unsigned long pos, i; UBYTE *scopedata = (UBYTE *) data; UBYTE *sbuffer = soundSource[SPEECH_SOURCE].sbuffer; @@ -569,7 +568,7 @@ GetSoundData (void *data) //fprintf (stderr, "played_data %d total_decoded %d delta %d\n", played_data, soundSource[SPEECH_SOURCE].total_decoded, delta); - pos = soundSource[SPEECH_SOURCE].sbuf_start + delta; + pos = soundSource[SPEECH_SOURCE].sbuf_offset + delta; if (pos % 2 == 1) pos++; @@ -612,12 +611,11 @@ GetSoundData (void *data) PlayingStream (MUSIC_SOURCE) && soundSource[MUSIC_SOURCE].sbuffer && soundSource[MUSIC_SOURCE].sbuf_size > 0) { - float played_time = (GetTimeCounter () - soundSource[MUSIC_SOURCE].start_time) / + float played_time = (GetTimeCounter () - soundSource[MUSIC_SOURCE].sbuf_lasttime) / (float)ONE_SECOND; - int played_data = (int) (played_time * (float)soundSource[MUSIC_SOURCE]. + int delta = (int) (played_time * (float)soundSource[MUSIC_SOURCE]. sample->decoder->frequency * 4.0f); - int delta = played_data - soundSource[MUSIC_SOURCE].total_decoded; - unsigned int pos, i, step; + unsigned long pos, i, step; UBYTE *scopedata = (UBYTE *) data; UBYTE *sbuffer = soundSource[MUSIC_SOURCE].sbuffer; @@ -641,7 +639,7 @@ GetSoundData (void *data) //fprintf (stderr, "played_data %d total_decoded %d delta %d\n", played_data, soundSource[MUSIC_SOURCE].total_decoded, delta); - pos = soundSource[MUSIC_SOURCE].sbuf_start + delta; + pos = soundSource[MUSIC_SOURCE].sbuf_offset + delta; if (pos % 2 == 1) pos++;