]> git.sesse.net Git - vlc/blob - modules/audio_filter/gain.c
access_dvb/dtv: support for libdvbpsi >= 1.0.0
[vlc] / modules / audio_filter / gain.c
1 /*****************************************************************************
2  * gain.c : gain control filter
3  *****************************************************************************
4  * Copyright © 2012 VLC authors and VideoLAN
5  *
6  * Authors: Ludovic Fauvet <etix@videolan.org>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_aout.h>
33 #include <vlc_aout_volume.h>
34 #include <vlc_filter.h>
35 #include <vlc_modules.h>
36 #include <vlc_plugin.h>
37
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42
43 static int      Open        ( vlc_object_t * );
44 static void     Close       ( vlc_object_t * );
45 static block_t  *Process    ( filter_t *, block_t * );
46
47 struct filter_sys_t
48 {
49     audio_volume_t volume;
50     float f_gain;
51     module_t *module;
52 };
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57
58 #define GAIN_VALUE_TEXT N_( "Gain multiplier" )
59 #define GAIN_VALUE_LONGTEXT N_( "Increase or decrease the gain (default 1.0)" )
60
61 vlc_module_begin()
62     set_shortname( N_("Gain") )
63     set_description( N_("Gain control filter") )
64     set_category( CAT_AUDIO )
65     set_subcategory( SUBCAT_AUDIO_AFILTER )
66
67     add_float( "gain-value", 1.0, GAIN_VALUE_TEXT,
68                GAIN_VALUE_LONGTEXT, false )
69
70     set_capability( "audio filter", 0 )
71     set_callbacks( Open, Close )
72 vlc_module_end()
73
74
75 /*****************************************************************************
76  * Open: initialize filter
77  *****************************************************************************/
78
79 static int Open( vlc_object_t *p_this )
80 {
81     filter_t *p_filter = (filter_t *)p_this;
82     filter_sys_t *p_sys = vlc_object_create( p_this, sizeof( *p_sys ) );
83     if( unlikely( p_sys == NULL ) )
84         return VLC_ENOMEM;
85
86     p_filter->p_sys = p_sys;
87     p_sys->volume.format = p_filter->fmt_in.audio.i_format;
88     p_sys->module = module_need( &p_sys->volume, "audio volume", NULL, false );
89     if( p_sys->module == NULL )
90     {
91         msg_Warn( p_filter, "unsupported format" );
92         vlc_object_release( &p_sys->volume );
93         return VLC_EGENERIC;
94     }
95
96     p_sys->f_gain = var_InheritFloat( p_filter->p_parent, "gain-value" );
97     msg_Dbg( p_filter, "gain multiplier sets to %.2fx", p_sys->f_gain );
98
99     p_filter->fmt_out.audio = p_filter->fmt_in.audio;
100     p_filter->pf_audio_filter = Process;
101     return VLC_SUCCESS;
102 }
103
104
105 /*****************************************************************************
106  * Process: process samples buffer
107  *****************************************************************************/
108
109 static block_t *Process( filter_t *p_filter, block_t *p_block )
110 {
111     filter_sys_t *p_sys = p_filter->p_sys;
112
113     p_sys->volume.amplify( &p_sys->volume, p_block, p_sys->f_gain );
114     return p_block;
115 }
116
117
118 /*****************************************************************************
119  * Close: close filter
120  *****************************************************************************/
121
122 static void Close( vlc_object_t *p_this )
123 {
124     filter_t *p_filter = (filter_t*)p_this;
125     filter_sys_t *p_sys = p_filter->p_sys;
126
127     module_unneed( &p_sys->volume, p_sys->module );
128     vlc_object_release( &p_sys->volume );
129 }