Skip to content

Commit

Permalink
Most decoders now report total sample play time, now. Technically, this
Browse files Browse the repository at this point in the history
breaks binary compatibility with the 1.0 branch, since it extends the
Sound_Sample struct, but most (all?) programs are just passing pointers
allocated by SDL_sound around, and might be okay.

Source-level compatibility is not broken...yet!   :)

--ryan.


-------- Original Message --------
Subject: SDL_sound patch: Finding total length of time of sound file.
Date: Sun, 26 Jan 2003 09:31:17 -0800 (PST)

Hi Ryan,

I am working with Eric Wing and helping him modify
SDL_sound.  AS part of our efforts in improving and
enhancing SDL_sound, we like to submit this patch.  We
modified the codecs to find the total time of a sound
file.  Below is the explanation of the patch.  The
patch is appended as an attachment to this email.


* MOTIVATION:
We needed the ability to get the total play time of a
sample (And we noticed that we're not the only ones).
Since SDL_sound blocks direct access to the specific
decoders, there is no way for a user to know this
information short of decoding the whole thing.
Because of this, we believe this will be a useful
addition, even though the accuracy may not be perfect
(subject to each decoder) or the information may not
always be available.


* CONTRIBUTORS:
Wesley Leong (modified the majority of the codecs and
verified the results)
Eric Wing (showed everyone how to do modify codec,
modified mikmod)
Wang Lam (modified a handful of codecs, researched
into specs and int overflow)
Ahilan Anantha (modified a few codecs and helped with
integer math)


* GENERAL ISSUES:
We chose the value to be milliseconds as an Sint32.
Milliseconds because that's what Sound_Seek takes as a
parameter and -1 to allow for instances/codecs where
the value could not be determined.  We are
not sure if this is the final convention you want, so
we are willing to work with you on this.

We also expect the total_time field to be set on open
and never again modified by SDL_sound. Users may
access it directly much like the sample buffer and
buffer_size.  We thought about recomputing the time
on DecodeAll, but since users may seek or decode small
chunks first, not all the data may be there.  So this
is better done by the user. This may be good
information to document.

Currently, all the main codecs are implemented except
for QuickTime.
  • Loading branch information
icculus committed May 8, 2004
1 parent 305b37b commit 472e9a3
Show file tree
Hide file tree
Showing 22 changed files with 302 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Expand Up @@ -2,6 +2,10 @@
* CHANGELOG.
*/

05082004 - Started 1.1.0 development branch with code to determine the total
length of a sample (thanks to Wesley, Eric, Wang, and Ahilan!)...
this patch was originally committed to CVS on 10252003, but it is
getting re-merged in the svn repository.
05072004 - Backed out some commits, converted repository to Subversion, and
branched off to a 1.1.0 development tree. Changed MikMod URL...old
one is now a porn site. :(
Expand Down
1 change: 1 addition & 0 deletions SDL_sound.h
Expand Up @@ -185,6 +185,7 @@ typedef struct
void *buffer; /**< Decoded sound data lands in here. */
Uint32 buffer_size; /**< Current size of (buffer), in bytes (Uint8). */
Sound_SampleFlags flags; /**< Flags relating to this sample. */
Sint32 total_time; /**< Total length of song or track */
} Sound_Sample;


Expand Down
5 changes: 5 additions & 0 deletions decoders/aiff.c
Expand Up @@ -476,6 +476,11 @@ static int AIFF_open(Sound_Sample *sample, const char *ext)
sample->actual.channels = (Uint8) c.numChannels;
sample->actual.rate = c.sampleRate;

/* Really, sample->total_time = (c.numSampleFrames*1000) c.sampleRate */
sample->total_time = (c.numSampleFrames / c.sampleRate) * 1000;
sample->total_time += (c.numSampleFrames % c.sampleRate)
* 1000 / c.sampleRate;

if (c.sampleSize <= 8)
{
sample->actual.format = AUDIO_S8;
Expand Down
11 changes: 9 additions & 2 deletions decoders/au.c
Expand Up @@ -166,7 +166,7 @@ static int AU_open(Sound_Sample *sample, const char *ext)
{
Sound_SampleInternal *internal = sample->opaque;
SDL_RWops *rw = internal->rw;
int skip, hsize, i;
int skip, hsize, i, bytes_per_second;
struct au_file_hdr hdr;
struct audec *dec;
char c;
Expand Down Expand Up @@ -242,7 +242,14 @@ static int AU_open(Sound_Sample *sample, const char *ext)
{
free(dec);
BAIL_MACRO("AU: Not an .AU stream.", 0);
} /* else */
} /* else */

bytes_per_second = ( ( dec->encoding == AU_ENC_LINEAR_16 ) ? 2 : 1 )
* sample->actual.rate * sample->actual.channels ;
sample->total_time = ((dec->remaining == -1) ? (-1) :
( ( dec->remaining / bytes_per_second ) * 1000 ) +
( ( dec->remaining % bytes_per_second ) * 1000 /
bytes_per_second ) );

sample->flags = SOUND_SAMPLEFLAG_CANSEEK;
dec->total = dec->remaining;
Expand Down
11 changes: 11 additions & 0 deletions decoders/flac.c
Expand Up @@ -247,6 +247,17 @@ static void metadata_callback(
f->sample->actual.channels = metadata->data.stream_info.channels;
f->sample->actual.rate = metadata->data.stream_info.sample_rate;

if (metadata->data.stream_info.sample_rate == 0 ||
metadata->data.stream_info.total_samples == 0)
f->sample->total_time = -1;
else {
f->sample->total_time = (metadata->data.stream_info.total_samples)
/ metadata->data.stream_info.sample_rate * 1000;
f->sample->total_time += (metadata->data.stream_info.total_samples
% metadata->data.stream_info.sample_rate) * 1000
/ metadata->data.stream_info.sample_rate;
}

if (metadata->data.stream_info.bits_per_sample > 8)
f->sample->actual.format = AUDIO_S16MSB;
else
Expand Down
1 change: 1 addition & 0 deletions decoders/midi.c
Expand Up @@ -112,6 +112,7 @@ static int MIDI_open(Sound_Sample *sample, const char *ext)
sample->actual.rate = 44100;
sample->actual.format = AUDIO_S16SYS;

sample->total_time = Timidity_GetSongLength(song);
sample->flags = SOUND_SAMPLEFLAG_CANSEEK;
return(1); /* we'll handle this data. */
} /* MIDI_open */
Expand Down
190 changes: 175 additions & 15 deletions decoders/mikmod.c
Expand Up @@ -202,7 +202,7 @@ static MREADER *_mm_new_rwops_reader(Sound_Sample *sample)

static void _mm_delete_rwops_reader(MREADER *reader)
{
/* SDL_sound will delete the RWops and sample at a higher level... */
/* SDL_sound will delete the RWops and sample at a higher level... */
if (reader != NULL)
free(reader);
} /* _mm_delete_rwops_reader */
Expand All @@ -213,16 +213,18 @@ static int MIKMOD_init(void)
{
MikMod_RegisterDriver(&drv_nos);

/* Quick and dirty hack to prevent an infinite loop problem
* found when using SDL_mixer and SDL_sound together and
* both have MikMod compiled in. So, check to see if
* MikMod has already been registered first before calling
* RegisterAllLoaders()
*/
if(MikMod_InfoLoader() == NULL)
/*
* Quick and dirty hack to prevent an infinite loop problem
* found when using SDL_mixer and SDL_sound together and
* both have MikMod compiled in. So, check to see if
* MikMod has already been registered first before calling
* RegisterAllLoaders()
*/
if (MikMod_InfoLoader() == NULL)
{
MikMod_RegisterAllLoaders();
}
} /* if */

/*
* Both DMODE_SOFT_MUSIC and DMODE_16BITS should be set by default,
* so this is just for clarity. I haven't experimented with any of
Expand Down Expand Up @@ -251,7 +253,9 @@ static int MIKMOD_open(Sound_Sample *sample, const char *ext)
Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
MREADER *reader;
MODULE *module;

Uint32 i; /* temp counter for time computation */
double segment_time = 0.0; /* temp holder for time */

reader = _mm_new_rwops_reader(sample);
BAIL_IF_MACRO(reader == NULL, ERR_OUT_OF_MEMORY, 0);
module = Player_LoadGeneric(reader, 64, 0);
Expand All @@ -276,10 +280,110 @@ static int MIKMOD_open(Sound_Sample *sample, const char *ext)

sample->flags = SOUND_SAMPLEFLAG_NONE;

/*
* module->sngtime = current song time in 2^-10 seconds
* sample->total_time = (module->sngtime * 1000) / (1<<10)
*/
sample->total_time = (module->sngtime * 1000) / (1<<10);

SNDDBG(("MIKMOD: Name: %s\n", module->songname));
SNDDBG(("MIKMOD: Type: %s\n", module->modtype));
SNDDBG(("MIKMOD: Accepting data stream\n"));


/*
* This is a quick and dirty way for getting the play time
* of a file. This will often be wrong because the tracker format
* allows for so much. If you want a better one, use ModPlug,
* demand that the Mikmod people write better functionality,
* or write a more complicated version of the code below.
*
* There are two dumb ways to compute the length. The really
* dumb way is to look at the header and take the initial
* speed/tempo values. However, speed values can change throughout
* the file. The slightly smarter way is to iterate through
* all the positions and add up each segment's time based
* on the idea that each segment will give us its own
* speed value. The hope is that this is more accurate.
* However, this demands that the file be seekable
* and that we can change the position of the sample.
* Depending on the assumptions of SDL_sound, this block
* of code should be enabled or disabled. If disabled,
* you still can make the computations doing the first method.
* For now, we will assume it's acceptable to seek a Mod file
* since this is essentially how Modplug also does it.
*
* Keep in mind that this will be incorrect for loops, jumps, short
* patterns and other features.
*/
sample->flags |= SOUND_SAMPLEFLAG_CANSEEK;

/*
* For each position (which corresponds to a particular pattern),
* get the speed values and compute the time length of the segment
*/
sample->total_time = 0;
for (i = 0; i < module->numpos; i++)
{
Player_SetPosition(i);
/* Must call update, or the speed values won't get reset */
MikMod_Update();
/* Now the magic formula:
* Multiply the number of positions by the
* Number of rows (usually 64 but can be different) by the
* time it takes to read one row (1/50)
* by the speed by the
* magic reference beats per minute / the beats per minute
*
* We're using positions instead of patterns because in our
* test cases, this seems to be the correct value for the
* number of sections you hear during normal playback.
* They typically map to a fewer number of patterns
* where some patterns get replayed multiple times
* in a song (think chorus). Since we're in a for-loop,
* the multiplication is implicit while we're adding
* all the segments.
*
* From a tracker format spec, it seems that 64 rows
* is the normal (00-3F), but I've seen songs that
* either have less or are jumping positions in the
* middle of a pattern. It looks like Mikmod might
* reveal this number for us.
*
* According to the spec, it seems that a speed of 1
* corresponds to reading 1 row in 50 ticks. However,
* I'm not sure if ticks are real seconds or this
* notion of second units:
* Assuming that it's just normal seconds, we get 1/50 = 0.02.
*
* The current speed and current tempo (beats per minute)
* we can just grab. However, we need a magic number
* to figure out what the tempo is based on. Our primitive
* stopwatch results and intuition seem to imply 120-130bpm
* is the magic number. Looking at the majority of tracker
* files I have, 125 seems to be the common value. Furthermore
* most (if not all) of my Future Crew .S3M (Scream Tracker 3)
* files also use 125. Since they invented that format,
* I'll also assume that's the base number.
*/
if(module->bpm == 0)
{
/*
* Should never get here, but I don't want any
* divide by zero errors
*/
continue;
} /* if */
segment_time += (module->numrow * .02 * module->sngspd *
125.0 / module->bpm);
} /* for */
/* Now convert to milliseconds and store the value */
sample->total_time = (Sint32)(segment_time * 1000);

/* Reset the sample to the beginning */
Player_SetPosition(0);
MikMod_Update();

return(1); /* we'll handle this data. */
} /* MIKMOD_open */

Expand Down Expand Up @@ -322,20 +426,76 @@ static int MIKMOD_rewind(Sound_Sample *sample)

static int MIKMOD_seek(Sound_Sample *sample, Uint32 ms)
{
#if 0
Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
MODULE *module = (MODULE *) internal->decoder_private;
double last_time = 0.0;
double current_time = 0.0;
double target_time;
Uint32 i;

/*
* Heaven may know what the argument to Player_SetPosition() is.
* I, however, haven't the faintest idea.
*/
Player_Start(module);
Player_SetPosition(ms);

/*
* Mikmod only lets you seek to the beginning of a pattern.
* This means we'll get very coarse grain seeks. The
* value we pass to SetPosition is a value between 0
* and the number of positions in the file. The
* dumb approach would be to take our total_time that
* we've already calculated and divide it up by the
* number of positions and seek to the position that results.
* However, because songs can alter their speed/tempo during
* playback, different patterns in the song can take
* up different amounts of time. So the slightly
* smarter approach is to repeat what was done in the
* total_time computation and traverse through the file
* until we find the closest position.
* The follwing is basically cut and paste from the
* open function.
*/
if (ms == 0) /* Check end conditions to simplify things */
{
Player_SetPosition(0);
return(1);
} /* if */

if (ms >= sample->total_time)
Player_SetPosition(module->numpos);

/* Convert time to seconds (double) to make comparisons easier */
target_time = ms / 1000.0;

for (i = 0; i < module->numpos; i++)
{
Player_SetPosition(i);
/* Must call update, or the speed values won't get reset */
MikMod_Update();
/* Divide by zero check */
if(module->bpm == 0)
continue;
last_time = current_time;
/* See the notes in the open function about the formula */
current_time += (module->numrow * .02
* module->sngspd * 125.0 / module->bpm);
if(target_time <= current_time)
break; /* We now have our interval, so break out */
} /* for */

if( (target_time-last_time) > (current_time-target_time) )
{
/* The target time is closer to the higher position, so go there */
Player_SetPosition(i+1);
} /* if */
else
{
/* The target time is closer to the lower position, so go there */
Player_SetPosition(i);
} /* else */

return(1);
#else
BAIL_MACRO("MIKMOD: Seeking not implemented", 0);
#endif
} /* MIKMOD_seek */

#endif /* SOUND_SUPPORTS_MIKMOD */
Expand Down
1 change: 1 addition & 0 deletions decoders/modplug.c
Expand Up @@ -270,6 +270,7 @@ static int MODPLUG_open(Sound_Sample *sample, const char *ext)
if (modplug_mutex != NULL)
SDL_UnlockMutex(modplug_mutex);

sample->total_time = ModPlug_GetLength(module);
SNDDBG(("MODPLUG: [%d ms] %s\n",
ModPlug_GetLength(module), ModPlug_GetName(module)));

Expand Down
27 changes: 27 additions & 0 deletions decoders/mpglib.c
Expand Up @@ -175,6 +175,33 @@ static int MPGLIB_open(Sound_Sample *sample, const char *ext)
sample->actual.format = AUDIO_S16SYS;
sample->flags = SOUND_SAMPLEFLAG_NONE;

{
/* Finds approximate length of the song. */
Uint32 pos, total_byte_size;
char tag_buffer[4];
memset(tag_buffer, '\0', sizeof(tag_buffer));

/* Get size of file first */
pos = SDL_RWtell(internal->rw);
total_byte_size = SDL_RWseek(internal->rw, -128, SEEK_END);
if( total_byte_size <= 0 ) {
BAIL_MACRO("MPGLIB: Not an MP3 stream.", 0);
}
if ( SDL_RWread(internal->rw, tag_buffer, 1, 3) != 3) {
BAIL_MACRO("MPGLIB: Cannot identify TAG section.", 0);
}
if ( strncmp(tag_buffer, "TAG", 3) != 0 ) {
/* ENDING TAG NOT FOUND */
total_byte_size = SDL_RWseek(internal->rw,0, SEEK_END);
}
if (SDL_RWseek(internal->rw, pos, SEEK_SET) != pos) {
BAIL_MACRO("MPGLIB: Cannot go back to save spot in file.", 0);
}
sample->total_time = total_byte_size / mpg->mp.fr.bitrate * 8.0;
sample->total_time += (total_byte_size % mpg->mp.fr.bitrate) * 8.0
/ mpg->mp.fr.bitrate;
}

return(1); /* we'll handle this data. */
} /* MPGLIB_open */

Expand Down
2 changes: 2 additions & 0 deletions decoders/mpglib/mpg123_sdlsound.h
Expand Up @@ -90,6 +90,8 @@ struct frame {
/* layer2 stuff */
int II_sblimit;
void *alloc;

int bitrate;
};

struct parameter {
Expand Down
2 changes: 2 additions & 0 deletions decoders/mpglib/mpglib_common.c
Expand Up @@ -146,6 +146,8 @@ int decode_header(struct frame *fr,unsigned long newhead)
default:
BAIL_MACRO("MPGLIB: Unknown layer type.", 0);
}

fr->bitrate = (int) tabsel_123[fr->lsf][(fr->lay - 1)][fr->bitrate_index];
return 1;
}

Expand Down

0 comments on commit 472e9a3

Please sign in to comment.