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.

230 lines
4.7KB

  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. }
  46. bool JackPort::IsUsed() const
  47. {
  48. return fInUse;
  49. }
  50. float* JackPort::GetBuffer()
  51. {
  52. return fBuffer;
  53. }
  54. int JackPort::GetRefNum() const
  55. {
  56. return fRefNum;
  57. }
  58. int JackPort::Lock()
  59. {
  60. fLocked = true;
  61. return 0;
  62. }
  63. int JackPort::Unlock()
  64. {
  65. fLocked = false;
  66. return 0;
  67. }
  68. jack_nframes_t JackPort::GetLatency() const
  69. {
  70. return fLatency;
  71. }
  72. void JackPort::SetLatency(jack_nframes_t nframes)
  73. {
  74. fLatency = nframes;
  75. }
  76. int JackPort::Tie(jack_port_id_t port_index)
  77. {
  78. fTied = port_index;
  79. return 0;
  80. }
  81. int JackPort::UnTie()
  82. {
  83. fTied = NO_PORT;
  84. return 0;
  85. }
  86. int JackPort::RequestMonitor(bool onoff)
  87. {
  88. /**
  89. jackd.h
  90. * If @ref JackPortCanMonitor is set for this @a port, turn input
  91. * monitoring on or off. Otherwise, do nothing.
  92. if (!(fFlags & JackPortCanMonitor))
  93. return -1;
  94. */
  95. if (onoff) {
  96. fMonitorRequests++;
  97. } else if (fMonitorRequests) {
  98. fMonitorRequests--;
  99. }
  100. return 0;
  101. }
  102. int JackPort::EnsureMonitor(bool onoff)
  103. {
  104. /**
  105. jackd.h
  106. * If @ref JackPortCanMonitor is set for this @a port, turn input
  107. * monitoring on or off. Otherwise, do nothing.
  108. if (!(fFlags & JackPortCanMonitor))
  109. return -1;
  110. */
  111. if (onoff) {
  112. if (fMonitorRequests == 0) {
  113. fMonitorRequests++;
  114. }
  115. } else {
  116. if (fMonitorRequests > 0) {
  117. fMonitorRequests = 0;
  118. }
  119. }
  120. return 0;
  121. }
  122. bool JackPort::MonitoringInput()
  123. {
  124. return (fMonitorRequests > 0);
  125. }
  126. const char* JackPort::GetName() const
  127. {
  128. return fName;
  129. }
  130. const char* JackPort::GetShortName() const
  131. {
  132. /* we know there is always a colon, because we put
  133. it there ...
  134. */
  135. return strchr(fName, ':') + 1;
  136. }
  137. int JackPort::Flags() const
  138. {
  139. return fFlags;
  140. }
  141. const char* JackPort::Type() const
  142. {
  143. // TO IMPROVE
  144. return "Audio";
  145. }
  146. int JackPort::SetName(const char* new_name)
  147. {
  148. char* colon = strchr(fName, ':');
  149. int len = sizeof(fName) - ((int) (colon - fName)) - 2;
  150. snprintf(colon + 1, len, "%s", new_name);
  151. return 0;
  152. }
  153. void JackPort::MixBuffer(float* mixbuffer, float* buffer, jack_nframes_t frames)
  154. {
  155. jack_nframes_t frames_group = frames / 4;
  156. frames = frames % 4;
  157. while (frames_group > 0) {
  158. register float mixFloat1 = *mixbuffer;
  159. register float sourceFloat1 = *buffer;
  160. register float mixFloat2 = *(mixbuffer + 1);
  161. register float sourceFloat2 = *(buffer + 1);
  162. register float mixFloat3 = *(mixbuffer + 2);
  163. register float sourceFloat3 = *(buffer + 2);
  164. register float mixFloat4 = *(mixbuffer + 3);
  165. register float sourceFloat4 = *(buffer + 3);
  166. buffer += 4;
  167. frames_group--;
  168. mixFloat1 += sourceFloat1;
  169. mixFloat2 += sourceFloat2;
  170. mixFloat3 += sourceFloat3;
  171. mixFloat4 += sourceFloat4;
  172. *mixbuffer = mixFloat1;
  173. *(mixbuffer + 1) = mixFloat2;
  174. *(mixbuffer + 2) = mixFloat3;
  175. *(mixbuffer + 3) = mixFloat4;
  176. mixbuffer += 4;
  177. }
  178. while (frames > 0) {
  179. register float mixFloat1 = *mixbuffer;
  180. register float sourceFloat1 = *buffer;
  181. buffer++;
  182. frames--;
  183. mixFloat1 += sourceFloat1;
  184. *mixbuffer = mixFloat1;
  185. mixbuffer++;
  186. }
  187. }
  188. } // end of namespace