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.

370 lines
9.3KB

  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 = strdup( rhs._name );
  43. _client->port_added( this );
  44. }
  45. /* nframes is the number of frames to buffer */
  46. Port::Port ( JACK::Client *client, jack_port_t *port )
  47. {
  48. _terminal = 0;
  49. _connections = NULL;
  50. _client = client;
  51. _port = port;
  52. _name = strdup( jack_port_name( port ) );
  53. _direction = ( jack_port_flags( _port ) & JackPortIsOutput ) ? Output : Input;
  54. const char *type = jack_port_type( _port );
  55. _type = Audio;
  56. if ( strstr( type, "MIDI") )
  57. _type = MIDI;
  58. _client->port_added( this );
  59. }
  60. Port::Port ( JACK::Client *client, const char *name, direction_e dir, type_e type )
  61. {
  62. _port = 0;
  63. _terminal = 0;
  64. _name = NULL;
  65. _connections = NULL;
  66. _client = client;
  67. _direction = dir;
  68. _type = type;
  69. _name = strdup( name );
  70. _client->port_added( this );
  71. }
  72. Port::Port ( JACK::Client *client, direction_e dir, type_e type, const char *base, int n, const char *subtype )
  73. {
  74. _port = 0;
  75. _terminal = 0;
  76. _name = NULL;
  77. _connections = NULL;
  78. _client = client;
  79. _name = name_for_port( dir, base, n, subtype );
  80. _direction = dir;
  81. _type = type;
  82. _client->port_added( this );
  83. }
  84. Port::Port ( JACK::Client *client, direction_e dir, type_e type, int n, const char *subtype )
  85. {
  86. _port = 0;
  87. _terminal = 0;
  88. _name = NULL;
  89. _connections = NULL;
  90. _client = client;
  91. _name = name_for_port( dir, NULL, n, subtype );
  92. _direction = dir;
  93. _type = type;
  94. _client->port_added( this );
  95. }
  96. Port::~Port ( )
  97. {
  98. _client->port_removed( this );
  99. if ( _name )
  100. {
  101. free( _name );
  102. _name = NULL;
  103. }
  104. }
  105. /* sort input before output and then by alpha */
  106. bool
  107. Port::operator < ( const Port & rhs ) const
  108. {
  109. if ( type() == rhs.type() )
  110. return strcmp( name(), rhs.name() );
  111. else
  112. return direction() == Port::Input;
  113. }
  114. static char *
  115. name_for_port ( Port::direction_e dir, const char *base, int n, const char *type )
  116. {
  117. char *pname;
  118. const char *dir_s = dir == Port::Output ? "out" : "in";
  119. if ( type )
  120. asprintf( &pname, "%s-%s%s%s-%d", type, base ? base : "", base ? "/" : "", dir_s, n + 1 );
  121. else
  122. asprintf( &pname, "%s%s%s-%d", base ? base : "", base ? "/" : "", dir_s, n + 1 );
  123. return pname;
  124. }
  125. bool
  126. Port::activate ( void )
  127. {
  128. /* assert( !_port ); */
  129. int flags = 0;
  130. if ( _direction == Output )
  131. flags |= JackPortIsOutput;
  132. else
  133. flags |= JackPortIsInput;
  134. if ( _terminal )
  135. flags |= JackPortIsTerminal;
  136. DMESSAGE( "Activating port name %s", _name );
  137. _port = jack_port_register( _client->jack_client(), _name,
  138. _type == Audio ? JACK_DEFAULT_AUDIO_TYPE : JACK_DEFAULT_MIDI_TYPE,
  139. flags,
  140. 0 );
  141. DMESSAGE( "Port = %p", _port );
  142. if ( ! _port )
  143. return false;
  144. _client->port_added( this );
  145. return true;
  146. }
  147. /** returns the sum of latency of all ports between this one and a
  148. terminal port. */
  149. /* FIMXE: how does JACK know that input A of client Foo connects to
  150. output Z of the same client in order to draw the line through Z to a
  151. terminal port? And, if this determination cannot be made, what use is
  152. this function? */
  153. nframes_t
  154. Port::total_latency ( void ) const
  155. {
  156. return jack_port_get_total_latency( _client->jack_client() , _port );
  157. }
  158. /** returns the number of frames of latency assigned to this port */
  159. nframes_t
  160. Port::latency ( void ) const
  161. {
  162. return jack_port_get_latency( _port );
  163. }
  164. /** inform JACK that port has /frames/ frames of latency */
  165. void
  166. Port::latency ( nframes_t frames )
  167. {
  168. jack_port_set_latency( _port, frames );
  169. }
  170. void
  171. Port::shutdown ( void )
  172. {
  173. deactivate();
  174. _client->port_removed( this );
  175. }
  176. void
  177. Port::deactivate ( void )
  178. {
  179. if ( _port )
  180. jack_port_unregister( _client->jack_client(), _port );
  181. _port = 0;
  182. }
  183. /** rename port */
  184. bool
  185. Port::name ( const char *name )
  186. {
  187. if ( _name )
  188. free( _name );
  189. _name = strdup( name );
  190. return 0 == jack_port_set_name( _port, name );
  191. }
  192. bool
  193. Port::name ( const char *base, int n, const char *type )
  194. {
  195. char *s = name_for_port( this->direction(), base, n, type );
  196. bool b = name( s );
  197. free(s);
  198. return b;
  199. }
  200. void
  201. Port::write ( sample_t *buf, nframes_t nframes )
  202. {
  203. memcpy( buffer( nframes ), buf, nframes * sizeof( sample_t ) );
  204. }
  205. void
  206. Port::read ( sample_t *buf, nframes_t nframes )
  207. {
  208. memcpy( buf, buffer( nframes ), nframes * sizeof( sample_t ) );
  209. }
  210. void *
  211. Port::buffer ( nframes_t nframes )
  212. {
  213. return jack_port_get_buffer( _port, nframes );
  214. }
  215. void
  216. Port::silence ( nframes_t nframes )
  217. {
  218. memset( buffer( nframes ), 0, nframes * sizeof( sample_t ) );
  219. }
  220. /** Return a malloc()'d null terminated array of strings
  221. * representing all ports to which this port is connected. */
  222. const char **
  223. Port::connections ( void )
  224. {
  225. return jack_port_get_connections( _port );
  226. }
  227. /** Restore the connections returned by connections() */
  228. bool
  229. Port::connections ( const char **port_names )
  230. {
  231. if ( ! port_names )
  232. return true;
  233. for ( const char **port_name = port_names; *port_name; ++port_name )
  234. {
  235. printf( "Attempting to reconnect to %s\n", *port_name );
  236. connect( *port_name );
  237. }
  238. return true;
  239. }
  240. int
  241. Port::connect ( const char *to )
  242. {
  243. const char *name = jack_port_name( _port );
  244. if ( _direction == Output )
  245. {
  246. return jack_connect( _client->jack_client(), name, to );
  247. }
  248. else
  249. {
  250. return jack_connect( _client->jack_client(), to, name );
  251. }
  252. }
  253. int
  254. Port::disconnect ( const char *from )
  255. {
  256. const char *name = jack_port_name( _port );
  257. if ( _direction == Output )
  258. {
  259. return jack_disconnect( _client->jack_client(), name, from );
  260. }
  261. else
  262. {
  263. return jack_disconnect( _client->jack_client(), from, name );
  264. }
  265. }
  266. bool
  267. Port::connected_to ( const char *to )
  268. {
  269. return jack_port_connected_to( _port, to );
  270. }
  271. void
  272. Port::freeze ( void )
  273. {
  274. if ( _connections )
  275. free( _connections );
  276. // DMESSAGE( "Freezing port %s", _name );
  277. _connections = connections();
  278. // deactivate();
  279. }
  280. void
  281. Port::thaw ( void )
  282. {
  283. // DMESSAGE( "Thawing port %s", _name );
  284. activate();
  285. if ( _connections )
  286. {
  287. connections( _connections );
  288. free( _connections );
  289. _connections = NULL;
  290. }
  291. }
  292. }