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.

265 lines
5.4KB

  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), fTotalLatency(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. fTotalLatency = 0;
  40. fTied = NO_PORT;
  41. // DB: At this point we do not know current buffer size in frames,
  42. // but every time buffer will be returned to any user,
  43. // it will be called with either ClearBuffer or MixBuffers
  44. // with correct current buffer size.
  45. // So it is safe to init with 0 here.
  46. ClearBuffer(0);
  47. return true;
  48. }
  49. void JackPort::Release()
  50. {
  51. fTypeId = 0;
  52. fFlags = JackPortIsInput;
  53. fRefNum = -1;
  54. fInUse = false;
  55. fLatency = 0;
  56. fTotalLatency = 0;
  57. fTied = NO_PORT;
  58. fAlias1[0] = '\0';
  59. fAlias2[0] = '\0';
  60. }
  61. bool JackPort::IsUsed() const
  62. {
  63. return fInUse;
  64. }
  65. float* JackPort::GetBuffer()
  66. {
  67. return fBuffer;
  68. }
  69. int JackPort::GetRefNum() const
  70. {
  71. return fRefNum;
  72. }
  73. jack_nframes_t JackPort::GetLatency() const
  74. {
  75. return fLatency;
  76. }
  77. jack_nframes_t JackPort::GetTotalLatency() const
  78. {
  79. return fTotalLatency;
  80. }
  81. void JackPort::SetLatency(jack_nframes_t nframes)
  82. {
  83. fLatency = nframes;
  84. }
  85. int JackPort::Tie(jack_port_id_t port_index)
  86. {
  87. fTied = port_index;
  88. return 0;
  89. }
  90. int JackPort::UnTie()
  91. {
  92. fTied = NO_PORT;
  93. return 0;
  94. }
  95. int JackPort::RequestMonitor(bool onoff)
  96. {
  97. /**
  98. jackd.h
  99. * If @ref JackPortCanMonitor is set for this @a port, turn input
  100. * monitoring on or off. Otherwise, do nothing.
  101. if (!(fFlags & JackPortCanMonitor))
  102. return -1;
  103. */
  104. if (onoff) {
  105. fMonitorRequests++;
  106. } else if (fMonitorRequests) {
  107. fMonitorRequests--;
  108. }
  109. return 0;
  110. }
  111. int JackPort::EnsureMonitor(bool onoff)
  112. {
  113. /**
  114. jackd.h
  115. * If @ref JackPortCanMonitor is set for this @a port, turn input
  116. * monitoring on or off. Otherwise, do nothing.
  117. if (!(fFlags & JackPortCanMonitor))
  118. return -1;
  119. */
  120. if (onoff) {
  121. if (fMonitorRequests == 0) {
  122. fMonitorRequests++;
  123. }
  124. } else {
  125. if (fMonitorRequests > 0) {
  126. fMonitorRequests = 0;
  127. }
  128. }
  129. return 0;
  130. }
  131. bool JackPort::MonitoringInput()
  132. {
  133. return (fMonitorRequests > 0);
  134. }
  135. const char* JackPort::GetName() const
  136. {
  137. return fName;
  138. }
  139. const char* JackPort::GetShortName() const
  140. {
  141. /* we know there is always a colon, because we put
  142. it there ...
  143. */
  144. return strchr(fName, ':') + 1;
  145. }
  146. int JackPort::GetFlags() const
  147. {
  148. return fFlags;
  149. }
  150. const char* JackPort::GetType() const
  151. {
  152. const JackPortType* type = GetPortType(fTypeId);
  153. return type->name;
  154. }
  155. int JackPort::SetName(const char* new_name)
  156. {
  157. char* colon = strchr(fName, ':');
  158. int len = sizeof(fName) - ((int) (colon - fName)) - 2;
  159. snprintf(colon + 1, len, "%s", new_name);
  160. return 0;
  161. }
  162. void JackPort::Rename(const char* name, int index)
  163. {
  164. SetAlias(GetName());
  165. snprintf(fName, sizeof(fName) - 1, name, index);
  166. }
  167. bool JackPort::NameEquals(const char* target)
  168. {
  169. return (strcmp(fName, target) == 0
  170. || strcmp(fAlias1, target) == 0
  171. || strcmp(fAlias2, target) == 0);
  172. }
  173. int JackPort::GetAliases(char* const aliases[2])
  174. {
  175. int cnt = 0;
  176. if (fAlias1[0] != '\0') {
  177. snprintf(aliases[0], JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE, "%s", fAlias1);
  178. cnt++;
  179. }
  180. if (fAlias2[0] != '\0') {
  181. snprintf(aliases[1], JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE, "%s", fAlias2);
  182. cnt++;
  183. }
  184. return cnt;
  185. }
  186. int JackPort::SetAlias(const char* alias)
  187. {
  188. if (fAlias1[0] == '\0') {
  189. snprintf(fAlias1, sizeof(fAlias1), "%s", alias);
  190. } else if (fAlias2[0] == '\0') {
  191. snprintf(fAlias2, sizeof(fAlias2), "%s", alias);
  192. } else {
  193. return -1;
  194. }
  195. return 0;
  196. }
  197. int JackPort::UnsetAlias(const char* alias)
  198. {
  199. if (strcmp(fAlias1, alias) == 0) {
  200. fAlias1[0] = '\0';
  201. } else if (strcmp(fAlias2, alias) == 0) {
  202. fAlias2[0] = '\0';
  203. } else {
  204. return -1;
  205. }
  206. return 0;
  207. }
  208. void JackPort::ClearBuffer(jack_nframes_t frames)
  209. {
  210. const JackPortType* type = GetPortType(fTypeId);
  211. (type->init)(fBuffer, BUFFER_SIZE_MAX * sizeof(float), frames);
  212. }
  213. void JackPort::MixBuffers(void** src_buffers, int src_count, jack_nframes_t buffer_size)
  214. {
  215. const JackPortType* type = GetPortType(fTypeId);
  216. (type->mixdown)(fBuffer, src_buffers, src_count, buffer_size);
  217. }
  218. } // end of namespace