Commit 88aaa6b7 authored by Max Kellermann's avatar Max Kellermann

pcm_utils: added pcm_range()

Make the code more readable by moving the range checks to pcm_range(). gcc does quite a good job at optimizing it: the resulting binary is exactly the same, although it contains a parametrized shift instead of hard-coded boundaries.
parent 5b5e46f5
...@@ -31,6 +31,20 @@ pcm_dither(void) ...@@ -31,6 +31,20 @@ pcm_dither(void)
return (rand() & 511) - (rand() & 511); 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, void pcm_volumeChange(char *buffer, int bufferSize,
const struct audio_format *format, const struct audio_format *format,
int volume) int volume)
...@@ -55,8 +69,7 @@ void pcm_volumeChange(char *buffer, int bufferSize, ...@@ -55,8 +69,7 @@ void pcm_volumeChange(char *buffer, int bufferSize,
temp32 += pcm_dither(); temp32 += pcm_dither();
temp32 += 500; temp32 += 500;
temp32 /= 1000; temp32 /= 1000;
*buffer16 = temp32 > 32767 ? 32767 : *buffer16 = pcm_range(temp32, 16);
(temp32 < -32768 ? -32768 : temp32);
buffer16++; buffer16++;
bufferSize -= 2; bufferSize -= 2;
} }
...@@ -68,8 +81,7 @@ void pcm_volumeChange(char *buffer, int bufferSize, ...@@ -68,8 +81,7 @@ void pcm_volumeChange(char *buffer, int bufferSize,
temp32 += pcm_dither(); temp32 += pcm_dither();
temp32 += 500; temp32 += 500;
temp32 /= 1000; temp32 /= 1000;
*buffer8 = temp32 > 127 ? 127 : *buffer8 = pcm_range(temp32, 8);
(temp32 < -128 ? -128 : temp32);
buffer8++; buffer8++;
bufferSize--; bufferSize--;
} }
...@@ -99,9 +111,7 @@ static void pcm_add(char *buffer1, const char *buffer2, size_t bufferSize1, ...@@ -99,9 +111,7 @@ static void pcm_add(char *buffer1, const char *buffer2, size_t bufferSize1,
temp32 += pcm_dither(); temp32 += pcm_dither();
temp32 += 500; temp32 += 500;
temp32 /= 1000; temp32 /= 1000;
*buffer16_1 = *buffer16_1 = pcm_range(temp32, 16);
temp32 > 32767 ? 32767 : (temp32 <
-32768 ? -32768 : temp32);
buffer16_1++; buffer16_1++;
buffer16_2++; buffer16_2++;
bufferSize1 -= 2; bufferSize1 -= 2;
...@@ -117,9 +127,7 @@ static void pcm_add(char *buffer1, const char *buffer2, size_t bufferSize1, ...@@ -117,9 +127,7 @@ static void pcm_add(char *buffer1, const char *buffer2, size_t bufferSize1,
temp32 += pcm_dither(); temp32 += pcm_dither();
temp32 += 500; temp32 += 500;
temp32 /= 1000; temp32 /= 1000;
*buffer8_1 = *buffer8_1 = pcm_range(temp32, 8);
temp32 > 127 ? 127 : (temp32 <
-128 ? -128 : temp32);
buffer8_1++; buffer8_1++;
buffer8_2++; buffer8_2++;
bufferSize1--; bufferSize1--;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment