Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
6.9KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #include "Client.H"
  19. #include "Port.H"
  20. #include <algorithm>
  21. namespace JACK
  22. {
  23. // nframes_t Client::_sample_rate = 0;
  24. Client::Client ( )
  25. {
  26. _freewheeling = false;
  27. _zombified = false;
  28. _client = NULL;
  29. _xruns = 0;
  30. }
  31. Client::~Client ( )
  32. {
  33. jack_client_close( _client );
  34. }
  35. /** Tell JACK to stop calling process callback. This MUST be called in
  36. * an inheriting class' destructor */
  37. void
  38. Client::deactivate ( )
  39. {
  40. jack_deactivate( _client );
  41. }
  42. /*******************/
  43. /* Static Wrappers */
  44. /*******************/
  45. int
  46. Client::process ( nframes_t nframes, void *arg )
  47. {
  48. return ((Client*)arg)->process( nframes );
  49. }
  50. int
  51. Client::sync ( jack_transport_state_t state, jack_position_t *pos, void *arg )
  52. {
  53. return ((Client*)arg)->sync( state, pos );
  54. }
  55. int
  56. Client::xrun ( void *arg )
  57. {
  58. ++((Client*)arg)->_xruns;
  59. return ((Client*)arg)->xrun();
  60. }
  61. void
  62. Client::timebase ( jack_transport_state_t state, jack_nframes_t nframes, jack_position_t *pos, int new_pos, void *arg )
  63. {
  64. ((Client*)arg)->timebase( state, nframes, pos, new_pos );
  65. }
  66. void
  67. Client::freewheel ( int starting, void *arg )
  68. {
  69. ((Client*)arg)->_freewheeling = starting;
  70. ((Client*)arg)->freewheel( starting );
  71. }
  72. int
  73. Client::buffer_size ( nframes_t nframes, void *arg )
  74. {
  75. return ((Client*)arg)->buffer_size( nframes );
  76. }
  77. void
  78. Client::thread_init ( void *arg )
  79. {
  80. ((Client*)arg)->thread_init();
  81. }
  82. void
  83. Client::shutdown ( void *arg )
  84. {
  85. ((Client*)arg)->_zombified = true;
  86. ((Client*)arg)->shutdown();
  87. }
  88. void
  89. Client::port_connect ( jack_port_id_t a, jack_port_id_t b, int connect, void *arg )
  90. {
  91. ((Client*)arg)->port_connect( a, b, connect );
  92. }
  93. int
  94. Client::sample_rate_changed ( nframes_t srate, void *arg )
  95. {
  96. // ((Client*)arg)->_sample_rate = srate;
  97. return ((Client*)arg)->sample_rate_changed( srate );
  98. }
  99. /** Connect to JACK using client name /client_name/. Return a static
  100. * pointer to actual name as reported by JACK */
  101. const char *
  102. Client::init ( const char *client_name, unsigned int opts )
  103. {
  104. if (( _client = jack_client_open ( client_name, (jack_options_t)0, NULL )) == 0 )
  105. return NULL;
  106. #define set_callback( name ) jack_set_ ## name ## _callback( _client, &Client:: name , this )
  107. set_callback( thread_init );
  108. set_callback( process );
  109. set_callback( xrun );
  110. set_callback( freewheel );
  111. set_callback( buffer_size );
  112. set_callback( port_connect );
  113. jack_set_sample_rate_callback( _client, &Client::sample_rate_changed, this );
  114. /* FIXME: should we wait to register this until after the project
  115. has been loaded (and we have disk threads running)? */
  116. if ( opts & SLOW_SYNC )
  117. set_callback( sync );
  118. if ( opts & TIMEBASE_MASTER )
  119. jack_set_timebase_callback( _client, 0, &Client::timebase, this );
  120. jack_on_shutdown( _client, &Client::shutdown, this );
  121. jack_activate( _client );
  122. // _sample_rate = frame_rate();
  123. return jack_get_client_name( _client );
  124. }
  125. /* THREAD: RT */
  126. /** enter or leave freehweeling mode */
  127. void
  128. Client::freewheeling ( bool yes )
  129. {
  130. if ( jack_set_freewheel( _client, yes ) )
  131. ;
  132. // WARNING( "Unkown error while setting freewheeling mode" );
  133. }
  134. void
  135. Client::port_added ( Port *p )
  136. {
  137. std::list < JACK::Port * >::iterator i = std::find( _active_ports.begin(), _active_ports.end(), p );
  138. if ( i != _active_ports.end() )
  139. return;
  140. _active_ports.push_back( p );
  141. }
  142. void
  143. Client::port_removed ( Port *p )
  144. {
  145. _active_ports.remove( p );
  146. }
  147. void
  148. Client::freeze_ports ( void )
  149. {
  150. for ( std::list < JACK::Port * >::iterator i = _active_ports.begin();
  151. i != _active_ports.end();
  152. ++i )
  153. {
  154. (*i)->freeze();
  155. }
  156. }
  157. void
  158. Client::thaw_ports ( void )
  159. {
  160. /* Sort ports for the sake of clients (e.g. patchage), for
  161. * whom the order of creation may matter (for display) */
  162. _active_ports.sort();
  163. for ( std::list < JACK::Port * >::iterator i = _active_ports.begin();
  164. i != _active_ports.end();
  165. ++i )
  166. {
  167. (*i)->thaw();
  168. }
  169. }
  170. void
  171. Client::close ( void )
  172. {
  173. jack_deactivate( _client );
  174. jack_client_close( _client );
  175. _client = NULL;
  176. }
  177. const char *
  178. Client::name ( const char *s )
  179. {
  180. /* Because the JACK API does not provide a mechanism for renaming
  181. * clients, we have to save connections, destroy our client,
  182. * create a client with the new name, and restore our
  183. * connections. Lovely. */
  184. freeze_ports();
  185. jack_deactivate( _client );
  186. jack_client_close( _client );
  187. _client = NULL;
  188. s = init( s );
  189. thaw_ports();
  190. return s;
  191. }
  192. void
  193. Client::transport_stop ( )
  194. {
  195. jack_transport_stop( _client );
  196. }
  197. void
  198. Client::transport_start ( )
  199. {
  200. jack_transport_start( _client );
  201. }
  202. void
  203. Client::transport_locate ( nframes_t frame )
  204. {
  205. jack_transport_locate( _client, frame );
  206. }
  207. jack_transport_state_t
  208. Client::transport_query ( jack_position_t *pos )
  209. {
  210. return jack_transport_query( _client, pos );
  211. }
  212. }