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.

174 lines
5.0KB

  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. namespace JACK
  23. {
  24. static const char *name_for_port ( Port::type_e dir, const char *base, int n, const char *type );
  25. int
  26. Port::max_name ( void )
  27. {
  28. return jack_port_name_size() - jack_client_name_size() - 6;
  29. }
  30. /* nframes is the number of frames to buffer */
  31. Port::Port ( jack_client_t *client, jack_port_t *port )
  32. {
  33. _client = client;
  34. _port = port;
  35. _name = jack_port_name( _port );
  36. }
  37. Port::Port ( jack_client_t *client, const char *name, type_e dir )
  38. {
  39. _client = client;
  40. activate( name, dir );
  41. }
  42. Port::Port ( jack_client_t *client, type_e dir, const char *base, int n, const char *type )
  43. {
  44. _client = client;
  45. const char *name = name_for_port( dir, base, n, type );
  46. activate( name, dir );
  47. }
  48. Port::~Port ( )
  49. {
  50. /* if ( _port ) */
  51. /* jack_port_unregister( _client, _port ); */
  52. }
  53. static const char *
  54. name_for_port ( Port::type_e dir, const char *base, int n, const char *type )
  55. {
  56. static char pname[ 512 ];
  57. const char *dir_s = dir == Port::Output ? "out" : "in";
  58. strncpy( pname, base, Port::max_name() );
  59. pname[ Port::max_name() - 1 ] = '\0';
  60. int l = strlen( pname );
  61. if ( type )
  62. snprintf( pname + l, sizeof( pname ) - l, "/%s-%s-%d", type, dir_s, n + 1 );
  63. else
  64. snprintf( pname + l, sizeof( pname ) - l, "/%s-%d", dir_s, n + 1 );
  65. return pname;
  66. }
  67. void
  68. Port::activate ( const char *name, type_e dir )
  69. {
  70. _name = name;
  71. _port = jack_port_register( _client, _name,
  72. JACK_DEFAULT_AUDIO_TYPE,
  73. dir == Output ? JackPortIsOutput : JackPortIsInput,
  74. 0 );
  75. }
  76. /** returns the sum of latency of all ports between this one and a
  77. terminal port. */
  78. /* FIMXE: how does JACK know that input A of client Foo connects to
  79. output Z of the same client in order to draw the line through Z to a
  80. terminal port? And, if this determination cannot be made, what use is
  81. this function? */
  82. nframes_t
  83. Port::total_latency ( void ) const
  84. {
  85. return jack_port_get_total_latency( _client, _port );
  86. }
  87. /** returns the number of frames of latency assigned to this port */
  88. nframes_t
  89. Port::latency ( void ) const
  90. {
  91. return jack_port_get_latency( _port );
  92. }
  93. /** inform JACK that port has /frames/ frames of latency */
  94. void
  95. Port::latency ( nframes_t frames )
  96. {
  97. jack_port_set_latency( _port, frames );
  98. }
  99. void
  100. Port::shutdown ( void )
  101. {
  102. if ( _port )
  103. jack_port_unregister( _client, _port );
  104. }
  105. /** rename port */
  106. bool
  107. Port::name ( const char *name )
  108. {
  109. _name = name;
  110. return 0 == jack_port_set_name( _port, name );
  111. }
  112. bool
  113. Port::name ( const char *base, int n, const char *type )
  114. {
  115. return name( name_for_port( this->type(), base, n, type ) );
  116. }
  117. void
  118. Port::write ( sample_t *buf, nframes_t nframes )
  119. {
  120. memcpy( buffer( nframes ), buf, nframes * sizeof( sample_t ) );
  121. }
  122. void
  123. Port::read ( sample_t *buf, nframes_t nframes )
  124. {
  125. memcpy( buf, buffer( nframes ), nframes * sizeof( sample_t ) );
  126. }
  127. void *
  128. Port::buffer ( nframes_t nframes )
  129. {
  130. return jack_port_get_buffer( _port, nframes );
  131. }
  132. void
  133. Port::silence ( nframes_t nframes )
  134. {
  135. memset( buffer( nframes ), 0, nframes * sizeof( sample_t ) );
  136. }
  137. }