]> git.sesse.net Git - mlt/blob - src/modules/core/transition_mix.c
Big modification - switch to macros for parent class access
[mlt] / 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_audio( 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_start = 0.5, mix_end = 0.5;
46         if ( mlt_properties_get( b_props, "audio.previous_mix" ) != NULL )
47                 mix_start = mlt_properties_get_double( b_props, "audio.previous_mix" );
48         if ( mlt_properties_get( b_props, "audio.mix" ) != NULL )
49                 mix_end = mlt_properties_get_double( b_props, "audio.mix" );
50         if ( mlt_properties_get_int( b_props, "audio.reverse" ) )
51         {
52                 mix_start = 1 - mix_start;
53                 mix_end = 1 - mix_end;
54         }
55
56         mlt_frame_mix_audio( frame, b_frame, mix_start, mix_end, buffer, format, frequency, channels, samples );
57
58         return 0;
59 }
60
61
62 /** Mix transition processing.
63 */
64
65 static mlt_frame transition_process( mlt_transition this, mlt_frame a_frame, mlt_frame b_frame )
66 {
67         mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
68         mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
69
70         // Only if mix is specified, otherwise a producer may set the mix
71         if ( mlt_properties_get( properties, "start" ) != NULL )
72         {
73                 // Determine the time position of this frame in the transition duration
74                 mlt_position in = mlt_transition_get_in( this );
75                 mlt_position out = mlt_transition_get_out( this );
76                 mlt_position time = mlt_frame_get_position( b_frame );
77                 double mix = ( double )( time - in ) / ( double )( out - in + 1 );
78                 
79                 // If there is an end mix level adjust mix to the range
80                 if ( mlt_properties_get( properties, "end" ) != NULL )
81                 {
82                         double start = mlt_properties_get_double( properties, "start" );
83                         double end = mlt_properties_get_double( properties, "end" );
84                         mix = start + ( end - start ) * mix;
85                 }
86                 // A negative means total crossfade (uses position)
87                 else if ( mlt_properties_get_double( properties, "start" ) >= 0 )
88                 {
89                         // Otherwise, start/constructor is a constant mix level
90                     mix = mlt_properties_get_double( properties, "start" );
91                 }
92         
93                 // Finally, set the mix property on the frame
94                 mlt_properties_set_double( b_props, "audio.mix", mix );
95
96                 // Initialise transition previous mix value to prevent an inadvertant jump from 0
97                 if ( mlt_properties_get( properties, "previous_mix" ) == NULL )
98                         mlt_properties_set_double( properties, "previous_mix", mlt_properties_get_double( b_props, "audio.mix" ) );
99                         
100                 // Tell b frame what the previous mix level was
101                 mlt_properties_set_double( b_props, "audio.previous_mix", mlt_properties_get_double( properties, "previous_mix" ) );
102
103                 // Save the current mix level for the next iteration
104                 mlt_properties_set_double( properties, "previous_mix", mlt_properties_get_double( b_props, "audio.mix" ) );
105                 
106                 mlt_properties_set_double( b_props, "audio.reverse", mlt_properties_get_double( properties, "reverse" ) );
107         }
108
109         // Ensure that the tractor knows this isn't test audio...
110         if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( a_frame ), "test_audio" ) )
111         {
112                 mlt_properties_set_int( MLT_FRAME_PROPERTIES( a_frame ), "test_audio", 0 );
113                 mlt_properties_set_int( MLT_FRAME_PROPERTIES( a_frame ), "silent_audio", 1 );
114         }
115
116         // Backup the original get_audio (it's still needed)
117         mlt_properties_set_data( MLT_FRAME_PROPERTIES( a_frame ), "mix.get_audio", a_frame->get_audio, 0, NULL, NULL );
118
119         // Override the get_audio method
120         a_frame->get_audio = transition_get_audio;
121         
122         mlt_frame_push_audio( a_frame, b_frame );
123         
124         return a_frame;
125 }
126
127 /** Constructor for the transition.
128 */
129
130 mlt_transition transition_mix_init( char *arg )
131 {
132         mlt_transition this = calloc( sizeof( struct mlt_transition_s ), 1 );
133         if ( this != NULL && mlt_transition_init( this, NULL ) == 0 )
134         {
135                 this->process = transition_process;
136                 if ( arg != NULL )
137                         mlt_properties_set_double( MLT_TRANSITION_PROPERTIES( this ), "start", atof( arg ) );
138                 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( this ), "_accepts_blanks", 1 );
139         }
140         return this;
141 }
142