From 472e9a3557ddc767bc59c8430a58c2e3fefb8749 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 8 May 2004 08:19:50 +0000 Subject: [PATCH] Most decoders now report total sample play time, now. Technically, this 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. --- CHANGELOG | 4 + SDL_sound.h | 1 + decoders/aiff.c | 5 + decoders/au.c | 11 +- decoders/flac.c | 11 ++ decoders/midi.c | 1 + decoders/mikmod.c | 190 +++++++++++++++++++++++++++--- decoders/modplug.c | 1 + decoders/mpglib.c | 27 +++++ decoders/mpglib/mpg123_sdlsound.h | 2 + decoders/mpglib/mpglib_common.c | 2 + decoders/ogg.c | 7 ++ decoders/quicktime.c | 2 + decoders/raw.c | 16 +++ decoders/shn.c | 5 +- decoders/smpeg.c | 1 + decoders/timidity/playmidi.c | 9 ++ decoders/timidity/readmidi.c | 2 +- decoders/timidity/timidity.c | 4 +- decoders/timidity/timidity.h | 2 + decoders/voc.c | 17 ++- decoders/wav.c | 5 +- 22 files changed, 302 insertions(+), 23 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index c6c76db..a76534a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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. :( diff --git a/SDL_sound.h b/SDL_sound.h index 06bc9b0..1b0c743 100644 --- a/SDL_sound.h +++ b/SDL_sound.h @@ -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; diff --git a/decoders/aiff.c b/decoders/aiff.c index 12125c0..93c49b4 100644 --- a/decoders/aiff.c +++ b/decoders/aiff.c @@ -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; diff --git a/decoders/au.c b/decoders/au.c index 323af18..e0483f8 100644 --- a/decoders/au.c +++ b/decoders/au.c @@ -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; @@ -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; diff --git a/decoders/flac.c b/decoders/flac.c index 78d521a..918c891 100644 --- a/decoders/flac.c +++ b/decoders/flac.c @@ -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 diff --git a/decoders/midi.c b/decoders/midi.c index f8f6e7c..a4ad6e0 100644 --- a/decoders/midi.c +++ b/decoders/midi.c @@ -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 */ diff --git a/decoders/mikmod.c b/decoders/mikmod.c index f6c315e..36f2e42 100644 --- a/decoders/mikmod.c +++ b/decoders/mikmod.c @@ -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 */ @@ -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 @@ -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); @@ -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 */ @@ -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 */ diff --git a/decoders/modplug.c b/decoders/modplug.c index 33b28ad..fd9c309 100644 --- a/decoders/modplug.c +++ b/decoders/modplug.c @@ -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))); diff --git a/decoders/mpglib.c b/decoders/mpglib.c index fd29ce2..9d3ad8d 100644 --- a/decoders/mpglib.c +++ b/decoders/mpglib.c @@ -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 */ diff --git a/decoders/mpglib/mpg123_sdlsound.h b/decoders/mpglib/mpg123_sdlsound.h index d8fc9bd..1907f12 100644 --- a/decoders/mpglib/mpg123_sdlsound.h +++ b/decoders/mpglib/mpg123_sdlsound.h @@ -90,6 +90,8 @@ struct frame { /* layer2 stuff */ int II_sblimit; void *alloc; + + int bitrate; }; struct parameter { diff --git a/decoders/mpglib/mpglib_common.c b/decoders/mpglib/mpglib_common.c index cc14662..6e03615 100644 --- a/decoders/mpglib/mpglib_common.c +++ b/decoders/mpglib/mpglib_common.c @@ -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; } diff --git a/decoders/ogg.c b/decoders/ogg.c index 94c466a..1c9f23a 100644 --- a/decoders/ogg.c +++ b/decoders/ogg.c @@ -171,6 +171,7 @@ static __inline__ void output_ogg_comments(OggVorbis_File *vf) static int OGG_open(Sound_Sample *sample, const char *ext) { int rc; + double total_time; OggVorbis_File *vf; vorbis_info *info; Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; @@ -209,6 +210,12 @@ static int OGG_open(Sound_Sample *sample, const char *ext) sample->flags = SOUND_SAMPLEFLAG_CANSEEK; sample->actual.rate = (Uint32) info->rate; sample->actual.channels = (Uint8) info->channels; + total_time = ov_time_total(vf, -1); + if (OV_EINVAL == total_time) + sample->total_time = -1; + else + sample->total_time = (Sint32)(total_time * 1000.0 + 0.5); + /* * Since we might have more than one logical bitstream in the OGG file, diff --git a/decoders/quicktime.c b/decoders/quicktime.c index 355ca6e..c816654 100644 --- a/decoders/quicktime.c +++ b/decoders/quicktime.c @@ -547,6 +547,8 @@ static int QT_open(Sound_Sample *sample, const char *ext) instance = QT_open_internal(sample, ext); internal->decoder_private = (void*)instance; + sample->total_time = -1; /* return -1 for total time of song for now */ + return(instance != NULL); } /* QT_open */ diff --git a/decoders/raw.c b/decoders/raw.c index c524302..d664420 100644 --- a/decoders/raw.c +++ b/decoders/raw.c @@ -95,6 +95,10 @@ static void RAW_quit(void) static int RAW_open(Sound_Sample *sample, const char *ext) { + Sound_SampleInternal *internal = sample->opaque; + SDL_RWops *rw = internal->rw; + Uint32 pos, sample_rate; + /* * We check this explicitly, since we have no other way to * determine whether we should handle this data or not. @@ -122,6 +126,18 @@ static int RAW_open(Sound_Sample *sample, const char *ext) memcpy(&sample->actual, &sample->desired, sizeof (Sound_AudioInfo)); sample->flags = SOUND_SAMPLEFLAG_CANSEEK; + if ( (pos = SDL_RWseek(internal->rw, 0, SEEK_END) ) <= 0) { + BAIL_MACRO("RAW: cannot seek the end of the file \"RAW\".", 0); + } + if ( SDL_RWseek(internal->rw, 0, SEEK_SET) ) { + BAIL_MACRO("RAW: cannot reset file \"RAW\".", 0); + } + + sample_rate = (sample->actual.rate * sample->actual.channels + * ( (sample->actual.format & 0x0018) >> 3) ); + sample->total_time = ( pos ) / sample_rate * 1000; + sample->total_time += (pos % sample_rate) * 1000 / sample_rate; + return(1); /* we'll handle this data. */ } /* RAW_open */ diff --git a/decoders/shn.c b/decoders/shn.c index ad443ab..506f417 100644 --- a/decoders/shn.c +++ b/decoders/shn.c @@ -551,6 +551,7 @@ static __inline__ int parse_riff_header(shn_t *shn, Sound_Sample *sample) Uint16 u16; Uint32 u32; Sint32 cklen; + Uint32 bytes_per_second; BAIL_IF_MACRO(!uvar_get(SHN_VERBATIM_CKSIZE_SIZE, shn, rw, &cklen), NULL, 0); @@ -572,13 +573,15 @@ static __inline__ int parse_riff_header(shn_t *shn, Sound_Sample *sample) sample->actual.rate = u32; BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* bytespersec */ + bytes_per_second = u32; BAIL_IF_MACRO(!verb_ReadLE16(shn, rw, &u16), NULL, 0); /* blockalign */ BAIL_IF_MACRO(!verb_ReadLE16(shn, rw, &u16), NULL, 0); /* bitspersample */ BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* 'data' header */ BAIL_IF_MACRO(u32 != dataID, "SHN: No 'data' header.", 0); BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* chunksize */ - + sample->total_time = u32 / bytes_per_second * 1000; + sample->total_time += (u32 % bytes_per_second) * 1000 / bytes_per_second; return(1); } /* parse_riff_header */ diff --git a/decoders/smpeg.c b/decoders/smpeg.c index 9022284..8af6321 100644 --- a/decoders/smpeg.c +++ b/decoders/smpeg.c @@ -212,6 +212,7 @@ static int _SMPEG_open(Sound_Sample *sample, const char *ext) sample->actual.rate = spec.freq; sample->actual.channels = spec.channels; sample->flags = SOUND_SAMPLEFLAG_CANSEEK; + sample->total_time = smpeg_info.total_time * 1000; internal->decoder_private = smpeg; SMPEG_play(smpeg); diff --git a/decoders/timidity/playmidi.c b/decoders/timidity/playmidi.c index cd0b3cd..89fc75f 100644 --- a/decoders/timidity/playmidi.c +++ b/decoders/timidity/playmidi.c @@ -662,6 +662,15 @@ void Timidity_Seek(MidiSong *song, Uint32 ms) skip_to(song, (ms * song->rate) / 1000); } +Uint32 Timidity_GetSongLength(MidiSong *song) +{ + MidiEvent *last_event = &song->events[song->groomed_event_count - 1]; + /* We want last_event->time * 1000 / song->rate */ + Uint32 retvalue = (last_event->time / song->rate) * 1000; + retvalue += (last_event->time % song->rate) * 1000 / song->rate; + return retvalue; +} + int Timidity_PlaySome(MidiSong *song, void *stream, Sint32 len) { Sint32 start_sample, end_sample, samples; diff --git a/decoders/timidity/readmidi.c b/decoders/timidity/readmidi.c index f3435f7..8b53357 100644 --- a/decoders/timidity/readmidi.c +++ b/decoders/timidity/readmidi.c @@ -488,7 +488,7 @@ static MidiEvent *groom_list(MidiSong *song, Sint32 divisions,Sint32 *eventsp, lp->type=ME_EOT; our_event_count++; free_midi_list(song); - + *eventsp=our_event_count; *samplesp=st; return groomed_list; diff --git a/decoders/timidity/timidity.c b/decoders/timidity/timidity.c index 244d6b1..09e4903 100644 --- a/decoders/timidity/timidity.c +++ b/decoders/timidity/timidity.c @@ -438,7 +438,6 @@ int Timidity_Init() MidiSong *Timidity_LoadDLSSong(SDL_RWops *rw, DLS_Patches *patches, SDL_AudioSpec *audio) { MidiSong *song; - Sint32 events; int i; if (rw == NULL) @@ -514,7 +513,8 @@ MidiSong *Timidity_LoadDLSSong(SDL_RWops *rw, DLS_Patches *patches, SDL_AudioSpe song->lost_notes = 0; song->cut_notes = 0; - song->events = read_midi_file(song, &events, &song->samples); + song->events = read_midi_file(song, &(song->groomed_event_count), + &song->samples); /* The RWops can safely be closed at this point, but let's make that the * responsibility of the caller. diff --git a/decoders/timidity/timidity.h b/decoders/timidity/timidity.h index 53ca825..74e5679 100644 --- a/decoders/timidity/timidity.h +++ b/decoders/timidity/timidity.h @@ -153,6 +153,7 @@ typedef struct { Sint32 current_sample; Sint32 event_count; Sint32 at; + Sint32 groomed_event_count; } MidiSong; /* Some of these are not defined in timidity.c but are here for convenience */ @@ -167,6 +168,7 @@ extern MidiSong *Timidity_LoadDLSSong(SDL_RWops *rw, DLS_Patches *patches, SDL_A extern MidiSong *Timidity_LoadSong(SDL_RWops *rw, SDL_AudioSpec *audio); extern void Timidity_Start(MidiSong *song); extern void Timidity_Seek(MidiSong *song, Uint32 ms); +extern Uint32 Timidity_GetSongLength(MidiSong *song); /* returns millseconds */ extern void Timidity_FreeSong(MidiSong *song); extern void Timidity_Exit(void); diff --git a/decoders/voc.c b/decoders/voc.c index 8c49c7c..7edd2d4 100644 --- a/decoders/voc.c +++ b/decoders/voc.c @@ -196,6 +196,7 @@ static int voc_get_block(Sound_Sample *sample, vs_t *v) Uint32 new_rate_long; Uint8 trash[6]; Uint16 period; + Uint32 bytes_per_second; int i; v->silent = 0; @@ -241,6 +242,12 @@ static int voc_get_block(Sound_Sample *sample, vs_t *v) v->extended = 0; v->rest = sblen - 2; v->size = ST_SIZE_BYTE; + + bytes_per_second = sample->actual.rate + * sample->actual.channels; + sample->total_time += ( v->rest ) / bytes_per_second * 1000; + sample->total_time += (v->rest % bytes_per_second) * 1000 + / bytes_per_second; return 1; case VOC_DATA_16: @@ -272,8 +279,13 @@ static int voc_get_block(Sound_Sample *sample, vs_t *v) if (!voc_readbytes(src, v, trash, sizeof (Uint8) * 6)) return 0; - v->rest = sblen - 12; + + bytes_per_second = ((v->size == ST_SIZE_WORD) ? (2) : (1)) * + sample->actual.rate * v->channels; + sample->total_time += v->rest / bytes_per_second * 1000; + sample->total_time += ( v->rest % bytes_per_second ) * 1000 + / bytes_per_second; return 1; case VOC_CONT: @@ -302,6 +314,9 @@ static int voc_get_block(Sound_Sample *sample, vs_t *v) v->rate = uc; v->rest = period; v->silent = 1; + + sample->total_time += (period) / (v->rate) * 1000; + sample->total_time += (period % v->rate) * 1000 / v->rate; return 1; case VOC_LOOP: diff --git a/decoders/wav.c b/decoders/wav.c index e0e8862..205f2ee 100644 --- a/decoders/wav.c +++ b/decoders/wav.c @@ -722,9 +722,12 @@ static int WAV_open_internal(Sound_Sample *sample, const char *ext, fmt_t *fmt) fmt->data_starting_offset = SDL_RWtell(rw); fmt->sample_frame_size = ( ((sample->actual.format & 0xFF) / 8) * sample->actual.channels ); - internal->decoder_private = (void *) w; + sample->total_time = (fmt->total_bytes / fmt->dwAvgBytesPerSec) * 1000; + sample->total_time += (fmt->total_bytes % fmt->dwAvgBytesPerSec) + * 1000 / fmt->dwAvgBytesPerSec; + sample->flags = SOUND_SAMPLEFLAG_NONE; if (fmt->seek_sample != NULL) sample->flags |= SOUND_SAMPLEFLAG_CANSEEK;