Skip to content

Commit

Permalink
Added framework for Sound_Seek() support.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Apr 21, 2002
1 parent 01b7350 commit 2a666d8
Show file tree
Hide file tree
Showing 17 changed files with 205 additions and 24 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG
Expand Up @@ -3,7 +3,9 @@
*/

04212002 - Initial work to add a Sound_Seek() API. Removed the NEEDSEEK
sample flag (replaced it with CANSEEK).
sample flag (replaced it with CANSEEK). Hack to change the internal
Sound_SetError() function to __Sound_SetError(). Added internal
function __Sound_convertMsToBytePos().
04082002 - Cleaned up the archive support in playsound a little bit, and
fixed a PhysicsFS bug in the process.
03252002 - Win32 patches and fixes from Tyler Montbriand: handled "inline"
Expand Down
34 changes: 32 additions & 2 deletions SDL_sound.c
Expand Up @@ -346,7 +346,7 @@ void Sound_ClearError(void)
/*
* This is declared in the internal header.
*/
void Sound_SetError(const char *str)
void __Sound_SetError(const char *str)
{
ErrMsg *err;

Expand Down Expand Up @@ -378,7 +378,17 @@ void Sound_SetError(const char *str)
err->error_available = 1;
strncpy(err->error_string, str, sizeof (err->error_string));
err->error_string[sizeof (err->error_string) - 1] = '\0';
} /* Sound_SetError */
} /* __Sound_SetError */


Uint32 __Sound_convertMsToBytePos(Sound_AudioInfo *info, Uint32 ms)
{
/* "frames" == "sample frames" */
float frames_per_ms = ((float) info->rate) / 1000.0;
Uint32 frame_offset = (Uint32) (frames_per_ms * ((float) ms));
Uint32 frame_size = (Uint32) ((info->format & 0xFF) / 8) * info->channels;
return(frame_offset * frame_size);
} /* __Sound_convertMsToBytePos */


/*
Expand Down Expand Up @@ -846,6 +856,26 @@ int Sound_Rewind(Sound_Sample *sample)
sample->flags &= !SOUND_SAMPLEFLAG_EAGAIN;
sample->flags &= !SOUND_SAMPLEFLAG_ERROR;
sample->flags &= !SOUND_SAMPLEFLAG_EOF;

return(1);
} /* Sound_Rewind */


int Sound_Seek(Sound_Sample *sample, Uint32 ms)
{
Sound_SampleInternal *internal;

BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
if (!(sample->flags & SOUND_SAMPLEFLAG_CANSEEK))
BAIL_MACRO(ERR_CANNOT_SEEK, 0);

internal = (Sound_SampleInternal *) sample->opaque;
BAIL_IF_MACRO(!internal->funcs->seek(sample, ms), NULL, 0);

sample->flags &= !SOUND_SAMPLEFLAG_EAGAIN;
sample->flags &= !SOUND_SAMPLEFLAG_ERROR;
sample->flags &= !SOUND_SAMPLEFLAG_EOF;

return(1);
} /* Sound_Rewind */

Expand Down
31 changes: 28 additions & 3 deletions SDL_sound_internal.h
Expand Up @@ -177,6 +177,22 @@ typedef struct __SOUND_DECODERFUNCTIONS__
* this method should ever fail!
*/
int (*rewind)(Sound_Sample *sample);

/*
* Reposition the decoding to an arbitrary point. Nonzero on
* success, zero on failure.
*
* The purpose of this method is to allow for higher efficiency than
* an application could get by just rewinding the sample and
* decoding to a given point.
*
* The decoder is responsible for calling seek() on the associated
* SDL_RWops.
*
* If there is an error, try to recover so that the next read will
* continue as if nothing happened.
*/
int (*seek)(Sound_Sample *sample, Uint32 ms);
} Sound_DecoderFunctions;


Expand Down Expand Up @@ -225,6 +241,7 @@ typedef struct __SOUND_SAMPLEINTERNAL__
#define ERR_COMPRESSION "(De)compression error"
#define ERR_PREV_ERROR "Previous decoding already caused an error"
#define ERR_PREV_EOF "Previous decoding already triggered EOF"
#define ERR_CANNOT_SEEK "Sample is not seekable"

/*
* Call this to set the message returned by Sound_GetError().
Expand All @@ -233,8 +250,16 @@ typedef struct __SOUND_SAMPLEINTERNAL__
*
* Calling this with a NULL argument is a safe no-op.
*/
void Sound_SetError(const char *err);
void __Sound_SetError(const char *err);

/* !!! FIXME: Clean up elsewhere and get rid of this. */
#define Sound_SetError __Sound_SetError

/*
* Call this to convert milliseconds to an actual byte position, based on
* audio data characteristics.
*/
Uint32 __Sound_convertMsToBytePos(Sound_AudioInfo *info, Uint32 ms);

/*
* Use this if you need a cross-platform stricmp().
Expand All @@ -243,8 +268,8 @@ int __Sound_strcasecmp(const char *x, const char *y);


/* These get used all over for lessening code clutter. */
#define BAIL_MACRO(e, r) { Sound_SetError(e); return r; }
#define BAIL_IF_MACRO(c, e, r) if (c) { Sound_SetError(e); return r; }
#define BAIL_MACRO(e, r) { __Sound_SetError(e); return r; }
#define BAIL_IF_MACRO(c, e, r) if (c) { __Sound_SetError(e); return r; }



Expand Down
12 changes: 10 additions & 2 deletions decoders/aiff.c
Expand Up @@ -66,6 +66,7 @@ static int AIFF_open(Sound_Sample *sample, const char *ext);
static void AIFF_close(Sound_Sample *sample);
static Uint32 AIFF_read(Sound_Sample *sample);
static int AIFF_rewind(Sound_Sample *sample);
static int AIFF_seek(Sound_Sample *sample, Uint32 ms);

static const char *extensions_aiff[] = { "AIFF", NULL };
const Sound_DecoderFunctions __Sound_DecoderFunctions_AIFF =
Expand All @@ -82,7 +83,8 @@ const Sound_DecoderFunctions __Sound_DecoderFunctions_AIFF =
AIFF_open, /* open() method */
AIFF_close, /* close() method */
AIFF_read, /* read() method */
AIFF_rewind /* rewind() method */
AIFF_rewind, /* rewind() method */
AIFF_seek /* seek() method */
};


Expand Down Expand Up @@ -520,7 +522,7 @@ static void AIFF_close(Sound_Sample *sample)
aiff_t *a = (aiff_t *) internal->decoder_private;
a->fmt.free(&(a->fmt));
free(a);
} /* WAV_close */
} /* AIFF_close */


static Uint32 AIFF_read(Sound_Sample *sample)
Expand All @@ -542,6 +544,12 @@ static int AIFF_rewind(Sound_Sample *sample)
return(fmt->rewind_sample(sample));
} /* AIFF_rewind */


static int AIFF_seek(Sound_Sample *sample, Uint32 ms)
{
BAIL_MACRO("!!! FIXME: Not implemented", 0);
} /* AIFF_seek */

#endif /* SOUND_SUPPORTS_AIFF */

/* end of aiff.c ... */
Expand Down
10 changes: 9 additions & 1 deletion decoders/au.c
Expand Up @@ -49,6 +49,7 @@ static int AU_open(Sound_Sample *sample, const char *ext);
static void AU_close(Sound_Sample *sample);
static Uint32 AU_read(Sound_Sample *sample);
static int AU_rewind(Sound_Sample *sample);
static int AU_seek(Sound_Sample *sample, Uint32 ms);

/*
* Sometimes the extension ".snd" is used for these files (mostly on the NeXT),
Expand All @@ -70,7 +71,8 @@ const Sound_DecoderFunctions __Sound_DecoderFunctions_AU =
AU_open, /* open() method */
AU_close, /* close() method */
AU_read, /* read() method */
AU_rewind /* rewind() method */
AU_rewind, /* rewind() method */
AU_seek /* seek() method */
};

/* no init/deinit needed */
Expand Down Expand Up @@ -316,5 +318,11 @@ static int AU_rewind(Sound_Sample *sample)
return(1);
} /* AU_rewind */


static int AU_seek(Sound_Sample *sample, Uint32 ms)
{
BAIL_MACRO("!!! FIXME: Not implemented", 0);
} /* AU_seek */

#endif /* SOUND_SUPPORTS_AU */

11 changes: 10 additions & 1 deletion decoders/flac.c
Expand Up @@ -60,6 +60,7 @@ static int FLAC_open(Sound_Sample *sample, const char *ext);
static void FLAC_close(Sound_Sample *sample);
static Uint32 FLAC_read(Sound_Sample *sample);
static int FLAC_rewind(Sound_Sample *sample);
static int FLAC_seek(Sound_Sample *sample, Uint32 ms);

static const char *extensions_flac[] = { "FLAC", "FLA", NULL };

Expand All @@ -77,7 +78,8 @@ const Sound_DecoderFunctions __Sound_DecoderFunctions_FLAC =
FLAC_open, /* open() method */
FLAC_close, /* close() method */
FLAC_read, /* read() method */
FLAC_rewind /* rewind() method */
FLAC_rewind, /* rewind() method */
FLAC_seek /* seek() method */
};

/* This is what we store in our internal->decoder_private field. */
Expand Down Expand Up @@ -390,6 +392,13 @@ static int FLAC_rewind(Sound_Sample *sample)
return(1);
} /* FLAC_rewind */


static int FLAC_seek(Sound_Sample *sample, Uint32 ms)
{
BAIL_MACRO("!!! FIXME: Not implemented", 0);
} /* FLAC_seek */


#endif /* SOUND_SUPPORTS_FLAC */


Expand Down
10 changes: 9 additions & 1 deletion decoders/midi.c
Expand Up @@ -53,6 +53,7 @@ static int MIDI_open(Sound_Sample *sample, const char *ext);
static void MIDI_close(Sound_Sample *sample);
static Uint32 MIDI_read(Sound_Sample *sample);
static int MIDI_rewind(Sound_Sample *sample);
static int MIDI_seek(Sound_Sample *sample, Uint32 ms);

static const char *extensions_midi[] = { "MIDI", "MID", NULL };
const Sound_DecoderFunctions __Sound_DecoderFunctions_MIDI =
Expand All @@ -69,7 +70,8 @@ const Sound_DecoderFunctions __Sound_DecoderFunctions_MIDI =
MIDI_open, /* open() method */
MIDI_close, /* close() method */
MIDI_read, /* read() method */
MIDI_rewind /* rewind() method */
MIDI_rewind, /* rewind() method */
MIDI_seek /* seek() method */
};


Expand Down Expand Up @@ -157,6 +159,12 @@ static int MIDI_rewind(Sound_Sample *sample)
return(1);
} /* MIDI_rewind */


static int MIDI_seek(Sound_Sample *sample, Uint32 ms)
{
BAIL_MACRO("!!! FIXME: Not implemented", 0);
} /* MIDI_seek */

#endif /* SOUND_SUPPORTS_MIDI */


Expand Down
8 changes: 7 additions & 1 deletion decoders/mikmod.c
Expand Up @@ -51,6 +51,7 @@ static int MIKMOD_open(Sound_Sample *sample, const char *ext);
static void MIKMOD_close(Sound_Sample *sample);
static Uint32 MIKMOD_read(Sound_Sample *sample);
static int MIKMOD_rewind(Sound_Sample *sample);
static int MIKMOD_seek(Sound_Sample *sample, Uint32 ms);

static const char *extensions_mikmod[] =
{
Expand Down Expand Up @@ -89,7 +90,8 @@ const Sound_DecoderFunctions __Sound_DecoderFunctions_MIKMOD =
MIKMOD_open, /* open() method */
MIKMOD_close, /* close() method */
MIKMOD_read, /* read() method */
MIKMOD_rewind /* rewind() method */
MIKMOD_rewind, /* rewind() method */
MIKMOD_seek /* seek() method */
};


Expand Down Expand Up @@ -295,6 +297,10 @@ static int MIKMOD_rewind(Sound_Sample *sample)
} /* MIKMOD_rewind */


static int MIKMOD_seek(Sound_Sample *sample, Uint32 ms)
{
BAIL_MACRO("!!! FIXME: Not implemented", 0);
} /* MIKMOD_seek */

#endif /* SOUND_SUPPORTS_MIKMOD */

Expand Down
10 changes: 9 additions & 1 deletion decoders/modplug.c
Expand Up @@ -50,6 +50,7 @@ static int MODPLUG_open(Sound_Sample *sample, const char *ext);
static void MODPLUG_close(Sound_Sample *sample);
static Uint32 MODPLUG_read(Sound_Sample *sample);
static int MODPLUG_rewind(Sound_Sample *sample);
static int MODPLUG_seek(Sound_Sample *sample, Uint32 ms);

static const char *extensions_modplug[] =
{
Expand Down Expand Up @@ -97,7 +98,8 @@ const Sound_DecoderFunctions __Sound_DecoderFunctions_MODPLUG =
MODPLUG_open, /* open() method */
MODPLUG_close, /* close() method */
MODPLUG_read, /* read() method */
MODPLUG_rewind /* rewind() method */
MODPLUG_rewind, /* rewind() method */
MODPLUG_seek /* seek() method */
};


Expand Down Expand Up @@ -244,6 +246,12 @@ static int MODPLUG_rewind(Sound_Sample *sample)
return(1);
} /* MODPLUG_rewind */


static int MODPLUG_seek(Sound_Sample *sample, Uint32 ms)
{
BAIL_MACRO("!!! FIXME: Not implemented", 0);
} /* MODPLUG_seek */

#endif /* SOUND_SUPPORTS_MODPLUG */


Expand Down
13 changes: 10 additions & 3 deletions decoders/mpglib.c
Expand Up @@ -27,8 +27,8 @@
* SMPEG, and, again, doesn't need an external library. You should test both
* decoders and use what you find works best for you.
*
* mpglib is part of mpg123, which can be found in its original form at:
* http://www.mpg123.de/
* mpglib is an LGPL'd portion of mpg123, which can be found in its original
* form at: http://www.mpg123.de/
*
* Please see the file COPYING in the source's root directory. The included
* source code for mpglib falls under the LGPL, which is the same license as
Expand Down Expand Up @@ -61,6 +61,7 @@ static int MPGLIB_open(Sound_Sample *sample, const char *ext);
static void MPGLIB_close(Sound_Sample *sample);
static Uint32 MPGLIB_read(Sound_Sample *sample);
static int MPGLIB_rewind(Sound_Sample *sample);
static int MPGLIB_seek(Sound_Sample *sample, Uint32 ms);

static const char *extensions_mpglib[] = { "MP3", NULL };
const Sound_DecoderFunctions __Sound_DecoderFunctions_MPGLIB =
Expand All @@ -77,7 +78,8 @@ const Sound_DecoderFunctions __Sound_DecoderFunctions_MPGLIB =
MPGLIB_open, /* open() method */
MPGLIB_close, /* close() method */
MPGLIB_read, /* read() method */
MPGLIB_rewind /* rewind() method */
MPGLIB_rewind, /* rewind() method */
MPGLIB_seek /* seek() method */
};


Expand Down Expand Up @@ -286,6 +288,11 @@ static int MPGLIB_rewind(Sound_Sample *sample)
} /* MPGLIB_rewind */


static int MPGLIB_seek(Sound_Sample *sample, Uint32 ms)
{
BAIL_MACRO("!!! FIXME: Not implemented", 0);
} /* MPGLIB_seek */

#endif /* SOUND_SUPPORTS_MPGLIB */


Expand Down
10 changes: 9 additions & 1 deletion decoders/ogg.c
Expand Up @@ -59,6 +59,7 @@ static int OGG_open(Sound_Sample *sample, const char *ext);
static void OGG_close(Sound_Sample *sample);
static Uint32 OGG_read(Sound_Sample *sample);
static int OGG_rewind(Sound_Sample *sample);
static int OGG_seek(Sound_Sample *sample, Uint32 ms);

static const char *extensions_ogg[] = { "OGG", NULL };
const Sound_DecoderFunctions __Sound_DecoderFunctions_OGG =
Expand All @@ -75,7 +76,8 @@ const Sound_DecoderFunctions __Sound_DecoderFunctions_OGG =
OGG_open, /* open() method */
OGG_close, /* close() method */
OGG_read, /* read() method */
OGG_rewind /* rewind() method */
OGG_rewind, /* rewind() method */
OGG_seek /* seek() method */
};


Expand Down Expand Up @@ -275,6 +277,12 @@ static int OGG_rewind(Sound_Sample *sample)
return(1);
} /* OGG_rewind */


static int OGG_seek(Sound_Sample *sample, Uint32 ms)
{
BAIL_MACRO("!!! FIXME: Not implemented", 0);
} /* OGG_seek */

#endif /* SOUND_SUPPORTS_OGG */


Expand Down

0 comments on commit 2a666d8

Please sign in to comment.