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.

315 lines
8.2KB

  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. namespace JACK
  24. {
  25. static const char *name_for_port ( Port::type_e dir, const char *base, int n, const char *type );
  26. int
  27. Port::max_name ( void )
  28. {
  29. return jack_port_name_size() - jack_client_name_size() - 6;
  30. }
  31. Port::Port ( const Port &rhs )
  32. {
  33. _freezer = rhs._freezer;
  34. _client = rhs._client;
  35. _port = rhs._port;
  36. _name = strdup( rhs._name );
  37. _client->port_added( this );
  38. }
  39. /* nframes is the number of frames to buffer */
  40. Port::Port ( JACK::Client *client, jack_port_t *port )
  41. {
  42. _freezer = NULL;
  43. _client = client;
  44. _port = port;
  45. _name = strdup( jack_port_name( port ) );
  46. }
  47. Port::Port ( JACK::Client *client, const char *name, type_e dir )
  48. {
  49. _name = NULL;
  50. _freezer = NULL;
  51. _client = client;
  52. activate( name, dir );
  53. }
  54. Port::Port ( JACK::Client *client, type_e dir, const char *base, int n, const char *type )
  55. {
  56. _name = NULL;
  57. _freezer = NULL;
  58. _client = client;
  59. const char *name = name_for_port( dir, base, n, type );
  60. activate( name, dir );
  61. }
  62. Port::Port ( JACK::Client *client, type_e dir, int n, const char *type )
  63. {
  64. _name = NULL;
  65. _freezer = NULL;
  66. _client = client;
  67. const char *name = name_for_port( dir, NULL, n, type );
  68. activate( name, dir );
  69. }
  70. Port::~Port ( )
  71. {
  72. if ( _name )
  73. free( _name );
  74. _client->port_removed( this );
  75. /* if ( _freezer ) */
  76. /* { */
  77. /* delete _freezer; */
  78. /* _freezer = NULL; */
  79. /* } */
  80. /* if ( _port ) */
  81. /* jack_port_unregister( _client, _port ); */
  82. }
  83. /* sort input before output and then by alpha */
  84. bool
  85. Port::operator < ( const Port & rhs ) const
  86. {
  87. if ( type() == rhs.type() )
  88. return strcmp( name(), rhs.name() );
  89. else
  90. return type() == Port::Input;
  91. }
  92. static const char *
  93. name_for_port ( Port::type_e dir, const char *base, int n, const char *type )
  94. {
  95. static char pname[ 512 ];
  96. const char *dir_s = dir == Port::Output ? "out" : "in";
  97. pname[0] = '\0';
  98. if ( base )
  99. {
  100. strncpy( pname, base, Port::max_name() );
  101. strcat( pname, "/" );
  102. }
  103. pname[ Port::max_name() - 1 ] = '\0';
  104. int l = strlen( pname );
  105. if ( type )
  106. snprintf( pname + l, sizeof( pname ) - l, "%s-%s-%d", type, dir_s, n + 1 );
  107. else
  108. snprintf( pname + l, sizeof( pname ) - l, "%s-%d", dir_s, n + 1 );
  109. return pname;
  110. }
  111. void
  112. Port::activate ( const char *name, type_e dir )
  113. {
  114. _name = strdup( name );
  115. _port = jack_port_register( _client->jack_client(), _name,
  116. JACK_DEFAULT_AUDIO_TYPE,
  117. dir == Output ? JackPortIsOutput : JackPortIsInput,
  118. 0 );
  119. _client->port_added( this );
  120. }
  121. /** returns the sum of latency of all ports between this one and a
  122. terminal port. */
  123. /* FIMXE: how does JACK know that input A of client Foo connects to
  124. output Z of the same client in order to draw the line through Z to a
  125. terminal port? And, if this determination cannot be made, what use is
  126. this function? */
  127. nframes_t
  128. Port::total_latency ( void ) const
  129. {
  130. return jack_port_get_total_latency( _client->jack_client() , _port );
  131. }
  132. /** returns the number of frames of latency assigned to this port */
  133. nframes_t
  134. Port::latency ( void ) const
  135. {
  136. return jack_port_get_latency( _port );
  137. }
  138. /** inform JACK that port has /frames/ frames of latency */
  139. void
  140. Port::latency ( nframes_t frames )
  141. {
  142. jack_port_set_latency( _port, frames );
  143. }
  144. void
  145. Port::shutdown ( void )
  146. {
  147. if ( _port )
  148. jack_port_unregister( _client->jack_client(), _port );
  149. _client->port_removed( this );
  150. }
  151. /** rename port */
  152. bool
  153. Port::name ( const char *name )
  154. {
  155. _name = strdup( name );
  156. return 0 == jack_port_set_name( _port, name );
  157. }
  158. bool
  159. Port::name ( const char *base, int n, const char *type )
  160. {
  161. return name( name_for_port( this->type(), base, n, type ) );
  162. }
  163. void
  164. Port::write ( sample_t *buf, nframes_t nframes )
  165. {
  166. memcpy( buffer( nframes ), buf, nframes * sizeof( sample_t ) );
  167. }
  168. void
  169. Port::read ( sample_t *buf, nframes_t nframes )
  170. {
  171. memcpy( buf, buffer( nframes ), nframes * sizeof( sample_t ) );
  172. }
  173. void *
  174. Port::buffer ( nframes_t nframes )
  175. {
  176. return jack_port_get_buffer( _port, nframes );
  177. }
  178. void
  179. Port::silence ( nframes_t nframes )
  180. {
  181. memset( buffer( nframes ), 0, nframes * sizeof( sample_t ) );
  182. }
  183. /** Return a malloc()'d null terminated array of strings
  184. * representing all ports to which this port is connected. */
  185. const char **
  186. Port::connections ( void )
  187. {
  188. return jack_port_get_connections( _port );
  189. }
  190. Port::type_e
  191. Port::type ( void ) const
  192. {
  193. if ( _freezer )
  194. return _freezer->direction;
  195. else
  196. return jack_port_flags( _port ) == JackPortIsOutput ? Output : Input;
  197. }
  198. /** Restore the connections returned by connections() */
  199. bool
  200. Port::connections ( const char **port_names )
  201. {
  202. if ( ! port_names )
  203. return true;
  204. for ( const char **port_name = port_names; *port_name; ++port_name )
  205. {
  206. const char *src;
  207. const char *dst;
  208. const char *name = jack_port_name( _port );
  209. if ( type() == Output )
  210. {
  211. src = name;
  212. dst = *port_name;
  213. }
  214. else
  215. {
  216. src = *port_name;
  217. dst = name;
  218. }
  219. if ( int err = jack_connect( _client->jack_client(), src, dst ) )
  220. {
  221. if ( EEXIST == err )
  222. {
  223. /* connection already exists, not a problem */
  224. }
  225. else
  226. {
  227. return false;
  228. }
  229. }
  230. }
  231. return true;
  232. }
  233. void
  234. Port::freeze ( void )
  235. {
  236. if ( _freezer )
  237. delete _freezer;
  238. freeze_state *f = new freeze_state();
  239. f->connections = connections();
  240. f->direction = type();
  241. f->name = strdup( name() );
  242. _freezer = f;
  243. }
  244. void
  245. Port::thaw ( void )
  246. {
  247. activate( name(), _freezer->direction );
  248. connections( _freezer->connections );
  249. delete _freezer;
  250. _freezer = NULL;
  251. }
  252. }