From eb67bff9ba7fba5dce4dd8fbc79cf389ce8aa12a Mon Sep 17 00:00:00 2001 From: Starbuck5 <46412508+Starbuck5@users.noreply.github.com> Date: Sun, 22 Dec 2024 03:50:06 -0800 Subject: [PATCH 1/6] Convert volume API from 0-128 ints to 0-1 floats. --- examples/playmus.c | 6 +- include/SDL3_mixer/SDL_mixer.h | 49 ++++++------ src/codecs/music_drflac.c | 8 +- src/codecs/music_flac.c | 8 +- src/codecs/music_fluidsynth.c | 12 +-- src/codecs/music_gme.c | 16 ++-- src/codecs/music_minimp3.c | 8 +- src/codecs/music_modplug.c | 12 +-- src/codecs/music_mpg123.c | 8 +- src/codecs/music_ogg.c | 8 +- src/codecs/music_ogg_stb.c | 8 +- src/codecs/music_opus.c | 8 +- src/codecs/music_timidity.c | 10 +-- src/codecs/music_wav.c | 8 +- src/codecs/music_wavpack.c | 8 +- src/codecs/music_xmp.c | 8 +- src/codecs/native_midi/native_midi_macosx.c | 9 +-- src/codecs/timidity/playmidi.c | 4 +- src/codecs/timidity/timidity.h | 2 +- src/mixer.c | 88 +++++++++++---------- src/music.c | 34 ++++---- src/music.h | 6 +- 22 files changed, 164 insertions(+), 164 deletions(-) diff --git a/examples/playmus.c b/examples/playmus.c index 5a5fa1e72..1f485a0ae 100644 --- a/examples/playmus.c +++ b/examples/playmus.c @@ -91,7 +91,7 @@ static void Menu(void) Mix_HaltMusic(); break; case 'v': case 'V': - Mix_VolumeMusic(SDL_atoi(buf+1)); + Mix_VolumeMusic((float)SDL_atof(buf+1)); break; } } @@ -112,7 +112,7 @@ static void IntHandler(int sig) int main(int argc, char *argv[]) { - int audio_volume = MIX_MAX_VOLUME; + float audio_volume = 1.0f; int looping = 0; bool interactive = false; bool use_io = false; @@ -151,7 +151,7 @@ int main(int argc, char *argv[]) } else if ((SDL_strcmp(argv[i], "-v") == 0) && argv[i+1]) { ++i; - audio_volume = SDL_atoi(argv[i]); + audio_volume = (float)SDL_atof(argv[i]); } else if (SDL_strcmp(argv[i], "-l") == 0) { looping = -1; diff --git a/include/SDL3_mixer/SDL_mixer.h b/include/SDL3_mixer/SDL_mixer.h index f2c674897..7bb954fe7 100644 --- a/include/SDL3_mixer/SDL_mixer.h +++ b/include/SDL3_mixer/SDL_mixer.h @@ -188,7 +188,6 @@ extern SDL_DECLSPEC void SDLCALL Mix_Quit(void); #define MIX_DEFAULT_FREQUENCY 44100 #define MIX_DEFAULT_FORMAT SDL_AUDIO_S16 #define MIX_DEFAULT_CHANNELS 2 -#define MIX_MAX_VOLUME 128 /* Volume of a chunk */ /** * The internal format for an audio chunk @@ -197,7 +196,7 @@ typedef struct Mix_Chunk { int allocated; Uint8 *abuf; Uint32 alen; - Uint8 volume; /* Per-sample volume, 0-128 */ + float volume; /* Per-sample volume */ } Mix_Chunk; /** @@ -1808,9 +1807,8 @@ extern SDL_DECLSPEC int SDLCALL Mix_FadeInChannelTimed(int channel, Mix_Chunk *c /** * Set the volume for a specific channel. * - * The volume must be between 0 (silence) and MIX_MAX_VOLUME (full volume). - * Note that MIX_MAX_VOLUME is 128. Values greater than MIX_MAX_VOLUME are - * clamped to MIX_MAX_VOLUME. + * The volume must be between 0 (silence) and 1 (full volume). + * Values greater than 1 are clamped to 1. * * Specifying a negative volume will not change the current volume; as such, * this can be used to query the current volume without making changes, as @@ -1820,18 +1818,18 @@ extern SDL_DECLSPEC int SDLCALL Mix_FadeInChannelTimed(int channel, Mix_Chunk *c * channels, and returns _the average_ of all channels' volumes prior to this * call. * - * The default volume for a channel is MIX_MAX_VOLUME (no attenuation). + * The default volume for a channel is 1 (no attenuation). * * \param channel the channel on set/query the volume on, or -1 for all * channels. - * \param volume the new volume, between 0 and MIX_MAX_VOLUME, or -1 to query. + * \param volume the new volume, between 0 and 1, or -1 to query. * \returns the previous volume. If the specified volume is -1, this returns * the current volume. If `channel` is -1, this returns the average * of all channels. * * \since This function is available since SDL_mixer 3.0.0. */ -extern SDL_DECLSPEC int SDLCALL Mix_Volume(int channel, int volume); +extern SDL_DECLSPEC float SDLCALL Mix_Volume(int channel, float volume); /** * Set the volume for a specific chunk. @@ -1842,55 +1840,53 @@ extern SDL_DECLSPEC int SDLCALL Mix_Volume(int channel, int volume); * volume for all instances of a sound in addition to specific instances of * that sound. * - * The volume must be between 0 (silence) and MIX_MAX_VOLUME (full volume). - * Note that MIX_MAX_VOLUME is 128. Values greater than MIX_MAX_VOLUME are - * clamped to MIX_MAX_VOLUME. + * The volume must be between 0 (silence) and 1 (full volume). + * Values greater than 1 are clamped to 1. * * Specifying a negative volume will not change the current volume; as such, * this can be used to query the current volume without making changes, as * this function returns the previous (in this case, still-current) value. * - * The default volume for a chunk is MIX_MAX_VOLUME (no attenuation). + * The default volume for a chunk is 1 (no attenuation). * * \param chunk the chunk whose volume to adjust. - * \param volume the new volume, between 0 and MIX_MAX_VOLUME, or -1 to query. + * \param volume the new volume, between 0 and 1, or -1 to query. * \returns the previous volume. If the specified volume is -1, this returns * the current volume. If `chunk` is NULL, this returns -1. * * \since This function is available since SDL_mixer 3.0.0. */ -extern SDL_DECLSPEC int SDLCALL Mix_VolumeChunk(Mix_Chunk *chunk, int volume); +extern SDL_DECLSPEC float SDLCALL Mix_VolumeChunk(Mix_Chunk *chunk, float volume); /** * Set the volume for the music channel. * - * The volume must be between 0 (silence) and MIX_MAX_VOLUME (full volume). - * Note that MIX_MAX_VOLUME is 128. Values greater than MIX_MAX_VOLUME are - * clamped to MIX_MAX_VOLUME. + * The volume must be between 0 (silence) and 1 (full volume). + * Values greater than 1 are clamped to 1. * * Specifying a negative volume will not change the current volume; as such, * this can be used to query the current volume without making changes, as * this function returns the previous (in this case, still-current) value. * - * The default volume for music is MIX_MAX_VOLUME (no attenuation). + * The default volume for music is 1 (no attenuation). * - * \param volume the new volume, between 0 and MIX_MAX_VOLUME, or -1 to query. + * \param volume the new volume, between 0 and 1, or -1 to query. * \returns the previous volume. If the specified volume is -1, this returns * the current volume. * * \since This function is available since SDL_mixer 3.0.0. */ -extern SDL_DECLSPEC int SDLCALL Mix_VolumeMusic(int volume); +extern SDL_DECLSPEC float SDLCALL Mix_VolumeMusic(float volume); /** * Query the current volume value for a music object. * * \param music the music object to query. - * \returns the music's current volume, between 0 and MIX_MAX_VOLUME (128). + * \returns the music's current volume, between 0 and 1. * * \since This function is available since SDL_mixer 3.0.0. */ -extern SDL_DECLSPEC int SDLCALL Mix_GetMusicVolume(Mix_Music *music); +extern SDL_DECLSPEC float SDLCALL Mix_GetMusicVolume(Mix_Music *music); /** * Set the master volume for all channels. @@ -1899,9 +1895,8 @@ extern SDL_DECLSPEC int SDLCALL Mix_GetMusicVolume(Mix_Music *music); * volume, and considers all three when mixing audio. This function sets the * master volume, which is applied to all playing channels when mixing. * - * The volume must be between 0 (silence) and MIX_MAX_VOLUME (full volume). - * Note that MIX_MAX_VOLUME is 128. Values greater than MIX_MAX_VOLUME are - * clamped to MIX_MAX_VOLUME. + * The volume must be between 0 (silence) and 1 (full volume). + * Values greater than 1 are clamped to 1. * * Specifying a negative volume will not change the current volume; as such, * this can be used to query the current volume without making changes, as @@ -1910,13 +1905,13 @@ extern SDL_DECLSPEC int SDLCALL Mix_GetMusicVolume(Mix_Music *music); * Note that the master volume does not affect any playing music; it is only * applied when mixing chunks. Use Mix_VolumeMusic() for that.\ * - * \param volume the new volume, between 0 and MIX_MAX_VOLUME, or -1 to query. + * \param volume the new volume, between 0 and 1, or -1 to query. * \returns the previous volume. If the specified volume is -1, this returns * the current volume. * * \since This function is available since SDL_mixer 3.0.0. */ -extern SDL_DECLSPEC int SDLCALL Mix_MasterVolume(int volume); +extern SDL_DECLSPEC float SDLCALL Mix_MasterVolume(float volume); /** * Halt playing of a particular channel. diff --git a/src/codecs/music_drflac.c b/src/codecs/music_drflac.c index 45a9a672b..76d140666 100644 --- a/src/codecs/music_drflac.c +++ b/src/codecs/music_drflac.c @@ -52,7 +52,7 @@ typedef struct { drflac *dec; int play_count; bool closeio; - int volume; + float volume; int status; int sample_rate; int channels; @@ -167,7 +167,7 @@ static void *DRFLAC_CreateFromIO(SDL_IOStream *src, bool closeio) if (!music) { return NULL; } - music->volume = MIX_MAX_VOLUME; + music->volume = 1.0f; if (MP3_IOinit(&music->file, src) < 0) { SDL_free(music); @@ -215,13 +215,13 @@ static void *DRFLAC_CreateFromIO(SDL_IOStream *src, bool closeio) return music; } -static void DRFLAC_SetVolume(void *context, int volume) +static void DRFLAC_SetVolume(void *context, float volume) { DRFLAC_Music *music = (DRFLAC_Music *)context; music->volume = volume; } -static int DRFLAC_GetVolume(void *context) +static float DRFLAC_GetVolume(void *context) { DRFLAC_Music *music = (DRFLAC_Music *)context; return music->volume; diff --git a/src/codecs/music_flac.c b/src/codecs/music_flac.c index 7e383628b..ab8b68db7 100644 --- a/src/codecs/music_flac.c +++ b/src/codecs/music_flac.c @@ -163,7 +163,7 @@ static void FLAC_Unload(void) typedef struct { - int volume; + float volume; int play_count; FLAC__StreamDecoder *flac_decoder; unsigned sample_rate; @@ -533,7 +533,7 @@ static void *FLAC_CreateFromIO(SDL_IOStream *src, bool closeio) return NULL; } music->src = src; - music->volume = MIX_MAX_VOLUME; + music->volume = 1.0f; music->flac_decoder = flac.FLAC__stream_decoder_new(); if (music->flac_decoder) { @@ -609,14 +609,14 @@ static const char* FLAC_GetMetaTag(void *context, Mix_MusicMetaTag tag_type) /* Set the volume for an FLAC stream */ -static void FLAC_SetVolume(void *context, int volume) +static void FLAC_SetVolume(void *context, float volume) { FLAC_Music *music = (FLAC_Music *)context; music->volume = volume; } /* Get the volume for an FLAC stream */ -static int FLAC_GetVolume(void *context) +static float FLAC_GetVolume(void *context) { FLAC_Music *music = (FLAC_Music *)context; return music->volume; diff --git a/src/codecs/music_fluidsynth.c b/src/codecs/music_fluidsynth.c index a355a08f2..e1d069cf6 100644 --- a/src/codecs/music_fluidsynth.c +++ b/src/codecs/music_fluidsynth.c @@ -135,7 +135,7 @@ typedef struct { SDL_AudioStream *stream; void *buffer; int buffer_size; - int volume; + float volume; bool is_paused; } FLUIDSYNTH_Music; @@ -187,7 +187,7 @@ static FLUIDSYNTH_Music *FLUIDSYNTH_LoadMusic(void *data) return NULL; } - music->volume = MIX_MAX_VOLUME; + music->volume = 1.0f; music->buffer_size = 4096/*music_spec.samples*/ * sizeof(Sint16) * channels; music->synth_write = fluidsynth.fluid_synth_write_s16; if (music_spec.format & 0x0020) { /* 32 bit. */ @@ -259,15 +259,15 @@ static void *FLUIDSYNTH_CreateFromIO(SDL_IOStream *src, bool closeio) return music; } -static void FLUIDSYNTH_SetVolume(void *context, int volume) +static void FLUIDSYNTH_SetVolume(void *context, float volume) { FLUIDSYNTH_Music *music = (FLUIDSYNTH_Music *)context; /* FluidSynth's default gain is 0.2. Make 1.0 the maximum gain value to avoid sound overload. */ music->volume = volume; - fluidsynth.fluid_synth_set_gain(music->synth, volume * 1.0f / MIX_MAX_VOLUME); + fluidsynth.fluid_synth_set_gain(music->synth, volume); } -static int FLUIDSYNTH_GetVolume(void *context) +static float FLUIDSYNTH_GetVolume(void *context) { FLUIDSYNTH_Music *music = (FLUIDSYNTH_Music *)context; return music->volume; @@ -321,7 +321,7 @@ static int FLUIDSYNTH_GetSome(void *context, void *data, int bytes, bool *done) } static int FLUIDSYNTH_GetAudio(void *context, void *data, int bytes) { - return music_pcm_getaudio(context, data, bytes, MIX_MAX_VOLUME, FLUIDSYNTH_GetSome); + return music_pcm_getaudio(context, data, bytes, 1.0f, FLUIDSYNTH_GetSome); } static void FLUIDSYNTH_Stop(void *context) diff --git a/src/codecs/music_gme.c b/src/codecs/music_gme.c index 89ae7ffee..eff1bb2e4 100644 --- a/src/codecs/music_gme.c +++ b/src/codecs/music_gme.c @@ -126,7 +126,7 @@ typedef struct int track_length; int intro_length; int loop_length; - int volume; + float volume; double tempo; double gain; SDL_AudioStream *stream; @@ -138,19 +138,17 @@ typedef struct static void GME_Delete(void *context); /* Set the volume for a GME stream */ -static void GME_SetVolume(void *music_p, int volume) +static void GME_SetVolume(void *music_p, float volume) { GME_Music *music = (GME_Music*)music_p; - double v = SDL_floor(((double)volume * music->gain) + 0.5); - music->volume = (int)v; + music->volume = volume; } /* Get the volume for a GME stream */ -static int GME_GetVolume(void *music_p) +static float GME_GetVolume(void *music_p) { GME_Music *music = (GME_Music*)music_p; - double v = SDL_floor(((double)(music->volume) / music->gain) + 0.5); - return (int)v; + return music->volume; } static int initialize_from_track_info(GME_Music *music, int track) @@ -265,7 +263,7 @@ static void *GME_CreateFromIO(struct SDL_IOStream *src, bool closeio) gme.gme_set_tempo(music->game_emu, music->tempo); - music->volume = MIX_MAX_VOLUME; + music->volume = 1.0f; meta_tags_init(&music->tags); if (initialize_from_track_info(music, 0) < 0) { @@ -328,7 +326,7 @@ static int GME_GetSome(void *context, void *data, int bytes, bool *done) static int GME_PlayAudio(void *music_p, void *data, int bytes) { GME_Music *music = (GME_Music*)music_p; - return music_pcm_getaudio(music_p, data, bytes, music->volume, GME_GetSome); + return music_pcm_getaudio(music_p, data, bytes, (float)SDL_floor(music->volume * music->gain + 0.5), GME_GetSome); } /* Close the given Game Music Emulators stream */ diff --git a/src/codecs/music_minimp3.c b/src/codecs/music_minimp3.c index 5c024bc90..1b45b387d 100644 --- a/src/codecs/music_minimp3.c +++ b/src/codecs/music_minimp3.c @@ -35,7 +35,7 @@ typedef struct { int closeio; mp3dec_ex_t dec; mp3dec_io_t io; - int volume; + float volume; int status; SDL_AudioStream *stream; mp3d_sample_t *buffer; @@ -72,7 +72,7 @@ static void *MINIMP3_CreateFromIO(SDL_IOStream *src, bool closeio) if (!music) { return NULL; } - music->volume = MIX_MAX_VOLUME; + music->volume = 1.0f; if (MP3_IOinit(&music->file, src) < 0) { SDL_free(music); @@ -125,13 +125,13 @@ static void *MINIMP3_CreateFromIO(SDL_IOStream *src, bool closeio) return music; } -static void MINIMP3_SetVolume(void *context, int volume) +static void MINIMP3_SetVolume(void *context, float volume) { MiniMP3_Music *music = (MiniMP3_Music *)context; music->volume = volume; } -static int MINIMP3_GetVolume(void *context) +static float MINIMP3_GetVolume(void *context) { MiniMP3_Music *music = (MiniMP3_Music *)context; return music->volume; diff --git a/src/codecs/music_modplug.c b/src/codecs/music_modplug.c index e16d117e8..05ff0b721 100644 --- a/src/codecs/music_modplug.c +++ b/src/codecs/music_modplug.c @@ -117,7 +117,7 @@ static void MODPLUG_Unload(void) typedef struct { - int volume; + float volume; int play_count; ModPlugFile *file; SDL_AudioStream *stream; @@ -177,7 +177,7 @@ void *MODPLUG_CreateFromIO(SDL_IOStream *src, bool closeio) return NULL; } - music->volume = MIX_MAX_VOLUME; + music->volume = 1.0f; SDL_zero(srcspec); srcspec.format = (settings.mBits == 8) ? SDL_AUDIO_U8 : SDL_AUDIO_S16; @@ -220,15 +220,15 @@ void *MODPLUG_CreateFromIO(SDL_IOStream *src, bool closeio) } /* Set the volume for a modplug stream */ -static void MODPLUG_SetVolume(void *context, int volume) +static void MODPLUG_SetVolume(void *context, float volume) { MODPLUG_Music *music = (MODPLUG_Music *)context; music->volume = volume; - modplug.ModPlug_SetMasterVolume(music->file, (unsigned int)volume * 2); /* 0-512, reduced to 0-256 to prevent clipping */ + modplug.ModPlug_SetMasterVolume(music->file, volume * 256); /* 0-512, reduced to 0-256 to prevent clipping */ } /* Get the volume for a modplug stream */ -static int MODPLUG_GetVolume(void *context) +static float MODPLUG_GetVolume(void *context) { MODPLUG_Music *music = (MODPLUG_Music *)context; return music->volume; @@ -289,7 +289,7 @@ static int MODPLUG_GetSome(void *context, void *data, int bytes, bool *done) static int MODPLUG_GetAudio(void *context, void *data, int bytes) { - return music_pcm_getaudio(context, data, bytes, MIX_MAX_VOLUME, MODPLUG_GetSome); + return music_pcm_getaudio(context, data, bytes, 1.0f, MODPLUG_GetSome); } /* Jump to a given order */ diff --git a/src/codecs/music_mpg123.c b/src/codecs/music_mpg123.c index 14a276b39..28c0ad380 100644 --- a/src/codecs/music_mpg123.c +++ b/src/codecs/music_mpg123.c @@ -141,7 +141,7 @@ typedef struct struct mp3file_t mp3file; int play_count; bool closeio; - int volume; + float volume; mpg123_handle* handle; SDL_AudioStream *stream; @@ -244,7 +244,7 @@ static void *MPG123_CreateFromIO(SDL_IOStream *src, bool closeio) if (!music) { return NULL; } - music->volume = MIX_MAX_VOLUME; + music->volume = 1.0f; if (MP3_IOinit(&music->mp3file, src) < 0) { SDL_free(music); @@ -339,13 +339,13 @@ static void *MPG123_CreateFromIO(SDL_IOStream *src, bool closeio) return music; } -static void MPG123_SetVolume(void *context, int volume) +static void MPG123_SetVolume(void *context, float volume) { MPG123_Music *music = (MPG123_Music *)context; music->volume = volume; } -static int MPG123_GetVolume(void *context) +static float MPG123_GetVolume(void *context) { MPG123_Music *music = (MPG123_Music *)context; return music->volume; diff --git a/src/codecs/music_ogg.c b/src/codecs/music_ogg.c index c4884c0f0..5ef5940d4 100644 --- a/src/codecs/music_ogg.c +++ b/src/codecs/music_ogg.c @@ -128,7 +128,7 @@ typedef struct { SDL_IOStream *src; bool closeio; int play_count; - int volume; + float volume; OggVorbis_File vf; vorbis_info vi; int section; @@ -254,7 +254,7 @@ static void *OGG_CreateFromIO(SDL_IOStream *src, bool closeio) return NULL; } music->src = src; - music->volume = MIX_MAX_VOLUME; + music->volume = 1.0f; music->section = -1; callbacks.read_func = sdl_read_func; @@ -343,14 +343,14 @@ static const char* OGG_GetMetaTag(void *context, Mix_MusicMetaTag tag_type) } /* Set the volume for an OGG stream */ -static void OGG_SetVolume(void *context, int volume) +static void OGG_SetVolume(void *context, float volume) { OGG_music *music = (OGG_music *)context; music->volume = volume; } /* Get the volume for an OGG stream */ -static int OGG_GetVolume(void *context) +static float OGG_GetVolume(void *context) { OGG_music *music = (OGG_music *)context; return music->volume; diff --git a/src/codecs/music_ogg_stb.c b/src/codecs/music_ogg_stb.c index 7a35328fa..0558165ae 100644 --- a/src/codecs/music_ogg_stb.c +++ b/src/codecs/music_ogg_stb.c @@ -71,7 +71,7 @@ typedef struct { SDL_IOStream *src; bool closeio; int play_count; - int volume; + float volume; stb_vorbis *vf; stb_vorbis_info vi; int section; @@ -177,7 +177,7 @@ static void *OGG_CreateFromIO(SDL_IOStream *src, bool closeio) return NULL; } music->src = src; - music->volume = MIX_MAX_VOLUME; + music->volume = 1.0f; music->section = -1; music->vf = stb_vorbis_open_io(src, 0, &error, NULL); @@ -279,14 +279,14 @@ static const char* OGG_GetMetaTag(void *context, Mix_MusicMetaTag tag_type) } /* Set the volume for an OGG stream */ -static void OGG_SetVolume(void *context, int volume) +static void OGG_SetVolume(void *context, float volume) { OGG_music *music = (OGG_music *)context; music->volume = volume; } /* Get the volume for an OGG stream */ -static int OGG_GetVolume(void *context) +static float OGG_GetVolume(void *context) { OGG_music *music = (OGG_music *)context; return music->volume; diff --git a/src/codecs/music_opus.c b/src/codecs/music_opus.c index c62579ef6..c7762bf35 100644 --- a/src/codecs/music_opus.c +++ b/src/codecs/music_opus.c @@ -106,7 +106,7 @@ typedef struct { SDL_IOStream *src; bool closeio; int play_count; - int volume; + float volume; OggOpusFile *of; const OpusHead *op_info; int section; @@ -224,7 +224,7 @@ static void *OPUS_CreateFromIO(SDL_IOStream *src, bool closeio) return NULL; } music->src = src; - music->volume = MIX_MAX_VOLUME; + music->volume = 1.0f; music->section = -1; SDL_zero(callbacks); @@ -321,14 +321,14 @@ static const char* OPUS_GetMetaTag(void *context, Mix_MusicMetaTag tag_type) } /* Set the volume for an Opus stream */ -static void OPUS_SetVolume(void *context, int volume) +static void OPUS_SetVolume(void *context, float volume) { OPUS_music *music = (OPUS_music *)context; music->volume = volume; } /* Get the volume for an Opus stream */ -static int OPUS_GetVolume(void *context) +static float OPUS_GetVolume(void *context) { OPUS_music *music = (OPUS_music *)context; return music->volume; diff --git a/src/codecs/music_timidity.c b/src/codecs/music_timidity.c index 8001c0718..e67e41f0d 100644 --- a/src/codecs/music_timidity.c +++ b/src/codecs/music_timidity.c @@ -35,7 +35,7 @@ typedef struct SDL_AudioStream *stream; void *buffer; Sint32 buffer_size; - int volume; + float volume; } TIMIDITY_Music; @@ -94,7 +94,7 @@ void *TIMIDITY_CreateFromIO(SDL_IOStream *src, bool closeio) return NULL; } - music->volume = MIX_MAX_VOLUME; + music->volume = 1.0f; SDL_memcpy(&spec, &music_spec, sizeof(spec)); if (spec.channels > 2) { @@ -128,14 +128,14 @@ void *TIMIDITY_CreateFromIO(SDL_IOStream *src, bool closeio) return music; } -static void TIMIDITY_SetVolume(void *context, int volume) +static void TIMIDITY_SetVolume(void *context, float volume) { TIMIDITY_Music *music = (TIMIDITY_Music *)context; music->volume = volume; Timidity_SetVolume(music->song, volume); } -static int TIMIDITY_GetVolume(void *context) +static float TIMIDITY_GetVolume(void *context) { TIMIDITY_Music *music = (TIMIDITY_Music *)context; return music->volume; @@ -209,7 +209,7 @@ static int TIMIDITY_GetSome(void *context, void *data, int bytes, bool *done) static int TIMIDITY_GetAudio(void *context, void *data, int bytes) { - return music_pcm_getaudio(context, data, bytes, MIX_MAX_VOLUME, TIMIDITY_GetSome); + return music_pcm_getaudio(context, data, bytes, 1.0f, TIMIDITY_GetSome); } static int TIMIDITY_Seek(void *context, double position) diff --git a/src/codecs/music_wav.c b/src/codecs/music_wav.c index a0182fc67..92b938ffe 100644 --- a/src/codecs/music_wav.c +++ b/src/codecs/music_wav.c @@ -80,7 +80,7 @@ typedef struct { SDL_IOStream *src; bool closeio; SDL_AudioSpec spec; - int volume; + float volume; int play_count; Sint64 start; Sint64 stop; @@ -233,7 +233,7 @@ static void *WAV_CreateFromIO(SDL_IOStream *src, bool closeio) return NULL; } music->src = src; - music->volume = MIX_MAX_VOLUME; + music->volume = 1.0f; /* Default decoder is PCM */ music->decode = fetch_pcm; music->encoding = PCM_CODE; @@ -271,13 +271,13 @@ static void *WAV_CreateFromIO(SDL_IOStream *src, bool closeio) return music; } -static void WAV_SetVolume(void *context, int volume) +static void WAV_SetVolume(void *context, float volume) { WAV_Music *music = (WAV_Music *)context; music->volume = volume; } -static int WAV_GetVolume(void *context) +static float WAV_GetVolume(void *context) { WAV_Music *music = (WAV_Music *)context; return music->volume; diff --git a/src/codecs/music_wavpack.c b/src/codecs/music_wavpack.c index fcd5eb8b1..9106fb5c6 100644 --- a/src/codecs/music_wavpack.c +++ b/src/codecs/music_wavpack.c @@ -182,7 +182,7 @@ typedef struct { SDL_IOStream *src2; /* correction file */ bool closeio; int play_count; - int volume; + float volume; WavpackContext *ctx; int64_t numsamples; @@ -364,7 +364,7 @@ static void *WAVPACK_CreateFromIO_internal(SDL_IOStream *src1, SDL_IOStream *src } music->src1 = src1; music->src2 = src2; - music->volume = MIX_MAX_VOLUME; + music->volume = 1.0f; music->ctx = (wvpk.WavpackOpenFileInputEx64 != NULL) ? wvpk.WavpackOpenFileInputEx64(&sdl_reader64, src1, src2, err, OPEN_NORMALIZE|OPEN_TAGS|FLAGS_DSD, 0) : @@ -477,13 +477,13 @@ static const char* WAVPACK_GetMetaTag(void *context, Mix_MusicMetaTag tag_type) return meta_tags_get(&music->tags, tag_type); } -static void WAVPACK_SetVolume(void *context, int volume) +static void WAVPACK_SetVolume(void *context, float volume) { WAVPACK_music *music = (WAVPACK_music *)context; music->volume = volume; } -static int WAVPACK_GetVolume(void *context) +static float WAVPACK_GetVolume(void *context) { WAVPACK_music *music = (WAVPACK_music *)context; return music->volume; diff --git a/src/codecs/music_xmp.c b/src/codecs/music_xmp.c index 64eca1a4c..f1162fa3b 100644 --- a/src/codecs/music_xmp.c +++ b/src/codecs/music_xmp.c @@ -135,7 +135,7 @@ typedef struct { SDL_IOStream *src; Sint64 src_offset; - int volume; + float volume; int play_count; struct xmp_module_info mi; struct xmp_frame_info fi; @@ -259,7 +259,7 @@ void *XMP_CreateFromIO(SDL_IOStream *src, bool closeio) goto e2; } - music->volume = MIX_MAX_VOLUME; + music->volume = 1.0f; SDL_zero(srcspec); srcspec.format = SDL_AUDIO_S16; srcspec.channels = 2; @@ -291,14 +291,14 @@ e0: SDL_free(music->buffer); SDL_free(music); } /* Set the volume for a libxmp stream */ -static void XMP_SetVolume(void *context, int volume) +static void XMP_SetVolume(void *context, float volume) { XMP_Music *music = (XMP_Music *)context; music->volume = volume; } /* Get the volume for a libxmp stream */ -static int XMP_GetVolume(void *context) +static float XMP_GetVolume(void *context) { XMP_Music *music = (XMP_Music *)context; return music->volume; diff --git a/src/codecs/native_midi/native_midi_macosx.c b/src/codecs/native_midi/native_midi_macosx.c index 00f8b7831..e33438a54 100644 --- a/src/codecs/native_midi/native_midi_macosx.c +++ b/src/codecs/native_midi/native_midi_macosx.c @@ -47,7 +47,7 @@ struct _NativeMidiSong }; static NativeMidiSong *currentsong = NULL; -static int latched_volume = MIX_MAX_VOLUME; +static float latched_volume = 1.0f; static OSStatus GetSequenceLength(MusicSequence sequence, MusicTimeStamp *_sequenceLength) @@ -278,7 +278,7 @@ void native_midi_freesong(NativeMidiSong *song) void native_midi_start(NativeMidiSong *song, int loops) { - int vol; + float vol; if (song == NULL) return; @@ -337,16 +337,15 @@ bool native_midi_active(void) return false; } -void native_midi_setvolume(int volume) +void native_midi_setvolume(float volume) { if (latched_volume == volume) return; latched_volume = volume; if ((currentsong) && (currentsong->audiounit)) { - const float floatvol = ((float) volume) / ((float) MIX_MAX_VOLUME); AudioUnitSetParameter(currentsong->audiounit, kHALOutputParam_Volume, - kAudioUnitScope_Global, 0, floatvol, 0); + kAudioUnitScope_Global, 0, volume, 0); } } diff --git a/src/codecs/timidity/playmidi.c b/src/codecs/timidity/playmidi.c index 7407ce706..3f3a43a24 100644 --- a/src/codecs/timidity/playmidi.c +++ b/src/codecs/timidity/playmidi.c @@ -782,9 +782,11 @@ int Timidity_PlaySome(MidiSong *song, void *stream, Sint32 len) return samples * bytes_per_sample; } -void Timidity_SetVolume(MidiSong *song, int volume) +void Timidity_SetVolume(MidiSong *song, float fvolume) { int i; + int volume = (int)(fvolume / 128); // How should this be handled? + if (volume > MAX_AMPLIFICATION) song->amplification = MAX_AMPLIFICATION; else diff --git a/src/codecs/timidity/timidity.h b/src/codecs/timidity/timidity.h index 2b3faf742..167b70bc8 100644 --- a/src/codecs/timidity/timidity.h +++ b/src/codecs/timidity/timidity.h @@ -149,7 +149,7 @@ typedef struct { extern int Timidity_Init(const char *config_file); extern int Timidity_Init_NoConfig(void); -extern void Timidity_SetVolume(MidiSong *song, int volume); +extern void Timidity_SetVolume(MidiSong *song, float fvolume); extern int Timidity_PlaySome(MidiSong *song, void *stream, Sint32 len); extern MidiSong *Timidity_LoadSong(SDL_IOStream *io, SDL_AudioSpec *audio); extern void Timidity_Start(MidiSong *song); diff --git a/src/mixer.c b/src/mixer.c index c5f615243..98b8adf28 100644 --- a/src/mixer.c +++ b/src/mixer.c @@ -75,14 +75,14 @@ static struct _Mix_Channel { int playing; Uint64 paused; Uint8 *samples; - int volume; + float volume; int looping; int tag; Uint64 expire; Uint64 start_time; Mix_Fading fading; - int fade_volume; - int fade_volume_reset; + float fade_volume; + float fade_volume_reset; Uint64 fade_length; Uint64 ticks_fade; effect_info *effects; @@ -109,7 +109,8 @@ static void *music_data = NULL; static const char **chunk_decoders = NULL; static int num_decoders = 0; -static SDL_AtomicInt master_volume = { MIX_MAX_VOLUME }; +static SDL_SpinLock master_volume_lock = 0; +static float master_volume = 1; int Mix_GetNumChunkDecoders(void) { @@ -336,7 +337,8 @@ mix_channels(void *udata, SDL_AudioStream *astream, int len, int total) { Uint8 *stream; Uint8 *mix_input; - int i, mixable, master_vol; + int i, mixable; + float master_vol; Uint64 sdl_ticks; (void)udata; @@ -360,7 +362,9 @@ mix_channels(void *udata, SDL_AudioStream *astream, int len, int total) /* Mix the music (must be done before the channels are added) */ mix_music(music_data, stream, len); - master_vol = SDL_GetAtomicInt(&master_volume); + SDL_LockSpinlock(&master_volume_lock); + master_vol = master_volume; + SDL_UnlockSpinlock(&master_volume_lock); /* Mix any playing channels... */ sdl_ticks = SDL_GetTicks(); @@ -386,17 +390,16 @@ mix_channels(void *udata, SDL_AudioStream *astream, int len, int total) mix_channel[i].fading = MIX_NO_FADING; } else { if (mix_channel[i].fading == MIX_FADING_OUT) { - int volume = (int)((mix_channel[i].fade_volume * (mix_channel[i].fade_length - ticks)) / mix_channel[i].fade_length); + float volume = (mix_channel[i].fade_volume * (mix_channel[i].fade_length - ticks)) / mix_channel[i].fade_length; Mix_Volume(i, volume); } else { - int volume = (int)((mix_channel[i].fade_volume * ticks) / mix_channel[i].fade_length); + float volume = (mix_channel[i].fade_volume * ticks) / mix_channel[i].fade_length; Mix_Volume(i, volume); } } } if (mix_channel[i].playing > 0) { - int volume = (master_vol * (mix_channel[i].volume * mix_channel[i].chunk->volume)) / (MIX_MAX_VOLUME * MIX_MAX_VOLUME); - float fvolume = (float)volume / (float)MIX_MAX_VOLUME; + float volume = master_vol * (mix_channel[i].volume * mix_channel[i].chunk->volume); int index = 0; int remaining = len; while (mix_channel[i].playing > 0 && index < len) { @@ -407,7 +410,7 @@ mix_channels(void *udata, SDL_AudioStream *astream, int len, int total) } mix_input = Mix_DoEffects(i, mix_channel[i].samples, mixable); - SDL_MixAudio(stream+index, mix_input, mixer.format, mixable, fvolume); + SDL_MixAudio(stream+index, mix_input, mixer.format, mixable, volume); if (mix_input != mix_channel[i].samples) SDL_free(mix_input); @@ -422,8 +425,7 @@ mix_channels(void *udata, SDL_AudioStream *astream, int len, int total) _Mix_channel_done_playing(i); /* Update the volume after the application callback */ - volume = (master_vol * (mix_channel[i].volume * mix_channel[i].chunk->volume)) / (MIX_MAX_VOLUME * MIX_MAX_VOLUME); - fvolume = (float)volume / (float)MIX_MAX_VOLUME; + volume = master_vol * (mix_channel[i].volume * mix_channel[i].chunk->volume); } } @@ -437,7 +439,7 @@ mix_channels(void *udata, SDL_AudioStream *astream, int len, int total) } mix_input = Mix_DoEffects(i, mix_channel[i].chunk->abuf, remaining); - SDL_MixAudio(stream+index, mix_input, mixer.format, remaining, fvolume); + SDL_MixAudio(stream+index, mix_input, mixer.format, remaining, volume); if (mix_input != mix_channel[i].chunk->abuf) SDL_free(mix_input); @@ -529,16 +531,16 @@ bool Mix_OpenAudio(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec) mix_channel[i].chunk = NULL; mix_channel[i].playing = 0; mix_channel[i].looping = 0; - mix_channel[i].volume = MIX_MAX_VOLUME; - mix_channel[i].fade_volume = MIX_MAX_VOLUME; - mix_channel[i].fade_volume_reset = MIX_MAX_VOLUME; + mix_channel[i].volume = 1.0f; + mix_channel[i].fade_volume = 1.0f; + mix_channel[i].fade_volume_reset = 1.0f; mix_channel[i].fading = MIX_NO_FADING; mix_channel[i].tag = -1; mix_channel[i].expire = 0; mix_channel[i].effects = NULL; mix_channel[i].paused = 0; } - Mix_VolumeMusic(MIX_MAX_VOLUME); + Mix_VolumeMusic(1.0f); _Mix_InitEffects(); @@ -608,9 +610,9 @@ int Mix_AllocateChannels(int numchans) mix_channel[i].chunk = NULL; mix_channel[i].playing = 0; mix_channel[i].looping = 0; - mix_channel[i].volume = MIX_MAX_VOLUME; - mix_channel[i].fade_volume = MIX_MAX_VOLUME; - mix_channel[i].fade_volume_reset = MIX_MAX_VOLUME; + mix_channel[i].volume = 1.0f; + mix_channel[i].fade_volume = 1.0f; + mix_channel[i].fade_volume_reset = 1.0f; mix_channel[i].fading = MIX_NO_FADING; mix_channel[i].tag = -1; mix_channel[i].expire = 0; @@ -861,7 +863,7 @@ Mix_Chunk *Mix_LoadWAV_IO(SDL_IOStream *src, bool closeio) #endif chunk->allocated = 1; - chunk->volume = MIX_MAX_VOLUME; + chunk->volume = 1.0f; /* Build the audio converter and create conversion buffers */ if (wavespec.format != mixer.format || @@ -920,7 +922,7 @@ Mix_Chunk *Mix_QuickLoad_WAV(Uint8 *mem) chunk->abuf = mem; mem += chunk->alen; } while (SDL_memcmp(magic, "data", 4) != 0); - chunk->volume = MIX_MAX_VOLUME; + chunk->volume = 1.0f; return chunk; } @@ -946,7 +948,7 @@ Mix_Chunk *Mix_QuickLoad_RAW(Uint8 *mem, Uint32 len) chunk->allocated = 0; chunk->alen = len; chunk->abuf = mem; - chunk->volume = MIX_MAX_VOLUME; + chunk->volume = 1.0f; return chunk; } @@ -1184,7 +1186,7 @@ int Mix_FadeInChannelTimed(int which, Mix_Chunk *chunk, int loops, int ms, int t } mix_channel[which].fading = MIX_FADING_IN; mix_channel[which].fade_volume = mix_channel[which].volume; - mix_channel[which].volume = 0; + mix_channel[which].volume = 0.0f; mix_channel[which].fade_length = (Uint64)ms; mix_channel[which].start_time = mix_channel[which].ticks_fade = sdl_ticks; mix_channel[which].expire = (ticks > 0) ? (sdl_ticks + ticks) : 0; @@ -1203,10 +1205,10 @@ int Mix_FadeInChannel(int channel, Mix_Chunk *chunk, int loops, int ms) /* Set volume of a particular channel */ -int Mix_Volume(int which, int volume) +float Mix_Volume(int which, float volume) { int i; - int prev_volume = 0; + float prev_volume = 0.0f; if (which == -1) { for (i = 0; i < num_channels; ++i) { @@ -1215,9 +1217,9 @@ int Mix_Volume(int which, int volume) prev_volume /= num_channels; } else if (which < num_channels) { prev_volume = mix_channel[which].volume; - if (volume >= 0) { - if (volume > MIX_MAX_VOLUME) { - volume = MIX_MAX_VOLUME; + if (volume >= 0.0f) { + if (volume > 1.0f) { + volume = 1.0f; } mix_channel[which].volume = volume; } @@ -1225,17 +1227,17 @@ int Mix_Volume(int which, int volume) return prev_volume; } /* Set volume of a particular chunk */ -int Mix_VolumeChunk(Mix_Chunk *chunk, int volume) +float Mix_VolumeChunk(Mix_Chunk *chunk, float volume) { - int prev_volume; + float prev_volume; if (chunk == NULL) { return -1; } prev_volume = chunk->volume; - if (volume >= 0) { - if (volume > MIX_MAX_VOLUME) { - volume = MIX_MAX_VOLUME; + if (volume >= 0.0f) { + if (volume > 1.0f) { + volume = 1.0f; } chunk->volume = volume; } @@ -1771,16 +1773,20 @@ void Mix_UnlockAudio(void) SDL_UnlockAudioStream(audio_stream); } -int Mix_MasterVolume(int volume) +float Mix_MasterVolume(float volume) { - int prev_volume = SDL_GetAtomicInt(&master_volume); - if (volume < 0) { + SDL_LockSpinlock(&master_volume_lock); + + float prev_volume = master_volume; + if (volume < 0.0f) { + SDL_UnlockSpinlock(&master_volume_lock); return prev_volume; } - if (volume > MIX_MAX_VOLUME) { - volume = MIX_MAX_VOLUME; + if (volume > 1.0f) { + volume = 1.0f; } - SDL_SetAtomicInt(&master_volume, volume); + master_volume = volume; + SDL_UnlockSpinlock(&master_volume_lock); return prev_volume; } diff --git a/src/music.c b/src/music.c index 668019e3c..6b693eadb 100644 --- a/src/music.c +++ b/src/music.c @@ -54,7 +54,7 @@ "SDL_MIXER_DEBUG_MUSIC_INTERFACES" static bool music_active = true; -static int music_volume = MIX_MAX_VOLUME; +static float music_volume = 1.0f; static Mix_Music *music_playing = NULL; SDL_AudioSpec music_spec; @@ -259,7 +259,7 @@ static void add_music_decoder(const char *decoder) /* Local low-level functions prototypes */ static void music_internal_initialize_volume(void); -static void music_internal_volume(int volume); +static void music_internal_volume(float volume); static int music_internal_play(Mix_Music *music, int play_count, double position); static int music_internal_position(double position); static bool music_internal_playing(void); @@ -279,7 +279,7 @@ void Mix_HookMusicFinished(void (SDLCALL *music_finished)(void)) /* Convenience function to fill audio and mix at the specified volume This is called from many music player's GetAudio callback. */ -int music_pcm_getaudio(void *context, void *data, int bytes, int volume, +int music_pcm_getaudio(void *context, void *data, int bytes, float volume, int (*GetSome)(void *context, void *data, int bytes, bool *done)) { Uint8 *snd = (Uint8 *)data; @@ -289,7 +289,7 @@ int music_pcm_getaudio(void *context, void *data, int bytes, int volume, const int MAX_ZERO_CYCLES = 10; /* just try to catch infinite loops */ bool done = false; - if (volume == MIX_MAX_VOLUME) { + if (volume == 1.0f) { dst = snd; } else { dst = SDL_stack_alloc(Uint8, (size_t)bytes); @@ -309,15 +309,15 @@ int music_pcm_getaudio(void *context, void *data, int bytes, int volume, } zero_cycles = 0; - if (volume == MIX_MAX_VOLUME) { + if (volume == 1.0f) { dst += consumed; } else { - SDL_MixAudio(snd, dst, music_spec.format, (Uint32)consumed, volume/(float)MIX_MAX_VOLUME); + SDL_MixAudio(snd, dst, music_spec.format, (Uint32)consumed, volume); snd += consumed; } len -= consumed; } - if (volume != MIX_MAX_VOLUME) { + if (volume != 1.0f) { SDL_stack_free(dst); } return len; @@ -334,7 +334,7 @@ void SDLCALL music_mixer(void *udata, Uint8 *stream, int len) /* Handle fading */ if (music_playing->fading != MIX_NO_FADING) { if (music_playing->fade_step++ < music_playing->fade_steps) { - int volume; + float volume; int fade_step = music_playing->fade_step; int fade_steps = music_playing->fade_steps; @@ -521,7 +521,7 @@ void open_music(const SDL_AudioSpec *spec) music_spec = *spec; open_music_type(MUS_NONE); - Mix_VolumeMusic(MIX_MAX_VOLUME); + Mix_VolumeMusic(1.0f); /* Calculate the number of ms for each callback */ ms_per_step = (int) ((4096.0f * 1000.0f) / spec->freq); @@ -1168,29 +1168,29 @@ double Mix_GetMusicLoopLengthTime(Mix_Music *music) static void music_internal_initialize_volume(void) { if (music_playing->fading == MIX_FADING_IN) { - music_internal_volume(0); + music_internal_volume(0.0f); } else { music_internal_volume(music_volume); } } /* Set the music volume */ -static void music_internal_volume(int volume) +static void music_internal_volume(float volume) { if (music_playing->interface->SetVolume) { music_playing->interface->SetVolume(music_playing->context, volume); } } -int Mix_VolumeMusic(int volume) +float Mix_VolumeMusic(float volume) { - int prev_volume; + float prev_volume; prev_volume = music_volume; if (volume < 0) { return prev_volume; } - if (volume > MIX_MAX_VOLUME) { - volume = MIX_MAX_VOLUME; + if (volume > 1.0f) { + volume = 1.0f; } music_volume = volume; Mix_LockAudio(); @@ -1201,9 +1201,9 @@ int Mix_VolumeMusic(int volume) return prev_volume; } -int Mix_GetMusicVolume(Mix_Music *music) +float Mix_GetMusicVolume(Mix_Music *music) { - int prev_volume; + float prev_volume; if (music && music->interface->GetVolume) prev_volume = music->interface->GetVolume(music->context); diff --git a/src/music.h b/src/music.h index 8d356d1b9..d2d0ef72d 100644 --- a/src/music.h +++ b/src/music.h @@ -95,10 +95,10 @@ typedef struct void *(*CreateFromFile)(const char *file); /* Set the volume */ - void (*SetVolume)(void *music, int volume); + void (*SetVolume)(void *music, float volume); /* Get the volume */ - int (*GetVolume)(void *music); + float (*GetVolume)(void *music); /* Start playing music from the beginning with an optional loop count */ int (*Play)(void *music, int play_count); @@ -166,7 +166,7 @@ extern bool load_music_type(Mix_MusicType type); extern bool open_music_type(Mix_MusicType type); extern bool has_music(Mix_MusicType type); extern void open_music(const SDL_AudioSpec *spec); -extern int music_pcm_getaudio(void *context, void *data, int bytes, int volume, +extern int music_pcm_getaudio(void *context, void *data, int bytes, float volume, int (*GetSome)(void *context, void *data, int bytes, bool *done)); extern void SDLCALL music_mixer(void *udata, Uint8 *stream, int len); extern void pause_async_music(int pause_on); From 62018373c9bd60ee43fb8f6cbf351a6fbd3ea21a Mon Sep 17 00:00:00 2001 From: Starbuck5 <46412508+Starbuck5@users.noreply.github.com> Date: Sun, 22 Dec 2024 14:45:24 -0800 Subject: [PATCH 2/6] Adjust int volumes I missed --- src/codecs/music_nativemidi.c | 2 +- src/codecs/native_midi/native_midi.h | 2 +- src/codecs/native_midi/native_midi_haiku.cpp | 8 ++++---- src/codecs/native_midi/native_midi_macosx.c | 2 +- src/codecs/native_midi/native_midi_win32.c | 13 +++++++------ src/codecs/timidity/playmidi.c | 2 +- 6 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/codecs/music_nativemidi.c b/src/codecs/music_nativemidi.c index 4ff0cbf92..6b0d88881 100644 --- a/src/codecs/music_nativemidi.c +++ b/src/codecs/music_nativemidi.c @@ -47,7 +47,7 @@ static int NATIVEMIDI_Play(void *context, int play_count) return 0; } -static void NATIVEMIDI_SetVolume(void *context, int volume) +static void NATIVEMIDI_SetVolume(void *context, float volume) { (void)context; native_midi_setvolume(volume); diff --git a/src/codecs/native_midi/native_midi.h b/src/codecs/native_midi/native_midi.h index 1981eca78..508a01487 100644 --- a/src/codecs/native_midi/native_midi.h +++ b/src/codecs/native_midi/native_midi.h @@ -34,7 +34,7 @@ void native_midi_pause(void); void native_midi_resume(void); void native_midi_stop(void); bool native_midi_active(void); -void native_midi_setvolume(int volume); +void native_midi_setvolume(float volume); const char *native_midi_error(void); #endif /* NATIVE_MIDI_H_ */ diff --git a/src/codecs/native_midi/native_midi_haiku.cpp b/src/codecs/native_midi/native_midi_haiku.cpp index ad21cae4e..109b2df24 100644 --- a/src/codecs/native_midi/native_midi_haiku.cpp +++ b/src/codecs/native_midi/native_midi_haiku.cpp @@ -212,11 +212,11 @@ bool native_midi_detect(void) return res == B_OK; } -void native_midi_setvolume(int volume) +void native_midi_setvolume(float volume) { - if (volume < 0) volume = 0; - if (volume > 128) volume = 128; - synth.SetVolume(volume / 128.0); + if (volume < 0.0f) volume = 0.0f; + if (volume > 1.0f) volume = 1.0f; + synth.SetVolume(volume); } NativeMidiSong *native_midi_loadsong_IO(SDL_IOStream *src, bool closeio) diff --git a/src/codecs/native_midi/native_midi_macosx.c b/src/codecs/native_midi/native_midi_macosx.c index e33438a54..3aef41a42 100644 --- a/src/codecs/native_midi/native_midi_macosx.c +++ b/src/codecs/native_midi/native_midi_macosx.c @@ -396,7 +396,7 @@ bool native_midi_active(void) { } -void native_midi_setvolume(int volume) +void native_midi_setvolume(float volume) { } diff --git a/src/codecs/native_midi/native_midi_win32.c b/src/codecs/native_midi/native_midi_win32.c index af6a62422..05dd5cbf9 100644 --- a/src/codecs/native_midi/native_midi_win32.c +++ b/src/codecs/native_midi/native_midi_win32.c @@ -320,16 +320,17 @@ bool native_midi_active(void) return currentsong->MusicPlaying; } -void native_midi_setvolume(int volume) +void native_midi_setvolume(float volume) { int calcVolume; - if (volume > 128) - volume = 128; - if (volume < 0) - volume = 0; - calcVolume = (65535 * volume / 128); + if (volume > 1.0f) + volume = 1.0f; + if (volume < 0.0f) + volume = 0.0f; + calcVolume = (int)(65535 * volume); midiOutSetVolume((HMIDIOUT)hMidiStream, MAKELONG(calcVolume , calcVolume)); + midiOutSetVolume((HMIDIOUT)hMidiStream, MAKELONG(calcVolume , calcVolume)); } const char *native_midi_error(void) diff --git a/src/codecs/timidity/playmidi.c b/src/codecs/timidity/playmidi.c index 3f3a43a24..84d7968b2 100644 --- a/src/codecs/timidity/playmidi.c +++ b/src/codecs/timidity/playmidi.c @@ -785,7 +785,7 @@ int Timidity_PlaySome(MidiSong *song, void *stream, Sint32 len) void Timidity_SetVolume(MidiSong *song, float fvolume) { int i; - int volume = (int)(fvolume / 128); // How should this be handled? + int volume = (int)(fvolume / 128); if (volume > MAX_AMPLIFICATION) song->amplification = MAX_AMPLIFICATION; From c2c004c583e84d1e30fefa99a396e607e250b8f2 Mon Sep 17 00:00:00 2001 From: Starbuck5 <46412508+Starbuck5@users.noreply.github.com> Date: Fri, 27 Dec 2024 23:51:35 -0600 Subject: [PATCH 3/6] Float volume: correct wrong timidity calculation --- src/codecs/timidity/playmidi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codecs/timidity/playmidi.c b/src/codecs/timidity/playmidi.c index 84d7968b2..74dfe2a57 100644 --- a/src/codecs/timidity/playmidi.c +++ b/src/codecs/timidity/playmidi.c @@ -785,7 +785,7 @@ int Timidity_PlaySome(MidiSong *song, void *stream, Sint32 len) void Timidity_SetVolume(MidiSong *song, float fvolume) { int i; - int volume = (int)(fvolume / 128); + int volume = (int)(fvolume * 128); if (volume > MAX_AMPLIFICATION) song->amplification = MAX_AMPLIFICATION; From c09946d5fed072cafe59158617761ccb5e820be6 Mon Sep 17 00:00:00 2001 From: Starbuck5 <46412508+Starbuck5@users.noreply.github.com> Date: Sat, 28 Dec 2024 00:17:02 -0600 Subject: [PATCH 4/6] Float volume: adjust music gme calculation --- src/codecs/music_gme.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codecs/music_gme.c b/src/codecs/music_gme.c index eff1bb2e4..ef1483f4d 100644 --- a/src/codecs/music_gme.c +++ b/src/codecs/music_gme.c @@ -326,7 +326,7 @@ static int GME_GetSome(void *context, void *data, int bytes, bool *done) static int GME_PlayAudio(void *music_p, void *data, int bytes) { GME_Music *music = (GME_Music*)music_p; - return music_pcm_getaudio(music_p, data, bytes, (float)SDL_floor(music->volume * music->gain + 0.5), GME_GetSome); + return music_pcm_getaudio(music_p, data, bytes, (float)(music->volume * music->gain), GME_GetSome); } /* Close the given Game Music Emulators stream */ From 982b3b5f198a6b05bdc7e8da40b0e2f1d176befd Mon Sep 17 00:00:00 2001 From: Starbuck5 <46412508+Starbuck5@users.noreply.github.com> Date: Sat, 28 Dec 2024 00:17:20 -0600 Subject: [PATCH 5/6] Float volume: add API changes to changelog --- CHANGES.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.txt b/CHANGES.txt index 9df178b17..787d72d86 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,2 +1,3 @@ 3.0.0: * Updated for SDL 3.0 + * Volume APIs now operate on floats, rather than integers between 0 and 128 From 437c63e4ec3cb9e8a4fd7cdc7567b3f5f107a5b3 Mon Sep 17 00:00:00 2001 From: Starbuck5 <46412508+Starbuck5@users.noreply.github.com> Date: Sat, 28 Dec 2024 00:26:49 -0600 Subject: [PATCH 6/6] Float volume: correct type for SetMasterVolume --- src/codecs/music_modplug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codecs/music_modplug.c b/src/codecs/music_modplug.c index 05ff0b721..0028dd1c2 100644 --- a/src/codecs/music_modplug.c +++ b/src/codecs/music_modplug.c @@ -224,7 +224,7 @@ static void MODPLUG_SetVolume(void *context, float volume) { MODPLUG_Music *music = (MODPLUG_Music *)context; music->volume = volume; - modplug.ModPlug_SetMasterVolume(music->file, volume * 256); /* 0-512, reduced to 0-256 to prevent clipping */ + modplug.ModPlug_SetMasterVolume(music->file, (unsigned int)(volume * 256)); /* 0-512, reduced to 0-256 to prevent clipping */ } /* Get the volume for a modplug stream */