util/Clamp: generic Clamp() function
This commit is contained in:
parent
6416198e9f
commit
86e72ffefb
@ -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 \
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
}
|
||||
|
49
src/util/Clamp.hxx
Normal file
49
src/util/Clamp.hxx
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user