default mix to 0.5
[melted] / src / modules / core / transition_mix.c
1 /*
2 * transition_mix.c -- mix two audio streams
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 "transition_mix.h"
22 #include <framework/mlt_frame.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26
27
28 /** Get the audio.
29 */
30
31 static int transition_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 a_props = mlt_frame_properties( frame );
35
36 // Get the b frame from the stack
37 mlt_frame b_frame = mlt_frame_pop_frame( frame );
38
39 // Get the properties of the b frame
40 mlt_properties b_props = mlt_frame_properties( b_frame );
41
42 // Restore the original get_audio
43 frame->get_audio = mlt_properties_get_data( a_props, "mix.get_audio", NULL );
44
45 double mix = 0.5;
46 if ( mlt_properties_get( b_props, "audio.mix" ) != NULL )
47 mix = mlt_properties_get_double( b_props, "audio.mix" );
48 mlt_frame_mix_audio( frame, b_frame, mix, buffer, format, frequency, channels, samples );
49
50 // Push the b_frame back on for get_image
51 mlt_frame_push_frame( frame, b_frame );
52
53 return 0;
54 }
55
56
57 /** Mix transition processing.
58 */
59
60 static mlt_frame transition_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
61 {
62 mlt_properties properties = mlt_transition_properties( this );
63 mlt_properties b_props = mlt_frame_properties( b_frame );
64
65 // Only if mix is specified, otherwise a producer may set the mix
66 if ( mlt_properties_get( properties, "mix" ) != NULL )
67 {
68 // A negative means crossfade
69 if ( mlt_properties_get_double( properties, "mix" ) < 0 )
70 {
71 // Determine the time position of this frame in the transition duration
72 mlt_position in = mlt_transition_get_in( this );
73 mlt_position out = mlt_transition_get_out( this );
74 mlt_position time = mlt_frame_get_position( b_frame );
75 double mix = ( double )( time - in ) / ( double )( out - in + 1 );
76 mlt_properties_set_double( b_props, "audio.mix", mix );
77 }
78 else
79 mlt_properties_set_double( b_props, "audio.mix", mlt_properties_get_double( properties, "mix" ) );
80 }
81
82 // Backup the original get_audio (it's still needed)
83 mlt_properties_set_data( mlt_frame_properties( a_frame ), "mix.get_audio", a_frame->get_audio, 0, NULL, NULL );
84
85 // Override the get_audio method
86 a_frame->get_audio = transition_get_audio;
87
88 mlt_frame_push_frame( a_frame, b_frame );
89
90 return a_frame;
91 }
92
93 /** Constructor for the transition.
94 */
95
96 mlt_transition transition_mix_init( char *arg )
97 {
98 mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
99 if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
100 {
101 this->process = transition_process;
102 if ( arg != NULL )
103 mlt_properties_set_double( mlt_transition_properties( this ), "mix", atof( arg ) );
104 }
105 return this;
106 }
107