]> git.sesse.net Git - mlt/blob - src/modules/core/filter_mono.c
b11fd1f6dc6036705f2d3b888dfd24a7a1da2130
[mlt] / 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 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_mono.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         int channels_out = mlt_properties_get_int( properties, "mono.channels" );
36         int i, j, size;
37         int16_t *new_buffer;
38
39         // Get the producer's audio
40         mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
41
42         size = *samples * channels_out * sizeof( int16_t );
43         new_buffer = mlt_pool_alloc( size );
44         mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
45
46         // Mix
47         for ( i = 0; i < *samples; i++ )
48         {
49                 int16_t mixdown = 0;
50                 for ( j = 0; j < *channels; j++ )
51                         mixdown += (*buffer)[ ( i * *channels ) + j ] / *channels;
52                 for ( j = 0; j < channels_out; j++ )
53                         new_buffer[ ( i * channels_out ) + j ] = mixdown;
54         }
55
56         // Apply results
57         *buffer = new_buffer;
58         *channels = channels_out;
59         
60         return 0;
61 }
62
63 /** Filter processing.
64 */
65
66 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
67 {
68         mlt_properties properties = MLT_FILTER_PROPERTIES( this );
69         mlt_properties frame_props = MLT_FRAME_PROPERTIES( frame );
70
71         // Propogate the parameters
72         mlt_properties_set_int( frame_props, "mono.channels", mlt_properties_get_int( properties, "channels" ) );
73
74         // Override the get_audio method
75         mlt_frame_push_audio( frame, filter_get_audio );
76
77         return frame;
78 }
79
80 /** Constructor for the filter.
81 */
82
83 mlt_filter filter_mono_init( char *arg )
84 {
85         mlt_filter this = mlt_filter_new( );
86         if ( this != NULL )
87         {
88                 this->process = filter_process;
89                 if ( arg != NULL )
90                         mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "channels", atoi( arg ) );
91                 else
92                         mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "channels", 2 );
93         }
94         return this;
95 }