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.

427 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. /* FIMXE: how does JACK know that input A of client Foo connects to
  166. output Z of the same client in order to draw the line through Z to a
  167. terminal port? And, if this determination cannot be made, what use is
  168. this function? */
  169. nframes_t
  170. Port::total_latency ( void ) const
  171. {
  172. #ifdef HAVE_JACK_PORT_GET_LATENCY_RANGE
  173. jack_latency_range_t range;
  174. jack_port_get_latency_range( _port, _direction == Output ? JackPlaybackLatency : JackCaptureLatency, &range );
  175. return range.max;
  176. #else
  177. return jack_port_get_total_latency( _client->jack_client() , _port );
  178. #endif
  179. }
  180. /** returns the number of frames of latency assigned to this port */
  181. nframes_t
  182. Port::latency ( void ) const
  183. {
  184. #ifdef HAVE_JACK_PORT_GET_LATENCY_RANGE
  185. jack_latency_range_t range;
  186. jack_port_get_latency_range( _port, _direction == Output ? JackPlaybackLatency : JackCaptureLatency, &range );
  187. return range.max;
  188. #else
  189. return jack_port_get_latency( _port );
  190. #endif
  191. }
  192. /** inform JACK that port has /frames/ frames of latency */
  193. void
  194. Port::latency ( nframes_t frames )
  195. {
  196. #ifdef HAVE_JACK_PORT_GET_LATENCY_RANGE
  197. jack_latency_range_t range;
  198. range.min = range.max = frames;
  199. jack_port_set_latency_range( _port, _direction == Output ? JackPlaybackLatency : JackCaptureLatency, &range );
  200. #else
  201. jack_port_set_latency( _port, frames );
  202. #endif
  203. }
  204. void
  205. Port::shutdown ( void )
  206. {
  207. deactivate();
  208. _client->port_removed( this );
  209. }
  210. void
  211. Port::deactivate ( void )
  212. {
  213. if ( _port )
  214. jack_port_unregister( _client->jack_client(), _port );
  215. _port = 0;
  216. }
  217. void
  218. Port::name ( const char *name )
  219. {
  220. if ( _name )
  221. free( _name );
  222. _name = strdup( name );
  223. }
  224. void
  225. Port::trackname ( const char *trackname )
  226. {
  227. if ( _trackname )
  228. free( _trackname );
  229. _trackname = NULL;
  230. if ( trackname )
  231. _trackname = strdup( trackname );
  232. }
  233. bool
  234. Port::rename ( void )
  235. {
  236. char jackname[max_name()];
  237. snprintf( jackname, sizeof(jackname), "%s%s%s", _trackname ? _trackname : "", _trackname ? "/" : "", _name );
  238. if ( _port )
  239. return 0 == jack_port_set_name( _port, jackname );
  240. else
  241. return false;
  242. }
  243. void
  244. Port::write ( sample_t *buf, nframes_t nframes )
  245. {
  246. memcpy( buffer( nframes ), buf, nframes * sizeof( sample_t ) );
  247. }
  248. void
  249. Port::read ( sample_t *buf, nframes_t nframes )
  250. {
  251. memcpy( buf, buffer( nframes ), nframes * sizeof( sample_t ) );
  252. }
  253. void *
  254. Port::buffer ( nframes_t nframes )
  255. {
  256. return jack_port_get_buffer( _port, nframes );
  257. }
  258. void
  259. Port::silence ( nframes_t nframes )
  260. {
  261. memset( buffer( nframes ), 0, nframes * sizeof( sample_t ) );
  262. }
  263. /** Return a malloc()'d null terminated array of strings
  264. * representing all ports to which this port is connected. */
  265. const char **
  266. Port::connections ( void )
  267. {
  268. return jack_port_get_connections( _port );
  269. }
  270. /** Restore the connections returned by connections() */
  271. bool
  272. Port::connections ( const char **port_names )
  273. {
  274. if ( ! port_names )
  275. return true;
  276. for ( const char **port_name = port_names; *port_name; ++port_name )
  277. {
  278. printf( "Attempting to reconnect to %s\n", *port_name );
  279. connect( *port_name );
  280. }
  281. return true;
  282. }
  283. int
  284. Port::connect ( const char *to )
  285. {
  286. const char *name = jack_port_name( _port );
  287. if ( _direction == Output )
  288. {
  289. return jack_connect( _client->jack_client(), name, to );
  290. }
  291. else
  292. {
  293. return jack_connect( _client->jack_client(), to, name );
  294. }
  295. }
  296. int
  297. Port::disconnect ( const char *from )
  298. {
  299. const char *name = jack_port_name( _port );
  300. if ( _direction == Output )
  301. {
  302. return jack_disconnect( _client->jack_client(), name, from );
  303. }
  304. else
  305. {
  306. return jack_disconnect( _client->jack_client(), from, name );
  307. }
  308. }
  309. bool
  310. Port::connected_to ( const char *to )
  311. {
  312. return jack_port_connected_to( _port, to );
  313. }
  314. void
  315. Port::freeze ( void )
  316. {
  317. if ( _connections )
  318. free( _connections );
  319. // DMESSAGE( "Freezing port %s", _name );
  320. _connections = connections();
  321. // deactivate();
  322. }
  323. void
  324. Port::thaw ( void )
  325. {
  326. // DMESSAGE( "Thawing port %s", _name );
  327. activate();
  328. if ( _connections )
  329. {
  330. connections( _connections );
  331. free( _connections );
  332. _connections = NULL;
  333. }
  334. }
  335. }