From edcd45df94bf69b78342943cd319d8ceb43ed6d1 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 11 Nov 2008 16:32:32 +0100 Subject: pcm_volume: added constant PCM_VOLUME_1 It may be desirable to change the range of integer volume levels (e.g. to 1024, which may utilize shifts instead of expensive integer divisions). Introduce the constant PCM_VOLUME_1 which describes the integer value for "100% volume". This is currently 1000. --- src/pcm_utils.c | 23 +++++++++++++---------- src/pcm_utils.h | 7 ++++++- src/player_control.c | 9 +++++++-- src/volume.c | 2 +- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/pcm_utils.c b/src/pcm_utils.c index c06c7e1ab..a68542f4a 100644 --- a/src/pcm_utils.c +++ b/src/pcm_utils.c @@ -53,7 +53,8 @@ pcm_volume_change_8(int8_t *buffer, unsigned num_samples, int volume) while (num_samples > 0) { int32_t sample = *buffer; - sample = (sample * volume + pcm_dither() + 500) / 1000; + sample = (sample * volume + pcm_dither() + PCM_VOLUME_1 / 2) + / PCM_VOLUME_1; *buffer++ = pcm_range(sample, 8); --num_samples; @@ -66,7 +67,8 @@ pcm_volume_change_16(int16_t *buffer, unsigned num_samples, int volume) while (num_samples > 0) { int32_t sample = *buffer; - sample = (sample * volume + pcm_dither() + 500) / 1000; + sample = (sample * volume + pcm_dither() + PCM_VOLUME_1 / 2) + / PCM_VOLUME_1; *buffer++ = pcm_range(sample, 16); --num_samples; @@ -79,7 +81,8 @@ pcm_volume_change_24(int32_t *buffer, unsigned num_samples, int volume) while (num_samples > 0) { int64_t sample = *buffer; - sample = (sample * volume + pcm_dither() + 500) / 1000; + sample = (sample * volume + pcm_dither() + PCM_VOLUME_1 / 2) + / PCM_VOLUME_1; *buffer++ = pcm_range(sample, 24); --num_samples; @@ -90,7 +93,7 @@ void pcm_volume(char *buffer, int bufferSize, const struct audio_format *format, int volume) { - if (volume >= 1000) + if (volume >= PCM_VOLUME_1) return; if (volume <= 0) { @@ -128,7 +131,7 @@ pcm_add_8(int8_t *buffer1, const int8_t *buffer2, int32_t sample2 = *buffer2++; sample1 = ((sample1 * volume1 + sample2 * volume2) + - pcm_dither() + 500) / 1000; + pcm_dither() + PCM_VOLUME_1 / 2) / PCM_VOLUME_1; *buffer1++ = pcm_range(sample1, 8); --num_samples; @@ -144,7 +147,7 @@ pcm_add_16(int16_t *buffer1, const int16_t *buffer2, int32_t sample2 = *buffer2++; sample1 = ((sample1 * volume1 + sample2 * volume2) + - pcm_dither() + 500) / 1000; + pcm_dither() + PCM_VOLUME_1 / 2) / PCM_VOLUME_1; *buffer1++ = pcm_range(sample1, 16); --num_samples; @@ -160,7 +163,7 @@ pcm_add_24(int32_t *buffer1, const int32_t *buffer2, int64_t sample2 = *buffer2++; sample1 = ((sample1 * volume1 + sample2 * volume2) + - pcm_dither() + 500) / 1000; + pcm_dither() + PCM_VOLUME_1 / 2) / PCM_VOLUME_1; *buffer1++ = pcm_range(sample1, 24); --num_samples; @@ -200,10 +203,10 @@ void pcm_mix(char *buffer1, const char *buffer2, size_t size, float s = sin(M_PI_2 * portion1); s *= s; - vol1 = s * 1000 + 0.5; - vol1 = vol1 > 1000 ? 1000 : (vol1 < 0 ? 0 : vol1); + vol1 = s * PCM_VOLUME_1 + 0.5; + vol1 = vol1 > PCM_VOLUME_1 ? PCM_VOLUME_1 : (vol1 < 0 ? 0 : vol1); - pcm_add(buffer1, buffer2, size, vol1, 1000 - vol1, format); + pcm_add(buffer1, buffer2, size, vol1, PCM_VOLUME_1 - vol1, format); } void pcm_convert_init(struct pcm_convert_state *state) diff --git a/src/pcm_utils.h b/src/pcm_utils.h index 069fb9f53..71398c015 100644 --- a/src/pcm_utils.h +++ b/src/pcm_utils.h @@ -27,6 +27,11 @@ struct audio_format; +enum { + /** this value means "100% volume" */ + PCM_VOLUME_1 = 1000, +}; + struct pcm_convert_state { struct pcm_resample_state resample; @@ -43,7 +48,7 @@ struct pcm_convert_state { static inline int pcm_float_to_volume(float volume) { - return volume * 1000.0 + 0.5; + return volume * PCM_VOLUME_1 + 0.5; } void pcm_volume(char *buffer, int bufferSize, diff --git a/src/player_control.c b/src/player_control.c index f65ba7c08..1d5c76aa0 100644 --- a/src/player_control.c +++ b/src/player_control.c @@ -22,6 +22,7 @@ #include "tag.h" #include "song.h" #include "idle.h" +#include "pcm_utils.h" #include "os_compat.h" #include "main_notify.h" @@ -35,7 +36,7 @@ void pc_init(unsigned int buffered_before_play) pc.error = PLAYER_ERROR_NOERROR; pc.state = PLAYER_STATE_STOP; pc.cross_fade_seconds = 0; - pc.software_volume = 1000; + pc.software_volume = PCM_VOLUME_1; } void pc_deinit(void) @@ -220,7 +221,11 @@ void setPlayerCrossFade(float crossFadeInSeconds) void setPlayerSoftwareVolume(int volume) { - volume = (volume > 1000) ? 1000 : (volume < 0 ? 0 : volume); + if (volume > PCM_VOLUME_1) + volume = PCM_VOLUME_1; + else if (volume < 0) + volume = 0; + pc.software_volume = volume; } diff --git a/src/volume.c b/src/volume.c index 8d497ed44..4b619f916 100644 --- a/src/volume.c +++ b/src/volume.c @@ -480,7 +480,7 @@ static int changeSoftwareVolume(int change, int rel) /*new = 100.0*(exp(new/50.0)-1)/(M_E*M_E-1)+0.5; */ if (new >= 100) - new = 1000; + new = PCM_VOLUME_1; else if (new <= 0) new = 0; else -- cgit v1.2.3