2 * filter_jackrack.c -- filter audio through Jack and/or LADSPA plugins
3 * Copyright (C) 2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
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.
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.
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.
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
26 #include <sys/types.h>
29 #include <jack/jack.h>
30 #include <jack/ringbuffer.h>
34 #include "jack_rack.h"
36 #define BUFFER_LEN 204800 * 3
38 static void initialise_jack_ports( mlt_properties properties
)
41 char mlt_name
[20], rack_name
[30];
42 jack_port_t
**port
= NULL
;
43 jack_client_t
*jack_client
= mlt_properties_get_data( properties
, "jack_client", NULL
);
44 jack_nframes_t jack_buffer_size
= jack_get_buffer_size( jack_client
);
46 // Propogate these for the Jack processing callback
47 int channels
= mlt_properties_get_int( properties
, "channels" );
50 if ( mlt_properties_get( properties
, "src" ) )
52 snprintf( rack_name
, sizeof( rack_name
), "jackrack%d", getpid() );
53 jack_rack_t
*jackrack
= jack_rack_new( rack_name
, mlt_properties_get_int( properties
, "channels" ) );
54 jack_rack_open_file( jackrack
, mlt_properties_get( properties
, "src" ) );
56 mlt_properties_set_data( properties
, "jackrack", jackrack
, 0, NULL
, NULL
);
57 mlt_properties_set( properties
, "_rack_client_name", rack_name
);
60 // Allocate buffers and ports
61 jack_ringbuffer_t
**output_buffers
= mlt_pool_alloc( sizeof( jack_ringbuffer_t
*) * channels
);
62 jack_ringbuffer_t
**input_buffers
= mlt_pool_alloc( sizeof( jack_ringbuffer_t
*) * channels
);
63 jack_port_t
**jack_output_ports
= mlt_pool_alloc( sizeof(jack_port_t
*) * channels
);
64 jack_port_t
**jack_input_ports
= mlt_pool_alloc( sizeof(jack_port_t
*) * channels
);
65 float **jack_output_buffers
= mlt_pool_alloc( sizeof(float *) * jack_buffer_size
);
66 float **jack_input_buffers
= mlt_pool_alloc( sizeof(float *) * jack_buffer_size
);
68 // Set properties - released inside filter_close
69 mlt_properties_set_data( properties
, "output_buffers", output_buffers
, sizeof( jack_ringbuffer_t
*) * channels
, NULL
, NULL
);
70 mlt_properties_set_data( properties
, "input_buffers", input_buffers
, sizeof( jack_ringbuffer_t
*) * channels
, NULL
, NULL
);
71 mlt_properties_set_data( properties
, "jack_output_ports", jack_output_ports
, sizeof( jack_port_t
*) * channels
, NULL
, NULL
);
72 mlt_properties_set_data( properties
, "jack_input_ports", jack_input_ports
, sizeof( jack_port_t
*) * channels
, NULL
, NULL
);
73 mlt_properties_set_data( properties
, "jack_output_buffers", jack_output_buffers
, sizeof( float *) * channels
, NULL
, NULL
);
74 mlt_properties_set_data( properties
, "jack_input_buffers", jack_input_buffers
, sizeof( float *) * channels
, NULL
, NULL
);
76 // Start Jack processing - required before registering ports
77 jack_activate( jack_client
);
79 // Register Jack ports
80 for ( i
= 0; i
< channels
; i
++ )
84 output_buffers
[i
] = jack_ringbuffer_create( BUFFER_LEN
* sizeof(float) );
85 input_buffers
[i
] = jack_ringbuffer_create( BUFFER_LEN
* sizeof(float) );
86 snprintf( mlt_name
, sizeof( mlt_name
), "obuf%d", i
);
87 mlt_properties_set_data( properties
, mlt_name
, output_buffers
[i
], BUFFER_LEN
* sizeof(float), NULL
, NULL
);
88 snprintf( mlt_name
, sizeof( mlt_name
), "ibuf%d", i
);
89 mlt_properties_set_data( properties
, mlt_name
, input_buffers
[i
], BUFFER_LEN
* sizeof(float), NULL
, NULL
);
91 for ( in
= 0; in
< 2; in
++ )
93 snprintf( mlt_name
, sizeof( mlt_name
), "%s_%d", in ?
"in" : "out", i
+ 1);
94 port
= ( in ?
&jack_input_ports
[i
] : &jack_output_ports
[i
] );
96 *port
= jack_port_register( jack_client
, mlt_name
, JACK_DEFAULT_AUDIO_TYPE
,
97 ( in ? JackPortIsInput
: JackPortIsOutput
) | JackPortIsTerminal
, 0 );
101 // Establish connections
102 for ( i
= 0; i
< channels
; i
++ )
105 for ( in
= 0; in
< 2; in
++ )
107 port
= ( in ?
&jack_input_ports
[i
] : &jack_output_ports
[i
] );
108 snprintf( mlt_name
, sizeof( mlt_name
), "%s", jack_port_name( *port
) );
110 snprintf( rack_name
, sizeof( rack_name
), "%s_%d", in ?
"in" : "out", i
+ 1 );
111 if ( mlt_properties_get( properties
, "_rack_client_name" ) )
112 snprintf( rack_name
, sizeof( rack_name
), "%s:%s_%d", mlt_properties_get( properties
, "_rack_client_name" ), in ?
"out" : "in", i
+ 1);
113 else if ( mlt_properties_get( properties
, rack_name
) )
114 snprintf( rack_name
, sizeof( rack_name
), "%s", mlt_properties_get( properties
, rack_name
) );
116 snprintf( rack_name
, sizeof( rack_name
), "%s:%s_%d", mlt_properties_get( properties
, "_client_name" ), in ?
"out" : "in", i
+ 1);
120 fprintf( stderr
, "jack connect %s to %s\n", rack_name
, mlt_name
);
121 jack_connect( jack_client
, rack_name
, mlt_name
);
125 fprintf( stderr
, "jack connect %s to %s\n", mlt_name
, rack_name
);
126 jack_connect( jack_client
, mlt_name
, rack_name
);
132 static int jack_process (jack_nframes_t frames
, void * data
)
134 mlt_filter filter
= (mlt_filter
) data
;
135 mlt_properties properties
= MLT_FILTER_PROPERTIES( filter
);
136 int channels
= mlt_properties_get_int( properties
, "channels" );
137 int frame_size
= mlt_properties_get_int( properties
, "_samples" ) * sizeof(float);
138 int sync
= mlt_properties_get_int( properties
, "_sync" );
141 static int total_size
= 0;
143 jack_ringbuffer_t
**output_buffers
= mlt_properties_get_data( properties
, "output_buffers", NULL
);
144 if ( output_buffers
== NULL
)
146 jack_ringbuffer_t
**input_buffers
= mlt_properties_get_data( properties
, "input_buffers", NULL
);
147 jack_port_t
**jack_output_ports
= mlt_properties_get_data( properties
, "jack_output_ports", NULL
);
148 jack_port_t
**jack_input_ports
= mlt_properties_get_data( properties
, "jack_input_ports", NULL
);
149 float **jack_output_buffers
= mlt_properties_get_data( properties
, "jack_output_buffers", NULL
);
150 float **jack_input_buffers
= mlt_properties_get_data( properties
, "jack_input_buffers", NULL
);
151 pthread_mutex_t
*output_lock
= mlt_properties_get_data( properties
, "output_lock", NULL
);
152 pthread_cond_t
*output_ready
= mlt_properties_get_data( properties
, "output_ready", NULL
);
154 for ( i
= 0; i
< channels
; i
++ )
156 size_t jack_size
= ( frames
* sizeof(float) );
159 // Send audio through out port
160 jack_output_buffers
[i
] = jack_port_get_buffer( jack_output_ports
[i
], frames
);
161 if ( ! jack_output_buffers
[i
] )
163 fprintf( stderr
, "%s: no jack buffer for output port %d\n", __FUNCTION__
, i
);
167 ring_size
= jack_ringbuffer_read_space( output_buffers
[i
] );
168 jack_ringbuffer_read( output_buffers
[i
], ( char * )jack_output_buffers
[i
], ring_size
< jack_size ? ring_size
: jack_size
);
170 // Return audio through in port
171 jack_input_buffers
[i
] = jack_port_get_buffer( jack_input_ports
[i
], frames
);
172 if ( ! jack_input_buffers
[i
] )
174 fprintf( stderr
, "%s: no jack buffer for input port %d\n", __FUNCTION__
, i
);
179 // Do not start returning audio until we have sent first mlt frame
180 if ( sync
&& i
== 0 && frame_size
> 0 )
181 total_size
+= ring_size
;
182 //fprintf(stderr, "sync %d frame_size %d ring_size %d jack_size %d\n", sync, frame_size, ring_size, jack_size );
184 if ( ! sync
|| ( frame_size
> 0 && total_size
>= frame_size
) )
186 ring_size
= jack_ringbuffer_write_space( input_buffers
[i
] );
187 jack_ringbuffer_write( input_buffers
[i
], ( char * )jack_input_buffers
[i
], ring_size
< jack_size ? ring_size
: jack_size
);
191 // Tell mlt that audio is available
192 pthread_mutex_lock( output_lock
);
193 pthread_cond_signal( output_ready
);
194 pthread_mutex_unlock( output_lock
);
197 mlt_properties_set_int( properties
, "_sync", 0 );
209 static int jackrack_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
211 // Get the filter service
212 mlt_filter filter
= mlt_frame_pop_audio( frame
);
214 // Get the filter properties
215 mlt_properties filter_properties
= MLT_FILTER_PROPERTIES( filter
);
217 int jack_frequency
= mlt_properties_get_int( filter_properties
, "_sample_rate" );
219 // Get the producer's audio
220 mlt_frame_get_audio( frame
, buffer
, format
, &jack_frequency
, channels
, samples
);
222 // TODO: Deal with sample rate differences
223 if ( *frequency
!= jack_frequency
)
224 fprintf( stderr
, "mismatching frequencies in filter jackrack\n" );
225 *frequency
= jack_frequency
;
227 // Initialise Jack ports and connections if needed
228 if ( mlt_properties_get_int( filter_properties
, "_samples" ) == 0 )
229 mlt_properties_set_int( filter_properties
, "_samples", *samples
);
231 // Get the filter-specific properties
232 jack_ringbuffer_t
**output_buffers
= mlt_properties_get_data( filter_properties
, "output_buffers", NULL
);
233 jack_ringbuffer_t
**input_buffers
= mlt_properties_get_data( filter_properties
, "input_buffers", NULL
);
234 // pthread_mutex_t *output_lock = mlt_properties_get_data( filter_properties, "output_lock", NULL );
235 // pthread_cond_t *output_ready = mlt_properties_get_data( filter_properties, "output_ready", NULL );
238 int16_t *q
= *buffer
;
239 float sample
[ 2 ][ 10000 ];
241 // struct timespec tm = { 0, 0 };
243 // Convert to floats and write into output ringbuffer
244 if ( jack_ringbuffer_write_space( output_buffers
[0] ) >= ( *samples
* sizeof(float) ) )
246 for ( i
= 0; i
< *samples
; i
++ )
247 for ( j
= 0; j
< *channels
; j
++ )
248 sample
[ j
][ i
] = ( float )( *q
++ ) / 32768.0;
250 for ( j
= 0; j
< *channels
; j
++ )
251 jack_ringbuffer_write( output_buffers
[j
], ( char * )sample
[ j
], *samples
* sizeof(float) );
254 // Synchronization phase - wait for signal from Jack process
255 while ( jack_ringbuffer_read_space( input_buffers
[ *channels
- 1 ] ) < ( *samples
* sizeof(float) ) ) ;
256 //pthread_cond_wait( output_ready, output_lock );
258 // Read from input ringbuffer and convert from floats
259 if ( jack_ringbuffer_read_space( input_buffers
[0] ) >= ( *samples
* sizeof(float) ) )
261 // Initialise to silence, but repeat last frame if available in case of
263 for ( j
= 0; j
< *channels
; j
++ )
264 jack_ringbuffer_read( input_buffers
[j
], ( char * )sample
[ j
], *samples
* sizeof(float) );
267 for ( i
= 0; i
< *samples
; i
++ )
268 for ( j
= 0; j
< *channels
; j
++ )
270 if ( sample
[ j
][ i
] > 1.0 )
271 sample
[ j
][ i
] = 1.0;
272 else if ( sample
[ j
][ i
] < -1.0 )
273 sample
[ j
][ i
] = -1.0;
275 if ( sample
[ j
][ i
] > 0 )
276 *q
++ = 32767 * sample
[ j
][ i
];
278 *q
++ = 32768 * sample
[ j
][ i
];
286 /** Filter processing.
289 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
292 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
293 mlt_frame_push_audio( frame
, this );
294 mlt_frame_push_audio( frame
, jackrack_get_audio
);
296 if ( mlt_properties_get_int( properties
, "_sync" ) )
297 initialise_jack_ports( properties
);
304 static void filter_close( mlt_filter
this )
308 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
309 jack_client_t
*jack_client
= mlt_properties_get_data( properties
, "jack_client", NULL
);
311 jack_deactivate( jack_client
);
312 jack_client_close( jack_client
);
313 for ( i
= 0; i
< mlt_properties_get_int( properties
, "channels" ); i
++ )
315 snprintf( mlt_name
, sizeof( mlt_name
), "obuf%d", i
);
316 jack_ringbuffer_free( mlt_properties_get_data( properties
, mlt_name
, NULL
) );
317 snprintf( mlt_name
, sizeof( mlt_name
), "ibuf%d", i
);
318 jack_ringbuffer_free( mlt_properties_get_data( properties
, mlt_name
, NULL
) );
320 mlt_pool_release( mlt_properties_get_data( properties
, "output_buffers", NULL
) );
321 mlt_pool_release( mlt_properties_get_data( properties
, "input_buffers", NULL
) );
322 mlt_pool_release( mlt_properties_get_data( properties
, "jack_output_ports", NULL
) );
323 mlt_pool_release( mlt_properties_get_data( properties
, "jack_input_ports", NULL
) );
324 mlt_pool_release( mlt_properties_get_data( properties
, "jack_output_buffers", NULL
) );
325 mlt_pool_release( mlt_properties_get_data( properties
, "jack_input_buffers", NULL
) );
326 mlt_pool_release( mlt_properties_get_data( properties
, "output_lock", NULL
) );
327 mlt_pool_release( mlt_properties_get_data( properties
, "output_ready", NULL
) );
329 jack_rack_t
*jackrack
= mlt_properties_get_data( properties
, "jackrack", NULL
);
330 jack_rack_destroy( jackrack
);
332 this->parent
.close
= NULL
;
333 mlt_service_close( &this->parent
);
336 /** Constructor for the filter.
339 mlt_filter
filter_jackrack_init( mlt_profile profile
, mlt_service_type type
, const char *id
, char *arg
)
341 mlt_filter
this = mlt_filter_new( );
346 snprintf( name
, sizeof( name
), "mlt%d", getpid() );
347 jack_client_t
*jack_client
= jack_client_new( name
);
350 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
351 pthread_mutex_t
*output_lock
= mlt_pool_alloc( sizeof( pthread_mutex_t
) );
352 pthread_cond_t
*output_ready
= mlt_pool_alloc( sizeof( pthread_cond_t
) );
354 jack_set_process_callback( jack_client
, jack_process
, this );
355 //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
356 this->process
= filter_process
;
357 this->close
= filter_close
;
358 pthread_mutex_init( output_lock
, NULL
);
359 pthread_cond_init( output_ready
, NULL
);
361 mlt_properties_set( properties
, "src", arg
);
362 mlt_properties_set( properties
, "_client_name", name
);
363 mlt_properties_set_data( properties
, "jack_client", jack_client
, 0, NULL
, NULL
);
364 mlt_properties_set_int( properties
, "_sample_rate", jack_get_sample_rate( jack_client
) );
365 mlt_properties_set_data( properties
, "output_lock", output_lock
, 0, NULL
, NULL
);
366 mlt_properties_set_data( properties
, "output_ready", output_ready
, 0, NULL
, NULL
);
367 mlt_properties_set_int( properties
, "_sync", 1 );
368 mlt_properties_set_int( properties
, "channels", 2 );