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.

272 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), fMonitorRequests(0), fInUse(false), fLocked(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. fLocked = false;
  39. fLatency = 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. fLocked = false;
  56. fLatency = 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. int JackPort::Lock()
  74. {
  75. fLocked = true;
  76. return 0;
  77. }
  78. int JackPort::Unlock()
  79. {
  80. fLocked = false;
  81. return 0;
  82. }
  83. jack_nframes_t JackPort::GetLatency() const
  84. {
  85. return fLatency;
  86. }
  87. void JackPort::SetLatency(jack_nframes_t nframes)
  88. {
  89. fLatency = nframes;
  90. }
  91. int JackPort::Tie(jack_port_id_t port_index)
  92. {
  93. fTied = port_index;
  94. return 0;
  95. }
  96. int JackPort::UnTie()
  97. {
  98. fTied = NO_PORT;
  99. return 0;
  100. }
  101. int JackPort::RequestMonitor(bool onoff)
  102. {
  103. /**
  104. jackd.h
  105. * If @ref JackPortCanMonitor is set for this @a port, turn input
  106. * monitoring on or off. Otherwise, do nothing.
  107. if (!(fFlags & JackPortCanMonitor))
  108. return -1;
  109. */
  110. if (onoff) {
  111. fMonitorRequests++;
  112. } else if (fMonitorRequests) {
  113. fMonitorRequests--;
  114. }
  115. return 0;
  116. }
  117. int JackPort::EnsureMonitor(bool onoff)
  118. {
  119. /**
  120. jackd.h
  121. * If @ref JackPortCanMonitor is set for this @a port, turn input
  122. * monitoring on or off. Otherwise, do nothing.
  123. if (!(fFlags & JackPortCanMonitor))
  124. return -1;
  125. */
  126. if (onoff) {
  127. if (fMonitorRequests == 0) {
  128. fMonitorRequests++;
  129. }
  130. } else {
  131. if (fMonitorRequests > 0) {
  132. fMonitorRequests = 0;
  133. }
  134. }
  135. return 0;
  136. }
  137. bool JackPort::MonitoringInput()
  138. {
  139. return (fMonitorRequests > 0);
  140. }
  141. const char* JackPort::GetName() const
  142. {
  143. return fName;
  144. }
  145. const char* JackPort::GetShortName() const
  146. {
  147. /* we know there is always a colon, because we put
  148. it there ...
  149. */
  150. return strchr(fName, ':') + 1;
  151. }
  152. int JackPort::Flags() const
  153. {
  154. return fFlags;
  155. }
  156. const char* JackPort::Type() const
  157. {
  158. const JackPortType* type = GetPortType(fTypeId);
  159. return type->name;
  160. }
  161. int JackPort::SetName(const char* new_name)
  162. {
  163. char* colon = strchr(fName, ':');
  164. int len = sizeof(fName) - ((int) (colon - fName)) - 2;
  165. snprintf(colon + 1, len, "%s", new_name);
  166. return 0;
  167. }
  168. void JackPort::Rename(const char* name, int index)
  169. {
  170. SetAlias(GetName());
  171. snprintf(fName, sizeof(fName) - 1, name, index);
  172. }
  173. bool JackPort::NameEquals(const char* target)
  174. {
  175. return (strcmp(fName, target) == 0
  176. || strcmp(fAlias1, target) == 0
  177. || strcmp(fAlias2, target) == 0);
  178. }
  179. int JackPort::GetAliases(char* const aliases[2])
  180. {
  181. int cnt = 0;
  182. if (fAlias1[0] != '\0') {
  183. snprintf(aliases[0], JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE, "%s", fAlias1);
  184. cnt++;
  185. }
  186. if (fAlias2[0] != '\0') {
  187. snprintf(aliases[1], JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE, "%s", fAlias2);
  188. cnt++;
  189. }
  190. return cnt;
  191. }
  192. int JackPort::SetAlias(const char* alias)
  193. {
  194. if (fAlias1[0] == '\0') {
  195. snprintf(fAlias1, sizeof(fAlias1), "%s", alias);
  196. } else if (fAlias2[0] == '\0') {
  197. snprintf(fAlias2, sizeof(fAlias2), "%s", alias);
  198. } else {
  199. return -1;
  200. }
  201. return 0;
  202. }
  203. int JackPort::UnsetAlias(const char* alias)
  204. {
  205. if (strcmp(fAlias1, alias) == 0) {
  206. fAlias1[0] = '\0';
  207. } else if (strcmp(fAlias2, alias) == 0) {
  208. fAlias2[0] = '\0';
  209. } else {
  210. return -1;
  211. }
  212. return 0;
  213. }
  214. void JackPort::ClearBuffer(jack_nframes_t frames)
  215. {
  216. const JackPortType* type = GetPortType(fTypeId);
  217. (type->init)(fBuffer, BUFFER_SIZE_MAX * sizeof(float), frames);
  218. }
  219. void JackPort::MixBuffers(void** src_buffers, int src_count, jack_nframes_t buffer_size)
  220. {
  221. const JackPortType* type = GetPortType(fTypeId);
  222. (type->mixdown)(fBuffer, src_buffers, src_count, buffer_size);
  223. }
  224. } // end of namespace