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.

170 lines
4.5KB

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