aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-12-22 21:08:06 +0100
committerMax Kellermann <max@duempel.org>2013-12-22 21:08:06 +0100
commit86e72ffefb886f6151add595fa33028859249d59 (patch)
treeb1678609df9c9875be5510205db8c90461eeff38
parent6416198e9f57e4ea59a9bdc4deb2856c562681e6 (diff)
downloadmpd-86e72ffefb886f6151add595fa33028859249d59.tar.gz
mpd-86e72ffefb886f6151add595fa33028859249d59.tar.xz
mpd-86e72ffefb886f6151add595fa33028859249d59.zip
util/Clamp: generic Clamp() function
-rw-r--r--Makefile.am1
-rw-r--r--src/mixer/AlsaMixerPlugin.cxx4
-rw-r--r--src/pcm/PcmMix.cxx3
-rw-r--r--src/util/Clamp.hxx49
4 files changed, 54 insertions, 3 deletions
diff --git a/Makefile.am b/Makefile.am
index 2b610a22f..78a5afeb5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -248,6 +248,7 @@ endif
libutil_a_SOURCES = \
src/util/Macros.hxx \
+ src/util/Clamp.hxx \
src/util/Error.cxx src/util/Error.hxx \
src/util/Domain.hxx \
src/util/ReusableArray.hxx \
diff --git a/src/mixer/AlsaMixerPlugin.cxx b/src/mixer/AlsaMixerPlugin.cxx
index d643a3325..75ba8825c 100644
--- a/src/mixer/AlsaMixerPlugin.cxx
+++ b/src/mixer/AlsaMixerPlugin.cxx
@@ -27,6 +27,7 @@
#include "event/Call.hxx"
#include "util/ASCII.hxx"
#include "util/ReusableArray.hxx"
+#include "util/Clamp.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"
@@ -372,8 +373,7 @@ AlsaMixer::SetVolume(unsigned volume, Error &error)
level = (long)(((vol / 100.0) * (volume_max - volume_min) +
volume_min) + 0.5);
- level = level > volume_max ? volume_max : level;
- level = level < volume_min ? volume_min : level;
+ level = Clamp(level, volume_min, volume_max);
err = snd_mixer_selem_set_playback_volume_all(elem, level);
if (err < 0) {
diff --git a/src/pcm/PcmMix.cxx b/src/pcm/PcmMix.cxx
index fab2d8154..2866aa456 100644
--- a/src/pcm/PcmMix.cxx
+++ b/src/pcm/PcmMix.cxx
@@ -23,6 +23,7 @@
#include "PcmUtils.hxx"
#include "AudioFormat.hxx"
#include "Traits.hxx"
+#include "util/Clamp.hxx"
#include <assert.h>
#include <math.h>
@@ -215,7 +216,7 @@ pcm_mix(void *buffer1, const void *buffer2, size_t size,
s *= s;
vol1 = s * PCM_VOLUME_1 + 0.5;
- vol1 = vol1 > PCM_VOLUME_1 ? PCM_VOLUME_1 : (vol1 < 0 ? 0 : vol1);
+ vol1 = Clamp<int>(vol1, 0, PCM_VOLUME_1);
return pcm_add_vol(buffer1, buffer2, size, vol1, PCM_VOLUME_1 - vol1, format);
}
diff --git a/src/util/Clamp.hxx b/src/util/Clamp.hxx
new file mode 100644
index 000000000..3217ef9f7
--- /dev/null
+++ b/src/util/Clamp.hxx
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2012 Max Kellermann <max@duempel.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef CLAMP_HPP
+#define CLAMP_HPP
+
+#include "Compiler.h"
+
+/**
+ * Clamps the specified value in a range. Returns #min or #max if the
+ * value is outside.
+ */
+template<typename T>
+static inline constexpr const T &
+Clamp(const T &value, const T &min, const T &max)
+{
+ return gcc_unlikely(value < min)
+ ? min
+ : (gcc_unlikely(value > max)
+ ? max : value);
+}
+
+#endif