Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Generate mixer buffer size from sample rate #483

Merged
merged 4 commits into from
Mar 10, 2022
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
30 changes: 28 additions & 2 deletions prboom2/src/SDL/i_sound.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,32 @@ static int addsfx(int sfxid, int channel, const unsigned char *data, size_t len)
return channel;
}

static int getSliceSize(void)
{
int limit, n;

if (snd_samplecount >= 32)
return snd_samplecount * snd_samplerate / 11025;

limit = snd_samplerate / TICRATE;

// Try all powers of two, not exceeding the limit.

for (n=0;; ++n)
{
// 2^n <= limit < 2^n+1 ?

if ((1 << (n + 1)) > limit)
{
return (1 << n);
}
}

// Should never happen?

return 1024;
}
facespkz marked this conversation as resolved.
Show resolved Hide resolved

static void updateSoundParams(int handle, int volume, int seperation, int pitch)
{
int slot = handle;
Expand Down Expand Up @@ -679,7 +705,7 @@ void I_InitSound(void)
/* Initialize variables */
audio_rate = snd_samplerate;
audio_channels = 2;
audio_buffers = snd_samplecount * snd_samplerate / 11025;
audio_buffers = getSliceSize();

if (Mix_OpenAudioDevice(audio_rate, MIX_DEFAULT_FORMAT, audio_channels, audio_buffers,
NULL, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE) < 0)
Expand Down Expand Up @@ -709,7 +735,7 @@ void I_InitSound(void)
audio.format = AUDIO_S16LSB;
#endif
audio.channels = 2;
audio.samples = snd_samplecount * snd_samplerate / 11025;
audio.samples = getSliceSize();
audio.callback = I_UpdateSound;
if ( SDL_OpenAudio(&audio, NULL) < 0 )
{
Expand Down
2 changes: 1 addition & 1 deletion prboom2/src/m_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ default_t defaults[] =
{"pitched_sounds",{&pitched_sounds},{0},0,1, // killough 2/21/98
def_bool,ss_none}, // enables variable pitch in sound effects (from id's original code)
{"samplerate",{&snd_samplerate},{44100},11025,48000, def_int,ss_none},
{"slice_samplecount",{&snd_samplecount},{512},32,8192, def_int,ss_none},
{"slice_samplecount",{&snd_samplecount},{0},0,8192, def_int,ss_none},
{"sfx_volume",{&snd_SfxVolume},{8},0,15, def_int,ss_none},
{"music_volume",{&snd_MusicVolume},{8},0,15, def_int,ss_none},
{"mus_pause_opt",{&mus_pause_opt},{1},0,2, // CPhipps - music pausing
Expand Down