Jack2 1.9.6

JackWeakAPI.cpp

00001 //=============================================================================
00002 //  MuseScore
00003 //  Linux Music Score Editor
00004 //  $Id:
00005 //
00006 //  jackWeakAPI based on code from Stéphane Letz (Grame)
00007 //  partly based on Julien Pommier (PianoTeq : http://www.pianoteq.com/) code.
00008 //
00009 //  Copyright (C) 2002-2007 Werner Schweer and others
00010 //  Copyright (C) 2009 Grame
00011 
00012 //  This program is free software; you can redistribute it and/or modify
00013 //  it under the terms of the GNU Lesser General Public License as published by
00014 //  the Free Software Foundation; either version 2.1 of the License, or
00015 //  (at your option) any later version.
00016 
00017 //  This program is distributed in the hope that it will be useful,
00018 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //  GNU Lesser General Public License for more details.
00021 
00022 //  You should have received a copy of the GNU Lesser General Public License
00023 //  along with this program; if not, write to the Free Software
00024 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00025 
00026 #include <jack/jack.h>
00027 #include <jack/thread.h>
00028 #include <jack/midiport.h>
00029 #include <math.h>
00030 #ifndef WIN32
00031 #include <dlfcn.h>
00032 #endif
00033 #include <stdlib.h>
00034 #include <iostream>
00035 
00036 /* dynamically load libjack and forward all registered calls to libjack 
00037    (similar to what relaytool is trying to do, but more portably..)
00038 */
00039 
00040 typedef void (*print_function)(const char *);
00041 typedef void *(*thread_routine)(void*);
00042 
00043 using std::cerr;
00044 
00045 int libjack_is_present = 0;     // public symbol, similar to what relaytool does.
00046 
00047 #ifdef WIN32
00048 HMODULE libjack_handle = 0;
00049 #else
00050 static void *libjack_handle = 0;
00051 #endif
00052 
00053 
00054 static void __attribute__((constructor)) tryload_libjack()
00055 {
00056     if (getenv("SKIP_LIBJACK") == 0) { // just in case libjack is causing troubles..
00057     #ifdef __APPLE__
00058         libjack_handle = dlopen("libjack.0.dylib", RTLD_LAZY);
00059     #elif defined(WIN32)
00060         libjack_handle = LoadLibrary("libjack.dll");
00061     #else
00062         libjack_handle = dlopen("libjack.so.0", RTLD_LAZY);
00063     #endif
00064         
00065     }
00066     libjack_is_present = (libjack_handle != 0);
00067 }
00068 
00069 void *load_jack_function(const char *fn_name) 
00070 {
00071     void *fn = 0;
00072     if (!libjack_handle) { 
00073         fprintf (stderr, "libjack not found, so do not try to load  %s ffs  !\n", fn_name);
00074         return 0;
00075     }
00076 #ifdef WIN32
00077     fn = (void*)GetProcAddress(libjack_handle, fn_name);
00078 #else
00079     fn = dlsym(libjack_handle, fn_name);
00080 #endif
00081     if (!fn) { 
00082 #ifdef WIN32
00083         char* lpMsgBuf;
00084         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,0,NULL );
00085         fprintf (stderr, "could not GetProcAddress( %s ), %s \n", fn_name, lpMsgBuf) ;
00086 #else        
00087         fprintf (stderr, "could not dlsym( %s ), %s \n", fn_name, dlerror()) ; 
00088 #endif
00089     }
00090     return fn;
00091 }
00092 
00093 #define DECL_FUNCTION(return_type, fn_name, arguments_types, arguments) \
00094   typedef return_type (*fn_name##_ptr_t)arguments_types;                \
00095   return_type fn_name arguments_types {                                 \
00096     static fn_name##_ptr_t fn = 0;                                      \
00097     if (fn == 0) { fn = (fn_name##_ptr_t)load_jack_function(#fn_name); } \
00098     if (fn) return (*fn)arguments;                                      \
00099     else return (return_type)-1;                                                      \
00100   }
00101 
00102 #define DECL_FUNCTION_NULL(return_type, fn_name, arguments_types, arguments) \
00103   typedef return_type (*fn_name##_ptr_t)arguments_types;                \
00104   return_type fn_name arguments_types {                                 \
00105     static fn_name##_ptr_t fn = 0;                                      \
00106     if (fn == 0) { fn = (fn_name##_ptr_t)load_jack_function(#fn_name); } \
00107     if (fn) return (*fn)arguments;                                      \
00108     else return (return_type)0;                                                      \
00109   }
00110   
00111 #define DECL_VOID_FUNCTION(fn_name, arguments_types, arguments)         \
00112   typedef void (*fn_name##_ptr_t)arguments_types;                       \
00113   void fn_name arguments_types {                                        \
00114     static fn_name##_ptr_t fn = 0;                                      \
00115     if (fn == 0) { fn = (fn_name##_ptr_t)load_jack_function(#fn_name); } \
00116     if (fn) (*fn)arguments;                                             \
00117   }
00118 
00119 
00120 DECL_VOID_FUNCTION(jack_get_version, (int *major_ptr, int *minor_ptr, int *micro_ptr, int *proto_ptr), (major_ptr, minor_ptr, micro_ptr, proto_ptr));
00121 DECL_FUNCTION_NULL(const char *, jack_get_version_string, (), ());      
00122 DECL_FUNCTION_NULL(jack_client_t *, jack_client_open, (const char *client_name, jack_options_t options, jack_status_t *status, ...), 
00123               (client_name, options, status));
00124 DECL_FUNCTION(int, jack_client_close, (jack_client_t *client), (client));
00125 DECL_FUNCTION_NULL(jack_client_t *, jack_client_new, (const char *client_name), (client_name));
00126 DECL_FUNCTION(int, jack_client_name_size, (), ());
00127 DECL_FUNCTION_NULL(char*, jack_get_client_name, (jack_client_t *client), (client));
00128 DECL_FUNCTION(int, jack_internal_client_new, (const char *client_name,
00129                                             const char *load_name,
00130                                             const char *load_init), (client_name, load_name, load_init));
00131 DECL_VOID_FUNCTION(jack_internal_client_close, (const char *client_name), (client_name));
00132 DECL_FUNCTION(int, jack_is_realtime, (jack_client_t *client), (client));
00133 DECL_VOID_FUNCTION(jack_on_shutdown, (jack_client_t *client, JackShutdownCallback shutdown_callback, void *arg), (client, shutdown_callback, arg));
00134 DECL_VOID_FUNCTION(jack_on_info_shutdown, (jack_client_t* client, JackInfoShutdownCallback shutdown_callback, void* arg), (client, shutdown_callback, arg));
00135 DECL_FUNCTION(int, jack_set_process_callback, (jack_client_t *client,
00136                                             JackProcessCallback process_callback,
00137                                             void *arg), (client, process_callback, arg));
00138 DECL_FUNCTION(jack_nframes_t, jack_thread_wait, (jack_client_t *client, int status), (client, status));      
00139                                       
00140 //
00141 DECL_FUNCTION(jack_nframes_t, jack_cycle_wait, (jack_client_t *client), (client));   
00142 DECL_VOID_FUNCTION(jack_cycle_signal, (jack_client_t *client, int status), (client, status));                                          
00143 DECL_FUNCTION(int, jack_set_process_thread, (jack_client_t *client,
00144                                             JackThreadCallback fun,
00145                                             void *arg), (client, fun, arg));
00146 DECL_FUNCTION(int, jack_set_thread_init_callback, (jack_client_t *client,
00147                                             JackThreadInitCallback thread_init_callback,
00148                                             void *arg), (client, thread_init_callback, arg));
00149 DECL_FUNCTION(int, jack_set_freewheel_callback, (jack_client_t *client,
00150                                             JackFreewheelCallback freewheel_callback,
00151                                             void *arg), (client, freewheel_callback, arg));
00152 DECL_FUNCTION(int, jack_set_freewheel, (jack_client_t *client, int onoff), (client, onoff));   
00153 DECL_FUNCTION(int, jack_set_buffer_size, (jack_client_t *client, jack_nframes_t nframes), (client, nframes));   
00154 DECL_FUNCTION(int, jack_set_buffer_size_callback, (jack_client_t *client,
00155                                             JackBufferSizeCallback bufsize_callback,
00156                                             void *arg), (client, bufsize_callback, arg));
00157 DECL_FUNCTION(int, jack_set_sample_rate_callback, (jack_client_t *client,
00158                                             JackSampleRateCallback srate_callback,
00159                                             void *arg), (client, srate_callback, arg));
00160 DECL_FUNCTION(int, jack_set_client_registration_callback, (jack_client_t *client,
00161                                             JackClientRegistrationCallback registration_callback,
00162                                             void *arg), (client, registration_callback, arg));
00163 DECL_FUNCTION(int, jack_set_port_registration_callback, (jack_client_t *client,
00164                                             JackPortRegistrationCallback registration_callback,
00165                                             void *arg), (client, registration_callback, arg));
00166 DECL_FUNCTION(int, jack_set_port_connect_callback, (jack_client_t *client,
00167                                             JackPortConnectCallback connect_callback,
00168                                             void *arg), (client, connect_callback, arg));
00169 DECL_FUNCTION(int, jack_set_port_rename_callback, (jack_client_t *client,
00170                                             JackPortRenameCallback rename_callback,
00171                                             void *arg), (client, rename_callback, arg));
00172 DECL_FUNCTION(int, jack_set_graph_order_callback, (jack_client_t *client,
00173                                             JackGraphOrderCallback graph_callback,
00174                                             void *arg), (client, graph_callback, arg));
00175 DECL_FUNCTION(int, jack_set_xrun_callback, (jack_client_t *client,
00176                                             JackXRunCallback xrun_callback,
00177                                             void *arg), (client, xrun_callback, arg));
00178 DECL_FUNCTION(int, jack_activate, (jack_client_t *client), (client));
00179 DECL_FUNCTION(int, jack_deactivate, (jack_client_t *client), (client));
00180 DECL_FUNCTION_NULL(jack_port_t *, jack_port_register, (jack_client_t *client, const char *port_name, const char *port_type,
00181                                                   unsigned long flags, unsigned long buffer_size),
00182               (client, port_name, port_type, flags, buffer_size));
00183 DECL_FUNCTION(int, jack_port_unregister, (jack_client_t *client, jack_port_t* port), (client, port));
00184 DECL_FUNCTION_NULL(void *, jack_port_get_buffer, (jack_port_t *port, jack_nframes_t nframes), (port, nframes));
00185 DECL_FUNCTION_NULL(const char*, jack_port_name, (const jack_port_t *port), (port));
00186 DECL_FUNCTION_NULL(const char*, jack_port_short_name, (const jack_port_t *port), (port));
00187 DECL_FUNCTION(int, jack_port_flags, (const jack_port_t *port), (port));
00188 DECL_FUNCTION_NULL(const char*, jack_port_type, (const jack_port_t *port), (port));
00189 DECL_FUNCTION(jack_port_type_id_t, jack_port_type_id, (const jack_port_t *port), (port));
00190 DECL_FUNCTION(int, jack_port_is_mine, (const jack_client_t *client, const jack_port_t* port), (client, port));
00191 DECL_FUNCTION(int, jack_port_connected, (const jack_port_t *port), (port));
00192 DECL_FUNCTION(int, jack_port_connected_to, (const jack_port_t *port, const char *port_name), (port, port_name));
00193 DECL_FUNCTION_NULL(const char**, jack_port_get_connections, (const jack_port_t *port), (port));
00194 DECL_FUNCTION_NULL(const char**, jack_port_get_all_connections, (const jack_client_t *client,const jack_port_t *port), (client, port));
00195 DECL_FUNCTION(int, jack_port_tie, (jack_port_t *src, jack_port_t *dst), (src, dst));
00196 DECL_FUNCTION(int, jack_port_untie, (jack_port_t *port), (port));
00197 DECL_FUNCTION(jack_nframes_t, jack_port_get_latency, (jack_port_t *port), (port));
00198 DECL_FUNCTION(jack_nframes_t, jack_port_get_total_latency ,(jack_client_t * client, jack_port_t *port), (client, port));
00199 DECL_VOID_FUNCTION(jack_port_set_latency, (jack_port_t * port, jack_nframes_t frames), (port, frames));
00200 DECL_FUNCTION(int, jack_recompute_total_latency, (jack_client_t* client, jack_port_t* port), (client, port));
00201 DECL_FUNCTION(int, jack_recompute_total_latencies, (jack_client_t* client),(client));
00202 
00203 DECL_FUNCTION(int, jack_port_set_name, (jack_port_t *port, const char *port_name), (port, port_name));
00204 DECL_FUNCTION(int, jack_port_set_alias, (jack_port_t *port, const char *alias), (port, alias));
00205 DECL_FUNCTION(int, jack_port_unset_alias, (jack_port_t *port, const char *alias), (port, alias));
00206 DECL_FUNCTION(int, jack_port_get_aliases, (const jack_port_t *port, char* const aliases[2]), (port,aliases));
00207 DECL_FUNCTION(int, jack_port_request_monitor, (jack_port_t *port, int onoff), (port, onoff));
00208 DECL_FUNCTION(int, jack_port_request_monitor_by_name, (jack_client_t *client, const char *port_name, int onoff), (client, port_name, onoff));
00209 DECL_FUNCTION(int, jack_port_ensure_monitor, (jack_port_t *port, int onoff), (port, onoff));
00210 DECL_FUNCTION(int, jack_port_monitoring_input, (jack_port_t *port) ,(port));
00211 DECL_FUNCTION(int, jack_connect, (jack_client_t * client, const char *source_port, const char *destination_port), (client, source_port, destination_port));
00212 DECL_FUNCTION(int, jack_disconnect, (jack_client_t * client, const char *source_port, const char *destination_port), (client, source_port, destination_port));
00213 DECL_FUNCTION(int, jack_port_disconnect, (jack_client_t * client, jack_port_t * port), (client, port));
00214 DECL_FUNCTION(int, jack_port_name_size,(),());
00215 DECL_FUNCTION(int, jack_port_type_size,(),());
00216             
00217 DECL_FUNCTION(jack_nframes_t, jack_get_sample_rate, (jack_client_t *client), (client));
00218 DECL_FUNCTION(jack_nframes_t, jack_get_buffer_size, (jack_client_t *client), (client));
00219 DECL_FUNCTION_NULL(const char**, jack_get_ports, (jack_client_t *client, const char *port_name_pattern, const char *    type_name_pattern,
00220                                              unsigned long flags), (client, port_name_pattern, type_name_pattern, flags));
00221 DECL_FUNCTION_NULL(jack_port_t *, jack_port_by_name, (jack_client_t * client, const char *port_name), (client, port_name));
00222 DECL_FUNCTION_NULL(jack_port_t *, jack_port_by_id, (jack_client_t *client, jack_port_id_t port_id), (client, port_id));
00223 
00224 DECL_FUNCTION(int, jack_engine_takeover_timebase, (jack_client_t * client), (client));
00225 DECL_FUNCTION(jack_nframes_t, jack_frames_since_cycle_start, (const jack_client_t * client), (client));
00226 DECL_FUNCTION(jack_time_t, jack_get_time, (), ());
00227 DECL_FUNCTION(jack_nframes_t, jack_time_to_frames, (const jack_client_t *client, jack_time_t time), (client, time));
00228 DECL_FUNCTION(jack_time_t, jack_frames_to_time, (const jack_client_t *client, jack_nframes_t frames), (client, frames));
00229 DECL_FUNCTION(jack_nframes_t, jack_frame_time, (const jack_client_t *client), (client));
00230 DECL_FUNCTION(jack_nframes_t, jack_last_frame_time, (const jack_client_t *client), (client));
00231 DECL_FUNCTION(float, jack_cpu_load, (jack_client_t *client), (client));
00232 DECL_FUNCTION_NULL(pthread_t, jack_client_thread_id, (jack_client_t *client), (client));
00233 DECL_VOID_FUNCTION(jack_set_error_function, (print_function fun), (fun));
00234 DECL_VOID_FUNCTION(jack_set_info_function, (print_function fun), (fun));
00235 
00236 DECL_FUNCTION(float, jack_get_max_delayed_usecs, (jack_client_t *client), (client));
00237 DECL_FUNCTION(float, jack_get_xrun_delayed_usecs, (jack_client_t *client), (client));
00238 DECL_VOID_FUNCTION(jack_reset_max_delayed_usecs, (jack_client_t *client), (client));
00239 
00240 DECL_FUNCTION(int, jack_release_timebase, (jack_client_t *client), (client));
00241 DECL_FUNCTION(int, jack_set_sync_callback, (jack_client_t *client, JackSyncCallback sync_callback, void *arg), (client, sync_callback, arg));
00242 DECL_FUNCTION(int, jack_set_sync_timeout, (jack_client_t *client, jack_time_t timeout), (client, timeout));
00243 DECL_FUNCTION(int, jack_set_timebase_callback, (jack_client_t *client, 
00244                                                 int conditional,
00245                                                 JackTimebaseCallback timebase_callback,
00246                                                 void *arg), (client, conditional, timebase_callback, arg));
00247 DECL_FUNCTION(int, jack_transport_locate, (jack_client_t *client, jack_nframes_t frame), (client, frame));
00248 DECL_FUNCTION(jack_transport_state_t, jack_transport_query, (const jack_client_t *client, jack_position_t *pos), (client, pos));
00249 DECL_FUNCTION(jack_nframes_t, jack_get_current_transport_frame, (const jack_client_t *client), (client));
00250 DECL_FUNCTION(int, jack_transport_reposition, (jack_client_t *client, jack_position_t *pos), (client, pos));
00251 DECL_VOID_FUNCTION(jack_transport_start, (jack_client_t *client), (client));
00252 DECL_VOID_FUNCTION(jack_transport_stop, (jack_client_t *client), (client));
00253 DECL_VOID_FUNCTION(jack_get_transport_info, (jack_client_t *client, jack_transport_info_t *tinfo), (client,tinfo));
00254 DECL_VOID_FUNCTION(jack_set_transport_info, (jack_client_t *client, jack_transport_info_t *tinfo), (client,tinfo));
00255 
00256 DECL_FUNCTION(int, jack_client_real_time_priority, (jack_client_t* client), (client));
00257 DECL_FUNCTION(int, jack_client_max_real_time_priority, (jack_client_t* client), (client));
00258 DECL_FUNCTION(int, jack_acquire_real_time_scheduling, (pthread_t thread, int priority), (thread, priority));
00259 DECL_FUNCTION(int, jack_client_create_thread, (jack_client_t* client,
00260                                       pthread_t *thread,
00261                                       int priority,
00262                                       int realtime,     // boolean
00263                                       thread_routine routine,
00264                                       void *arg), (client, thread, priority, realtime, routine, arg));
00265 DECL_FUNCTION(int, jack_drop_real_time_scheduling, (pthread_t thread), (thread));
00266 
00267 DECL_FUNCTION(int, jack_client_stop_thread, (jack_client_t* client, pthread_t thread), (client, thread));
00268 DECL_FUNCTION(int, jack_client_kill_thread, (jack_client_t* client, pthread_t thread), (client, thread));
00269 #ifndef WIN32
00270 DECL_VOID_FUNCTION(jack_set_thread_creator, (jack_thread_creator_t jtc), (jtc));
00271 #endif
00272 DECL_FUNCTION(char *, jack_get_internal_client_name, (jack_client_t *client, jack_intclient_t intclient), (client, intclient));
00273 DECL_FUNCTION(jack_intclient_t, jack_internal_client_handle, (jack_client_t *client, const char *client_name, jack_status_t *status), (client, client_name, status));
00274 /*
00275 DECL_FUNCTION(jack_intclient_t, jack_internal_client_load, (jack_client_t *client, 
00276                                                             const char *client_name, 
00277                                                             jack_options_t options, 
00278                                                             jack_status_t *status
00279                                                             , ...), (client, client_name, options, status, ...));
00280 */
00281 DECL_FUNCTION(jack_status_t, jack_internal_client_unload, (jack_client_t *client, jack_intclient_t intclient), (client, intclient));
00282 DECL_VOID_FUNCTION(jack_free, (void* ptr), (ptr));
00283 
00284 // MIDI
00285 
00286 DECL_FUNCTION(jack_nframes_t, jack_midi_get_event_count, (void* port_buffer), (port_buffer));
00287 DECL_FUNCTION(int, jack_midi_event_get, (jack_midi_event_t* event, void* port_buffer, jack_nframes_t event_index), (event, port_buffer, event_index)) ;
00288 DECL_VOID_FUNCTION(jack_midi_clear_buffer, (void* port_buffer), (port_buffer));
00289 DECL_FUNCTION(size_t, jack_midi_max_event_size, (void* port_buffer), (port_buffer));
00290 DECL_FUNCTION_NULL(jack_midi_data_t*, jack_midi_event_reserve, (void* port_buffer, jack_nframes_t time, size_t data_size), (port_buffer, time, data_size));
00291 DECL_FUNCTION(int, jack_midi_event_write, (void* port_buffer, jack_nframes_t time, const jack_midi_data_t* data, size_t data_size), (port_buffer, time, data, data_size));
00292 DECL_FUNCTION(jack_nframes_t, jack_midi_get_lost_event_count, (void* port_buffer), (port_buffer));