]> git.sesse.net Git - mlt/blob - src/modules/core/filter_mono.c
A little debugging.
[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-2012 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 #include <framework/mlt_log.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, void **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
38         // Get the producer's audio
39         mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
40
41         if ( channels_out == -1 )
42                 channels_out = *channels;
43         size = mlt_audio_format_size( *format, *samples, channels_out );
44
45         switch ( *format )
46         {
47                 case mlt_audio_u8:
48                 {
49                         uint8_t *new_buffer = mlt_pool_alloc( size );
50                         for ( i = 0; i < *samples; i++ )
51                         {
52                                 uint8_t mixdown = 0;
53                                 for ( j = 0; j < *channels; j++ )
54                                         mixdown += ((uint8_t*) *buffer)[ ( i * *channels ) + j ] / *channels;
55                                 for ( j = 0; j < channels_out; j++ )
56                                         new_buffer[ ( i * channels_out ) + j ] = mixdown;
57                         }
58                         *buffer = new_buffer;
59                         break;
60                 }
61                 case mlt_audio_s16:
62                 {
63                         int16_t *new_buffer = mlt_pool_alloc( size );
64                         for ( i = 0; i < *samples; i++ )
65                         {
66                                 int16_t mixdown = 0;
67                                 for ( j = 0; j < *channels; j++ )
68                                         mixdown += ((int16_t*) *buffer)[ ( i * *channels ) + j ] / *channels;
69                                 for ( j = 0; j < channels_out; j++ )
70                                         new_buffer[ ( i * channels_out ) + j ] = mixdown;
71                         }
72                         *buffer = new_buffer;
73                         break;
74                 }
75                 case mlt_audio_s32le:
76                 {
77                         int32_t *new_buffer = mlt_pool_alloc( size );
78                         for ( i = 0; i < *samples; i++ )
79                         {
80                                 int32_t mixdown = 0;
81                                 for ( j = 0; j < *channels; j++ )
82                                         mixdown += ((int32_t*) *buffer)[ ( i * *channels ) + j ] / *channels;
83                                 for ( j = 0; j < channels_out; j++ )
84                                         new_buffer[ ( i * channels_out ) + j ] = mixdown;
85                         }
86                         *buffer = new_buffer;
87                         break;
88                 }
89                 case mlt_audio_f32le:
90                 {
91                         float *new_buffer = mlt_pool_alloc( size );
92                         for ( i = 0; i < *samples; i++ )
93                         {
94                                 float mixdown = 0;
95                                 for ( j = 0; j < *channels; j++ )
96                                         mixdown += ((float*) *buffer)[ ( i * *channels ) + j ] / *channels;
97                                 for ( j = 0; j < channels_out; j++ )
98                                         new_buffer[ ( i * channels_out ) + j ] = mixdown;
99                         }
100                         *buffer = new_buffer;
101                         break;
102                 }
103                 case mlt_audio_s32:
104                 {
105                         int32_t *new_buffer = mlt_pool_alloc( size );
106                         for ( i = 0; i < *samples; i++ )
107                         {
108                                 int32_t mixdown = 0;
109                                 for ( j = 0; j < *channels; j++ )
110                                         mixdown += ((int32_t*) *buffer)[ ( j * *channels ) + i ] / *channels;
111                                 for ( j = 0; j < channels_out; j++ )
112                                         new_buffer[ ( j * *samples ) + i ] = mixdown;
113                         }
114                         *buffer = new_buffer;
115                         break;
116                 }
117                 case mlt_audio_float:
118                 {
119                         float *new_buffer = mlt_pool_alloc( size );
120                         for ( i = 0; i < *samples; i++ )
121                         {
122                                 float mixdown = 0;
123                                 for ( j = 0; j < *channels; j++ )
124                                         mixdown += ((float*) *buffer)[ ( j * *channels ) + i ] / *channels;
125                                 for ( j = 0; j < channels_out; j++ )
126                                         new_buffer[ ( j * *samples ) + i ] = mixdown;
127                         }
128                         *buffer = new_buffer;
129                         break;
130                 }
131                 default:
132                         mlt_log_error( NULL, "[filter mono] Invalid audio format\n" );
133                         break;
134         }
135         if ( size > *samples * channels_out )
136         {
137                 mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
138                 *channels = channels_out;
139         }
140
141         return 0;
142 }
143
144 /** Filter processing.
145 */
146
147 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
148 {
149         mlt_properties properties = MLT_FILTER_PROPERTIES( this );
150         mlt_properties frame_props = MLT_FRAME_PROPERTIES( frame );
151
152         // Propogate the parameters
153         mlt_properties_set_int( frame_props, "mono.channels", mlt_properties_get_int( properties, "channels" ) );
154
155         // Override the get_audio method
156         mlt_frame_push_audio( frame, filter_get_audio );
157
158         return frame;
159 }
160
161 /** Constructor for the filter.
162 */
163
164 mlt_filter filter_mono_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
165 {
166         mlt_filter this = mlt_filter_new( );
167         if ( this != NULL )
168         {
169                 this->process = filter_process;
170                 if ( arg != NULL )
171                         mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "channels", atoi( arg ) );
172                 else
173                         mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "channels", -1 );
174         }
175         return this;
176 }