Skip to content

Commit

Permalink
stb_vorbis: sync with stb_vorbis SDL fork, and SDL_mixer
Browse files Browse the repository at this point in the history
  • Loading branch information
sezero committed Dec 13, 2023
1 parent 6ee6c83 commit c563941
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/SDL_sound_vorbis.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@
#if SOUND_SUPPORTS_VORBIS

/* Configure and include stb_vorbis for compiling... */
#define STB_VORBIS_SDL 1 /* for SDL_sound-specific stuff. */
#define STB_VORBIS_NO_STDIO 1
#define STB_VORBIS_NO_CRT 1
#define STB_VORBIS_NO_PUSHDATA_API 1
#define STB_VORBIS_MAX_CHANNELS 6
#define STB_VORBIS_MAX_CHANNELS 8 /* For 7.1 surround sound */
#define STB_VORBIS_NO_COMMENTS 1
#define STB_FORCEINLINE SDL_FORCE_INLINE
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#define STB_VORBIS_BIG_ENDIAN 1
#endif
#define STBV_CDECL SDLCALL /* for SDL_qsort */
#define STBV_CDECL SDLCALL /* for SDL_qsort() */

#if !defined(__clang_analyzer__)
#ifdef assert
Expand Down
63 changes: 48 additions & 15 deletions src/stb_vorbis.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//
// Feature contributors:
// Dougall Johnson (sample-exact seeking)
// Vitaly Novichkov (sample-accurate tell)
//
// Bugfix/warning contributors:
// Terje Mathisen Niklas Frykholm Andy Hill
Expand Down Expand Up @@ -150,8 +151,10 @@ typedef struct
// get general information about the file
extern stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f);

#ifndef STB_VORBIS_NO_COMMENTS
// get ogg comments
extern stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f);
#endif

// get the last error detected (clears it, too)
extern int stb_vorbis_get_error(stb_vorbis *f);
Expand All @@ -166,6 +169,12 @@ extern void stb_vorbis_close(stb_vorbis *f);
// NOT WORKING YET after a seek with PULLDATA API
extern int stb_vorbis_get_sample_offset(stb_vorbis *f);

// this function returns the count of returned samples from the beginning of the
// file. Functions "stb_vorbis_get_samples_*", "stb_vorbis_seek_*()" will
// affect the returned value. Use this call to get the accurate sample position
// during playback.
extern int stb_vorbis_get_playback_sample_offset(stb_vorbis *f);

// returns the current seek point within the file, or offset from the beginning
// of the memory buffer. In pushdata mode it returns 0.
extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f);
Expand Down Expand Up @@ -296,7 +305,7 @@ extern stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_cl
// confused.
#endif

#ifdef __SDL_SOUND_INTERNAL__
#ifdef STB_VORBIS_SDL
extern stb_vorbis * stb_vorbis_open_rwops_section(SDL_RWops *rwops, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length);
extern stb_vorbis * stb_vorbis_open_rwops(SDL_RWops *rwops, int close_on_free, int *error, const stb_vorbis_alloc *alloc);
#endif
Expand Down Expand Up @@ -551,10 +560,12 @@ enum STBVorbisError
// #define STB_VORBIS_NO_DEFER_FLOOR

// STB_VORBIS_NO_COMMENTS
// disables reading and storing user comments
// Disables reading and storing user comments.
// #define STB_VORBIS_NO_COMMENTS




//////////////////////////////////////////////////////////////////////////////

#ifdef STB_VORBIS_NO_PULLDATA_API
Expand Down Expand Up @@ -637,7 +648,7 @@ enum STBVorbisError
#define MAX_BLOCKSIZE_LOG 13 // from specification
#define MAX_BLOCKSIZE (1 << MAX_BLOCKSIZE_LOG)

#ifdef __SDL_SOUND_INTERNAL__
#ifdef STB_VORBIS_SDL
typedef Uint8 uint8;
typedef Sint8 int8;
typedef Uint16 uint16;
Expand Down Expand Up @@ -768,7 +779,6 @@ typedef struct

typedef struct
{
// https://github.com/nothings/stb/pull/1312
MappingChannel *chan;
uint16 coupling_steps;
uint8 submaps;
Expand Down Expand Up @@ -821,7 +831,7 @@ struct stb_vorbis
uint32 f_start;
int close_on_free;
#endif
#ifdef __SDL_SOUND_INTERNAL__
#ifdef STB_VORBIS_SDL
SDL_RWops *rwops;
uint32 rwops_start;
int close_on_free;
Expand Down Expand Up @@ -887,6 +897,9 @@ struct stb_vorbis
uint32 current_loc; // sample location of next frame to decode
int current_loc_valid;

int32 current_playback_loc; // sample location of played samples
int current_playback_loc_valid;

// per-blocksize precomputed data

// twiddle factors
Expand Down Expand Up @@ -1374,7 +1387,7 @@ static int STBV_CDECL point_compare(const void *p, const void *q)
/////////////////////// END LEAF SETUP FUNCTIONS //////////////////////////


#ifdef __SDL_SOUND_INTERNAL__
#ifdef STB_VORBIS_SDL
#define USE_MEMORY(z) FALSE
#elif defined(STB_VORBIS_NO_STDIO)
#define USE_MEMORY(z) TRUE
Expand All @@ -1384,7 +1397,7 @@ static int STBV_CDECL point_compare(const void *p, const void *q)

static uint8 get8(vorb *z)
{
#ifdef __SDL_SOUND_INTERNAL__
#ifdef STB_VORBIS_SDL
uint8 c;
if (SDL_RWread(z->rwops, &c, 1, 1) != 1) { z->eof = TRUE; return 0; }
return c;
Expand Down Expand Up @@ -1417,7 +1430,7 @@ static uint32 get32(vorb *f)

static int getn(vorb *z, uint8 *data, int n)
{
#ifdef __SDL_SOUND_INTERNAL__
#ifdef STB_VORBIS_SDL
if (SDL_RWread(z->rwops, data, n, 1) == 1) return 1;
z->eof = 1;
return 0;
Expand All @@ -1443,7 +1456,7 @@ static int getn(vorb *z, uint8 *data, int n)

static void skip(vorb *z, int n)
{
#ifdef __SDL_SOUND_INTERNAL__
#ifdef STB_VORBIS_SDL
SDL_RWseek(z->rwops, n, RW_SEEK_CUR);

#else
Expand All @@ -1469,7 +1482,7 @@ static int set_file_offset(stb_vorbis *f, unsigned int loc)
#endif
f->eof = 0;

#ifdef __SDL_SOUND_INTERNAL__
#ifdef STB_VORBIS_SDL
if (loc + f->rwops_start < loc || loc >= 0x80000000) {
loc = 0x7fffffff;
f->eof = 1;
Expand Down Expand Up @@ -3601,6 +3614,8 @@ static int vorbis_pump_first_frame(stb_vorbis *f)
res = vorbis_decode_packet(f, &len, &left, &right);
if (res)
vorbis_finish_frame(f, len, left, right);
f->current_playback_loc = 0;
f->current_playback_loc_valid = TRUE;
return res;
}

Expand Down Expand Up @@ -4392,7 +4407,7 @@ static void vorbis_deinit(stb_vorbis *p)
setup_temp_free(p, &p->temp_values, 0);
setup_temp_free(p, &p->temp_mults, 0);
}
#ifdef __SDL_SOUND_INTERNAL__
#ifdef STB_VORBIS_SDL
if (p->close_on_free) SDL_RWclose(p->rwops);
#endif
#ifndef STB_VORBIS_NO_STDIO
Expand Down Expand Up @@ -4420,7 +4435,7 @@ static void vorbis_init(stb_vorbis *p, const stb_vorbis_alloc *z)
p->stream = NULL;
p->codebooks = NULL;
p->page_crc_tests = -1;
#ifdef __SDL_SOUND_INTERNAL__
#ifdef STB_VORBIS_SDL
p->close_on_free = FALSE;
p->rwops = NULL;
#endif
Expand All @@ -4438,6 +4453,14 @@ int stb_vorbis_get_sample_offset(stb_vorbis *f)
return -1;
}

int stb_vorbis_get_playback_sample_offset(stb_vorbis *f)
{
if (f->current_playback_loc_valid)
return f->current_playback_loc;
else
return -1;
}

stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f)
{
stb_vorbis_info d;
Expand Down Expand Up @@ -4685,7 +4708,7 @@ unsigned int stb_vorbis_get_file_offset(stb_vorbis *f)
#ifndef STB_VORBIS_NO_PUSHDATA_API
if (f->push_mode) return 0;
#endif
#ifdef __SDL_SOUND_INTERNAL__
#ifdef STB_VORBIS_SDL
return (unsigned int) (SDL_RWtell(f->rwops) - f->rwops_start);
#else
if (USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start);
Expand Down Expand Up @@ -5058,13 +5081,16 @@ int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number)
}
// the next frame should start with the sample
if (f->current_loc != sample_number) return error(f, VORBIS_seek_failed);
f->current_playback_loc = sample_number;
return 1;
}

int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number)
{
if (!stb_vorbis_seek_frame(f, sample_number))
if (!stb_vorbis_seek_frame(f, sample_number)) {
f->current_playback_loc_valid = FALSE;
return 0;
}

if (sample_number != f->current_loc) {
int n;
Expand All @@ -5075,6 +5101,9 @@ int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number)
f->channel_buffer_start += (sample_number - frame_start);
}

f->current_playback_loc_valid = TRUE;
f->current_playback_loc = sample_number;

return 1;
}

Expand Down Expand Up @@ -5241,7 +5270,7 @@ stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const st
}
#endif // STB_VORBIS_NO_STDIO

#ifdef __SDL_SOUND_INTERNAL__
#ifdef STB_VORBIS_SDL
stb_vorbis * stb_vorbis_open_rwops_section(SDL_RWops *rwops, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length)
{
stb_vorbis *f, p;
Expand Down Expand Up @@ -5499,6 +5528,7 @@ int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short
if (n == len) break;
if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
}
f->current_playback_loc += n;
return n;
}

Expand All @@ -5516,6 +5546,7 @@ int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, in
if (n == len) break;
if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
}
f->current_playback_loc += n;
return n;
}

Expand Down Expand Up @@ -5624,6 +5655,7 @@ int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float
if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
break;
}
f->current_playback_loc += n;
return n;
}

Expand All @@ -5650,6 +5682,7 @@ int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, in
if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
break;
}
f->current_playback_loc += n;
return n;
}
#endif // STB_VORBIS_NO_PULLDATA_API
Expand Down

0 comments on commit c563941

Please sign in to comment.