jack2 codebase
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.

258 lines
5.3KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. Copyright (C) 2004-2006 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "JackPort.h"
  17. #include "JackError.h"
  18. #include "JackPortType.h"
  19. #include <stdio.h>
  20. #include <assert.h>
  21. namespace Jack
  22. {
  23. JackPort::JackPort()
  24. : fFlags(JackPortIsInput), fRefNum( -1), fLatency(0), fMonitorRequests(0), fInUse(false), fTied(NO_PORT)
  25. {}
  26. JackPort::~JackPort()
  27. {}
  28. bool JackPort::Allocate(int refnum, const char* port_name, const char* port_type, JackPortFlags flags)
  29. {
  30. int id = GetPortTypeId(port_type);
  31. if (id < 0)
  32. return false;
  33. fTypeId = id;
  34. fFlags = flags;
  35. fRefNum = refnum;
  36. strcpy(fName, port_name);
  37. fInUse = true;
  38. fLatency = 0;
  39. fTied = NO_PORT;
  40. // DB: At this point we do not know current buffer size in frames,
  41. // but every time buffer will be returned to any user,
  42. // it will be called with either ClearBuffer or MixBuffers
  43. // with correct current buffer size.
  44. // So it is safe to init with 0 here.
  45. ClearBuffer(0);
  46. return true;
  47. }
  48. void JackPort::Release()
  49. {
  50. fTypeId = 0;
  51. fFlags = JackPortIsInput;
  52. fRefNum = -1;
  53. fInUse = false;
  54. fLatency = 0;
  55. fTied = NO_PORT;
  56. fAlias1[0] = '\0';
  57. fAlias2[0] = '\0';
  58. }
  59. bool JackPort::IsUsed() const
  60. {
  61. return fInUse;
  62. }
  63. float* JackPort::GetBuffer()
  64. {
  65. return fBuffer;
  66. }
  67. int JackPort::GetRefNum() const
  68. {
  69. return fRefNum;
  70. }
  71. jack_nframes_t JackPort::GetLatency() const
  72. {
  73. return fLatency;
  74. }
  75. void JackPort::SetLatency(jack_nframes_t nframes)
  76. {
  77. fLatency = nframes;
  78. }
  79. int JackPort::Tie(jack_port_id_t port_index)
  80. {
  81. fTied = port_index;
  82. return 0;
  83. }
  84. int JackPort::UnTie()
  85. {
  86. fTied = NO_PORT;
  87. return 0;
  88. }
  89. int JackPort::RequestMonitor(bool onoff)
  90. {
  91. /**
  92. jackd.h
  93. * If @ref JackPortCanMonitor is set for this @a port, turn input
  94. * monitoring on or off. Otherwise, do nothing.
  95. if (!(fFlags & JackPortCanMonitor))
  96. return -1;
  97. */
  98. if (onoff) {
  99. fMonitorRequests++;
  100. } else if (fMonitorRequests) {
  101. fMonitorRequests--;
  102. }
  103. return 0;
  104. }
  105. int JackPort::EnsureMonitor(bool onoff)
  106. {
  107. /**
  108. jackd.h
  109. * If @ref JackPortCanMonitor is set for this @a port, turn input
  110. * monitoring on or off. Otherwise, do nothing.
  111. if (!(fFlags & JackPortCanMonitor))
  112. return -1;
  113. */
  114. if (onoff) {
  115. if (fMonitorRequests == 0) {
  116. fMonitorRequests++;
  117. }
  118. } else {
  119. if (fMonitorRequests > 0) {
  120. fMonitorRequests = 0;
  121. }
  122. }
  123. return 0;
  124. }
  125. bool JackPort::MonitoringInput()
  126. {
  127. return (fMonitorRequests > 0);
  128. }
  129. const char* JackPort::GetName() const
  130. {
  131. return fName;
  132. }
  133. const char* JackPort::GetShortName() const
  134. {
  135. /* we know there is always a colon, because we put
  136. it there ...
  137. */
  138. return strchr(fName, ':') + 1;
  139. }
  140. int JackPort::GetFlags() const
  141. {
  142. return fFlags;
  143. }
  144. const char* JackPort::GetType() const
  145. {
  146. const JackPortType* type = GetPortType(fTypeId);
  147. return type->name;
  148. }
  149. int JackPort::SetName(const char* new_name)
  150. {
  151. char* colon = strchr(fName, ':');
  152. int len = sizeof(fName) - ((int) (colon - fName)) - 2;
  153. snprintf(colon + 1, len, "%s", new_name);
  154. return 0;
  155. }
  156. void JackPort::Rename(const char* name, int index)
  157. {
  158. SetAlias(GetName());
  159. snprintf(fName, sizeof(fName) - 1, name, index);
  160. }
  161. bool JackPort::NameEquals(const char* target)
  162. {
  163. return (strcmp(fName, target) == 0
  164. || strcmp(fAlias1, target) == 0
  165. || strcmp(fAlias2, target) == 0);
  166. }
  167. int JackPort::GetAliases(char* const aliases[2])
  168. {
  169. int cnt = 0;
  170. if (fAlias1[0] != '\0') {
  171. snprintf(aliases[0], JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE, "%s", fAlias1);
  172. cnt++;
  173. }
  174. if (fAlias2[0] != '\0') {
  175. snprintf(aliases[1], JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE, "%s", fAlias2);
  176. cnt++;
  177. }
  178. return cnt;
  179. }
  180. int JackPort::SetAlias(const char* alias)
  181. {
  182. if (fAlias1[0] == '\0') {
  183. snprintf(fAlias1, sizeof(fAlias1), "%s", alias);
  184. } else if (fAlias2[0] == '\0') {
  185. snprintf(fAlias2, sizeof(fAlias2), "%s", alias);
  186. } else {
  187. return -1;
  188. }
  189. return 0;
  190. }
  191. int JackPort::UnsetAlias(const char* alias)
  192. {
  193. if (strcmp(fAlias1, alias) == 0) {
  194. fAlias1[0] = '\0';
  195. } else if (strcmp(fAlias2, alias) == 0) {
  196. fAlias2[0] = '\0';
  197. } else {
  198. return -1;
  199. }
  200. return 0;
  201. }
  202. void JackPort::ClearBuffer(jack_nframes_t frames)
  203. {
  204. const JackPortType* type = GetPortType(fTypeId);
  205. (type->init)(fBuffer, BUFFER_SIZE_MAX * sizeof(float), frames);
  206. }
  207. void JackPort::MixBuffers(void** src_buffers, int src_count, jack_nframes_t buffer_size)
  208. {
  209. const JackPortType* type = GetPortType(fTypeId);
  210. (type->mixdown)(fBuffer, src_buffers, src_count, buffer_size);
  211. }
  212. } // end of namespace