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.

226 lines
5.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 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. /** Connect to JACK using client name /client_name/. Return a static
  89. * pointer to actual name as reported by JACK */
  90. const char *
  91. Client::init ( const char *client_name, unsigned int opts )
  92. {
  93. if (( _client = jack_client_open ( client_name, (jack_options_t)0, NULL )) == 0 )
  94. return NULL;
  95. #define set_callback( name ) jack_set_ ## name ## _callback( _client, &Client:: name , this )
  96. set_callback( thread_init );
  97. set_callback( process );
  98. set_callback( xrun );
  99. set_callback( freewheel );
  100. set_callback( buffer_size );
  101. /* FIXME: should we wait to register this until after the project
  102. has been loaded (and we have disk threads running)? */
  103. if ( opts & SLOW_SYNC )
  104. set_callback( sync );
  105. if ( opts & TIMEBASE_MASTER )
  106. jack_set_timebase_callback( _client, 0, &Client::timebase, this );
  107. jack_on_shutdown( _client, &Client::shutdown, this );
  108. jack_activate( _client );
  109. _sample_rate = frame_rate();
  110. return jack_get_client_name( _client );
  111. }
  112. /* THREAD: RT */
  113. /** enter or leave freehweeling mode */
  114. void
  115. Client::freewheeling ( bool yes )
  116. {
  117. if ( jack_set_freewheel( _client, yes ) )
  118. ;
  119. // WARNING( "Unkown error while setting freewheeling mode" );
  120. }
  121. void
  122. Client::port_added ( Port *p )
  123. {
  124. std::list < JACK::Port * >::iterator i = std::find( _active_ports.begin(), _active_ports.end(), p );
  125. if ( i != _active_ports.end() )
  126. return;
  127. _active_ports.push_back( p );
  128. }
  129. void
  130. Client::port_removed ( Port *p )
  131. {
  132. _active_ports.remove( p );
  133. }
  134. void
  135. Client::freeze_ports ( void )
  136. {
  137. for ( std::list < JACK::Port * >::iterator i = _active_ports.begin();
  138. i != _active_ports.end();
  139. ++i )
  140. {
  141. (*i)->freeze();
  142. }
  143. }
  144. void
  145. Client::thaw_ports ( void )
  146. {
  147. /* Sort ports for the sake of clients (e.g. patchage), for
  148. * whom the order of creation may matter (for display) */
  149. _active_ports.sort();
  150. for ( std::list < JACK::Port * >::iterator i = _active_ports.begin();
  151. i != _active_ports.end();
  152. ++i )
  153. {
  154. (*i)->thaw();
  155. }
  156. }
  157. const char *
  158. Client::name ( const char *s )
  159. {
  160. /* Because the JACK API does not provide a mechanism for renaming
  161. * clients, we have to save connections, destroy our client,
  162. * create a client with the new name, and restore our
  163. * connections. Lovely. */
  164. freeze_ports();
  165. jack_deactivate( _client );
  166. jack_client_close( _client );
  167. _client = NULL;
  168. s = init( s );
  169. thaw_ports();
  170. return s;
  171. }
  172. }