Merge ../mlt
[melted] / src / modules / core / filter_mono.c
1 /*
2 * filter_mono.c -- mix all channels to a mono signal across n channels
3 * Copyright (C) 2003-2006 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 /** Get the audio.
28 */
29
30 static int filter_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
31 {
32 // Get the properties of the a frame
33 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
34 int channels_out = mlt_properties_get_int( properties, "mono.channels" );
35 int i, j, size;
36 int16_t *new_buffer;
37
38 // Get the producer's audio
39 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
40
41 size = *samples * channels_out * sizeof( int16_t );
42 new_buffer = mlt_pool_alloc( size );
43 mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
44
45 // Mix
46 for ( i = 0; i < *samples; i++ )
47 {
48 int16_t mixdown = 0;
49 for ( j = 0; j < *channels; j++ )
50 mixdown += (*buffer)[ ( i * *channels ) + j ] / *channels;
51 for ( j = 0; j < channels_out; j++ )
52 new_buffer[ ( i * channels_out ) + j ] = mixdown;
53 }
54
55 // Apply results
56 *buffer = new_buffer;
57 *channels = channels_out;
58
59 return 0;
60 }
61
62 /** Filter processing.
63 */
64
65 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
66 {
67 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
68 mlt_properties frame_props = MLT_FRAME_PROPERTIES( frame );
69
70 // Propogate the parameters
71 mlt_properties_set_int( frame_props, "mono.channels", mlt_properties_get_int( properties, "channels" ) );
72
73 // Override the get_audio method
74 mlt_frame_push_audio( frame, filter_get_audio );
75
76 return frame;
77 }
78
79 /** Constructor for the filter.
80 */
81
82 mlt_filter filter_mono_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
83 {
84 mlt_filter this = mlt_filter_new( );
85 if ( this != NULL )
86 {
87 this->process = filter_process;
88 if ( arg != NULL )
89 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "channels", atoi( arg ) );
90 else
91 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "channels", 2 );
92 }
93 return this;
94 }