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.

445 lines
12KB

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