Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ add_library(SDL2_mixer
src/codecs/music_wav.c
src/codecs/music_wavpack.c
src/codecs/music_xmp.c
src/codecs/remap_channels.c
src/effect_position.c
src/effect_stereoreverse.c
src/effects_internal.c
Expand Down
33 changes: 23 additions & 10 deletions src/codecs/music_ogg.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "SDL_loadso.h"

#include "music_ogg.h"
#include "remap_channels.h"
#include "utils.h"

#define OV_EXCLUDE_STATIC_CALLBACKS
Expand Down Expand Up @@ -195,6 +196,7 @@ static void OGG_Delete(void *context);
static int OGG_UpdateSection(OGG_music *music)
{
vorbis_info *vi;
int new_buffer_size;

vi = vorbis.ov_info(&music->vf, -1);
if (!vi) {
Expand All @@ -206,11 +208,6 @@ static int OGG_UpdateSection(OGG_music *music)
}
SDL_memcpy(&music->vi, vi, sizeof(*vi));

if (music->buffer) {
SDL_free(music->buffer);
music->buffer = NULL;
}

if (music->stream) {
SDL_FreeAudioStream(music->stream);
music->stream = NULL;
Expand All @@ -219,13 +216,25 @@ static int OGG_UpdateSection(OGG_music *music)
music->stream = SDL_NewAudioStream(AUDIO_S16SYS, (Uint8)vi->channels, (int)vi->rate,
music_spec.format, music_spec.channels, music_spec.freq);
if (!music->stream) {
SDL_free(music->buffer);
music->buffer = NULL;
music->buffer_size = 0;
return -1;
}

music->buffer_size = music_spec.samples * (int)sizeof(Sint16) * vi->channels;
music->buffer = (char *)SDL_malloc((size_t)music->buffer_size);
if (!music->buffer) {
return -1;
new_buffer_size = music_spec.samples * (int)sizeof(Sint16) * vi->channels;

/* Note: never shrink the buffer, we just decoded data in there. */
if (new_buffer_size > music->buffer_size) {
char *new_buffer = (char *)SDL_realloc(music->buffer, new_buffer_size);
if (!new_buffer) {
SDL_free(music->buffer);
music->buffer = NULL;
music->buffer_size = 0;
return -1;
}
music->buffer = new_buffer;
music->buffer_size = new_buffer_size;
}
return 0;
}
Expand Down Expand Up @@ -387,7 +396,7 @@ static int OGG_GetSome(void *context, void *data, int bytes, SDL_bool *done)
#ifdef OGG_USE_TREMOR
amount = (int)vorbis.ov_read(&music->vf, music->buffer, music->buffer_size, &section);
#else
amount = (int)vorbis.ov_read(&music->vf, music->buffer, music->buffer_size, SDL_BYTEORDER == SDL_BIG_ENDIAN, 2, 1, &section);
amount = (int)vorbis.ov_read(&music->vf, music->buffer, music->buffer_size, SDL_BYTEORDER == SDL_BIG_ENDIAN, (int)sizeof(Sint16), 1, &section);
#endif
if (amount < 0) {
return set_ov_error("ov_read", amount);
Expand All @@ -400,6 +409,10 @@ static int OGG_GetSome(void *context, void *data, int bytes, SDL_bool *done)
}
}

remap_channels_vorbis_s16((Sint16 *)music->buffer,
amount / (int)sizeof(Sint16),
music->vi.channels);

pcmPos = vorbis.ov_pcm_tell(&music->vf);
if (music->loop && (music->play_count != 1) && (pcmPos >= music->loop_end)) {
amount -= (int)((pcmPos - music->loop_end) * music->vi.channels) * (int)sizeof(Sint16);
Expand Down
60 changes: 34 additions & 26 deletions src/codecs/music_ogg_stb.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
/* This file supports Ogg Vorbis music streams using a modified stb_vorbis module */

#include "music_ogg.h"
#include "remap_channels.h"
#include "utils.h"
#include "SDL_assert.h"

Expand Down Expand Up @@ -74,7 +75,6 @@ typedef struct {
int volume;
stb_vorbis *vf;
stb_vorbis_info vi;
int section;
SDL_AudioStream *stream;
char *buffer;
int buffer_size;
Expand Down Expand Up @@ -123,18 +123,15 @@ static void OGG_Delete(void *context);
static int OGG_UpdateSection(OGG_music *music)
{
stb_vorbis_info vi;
int new_buffer_size;

vi = stb_vorbis_get_info(music->vf);

if (vi.channels == music->vi.channels && vi.sample_rate == music->vi.sample_rate) {
return 0;
}
SDL_memcpy(&music->vi, &vi, sizeof(vi));

if (music->buffer) {
SDL_free(music->buffer);
music->buffer = NULL;
}
music->vi = vi;

if (music->stream) {
SDL_FreeAudioStream(music->stream);
Expand All @@ -144,17 +141,30 @@ static int OGG_UpdateSection(OGG_music *music)
music->stream = SDL_NewAudioStream(AUDIO_F32SYS, (Uint8)vi.channels, (int)vi.sample_rate,
music_spec.format, music_spec.channels, music_spec.freq);
if (!music->stream) {
SDL_free(music->buffer);
music->buffer = NULL;
music->buffer_size = 0;
return -1;
}

music->buffer_size = music_spec.samples * (int)sizeof(float) * vi.channels;
if (music->buffer_size <= 0) {
new_buffer_size = music_spec.samples * (int)sizeof(float) * vi.channels;
if (new_buffer_size <= 0) {
music->buffer = NULL;
music->buffer_size = 0;
return -1;
}

music->buffer = (char *)SDL_malloc((size_t)music->buffer_size);
if (!music->buffer) {
return -1;
/* Note: never shrink the buffer, we just decoded data in there. */
if (new_buffer_size > music->buffer_size) {
char *new_buffer = (char *)SDL_realloc(music->buffer, new_buffer_size);
if (!new_buffer) {
SDL_free(music->buffer);
music->buffer = NULL;
music->buffer_size = 0;
return -1;
}
music->buffer = new_buffer;
music->buffer_size = new_buffer_size;
}
return 0;
}
Expand All @@ -175,7 +185,6 @@ static void *OGG_CreateFromRW(SDL_RWops *src, int freesrc)
}
music->src = src;
music->volume = MIX_MAX_VOLUME;
music->section = -1;

music->vf = stb_vorbis_open_rwops(src, 0, &error, NULL);

Expand Down Expand Up @@ -308,8 +317,7 @@ static int OGG_GetSome(void *context, void *data, int bytes, SDL_bool *done)
{
OGG_music *music = (OGG_music *)context;
SDL_bool looped = SDL_FALSE;
int filled, amount, result;
int section;
int filled, amount, samples, result;
Sint64 pcmPos;

filled = SDL_AudioStreamGet(music->stream, data, bytes);
Expand All @@ -323,21 +331,21 @@ static int OGG_GetSome(void *context, void *data, int bytes, SDL_bool *done)
return 0;
}

section = music->section;
amount = stb_vorbis_get_samples_float_interleaved(music->vf,
music->vi.channels,
(float *)music->buffer,
music_spec.samples * music->vi.channels);
samples = stb_vorbis_get_samples_float_interleaved(music->vf,
music->vi.channels,
(float *)music->buffer,
music->buffer_size / (int)sizeof(float));

amount *= music->vi.channels * sizeof(float);

if (section != music->section) {
music->section = section;
if (OGG_UpdateSection(music) < 0) {
return -1;
}
if (OGG_UpdateSection(music) < 0) {
return -1;
}

amount = samples * music->vi.channels * sizeof(float);

remap_channels_vorbis_flt((float *)music->buffer,
samples * music->vi.channels,
music->vi.channels);

pcmPos = stb_vorbis_get_playback_sample_offset(music->vf);
if (music->loop && (music->play_count != 1) && (pcmPos >= music->loop_end)) {
amount -= (int)((pcmPos - music->loop_end) * music->vi.channels) * (int)sizeof(float);
Expand Down
33 changes: 24 additions & 9 deletions src/codecs/music_opus.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "SDL_loadso.h"

#include "music_opus.h"
#include "remap_channels.h"
#include "utils.h"

#ifdef OPUSFILE_HEADER
Expand Down Expand Up @@ -169,6 +170,7 @@ static void OPUS_Delete(void*);
static int OPUS_UpdateSection(OPUS_music *music)
{
const OpusHead *op_info;
int new_buffer_size;

op_info = opus.op_head(music->of, -1);
if (!op_info) {
Expand All @@ -180,11 +182,6 @@ static int OPUS_UpdateSection(OPUS_music *music)
}
music->op_info = op_info;

if (music->buffer) {
SDL_free(music->buffer);
music->buffer = NULL;
}

if (music->stream) {
SDL_FreeAudioStream(music->stream);
music->stream = NULL;
Expand All @@ -193,13 +190,25 @@ static int OPUS_UpdateSection(OPUS_music *music)
music->stream = SDL_NewAudioStream(AUDIO_S16SYS, (Uint8)op_info->channel_count, 48000,
music_spec.format, music_spec.channels, music_spec.freq);
if (!music->stream) {
SDL_free(music->buffer);
music->buffer = NULL;
music->buffer_size = 0;
return -1;
}

music->buffer_size = (int)music_spec.samples * (int)sizeof(opus_int16) * op_info->channel_count;
music->buffer = (char *)SDL_malloc((size_t)music->buffer_size);
if (!music->buffer) {
return -1;
new_buffer_size = (int)music_spec.samples * (int)sizeof(opus_int16) * op_info->channel_count;

/* Note: never shrink the buffer, we just decoded data in there. */
if (new_buffer_size > music->buffer_size) {
char *new_buffer = (char *)SDL_realloc(music->buffer, (size_t)new_buffer_size);
if (!new_buffer) {
SDL_free(music->buffer);
music->buffer = NULL;
music->buffer_size = 0;
return -1;
}
music->buffer = new_buffer;
music->buffer_size = new_buffer_size;
}
return 0;
}
Expand Down Expand Up @@ -378,6 +387,12 @@ static int OPUS_GetSome(void *context, void *data, int bytes, SDL_bool *done)
}
}

if (music->op_info->mapping_family == 1) {
remap_channels_vorbis_s16((Sint16 *)music->buffer,
samples * music->op_info->channel_count,
music->op_info->channel_count);
}

pcmPos = opus.op_pcm_tell(music->of);
if (music->loop && (music->play_count != 1) && (pcmPos >= music->loop_end)) {
samples -= (int)((pcmPos - music->loop_end) * music->op_info->channel_count) * (int)sizeof(Sint16);
Expand Down
Loading