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.
This commit is contained in:
Max Kellermann 2008-11-11 16:32:32 +01:00
parent ad77a3e0ac
commit edcd45df94
4 changed files with 27 additions and 14 deletions

View File

@ -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)

View File

@ -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,

View File

@ -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;
}

View File

@ -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