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.

289 lines
5.8KB

  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 <stdio.h>
  19. namespace Jack
  20. {
  21. JackPort::JackPort()
  22. : fFlags(JackPortIsInput), fRefNum( -1), fLatency(0), fMonitorRequests(0), fInUse(false), fLocked(false), fTied(NO_PORT)
  23. {}
  24. JackPort::~JackPort()
  25. {}
  26. void JackPort::Allocate(int refnum, const char* port_name, JackPortFlags flags)
  27. {
  28. fFlags = flags;
  29. fRefNum = refnum;
  30. strcpy(fName, port_name);
  31. memset(fBuffer, 0, BUFFER_SIZE_MAX * sizeof(float));
  32. fInUse = true;
  33. fLocked = false;
  34. fLatency = 0;
  35. fTied = NO_PORT;
  36. }
  37. void JackPort::Release()
  38. {
  39. fFlags = JackPortIsInput;
  40. fRefNum = -1;
  41. fInUse = false;
  42. fLocked = false;
  43. fLatency = 0;
  44. fTied = NO_PORT;
  45. fAlias1[0] = '\0';
  46. fAlias2[0] = '\0';
  47. }
  48. bool JackPort::IsUsed() const
  49. {
  50. return fInUse;
  51. }
  52. float* JackPort::GetBuffer()
  53. {
  54. return fBuffer;
  55. }
  56. int JackPort::GetRefNum() const
  57. {
  58. return fRefNum;
  59. }
  60. int JackPort::Lock()
  61. {
  62. fLocked = true;
  63. return 0;
  64. }
  65. int JackPort::Unlock()
  66. {
  67. fLocked = false;
  68. return 0;
  69. }
  70. jack_nframes_t JackPort::GetLatency() const
  71. {
  72. return fLatency;
  73. }
  74. void JackPort::SetLatency(jack_nframes_t nframes)
  75. {
  76. fLatency = nframes;
  77. }
  78. int JackPort::Tie(jack_port_id_t port_index)
  79. {
  80. fTied = port_index;
  81. return 0;
  82. }
  83. int JackPort::UnTie()
  84. {
  85. fTied = NO_PORT;
  86. return 0;
  87. }
  88. int JackPort::RequestMonitor(bool onoff)
  89. {
  90. /**
  91. jackd.h
  92. * If @ref JackPortCanMonitor is set for this @a port, turn input
  93. * monitoring on or off. Otherwise, do nothing.
  94. if (!(fFlags & JackPortCanMonitor))
  95. return -1;
  96. */
  97. if (onoff) {
  98. fMonitorRequests++;
  99. } else if (fMonitorRequests) {
  100. fMonitorRequests--;
  101. }
  102. return 0;
  103. }
  104. int JackPort::EnsureMonitor(bool onoff)
  105. {
  106. /**
  107. jackd.h
  108. * If @ref JackPortCanMonitor is set for this @a port, turn input
  109. * monitoring on or off. Otherwise, do nothing.
  110. if (!(fFlags & JackPortCanMonitor))
  111. return -1;
  112. */
  113. if (onoff) {
  114. if (fMonitorRequests == 0) {
  115. fMonitorRequests++;
  116. }
  117. } else {
  118. if (fMonitorRequests > 0) {
  119. fMonitorRequests = 0;
  120. }
  121. }
  122. return 0;
  123. }
  124. bool JackPort::MonitoringInput()
  125. {
  126. return (fMonitorRequests > 0);
  127. }
  128. const char* JackPort::GetName() const
  129. {
  130. return fName;
  131. }
  132. const char* JackPort::GetShortName() const
  133. {
  134. /* we know there is always a colon, because we put
  135. it there ...
  136. */
  137. return strchr(fName, ':') + 1;
  138. }
  139. int JackPort::Flags() const
  140. {
  141. return fFlags;
  142. }
  143. const char* JackPort::Type() const
  144. {
  145. // TO IMPROVE
  146. return JACK_DEFAULT_AUDIO_TYPE;
  147. }
  148. int JackPort::SetName(const char* new_name)
  149. {
  150. char* colon = strchr(fName, ':');
  151. int len = sizeof(fName) - ((int) (colon - fName)) - 2;
  152. snprintf(colon + 1, len, "%s", new_name);
  153. return 0;
  154. }
  155. int JackPort::SetFullName(const char* new_name)
  156. {
  157. assert(strlen(new_name) < JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE);
  158. strcpy(fName, new_name);
  159. return 0;
  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::MixBuffer(float* mixbuffer, float* buffer, jack_nframes_t frames)
  203. {
  204. jack_nframes_t frames_group = frames / 4;
  205. frames = frames % 4;
  206. while (frames_group > 0) {
  207. register float mixFloat1 = *mixbuffer;
  208. register float sourceFloat1 = *buffer;
  209. register float mixFloat2 = *(mixbuffer + 1);
  210. register float sourceFloat2 = *(buffer + 1);
  211. register float mixFloat3 = *(mixbuffer + 2);
  212. register float sourceFloat3 = *(buffer + 2);
  213. register float mixFloat4 = *(mixbuffer + 3);
  214. register float sourceFloat4 = *(buffer + 3);
  215. buffer += 4;
  216. frames_group--;
  217. mixFloat1 += sourceFloat1;
  218. mixFloat2 += sourceFloat2;
  219. mixFloat3 += sourceFloat3;
  220. mixFloat4 += sourceFloat4;
  221. *mixbuffer = mixFloat1;
  222. *(mixbuffer + 1) = mixFloat2;
  223. *(mixbuffer + 2) = mixFloat3;
  224. *(mixbuffer + 3) = mixFloat4;
  225. mixbuffer += 4;
  226. }
  227. while (frames > 0) {
  228. register float mixFloat1 = *mixbuffer;
  229. register float sourceFloat1 = *buffer;
  230. buffer++;
  231. frames--;
  232. mixFloat1 += sourceFloat1;
  233. *mixbuffer = mixFloat1;
  234. mixbuffer++;
  235. }
  236. }
  237. } // end of namespace