From 5b7a89d714c46b920b3b077960c05c9db88a0d60 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 14 Mar 2023 20:16:04 +0100 Subject: [PATCH] mixer/plugins/volume_mapping: convert to C++ --- src/mixer/plugins/AlsaMixerPlugin.cxx | 5 +- .../{volume_mapping.c => VolumeMapping.cxx} | 68 +++++++++++-------- src/mixer/plugins/VolumeMapping.hxx | 25 +++++++ src/mixer/plugins/meson.build | 2 +- src/mixer/plugins/volume_mapping.h | 16 ----- 5 files changed, 67 insertions(+), 49 deletions(-) rename src/mixer/plugins/{volume_mapping.c => VolumeMapping.cxx} (71%) create mode 100644 src/mixer/plugins/VolumeMapping.hxx delete mode 100644 src/mixer/plugins/volume_mapping.h diff --git a/src/mixer/plugins/AlsaMixerPlugin.cxx b/src/mixer/plugins/AlsaMixerPlugin.cxx index 43ba6c2f7..c816446b3 100644 --- a/src/mixer/plugins/AlsaMixerPlugin.cxx +++ b/src/mixer/plugins/AlsaMixerPlugin.cxx @@ -2,6 +2,7 @@ // Copyright The Music Player Daemon Project #include "AlsaMixerPlugin.hxx" +#include "VolumeMapping.hxx" #include "lib/alsa/NonBlock.hxx" #include "lib/alsa/Error.hxx" #include "lib/fmt/RuntimeError.hxx" @@ -17,10 +18,6 @@ #include "util/Math.hxx" #include "Log.hxx" -extern "C" { -#include "volume_mapping.h" -} - #include #define VOLUME_MIXER_ALSA_DEFAULT "default" diff --git a/src/mixer/plugins/volume_mapping.c b/src/mixer/plugins/VolumeMapping.cxx similarity index 71% rename from src/mixer/plugins/volume_mapping.c rename to src/mixer/plugins/VolumeMapping.cxx index 27e6e3761..c0b073987 100644 --- a/src/mixer/plugins/volume_mapping.c +++ b/src/mixer/plugins/VolumeMapping.cxx @@ -17,18 +17,19 @@ * -1/0/1 = down/nearest/up. */ +#include "VolumeMapping.hxx" + #include -#include -#include "volume_mapping.h" #define MAX_LINEAR_DB_SCALE 24 -static inline bool use_linear_dB_scale(long dBmin, long dBmax) +static constexpr bool use_linear_dB_scale(long dBmin, long dBmax) noexcept { return dBmax - dBmin <= MAX_LINEAR_DB_SCALE * 100; } -static long lrint_dir(double x, int dir) +[[gnu::const]] +static long lrint_dir(double x, int dir) noexcept { if (dir > 0) return lrint(ceil(x)); @@ -40,34 +41,41 @@ static long lrint_dir(double x, int dir) enum ctl_dir { PLAYBACK, CAPTURE }; -static int (* const get_dB_range[2])(snd_mixer_elem_t *, long *, long *) = { +static constexpr int (* const get_dB_range[2])(snd_mixer_elem_t *, long *, long *) = { snd_mixer_selem_get_playback_dB_range, snd_mixer_selem_get_capture_dB_range, }; -static int (* const get_raw_range[2])(snd_mixer_elem_t *, long *, long *) = { + +static constexpr int (* const get_raw_range[2])(snd_mixer_elem_t *, long *, long *) = { snd_mixer_selem_get_playback_volume_range, snd_mixer_selem_get_capture_volume_range, }; -static int (* const get_dB[2])(snd_mixer_elem_t *, snd_mixer_selem_channel_id_t, long *) = { + +static constexpr int (* const get_dB[2])(snd_mixer_elem_t *, snd_mixer_selem_channel_id_t, long *) = { snd_mixer_selem_get_playback_dB, snd_mixer_selem_get_capture_dB, }; -static int (* const get_raw[2])(snd_mixer_elem_t *, snd_mixer_selem_channel_id_t, long *) = { + +static constexpr int (* const get_raw[2])(snd_mixer_elem_t *, snd_mixer_selem_channel_id_t, long *) = { snd_mixer_selem_get_playback_volume, snd_mixer_selem_get_capture_volume, }; -static int (* const set_dB[2])(snd_mixer_elem_t *, long, int) = { + +static constexpr int (* const set_dB[2])(snd_mixer_elem_t *, long, int) = { snd_mixer_selem_set_playback_dB_all, snd_mixer_selem_set_capture_dB_all, }; -static int (* const set_raw[2])(snd_mixer_elem_t *, long) = { + +static constexpr int (* const set_raw[2])(snd_mixer_elem_t *, long) = { snd_mixer_selem_set_playback_volume_all, snd_mixer_selem_set_capture_volume_all, }; -static double get_normalized_volume(snd_mixer_elem_t *elem, - snd_mixer_selem_channel_id_t channel, - enum ctl_dir ctl_dir) +[[gnu::pure]] +static double +get_normalized_volume(snd_mixer_elem_t *elem, + snd_mixer_selem_channel_id_t channel, + enum ctl_dir ctl_dir) noexcept { long min, max, value; double normalized, min_norm; @@ -102,10 +110,11 @@ static double get_normalized_volume(snd_mixer_elem_t *elem, return normalized; } -static int set_normalized_volume(snd_mixer_elem_t *elem, - double volume, - int dir, - enum ctl_dir ctl_dir) +static int +set_normalized_volume(snd_mixer_elem_t *elem, + double volume, + int dir, + enum ctl_dir ctl_dir) noexcept { long min, max, value; double min_norm; @@ -148,29 +157,32 @@ static int set_normalized_volume(snd_mixer_elem_t *elem, return set_dB[ctl_dir](elem, value, dir); } -double get_normalized_playback_volume(snd_mixer_elem_t *elem, - snd_mixer_selem_channel_id_t channel) +double +get_normalized_playback_volume(snd_mixer_elem_t *elem, + snd_mixer_selem_channel_id_t channel) noexcept { return get_normalized_volume(elem, channel, PLAYBACK); } -double get_normalized_capture_volume(snd_mixer_elem_t *elem, - snd_mixer_selem_channel_id_t channel) +double +get_normalized_capture_volume(snd_mixer_elem_t *elem, + snd_mixer_selem_channel_id_t channel) noexcept { return get_normalized_volume(elem, channel, CAPTURE); } - -int set_normalized_playback_volume(snd_mixer_elem_t *elem, - double volume, - int dir) +int +set_normalized_playback_volume(snd_mixer_elem_t *elem, + double volume, + int dir) noexcept { return set_normalized_volume(elem, volume, dir, PLAYBACK); } -int set_normalized_capture_volume(snd_mixer_elem_t *elem, - double volume, - int dir) +int +set_normalized_capture_volume(snd_mixer_elem_t *elem, + double volume, + int dir) noexcept { return set_normalized_volume(elem, volume, dir, CAPTURE); } diff --git a/src/mixer/plugins/VolumeMapping.hxx b/src/mixer/plugins/VolumeMapping.hxx new file mode 100644 index 000000000..faf46c4b6 --- /dev/null +++ b/src/mixer/plugins/VolumeMapping.hxx @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: ISC + +#pragma once + +#include + +[[gnu::pure]] +double +get_normalized_playback_volume(snd_mixer_elem_t *elem, + snd_mixer_selem_channel_id_t channel) noexcept; + +[[gnu::pure]] +double +get_normalized_capture_volume(snd_mixer_elem_t *elem, + snd_mixer_selem_channel_id_t channel) noexcept; + +int +set_normalized_playback_volume(snd_mixer_elem_t *elem, + double volume, + int dir) noexcept; + +int +set_normalized_capture_volume(snd_mixer_elem_t *elem, + double volume, + int dir) noexcept; diff --git a/src/mixer/plugins/meson.build b/src/mixer/plugins/meson.build index a482b141f..6531ae8d9 100644 --- a/src/mixer/plugins/meson.build +++ b/src/mixer/plugins/meson.build @@ -6,7 +6,7 @@ mixer_plugins_sources = [ if alsa_dep.found() mixer_plugins_sources += [ 'AlsaMixerPlugin.cxx', - 'volume_mapping.c', + 'VolumeMapping.cxx', ] endif diff --git a/src/mixer/plugins/volume_mapping.h b/src/mixer/plugins/volume_mapping.h deleted file mode 100644 index 8790908f4..000000000 --- a/src/mixer/plugins/volume_mapping.h +++ /dev/null @@ -1,16 +0,0 @@ -// SPDX-License-Identifier: ISC - -#pragma once - -#include - -double get_normalized_playback_volume(snd_mixer_elem_t *elem, - snd_mixer_selem_channel_id_t channel); -double get_normalized_capture_volume(snd_mixer_elem_t *elem, - snd_mixer_selem_channel_id_t channel); -int set_normalized_playback_volume(snd_mixer_elem_t *elem, - double volume, - int dir); -int set_normalized_capture_volume(snd_mixer_elem_t *elem, - double volume, - int dir);