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.

424 lines
11KB

  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. /* Wrapper for a JACK audio port */
  19. #include "Port.H"
  20. #include <string.h>
  21. #include <stdio.h> // sprintf
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include "debug.h"
  25. namespace JACK
  26. {
  27. /* static char *name_for_port ( Port::direction_e dir, const char *base, int n, const char *type ); */
  28. int
  29. Port::max_name ( void )
  30. {
  31. return jack_port_name_size() - jack_client_name_size() - 6;
  32. }
  33. Port::Port ( const Port &rhs )
  34. {
  35. _connections = NULL;
  36. _terminal = rhs._terminal;
  37. // _connections = rhs._connections;
  38. _client = rhs._client;
  39. _port = rhs._port;
  40. _direction = rhs._direction;
  41. _type = rhs._type;
  42. _name = NULL;
  43. _name = strdup( rhs._name );
  44. _trackname = NULL;
  45. if ( rhs._trackname )
  46. _trackname = strdup( rhs._trackname );
  47. _client->port_added( this );
  48. }
  49. /* nframes is the number of frames to buffer */
  50. Port::Port ( JACK::Client *client, jack_port_t *port )
  51. {
  52. _terminal = 0;
  53. _connections = NULL;
  54. _client = client;
  55. _port = port;
  56. _name = strdup( jack_port_name( port ) );
  57. _trackname = NULL;
  58. _direction = ( jack_port_flags( _port ) & JackPortIsOutput ) ? Output : Input;
  59. const char *type = jack_port_type( _port );
  60. _type = Audio;
  61. if ( strstr( type, "MIDI") )
  62. _type = MIDI;
  63. _client->port_added( this );
  64. }
  65. Port::Port ( JACK::Client *client, const char *trackname, const char *name, direction_e dir, type_e type )
  66. {
  67. _port = 0;
  68. _terminal = 0;
  69. _name = NULL;
  70. _trackname = NULL;
  71. _connections = NULL;
  72. _client = client;
  73. _direction = dir;
  74. _type = type;
  75. _trackname = NULL;
  76. if ( trackname )
  77. _trackname = strdup( trackname );
  78. _name = strdup( name );
  79. _client->port_added( this );
  80. }
  81. /* Port::Port ( JACK::Client *client, direction_e dir, type_e type, const char *base, int n, const char *subtype ) */
  82. /* { */
  83. /* _port = 0; */
  84. /* _terminal = 0; */
  85. /* _name = NULL; */
  86. /* _connections = NULL; */
  87. /* _client = client; */
  88. /* _name = name_for_port( dir, base, n, subtype ); */
  89. /* _direction = dir; */
  90. /* _type = type; */
  91. /* _client->port_added( this ); */
  92. /* } */
  93. /* Port::Port ( JACK::Client *client, direction_e dir, type_e type, int n, const char *subtype ) */
  94. /* { */
  95. /* _port = 0; */
  96. /* _terminal = 0; */
  97. /* _name = NULL; */
  98. /* _connections = NULL; */
  99. /* _client = client; */
  100. /* _name = name_for_port( dir, NULL, n, subtype ); */
  101. /* _direction = dir; */
  102. /* _type = type; */
  103. /* _client->port_added( this ); */
  104. /* } */
  105. Port::~Port ( )
  106. {
  107. _client->port_removed( this );
  108. if ( _name )
  109. {
  110. free( _name );
  111. _name = NULL;
  112. }
  113. if ( _trackname )
  114. {
  115. free( _trackname );
  116. _trackname = NULL;
  117. }
  118. }
  119. /* sort input before output and then by alpha */
  120. bool
  121. Port::operator < ( const Port & rhs ) const
  122. {
  123. if ( type() == rhs.type() )
  124. return strcmp( name(), rhs.name() );
  125. else
  126. return direction() == Port::Input;
  127. }
  128. /* static char * */
  129. /* name_for_port ( Port::direction_e dir, const char *base, int n, const char *type ) */
  130. /* { */
  131. /* char *pname; */
  132. /* const char *dir_s = dir == Port::Output ? "out" : "in"; */
  133. /* if ( type ) */
  134. /* asprintf( &pname, "%s-%s%s%s-%d", type, base ? base : "", base ? "/" : "", dir_s, n + 1 ); */
  135. /* else */
  136. /* asprintf( &pname, "%s%s%s-%d", base ? base : "", base ? "/" : "", dir_s, n + 1 ); */
  137. /* return pname; */
  138. /* } */
  139. bool
  140. Port::activate ( void )
  141. {
  142. /* assert( !_port ); */
  143. int flags = 0;
  144. if ( _direction == Output )
  145. flags |= JackPortIsOutput;
  146. else
  147. flags |= JackPortIsInput;
  148. if ( _terminal )
  149. flags |= JackPortIsTerminal;
  150. char jackname[max_name()];
  151. snprintf( jackname, sizeof(jackname), "%s%s%s", _trackname ? _trackname : "", _trackname ? "/" : "", _name );
  152. DMESSAGE( "Activating port name %s", jackname );
  153. _port = jack_port_register( _client->jack_client(), jackname,
  154. _type == Audio ? JACK_DEFAULT_AUDIO_TYPE : JACK_DEFAULT_MIDI_TYPE,
  155. flags,
  156. 0 );
  157. DMESSAGE( "Port = %p", _port );
  158. if ( ! _port )
  159. return false;
  160. _client->port_added( this );
  161. return true;
  162. }
  163. /** returns the sum of latency of all ports between this one and a
  164. terminal port. */
  165. nframes_t
  166. Port::total_latency ( void ) const
  167. {
  168. #ifdef HAVE_JACK_PORT_GET_LATENCY_RANGE
  169. jack_latency_range_t range;
  170. jack_port_get_latency_range( _port, _direction == Input ? JackPlaybackLatency : JackCaptureLatency, &range );
  171. return range.max;
  172. #else
  173. return jack_port_get_total_latency( _client->jack_client() , _port );
  174. #endif
  175. }
  176. /** returns the number of frames of latency assigned to this port */
  177. void
  178. Port::get_latency ( direction_e dir, nframes_t *min, nframes_t *max ) const
  179. {
  180. #ifdef HAVE_JACK_PORT_GET_LATENCY_RANGE
  181. jack_latency_range_t range;
  182. jack_port_get_latency_range( _port, dir == Output ? JackPlaybackLatency : JackCaptureLatency, &range );
  183. *min = range.min;
  184. *max = range.max;
  185. #else
  186. *min = *max = jack_port_get_latency( _port );
  187. #endif
  188. }
  189. /** inform JACK that port has /frames/ frames of latency */
  190. void
  191. Port::set_latency ( direction_e dir, nframes_t min, nframes_t max )
  192. {
  193. #ifdef HAVE_JACK_PORT_GET_LATENCY_RANGE
  194. jack_latency_range_t range;
  195. // DMESSAGE( "Setting port latency!" );
  196. range.max = max;
  197. range.min = min;
  198. jack_port_set_latency_range( _port, dir == Output ? JackPlaybackLatency : JackCaptureLatency, &range );
  199. #else
  200. jack_port_set_latency( _port, max );
  201. #endif
  202. }
  203. void
  204. Port::shutdown ( void )
  205. {
  206. deactivate();
  207. _client->port_removed( this );
  208. }
  209. void
  210. Port::deactivate ( void )
  211. {
  212. if ( _port )
  213. jack_port_unregister( _client->jack_client(), _port );
  214. _port = 0;
  215. }
  216. void
  217. Port::name ( const char *name )
  218. {
  219. if ( _name )
  220. free( _name );
  221. _name = strdup( name );
  222. }
  223. void
  224. Port::trackname ( const char *trackname )
  225. {
  226. if ( _trackname )
  227. free( _trackname );
  228. _trackname = NULL;
  229. if ( trackname )
  230. _trackname = strdup( trackname );
  231. }
  232. bool
  233. Port::rename ( void )
  234. {
  235. char jackname[max_name()];
  236. snprintf( jackname, sizeof(jackname), "%s%s%s", _trackname ? _trackname : "", _trackname ? "/" : "", _name );
  237. if ( _port )
  238. return 0 == jack_port_set_name( _port, jackname );
  239. else
  240. return false;
  241. }
  242. void
  243. Port::write ( sample_t *buf, nframes_t nframes )
  244. {
  245. memcpy( buffer( nframes ), buf, nframes * sizeof( sample_t ) );
  246. }
  247. void
  248. Port::read ( sample_t *buf, nframes_t nframes )
  249. {
  250. memcpy( buf, buffer( nframes ), nframes * sizeof( sample_t ) );
  251. }
  252. void *
  253. Port::buffer ( nframes_t nframes )
  254. {
  255. return jack_port_get_buffer( _port, nframes );
  256. }
  257. void
  258. Port::silence ( nframes_t nframes )
  259. {
  260. memset( buffer( nframes ), 0, nframes * sizeof( sample_t ) );
  261. }
  262. /** Return a malloc()'d null terminated array of strings
  263. * representing all ports to which this port is connected. */
  264. const char **
  265. Port::connections ( void )
  266. {
  267. return jack_port_get_connections( _port );
  268. }
  269. /** Restore the connections returned by connections() */
  270. bool
  271. Port::connections ( const char **port_names )
  272. {
  273. if ( ! port_names )
  274. return true;
  275. for ( const char **port_name = port_names; *port_name; ++port_name )
  276. {
  277. printf( "Attempting to reconnect to %s\n", *port_name );
  278. connect( *port_name );
  279. }
  280. return true;
  281. }
  282. int
  283. Port::connect ( const char *to )
  284. {
  285. const char *name = jack_port_name( _port );
  286. if ( _direction == Output )
  287. {
  288. return jack_connect( _client->jack_client(), name, to );
  289. }
  290. else
  291. {
  292. return jack_connect( _client->jack_client(), to, name );
  293. }
  294. }
  295. int
  296. Port::disconnect ( const char *from )
  297. {
  298. const char *name = jack_port_name( _port );
  299. if ( _direction == Output )
  300. {
  301. return jack_disconnect( _client->jack_client(), name, from );
  302. }
  303. else
  304. {
  305. return jack_disconnect( _client->jack_client(), from, name );
  306. }
  307. }
  308. bool
  309. Port::connected_to ( const char *to )
  310. {
  311. return jack_port_connected_to( _port, to );
  312. }
  313. void
  314. Port::freeze ( void )
  315. {
  316. if ( _connections )
  317. free( _connections );
  318. // DMESSAGE( "Freezing port %s", _name );
  319. _connections = connections();
  320. // deactivate();
  321. }
  322. void
  323. Port::thaw ( void )
  324. {
  325. // DMESSAGE( "Thawing port %s", _name );
  326. activate();
  327. if ( _connections )
  328. {
  329. connections( _connections );
  330. free( _connections );
  331. _connections = NULL;
  332. }
  333. }
  334. }