1 /* 2 This file is part of Deadbeef Player source code 3 http://deadbeef.sourceforge.net 4 5 pcm volume manipulation routines 6 7 Copyright (C) 2009-2013 Alexey Yakovenko 8 9 This software is provided 'as-is', without any express or implied 10 warranty. In no event will the authors be held liable for any damages 11 arising from the use of this software. 12 13 Permission is granted to anyone to use this software for any purpose, 14 including commercial applications, and to alter it and redistribute it 15 freely, subject to the following restrictions: 16 17 1. The origin of this software must not be misrepresented; you must not 18 claim that you wrote the original software. If you use this software 19 in a product, an acknowledgment in the product documentation would be 20 appreciated but is not required. 21 2. Altered source versions must be plainly marked as such, and must not be 22 misrepresented as being the original software. 23 3. This notice may not be removed or altered from any source distribution. 24 25 Alexey Yakovenko waker@users.sourceforge.net 26 */ 27 #include <math.h> 28 #include <stdio.h> 29 #include "volume.h" 30 #include "conf.h" 31 32 #define VOLUME_MIN (-50.f) 33 34 static float volume_db = 0; // in dB 35 static float volume_amp = 1; // amplitude [0..1] 36 static int audio_mute = 0; 37 38 void volume_set_db(float dB)39volume_set_db (float dB) { 40 if (dB < VOLUME_MIN) { 41 dB = VOLUME_MIN; 42 } 43 if (dB > 0) { 44 dB = 0; 45 } 46 conf_set_float ("playback.volume", dB); 47 volume_db = dB; 48 volume_amp = dB > VOLUME_MIN ? db_to_amp (dB) : 0; 49 audio_mute = 0; 50 } 51 52 float volume_get_db(void)53volume_get_db (void) { 54 return volume_db; 55 } 56 57 void volume_set_amp(float amp)58volume_set_amp (float amp) { 59 if (amp < 0) { 60 amp = 0; 61 } 62 if (amp > 1) { 63 amp = 1; 64 } 65 volume_amp = amp; 66 volume_db = amp > 0 ? amp_to_db (amp) : VOLUME_MIN; 67 conf_set_float ("playback.volume", volume_db); 68 audio_mute = 0; 69 } 70 71 float volume_get_amp(void)72volume_get_amp (void) { 73 return volume_amp; 74 } 75 76 float db_to_amp(float dB)77db_to_amp (float dB) { 78 // return pow (10, dB/20.f); 79 // thanks to he for this hack 80 const float ln10=2.3025850929940002f; 81 return exp(ln10*dB/20.f); 82 } 83 84 float amp_to_db(float amp)85amp_to_db (float amp) { 86 return 20*log10 (amp); 87 } 88 89 float volume_get_min_db(void)90volume_get_min_db (void) { 91 return VOLUME_MIN; 92 } 93 94 void audio_set_mute(int mute)95audio_set_mute (int mute) { 96 audio_mute = mute; 97 } 98 99 int audio_is_mute(void)100audio_is_mute (void) { 101 return audio_mute; 102 } 103