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.

402 lines
10KB

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