diff options
author | Eric Wong <normalperson@yhbt.net> | 2008-09-29 02:09:28 -0700 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2008-09-29 02:09:28 -0700 |
commit | 8e3bfa4e5d5a3efa9661f938fa79dd031c3b66e3 (patch) | |
tree | 2ebe7402247f95acce7293285d5f903033ae1828 /src | |
parent | 769939b62f7557f8e7c483223d68a8b39af43e37 (diff) | |
parent | 33df4c65594384c32f91f0445d80fb2453d5ff7a (diff) | |
download | mpd-8e3bfa4e5d5a3efa9661f938fa79dd031c3b66e3.tar.gz mpd-8e3bfa4e5d5a3efa9661f938fa79dd031c3b66e3.tar.xz mpd-8e3bfa4e5d5a3efa9661f938fa79dd031c3b66e3.zip |
Merge branch 'mk/pcm'
* mk/pcm:
pcm_utils: added pcm_range()
pcm_utils: added inline function pcm_dither()
Diffstat (limited to '')
-rw-r--r-- | src/pcm_utils.c | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/src/pcm_utils.c b/src/pcm_utils.c index 90856fa1d..077010062 100644 --- a/src/pcm_utils.c +++ b/src/pcm_utils.c @@ -24,6 +24,26 @@ #include "conf.h" #include "os_compat.h" +static inline int +pcm_dither(void) +{ + return (rand() & 511) - (rand() & 511); +} + +/** + * Check if the value is within the range of the provided bit size, + * and caps it if necessary. + */ +static mpd_sint32 +pcm_range(mpd_sint32 sample, unsigned bits) +{ + if (mpd_unlikely(sample < (-1 << (bits - 1)))) + return -1 << (bits - 1); + if (mpd_unlikely(sample >= (1 << (bits - 1)))) + return (1 << (bits - 1)) - 1; + return sample; +} + void pcm_volumeChange(char *buffer, int bufferSize, const AudioFormat * format, int volume) { @@ -44,12 +64,10 @@ void pcm_volumeChange(char *buffer, int bufferSize, const AudioFormat * format, while (bufferSize > 0) { temp32 = *buffer16; temp32 *= volume; - temp32 += rand() & 511; - temp32 -= rand() & 511; + temp32 += pcm_dither(); temp32 += 500; temp32 /= 1000; - *buffer16 = temp32 > 32767 ? 32767 : - (temp32 < -32768 ? -32768 : temp32); + *buffer16 = pcm_range(temp32, 16); buffer16++; bufferSize -= 2; } @@ -58,12 +76,10 @@ void pcm_volumeChange(char *buffer, int bufferSize, const AudioFormat * format, while (bufferSize > 0) { temp32 = *buffer8; temp32 *= volume; - temp32 += rand() & 511; - temp32 -= rand() & 511; + temp32 += pcm_dither(); temp32 += 500; temp32 /= 1000; - *buffer8 = temp32 > 127 ? 127 : - (temp32 < -128 ? -128 : temp32); + *buffer8 = pcm_range(temp32, 8); buffer8++; bufferSize--; } @@ -90,13 +106,10 @@ static void pcm_add(char *buffer1, const char *buffer2, size_t bufferSize1, temp32 = (vol1 * (*buffer16_1) + vol2 * (*buffer16_2)); - temp32 += rand() & 511; - temp32 -= rand() & 511; + temp32 += pcm_dither(); temp32 += 500; temp32 /= 1000; - *buffer16_1 = - temp32 > 32767 ? 32767 : (temp32 < - -32768 ? -32768 : temp32); + *buffer16_1 = pcm_range(temp32, 16); buffer16_1++; buffer16_2++; bufferSize1 -= 2; @@ -109,13 +122,10 @@ static void pcm_add(char *buffer1, const char *buffer2, size_t bufferSize1, while (bufferSize1 > 0 && bufferSize2 > 0) { temp32 = (vol1 * (*buffer8_1) + vol2 * (*buffer8_2)); - temp32 += rand() & 511; - temp32 -= rand() & 511; + temp32 += pcm_dither(); temp32 += 500; temp32 /= 1000; - *buffer8_1 = - temp32 > 127 ? 127 : (temp32 < - -128 ? -128 : temp32); + *buffer8_1 = pcm_range(temp32, 8); buffer8_1++; buffer8_2++; bufferSize1--; |