]> git.sesse.net Git - mlt/blob - src/modules/core/filter_volume.c
new volume, mix, and resample filters and transitions
[mlt] / src / modules / core / filter_volume.c
1 /*
2  * filter_volume.c -- adjust audio volume
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "filter_volume.h"
22
23 #include <framework/mlt_frame.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 /** Get the audio.
29 */
30
31 static int filter_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
32 {
33         // Get the properties of the a frame
34         mlt_properties properties = mlt_frame_properties( frame );
35         double volume = mlt_properties_get_double( properties, "volume" );
36
37         // Restore the original get_audio
38         frame->get_audio = mlt_properties_get_data( properties, "volume.get_audio", NULL );
39
40         // Get the producer's audio
41         mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
42
43         // Apply the volume
44         int i;
45         for ( i = 0; i < ( *channels * *samples ); i++ )
46                 (*buffer)[i] *= volume;
47         
48         return 0;
49 }
50
51 /** Filter processing.
52 */
53
54 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
55 {
56         mlt_properties properties = mlt_frame_properties( frame );
57
58         // Propogate the level property
59         if ( mlt_properties_get( mlt_filter_properties( this ), "volume" ) != NULL )
60                 mlt_properties_set_double( properties, "volume",
61                         mlt_properties_get_double( mlt_filter_properties( this ), "volume" ) );
62         
63         // Backup the original get_audio (it's still needed)
64         mlt_properties_set_data( properties, "volume.get_audio", frame->get_audio, 0, NULL, NULL );
65
66         // Override the get_audio method
67         frame->get_audio = filter_get_audio;
68
69         return frame;
70 }
71
72 /** Constructor for the filter.
73 */
74
75 mlt_filter filter_volume_init( char *arg )
76 {
77         mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
78         if ( this != NULL && mlt_filter_init( this, NULL ) == 0 )
79         {
80                 this->process = filter_process;
81                 if ( arg != NULL )
82                         mlt_properties_set_double( mlt_filter_properties( this ), "volume", atof( arg ) );
83         }
84         return this;
85 }
86