jack1 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.

982 lines
23KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. Copyright (C) 2005 Jussi Laako
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. $Id$
  16. */
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <math.h>
  20. #include <config.h>
  21. #include <jack/jack.h>
  22. #include <jack/types.h>
  23. #include <jack/internal.h>
  24. #include <jack/engine.h>
  25. #include <jack/pool.h>
  26. #include <jack/port.h>
  27. #include <jack/midiport.h>
  28. #include <jack/jslist.h>
  29. #include <jack/intsimd.h>
  30. #include "local.h"
  31. static void jack_generic_buffer_init(void *port_buffer,
  32. size_t buffer_size);
  33. static void jack_audio_port_mixdown (jack_port_t *port,
  34. jack_nframes_t nframes);
  35. /* These function pointers are local to each address space. For
  36. * internal clients they reside within jackd; for external clients in
  37. * the application process. */
  38. jack_port_functions_t jack_builtin_audio_functions = {
  39. .buffer_init = jack_generic_buffer_init,
  40. .mixdown = jack_audio_port_mixdown,
  41. };
  42. extern jack_port_functions_t jack_builtin_midi_functions;
  43. jack_port_functions_t jack_builtin_NULL_functions = {
  44. .buffer_init = jack_generic_buffer_init,
  45. .mixdown = NULL,
  46. };
  47. /* Only the Audio and MIDI port types are currently built in. */
  48. jack_port_type_info_t jack_builtin_port_types[] = {
  49. { .type_name = JACK_DEFAULT_AUDIO_TYPE,
  50. .buffer_scale_factor = 1,
  51. },
  52. { .type_name = JACK_DEFAULT_MIDI_TYPE,
  53. .buffer_scale_factor = 1,
  54. },
  55. { .type_name = "", }
  56. };
  57. /* these functions have been taken from libDSP X86.c -jl */
  58. #ifdef USE_DYNSIMD
  59. static void (*opt_copy) (float *, const float *, int);
  60. static void (*opt_mix) (float *, const float *, int);
  61. static void
  62. gen_copyf (float *dest, const float *src, int length)
  63. {
  64. memcpy(dest, src, length * sizeof(float));
  65. }
  66. static void
  67. gen_mixf (float *dest, const float *src, int length)
  68. {
  69. int n;
  70. n = length;
  71. while (n--)
  72. *dest++ += *src++;
  73. /*for (iSample = 0; iSample < iDataLength; iSample++)
  74. fpDest[iSample] += fpSrc[iSample];*/
  75. }
  76. #ifdef ARCH_X86
  77. static void
  78. x86_3dnow_add2f (float *dest, const float *src, int length)
  79. {
  80. int i, n;
  81. pv2sf m64p_dest = (pv2sf) dest;
  82. pv2sf m64p_src = (pv2sf) src;
  83. n = (length >> 1);
  84. for (i = 0; i < n; i++)
  85. {
  86. asm volatile (
  87. "movq %1, %%mm0\n\t" \
  88. "pfadd %2, %%mm0\n\t" \
  89. "movq %%mm0, %0\n\t"
  90. : "=m" (m64p_dest[i])
  91. : "m0" (m64p_dest[i]),
  92. "m" (m64p_src[i])
  93. : "mm0", "memory");
  94. }
  95. if (n & 0x1)
  96. {
  97. asm volatile (
  98. "movd %1, %%mm0\n\t" \
  99. "movd %2, %%mm1\n\t" \
  100. "pfadd %%mm1, %%mm0\n\t" \
  101. "movd %%mm0, %0\n\t"
  102. : "=m" (dest[length - 1])
  103. : "m0" (dest[length - 1]),
  104. "m" (src[length - 1])
  105. : "mm0", "mm1", "memory");
  106. }
  107. asm volatile (
  108. "femms\n\t" \
  109. "sfence\n\t");
  110. }
  111. static void
  112. x86_3dnow_copyf (float *dest, const float *src, int length)
  113. {
  114. int i, n1, n2;
  115. pv2sf m64p_src = (pv2sf) src;
  116. pv2sf m64p_dest = (pv2sf) dest;
  117. n1 = (length >> 4);
  118. n2 = ((length & 0xf) >> 1);
  119. for (i = 0; i < n1; i++)
  120. {
  121. asm volatile ("movq %0, %%mm0\n\t"
  122. : : "m" (*m64p_src++) : "mm0", "memory");
  123. asm volatile ("movq %0, %%mm1\n\t"
  124. : : "m" (*m64p_src++) : "mm1", "memory");
  125. asm volatile ("movq %0, %%mm2\n\t"
  126. : : "m" (*m64p_src++) : "mm2", "memory");
  127. asm volatile ("movq %0, %%mm3\n\t"
  128. : : "m" (*m64p_src++) : "mm3", "memory");
  129. asm volatile ("movq %0, %%mm4\n\t"
  130. : : "m" (*m64p_src++) : "mm4", "memory");
  131. asm volatile ("movq %0, %%mm5\n\t"
  132. : : "m" (*m64p_src++) : "mm5", "memory");
  133. asm volatile ("movq %0, %%mm6\n\t"
  134. : : "m" (*m64p_src++) : "mm6", "memory");
  135. asm volatile ("movq %0, %%mm7\n\t"
  136. : : "m" (*m64p_src++) : "xmm7", "memory");
  137. asm volatile ("movq %%mm0, %0\n\t"
  138. : "=m" (*m64p_dest++) : : "mm0", "memory");
  139. asm volatile ("movq %%mm1, %0\n\t"
  140. : "=m" (*m64p_dest++) : : "mm1", "memory");
  141. asm volatile ("movq %%mm2, %0\n\t"
  142. : "=m" (*m64p_dest++) : : "mm2", "memory");
  143. asm volatile ("movq %%mm3, %0\n\t"
  144. : "=m" (*m64p_dest++) : : "mm3", "memory");
  145. asm volatile ("movq %%mm4, %0\n\t"
  146. : "=m" (*m64p_dest++) : : "mm4", "memory");
  147. asm volatile ("movq %%mm5, %0\n\t"
  148. : "=m" (*m64p_dest++) : : "mm5", "memory");
  149. asm volatile ("movq %%mm6, %0\n\t"
  150. : "=m" (*m64p_dest++) : : "mm6", "memory");
  151. asm volatile ("movq %%mm7, %0\n\t"
  152. : "=m" (*m64p_dest++) : : "mm7", "memory");
  153. }
  154. for (i = 0; i < n2; i++)
  155. {
  156. asm volatile (
  157. "movq %1, %%mm0\n\t" \
  158. "movq %%mm0, %0\n\t"
  159. : "=m" (*m64p_dest++)
  160. : "m" (*m64p_src++)
  161. : "mm0", "memory");
  162. }
  163. if (length & 0x1)
  164. {
  165. asm volatile (
  166. "movd %1, %%mm0\n\t" \
  167. "movd %%mm0, %0\n\t"
  168. : "=m" (dest[length - 1])
  169. : "m" (src[length - 1])
  170. : "mm0", "memory");
  171. }
  172. asm volatile (
  173. "femms\n\t" \
  174. "sfence\n\t");
  175. }
  176. static void
  177. x86_sse_copyf (float *dest, const float *src, int length)
  178. {
  179. int i, n1, n2, si3;
  180. pv4sf m128p_src = (pv4sf) src;
  181. pv4sf m128p_dest = (pv4sf) dest;
  182. n1 = (length >> 5);
  183. n2 = ((length & 0x1f) >> 2);
  184. si3 = (length & ~0x3);
  185. for (i = 0; i < n1; i++)
  186. {
  187. asm volatile ("movaps %0, %%xmm0\n\t"
  188. : : "m" (*m128p_src++) : "xmm0", "memory");
  189. asm volatile ("movaps %0, %%xmm1\n\t"
  190. : : "m" (*m128p_src++) : "xmm1", "memory");
  191. asm volatile ("movaps %0, %%xmm2\n\t"
  192. : : "m" (*m128p_src++) : "xmm2", "memory");
  193. asm volatile ("movaps %0, %%xmm3\n\t"
  194. : : "m" (*m128p_src++) : "xmm3", "memory");
  195. asm volatile ("movaps %0, %%xmm4\n\t"
  196. : : "m" (*m128p_src++) : "xmm4", "memory");
  197. asm volatile ("movaps %0, %%xmm5\n\t"
  198. : : "m" (*m128p_src++) : "xmm5", "memory");
  199. asm volatile ("movaps %0, %%xmm6\n\t"
  200. : : "m" (*m128p_src++) : "xmm6", "memory");
  201. asm volatile ("movaps %0, %%xmm7\n\t"
  202. : : "m" (*m128p_src++) : "xmm7", "memory");
  203. asm volatile ("movaps %%xmm0, %0\n\t"
  204. : "=m" (*m128p_dest++) : : "xmm0", "memory");
  205. asm volatile ("movaps %%xmm1, %0\n\t"
  206. : "=m" (*m128p_dest++) : : "xmm1", "memory");
  207. asm volatile ("movaps %%xmm2, %0\n\t"
  208. : "=m" (*m128p_dest++) : : "xmm2", "memory");
  209. asm volatile ("movaps %%xmm3, %0\n\t"
  210. : "=m" (*m128p_dest++) : : "xmm3", "memory");
  211. asm volatile ("movaps %%xmm4, %0\n\t"
  212. : "=m" (*m128p_dest++) : : "xmm4", "memory");
  213. asm volatile ("movaps %%xmm5, %0\n\t"
  214. : "=m" (*m128p_dest++) : : "xmm5", "memory");
  215. asm volatile ("movaps %%xmm6, %0\n\t"
  216. : "=m" (*m128p_dest++) : : "xmm6", "memory");
  217. asm volatile ("movaps %%xmm7, %0\n\t"
  218. : "=m" (*m128p_dest++) : : "xmm7", "memory");
  219. }
  220. for (i = 0; i < n2; i++)
  221. {
  222. asm volatile (
  223. "movaps %1, %%xmm0\n\t" \
  224. "movaps %%xmm0, %0\n\t"
  225. : "=m" (*m128p_dest++)
  226. : "m" (*m128p_src++)
  227. : "xmm0", "memory");
  228. }
  229. for (i = si3; i < length; i++)
  230. {
  231. asm volatile (
  232. "movss %1, %%xmm0\n\t" \
  233. "movss %%xmm0, %0\n\t"
  234. : "=m" (dest[i])
  235. : "m" (src[i])
  236. : "xmm0", "memory");
  237. }
  238. }
  239. static void
  240. x86_sse_add2f (float *dest, const float *src, int length)
  241. {
  242. int i, n, si2;
  243. pv4sf m128p_src = (pv4sf) src;
  244. pv4sf m128p_dest = (pv4sf) dest;
  245. if (__builtin_expect(((long) src & 0xf) || ((long) dest & 0xf), 0))
  246. {
  247. /*fprintf(stderr, "x86_sse_add2f(): non aligned pointers!\n");*/
  248. si2 = 0;
  249. goto sse_nonalign;
  250. }
  251. si2 = (length & ~0x3);
  252. n = (length >> 2);
  253. for (i = 0; i < n; i++)
  254. {
  255. asm volatile (
  256. "movaps %1, %%xmm0\n\t" \
  257. "addps %2, %%xmm0\n\t" \
  258. "movaps %%xmm0, %0\n\t"
  259. : "=m" (m128p_dest[i])
  260. : "m0" (m128p_dest[i]),
  261. "m" (m128p_src[i])
  262. : "xmm0", "memory");
  263. }
  264. sse_nonalign:
  265. for (i = si2; i < length; i++)
  266. {
  267. asm volatile (
  268. "movss %1, %%xmm0\n\t" \
  269. "addss %2, %%xmm0\n\t" \
  270. "movss %%xmm0, %0\n\t"
  271. : "=m" (dest[i])
  272. : "m0" (dest[i]),
  273. "m" (src[i])
  274. : "xmm0", "memory");
  275. }
  276. }
  277. void jack_port_set_funcs ()
  278. {
  279. if (ARCH_X86_HAVE_SSE2(cpu_type)) {
  280. opt_copy = x86_sse_copyf;
  281. opt_mix = x86_sse_add2f;
  282. }
  283. else if (ARCH_X86_HAVE_3DNOW(cpu_type)) {
  284. opt_copy = x86_3dnow_copyf;
  285. opt_mix = x86_3dnow_add2f;
  286. }
  287. else {
  288. opt_copy = gen_copyf;
  289. opt_mix = gen_mixf;
  290. }
  291. }
  292. #else /* ARCH_X86 */
  293. void jack_port_set_funcs ()
  294. {
  295. opt_copy = gen_copyf;
  296. opt_mix = gen_mixf;
  297. }
  298. #endif /* ARCH_X86 */
  299. #endif /* USE_DYNSIMD */
  300. jack_port_functions_t *
  301. jack_get_port_functions(jack_port_type_id_t ptid)
  302. {
  303. switch (ptid) {
  304. case JACK_AUDIO_PORT_TYPE:
  305. return &jack_builtin_audio_functions;
  306. case JACK_MIDI_PORT_TYPE:
  307. return &jack_builtin_midi_functions;
  308. /* no other builtin functions */
  309. default:
  310. return NULL;
  311. }
  312. }
  313. /*
  314. * Fills buffer with zeroes. For audio ports, engine->silent_buffer relies on it.
  315. */
  316. static void
  317. jack_generic_buffer_init(void *buffer, size_t size)
  318. {
  319. memset(buffer, 0, size);
  320. }
  321. jack_port_t *
  322. jack_port_new (const jack_client_t *client, jack_port_id_t port_id,
  323. jack_control_t *control)
  324. {
  325. jack_port_shared_t *shared = &control->ports[port_id];
  326. jack_port_type_id_t ptid = shared->ptype_id;
  327. jack_port_t *port = (jack_port_t *) malloc (sizeof (jack_port_t));
  328. port->mix_buffer = NULL;
  329. port->client_segment_base = NULL;
  330. port->shared = shared;
  331. port->type_info = &client->engine->port_types[ptid];
  332. pthread_mutex_init (&port->connection_lock, NULL);
  333. port->connections = 0;
  334. port->tied = NULL;
  335. if (client->control->id == port->shared->client_id) {
  336. /* It's our port, so initialize the pointers to port
  337. * functions within this address space. These builtin
  338. * definitions can be overridden by the client.
  339. */
  340. jack_port_functions_t *port_functions = jack_get_port_functions(ptid);
  341. if (port_functions == NULL)
  342. port_functions = &jack_builtin_NULL_functions;
  343. port->fptr = *port_functions;
  344. port->shared->has_mixdown = (port->fptr.mixdown ? TRUE : FALSE);
  345. }
  346. /* set up a base address so that port->offset can be used to
  347. compute the correct location. we don't store the location
  348. directly, because port->client_segment_base and/or
  349. port->offset can change if the buffer size or port counts
  350. are changed.
  351. */
  352. port->client_segment_base =
  353. (void **) &client->port_segment[ptid].attached_at;
  354. return port;
  355. }
  356. jack_port_t *
  357. jack_port_register (jack_client_t *client,
  358. const char *port_name,
  359. const char *port_type,
  360. unsigned long flags,
  361. unsigned long buffer_size)
  362. {
  363. jack_request_t req;
  364. jack_port_t *port = 0;
  365. int length ;
  366. req.type = RegisterPort;
  367. length = strlen ((const char *) client->control->name)
  368. + 1 + strlen (port_name);
  369. if ( length >= sizeof (req.x.port_info.name) ) {
  370. jack_error ("\"%s:%s\" is too long to be used as a JACK port name.\n"
  371. "Please use %lu characters or less.",
  372. client->control->name ,
  373. port_name,
  374. sizeof (req.x.port_info.name) - 1);
  375. return NULL ;
  376. }
  377. strcpy ((char *) req.x.port_info.name,
  378. (const char *) client->control->name);
  379. strcat ((char *) req.x.port_info.name, ":");
  380. strcat ((char *) req.x.port_info.name, port_name);
  381. snprintf (req.x.port_info.type, sizeof (req.x.port_info.type),
  382. "%s", port_type);
  383. req.x.port_info.flags = flags;
  384. req.x.port_info.buffer_size = buffer_size;
  385. req.x.port_info.client_id = client->control->id;
  386. if (jack_client_deliver_request (client, &req)) {
  387. jack_error ("cannot deliver port registration request");
  388. return NULL;
  389. }
  390. if ((port = jack_port_new (client, req.x.port_info.port_id,
  391. client->engine)) == NULL) {
  392. jack_error ("cannot allocate client side port structure");
  393. return NULL;
  394. }
  395. client->ports = jack_slist_prepend (client->ports, port);
  396. return port;
  397. }
  398. int
  399. jack_port_unregister (jack_client_t *client, jack_port_t *port)
  400. {
  401. jack_request_t req;
  402. req.type = UnRegisterPort;
  403. req.x.port_info.port_id = port->shared->id;
  404. req.x.port_info.client_id = client->control->id;
  405. return jack_client_deliver_request (client, &req);
  406. }
  407. /* LOCAL (in-client) connection querying only */
  408. int
  409. jack_port_connected (const jack_port_t *port)
  410. {
  411. return jack_slist_length (port->connections);
  412. }
  413. int
  414. jack_port_connected_to (const jack_port_t *port, const char *portname)
  415. {
  416. JSList *node;
  417. int ret = FALSE;
  418. /* XXX this really requires a cross-process lock
  419. so that ports/connections cannot go away
  420. while we are checking for them. that's hard,
  421. and has a non-trivial performance impact
  422. for jackd.
  423. */
  424. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  425. for (node = port->connections; node; node = jack_slist_next (node)) {
  426. jack_port_t *other_port = (jack_port_t *) node->data;
  427. if (strcmp (other_port->shared->name, portname) == 0) {
  428. ret = TRUE;
  429. break;
  430. }
  431. }
  432. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  433. return ret;
  434. }
  435. const char **
  436. jack_port_get_connections (const jack_port_t *port)
  437. {
  438. const char **ret = NULL;
  439. JSList *node;
  440. unsigned int n;
  441. /* XXX this really requires a cross-process lock
  442. so that ports/connections cannot go away
  443. while we are checking for them. that's hard,
  444. and has a non-trivial performance impact
  445. for jackd.
  446. */
  447. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  448. if (port->connections != NULL) {
  449. ret = (const char **)
  450. malloc (sizeof (char *)
  451. * (jack_slist_length (port->connections) + 1));
  452. for (n = 0, node = port->connections; node;
  453. node = jack_slist_next (node), ++n) {
  454. ret[n] = ((jack_port_t *) node->data)->shared->name;
  455. }
  456. ret[n] = NULL;
  457. }
  458. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  459. return ret;
  460. }
  461. /* SERVER-SIDE (all) connection querying */
  462. const char **
  463. jack_port_get_all_connections (const jack_client_t *client,
  464. const jack_port_t *port)
  465. {
  466. const char **ret;
  467. jack_request_t req;
  468. jack_port_t *tmp;
  469. unsigned int i;
  470. int need_free = FALSE;
  471. if (port == NULL) {
  472. return NULL;
  473. }
  474. req.type = GetPortConnections;
  475. req.x.port_info.name[0] = '\0';
  476. req.x.port_info.type[0] = '\0';
  477. req.x.port_info.flags = 0;
  478. req.x.port_info.buffer_size = 0;
  479. req.x.port_info.client_id = 0;
  480. req.x.port_info.port_id = port->shared->id;
  481. jack_client_deliver_request (client, &req);
  482. if (req.status != 0 || req.x.port_connections.nports == 0) {
  483. return NULL;
  484. }
  485. if (client->request_fd < 0) {
  486. /* internal client */
  487. return req.x.port_connections.ports;
  488. }
  489. ret = (const char **)
  490. malloc (sizeof (char *) * (req.x.port_connections.nports + 1));
  491. for (i = 0; i < req.x.port_connections.nports; ++i ) {
  492. jack_port_id_t port_id;
  493. if (read (client->request_fd, &port_id, sizeof (port_id))
  494. != sizeof (port_id)) {
  495. jack_error ("cannot read port id from server");
  496. return 0;
  497. }
  498. tmp = jack_port_by_id_int (client, port_id, &need_free);
  499. ret[i] = tmp->shared->name;
  500. if (need_free) {
  501. free (tmp);
  502. need_free = FALSE;
  503. }
  504. }
  505. ret[i] = NULL;
  506. return ret;
  507. }
  508. jack_port_t *
  509. jack_port_by_id_int (const jack_client_t *client, jack_port_id_t id, int* free)
  510. {
  511. JSList *node;
  512. for (node = client->ports; node; node = jack_slist_next (node)) {
  513. if (((jack_port_t *) node->data)->shared->id == id) {
  514. *free = FALSE;
  515. return (jack_port_t *) node->data;
  516. }
  517. }
  518. if (id >= client->engine->port_max)
  519. return NULL;
  520. if (client->engine->ports[id].in_use) {
  521. *free = TRUE;
  522. return jack_port_new (client, id, client->engine);
  523. }
  524. return NULL;
  525. }
  526. jack_port_t *
  527. jack_port_by_id (jack_client_t *client, jack_port_id_t id)
  528. {
  529. JSList *node;
  530. jack_port_t* port;
  531. int need_free = FALSE;
  532. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  533. port = node->data;
  534. if (port->shared->id == id) { // Found port, return the cached structure
  535. return port;
  536. }
  537. }
  538. // Otherwise possibly allocate a new port structure, keep it in the ports_ext list for later use
  539. port = jack_port_by_id_int (client,id,&need_free);
  540. if (port != NULL && need_free)
  541. client->ports_ext =
  542. jack_slist_prepend (client->ports_ext, port);
  543. return port;
  544. }
  545. jack_port_t *
  546. jack_port_by_name_int (jack_client_t *client, const char *port_name)
  547. {
  548. unsigned long i, limit;
  549. jack_port_shared_t *port;
  550. limit = client->engine->port_max;
  551. port = &client->engine->ports[0];
  552. for (i = 0; i < limit; i++) {
  553. if (port[i].in_use && strcmp (port[i].name, port_name) == 0) {
  554. return jack_port_new (client, port[i].id,
  555. client->engine);
  556. }
  557. }
  558. return NULL;
  559. }
  560. jack_port_t *
  561. jack_port_by_name (jack_client_t *client, const char *port_name)
  562. {
  563. JSList *node;
  564. jack_port_t* port;
  565. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  566. port = node->data;
  567. if (strcmp (port->shared->name, port_name) == 0) {
  568. /* Found port, return the cached structure. */
  569. return port;
  570. }
  571. }
  572. /* Otherwise allocate a new port structure, keep it in the
  573. * ports_ext list for later use. */
  574. port = jack_port_by_name_int (client, port_name);
  575. if (port != NULL)
  576. client->ports_ext =
  577. jack_slist_prepend (client->ports_ext, port);
  578. return port;
  579. }
  580. jack_nframes_t
  581. jack_port_get_latency (jack_port_t *port)
  582. {
  583. return port->shared->latency;
  584. }
  585. jack_nframes_t
  586. jack_port_get_total_latency (jack_client_t *client, jack_port_t *port)
  587. {
  588. return port->shared->total_latency;
  589. }
  590. void
  591. jack_port_set_latency (jack_port_t *port, jack_nframes_t nframes)
  592. {
  593. port->shared->latency = nframes;
  594. }
  595. void *
  596. jack_port_get_buffer (jack_port_t *port, jack_nframes_t nframes)
  597. {
  598. JSList *node, *next;
  599. /* Output port. The buffer was assigned by the engine
  600. when the port was registered.
  601. */
  602. if (port->shared->flags & JackPortIsOutput) {
  603. if (port->tied) {
  604. return jack_port_get_buffer (port->tied, nframes);
  605. }
  606. return jack_output_port_buffer (port);
  607. }
  608. /* Input port. Since this can only be called from the
  609. process() callback, and since no connections can be
  610. made/broken during this phase (enforced by the jack
  611. server), there is no need to take the connection lock here
  612. */
  613. if ((node = port->connections) == NULL) {
  614. /* no connections; return a zero-filled buffer */
  615. return (void *) (*(port->client_segment_base) + port->type_info->zero_buffer_offset);
  616. }
  617. if ((next = jack_slist_next (node)) == NULL) {
  618. /* one connection: use zero-copy mode - just pass
  619. the buffer of the connected (output) port.
  620. */
  621. return jack_port_get_buffer (((jack_port_t *) node->data),
  622. nframes);
  623. }
  624. /* Multiple connections. Use a local buffer and mix the
  625. incoming data into that buffer. We have already
  626. established the existence of a mixdown function during the
  627. connection process.
  628. */
  629. if (port->mix_buffer == NULL) {
  630. size_t buffer_size =
  631. port->type_info->buffer_scale_factor
  632. * sizeof (jack_default_audio_sample_t)
  633. * nframes;
  634. port->mix_buffer = jack_pool_alloc (buffer_size);
  635. port->fptr.buffer_init (port->mix_buffer, buffer_size);
  636. }
  637. port->fptr.mixdown (port, nframes);
  638. return (void *) port->mix_buffer;
  639. }
  640. int
  641. jack_port_tie (jack_port_t *src, jack_port_t *dst)
  642. {
  643. if (dst->shared->client_id != src->shared->client_id) {
  644. jack_error ("cannot tie ports not owned by the same client");
  645. return -1;
  646. }
  647. if (dst->shared->flags & JackPortIsOutput) {
  648. jack_error ("cannot tie an input port");
  649. return -1;
  650. }
  651. dst->tied = src;
  652. return 0;
  653. }
  654. int
  655. jack_port_untie (jack_port_t *port)
  656. {
  657. if (port->tied == NULL) {
  658. jack_error ("port \"%s\" is not tied", port->shared->name);
  659. return -1;
  660. }
  661. port->tied = NULL;
  662. return 0;
  663. }
  664. int
  665. jack_port_request_monitor (jack_port_t *port, int onoff)
  666. {
  667. if (onoff) {
  668. port->shared->monitor_requests++;
  669. } else if (port->shared->monitor_requests) {
  670. port->shared->monitor_requests--;
  671. }
  672. if ((port->shared->flags & JackPortIsOutput) == 0) {
  673. JSList *node;
  674. /* this port is for input, so recurse over each of the
  675. connected ports.
  676. */
  677. pthread_mutex_lock (&port->connection_lock);
  678. for (node = port->connections; node;
  679. node = jack_slist_next (node)) {
  680. /* drop the lock because if there is a feedback loop,
  681. we will deadlock. XXX much worse things will
  682. happen if there is a feedback loop !!!
  683. */
  684. pthread_mutex_unlock (&port->connection_lock);
  685. jack_port_request_monitor ((jack_port_t *) node->data,
  686. onoff);
  687. pthread_mutex_lock (&port->connection_lock);
  688. }
  689. pthread_mutex_unlock (&port->connection_lock);
  690. }
  691. return 0;
  692. }
  693. int
  694. jack_port_request_monitor_by_name (jack_client_t *client,
  695. const char *port_name, int onoff)
  696. {
  697. jack_port_t *port;
  698. unsigned long i, limit;
  699. jack_port_shared_t *ports;
  700. limit = client->engine->port_max;
  701. ports = &client->engine->ports[0];
  702. for (i = 0; i < limit; i++) {
  703. if (ports[i].in_use &&
  704. strcmp (ports[i].name, port_name) == 0) {
  705. port = jack_port_new (client, ports[i].id,
  706. client->engine);
  707. return jack_port_request_monitor (port, onoff);
  708. free (port);
  709. return 0;
  710. }
  711. }
  712. return -1;
  713. }
  714. int
  715. jack_port_ensure_monitor (jack_port_t *port, int yn)
  716. {
  717. if (yn) {
  718. if (port->shared->monitor_requests == 0) {
  719. port->shared->monitor_requests++;
  720. }
  721. } else {
  722. if (port->shared->monitor_requests > 0) {
  723. port->shared->monitor_requests = 0;
  724. }
  725. }
  726. return 0;
  727. }
  728. int
  729. jack_port_monitoring_input (jack_port_t *port)
  730. {
  731. return port->shared->monitor_requests > 0;
  732. }
  733. const char *
  734. jack_port_name (const jack_port_t *port)
  735. {
  736. return port->shared->name;
  737. }
  738. const char *
  739. jack_port_short_name (const jack_port_t *port)
  740. {
  741. /* we know there is always a colon, because we put
  742. it there ...
  743. */
  744. return strchr (port->shared->name, ':') + 1;
  745. }
  746. int
  747. jack_port_is_mine (const jack_client_t *client, const jack_port_t *port)
  748. {
  749. return port->shared->client_id == client->control->id;
  750. }
  751. int
  752. jack_port_flags (const jack_port_t *port)
  753. {
  754. return port->shared->flags;
  755. }
  756. const char *
  757. jack_port_type (const jack_port_t *port)
  758. {
  759. return port->type_info->type_name;
  760. }
  761. int
  762. jack_port_set_name (jack_port_t *port, const char *new_name)
  763. {
  764. char *colon;
  765. int len;
  766. colon = strchr (port->shared->name, ':');
  767. len = sizeof (port->shared->name) -
  768. ((int) (colon - port->shared->name)) - 2;
  769. snprintf (colon+1, len, "%s", new_name);
  770. return 0;
  771. }
  772. /* AUDIO PORT SUPPORT */
  773. static inline float f_max(float x, float a)
  774. {
  775. x -= a;
  776. x += fabs (x);
  777. x *= 0.5;
  778. x += a;
  779. return (x);
  780. }
  781. static void
  782. jack_audio_port_mixdown (jack_port_t *port, jack_nframes_t nframes)
  783. {
  784. JSList *node;
  785. jack_port_t *input;
  786. #ifndef ARCH_X86
  787. jack_nframes_t n;
  788. jack_default_audio_sample_t *dst, *src;
  789. #endif
  790. jack_default_audio_sample_t *buffer;
  791. /* by the time we've called this, we've already established
  792. the existence of more than one connection to this input
  793. port and allocated a mix_buffer.
  794. */
  795. /* no need to take connection lock, since this is called
  796. from the process() callback, and the jack server
  797. ensures that no changes to connections happen
  798. during this time.
  799. */
  800. node = port->connections;
  801. input = (jack_port_t *) node->data;
  802. buffer = port->mix_buffer;
  803. #ifndef USE_DYNSIMD
  804. memcpy (buffer, jack_output_port_buffer (input),
  805. sizeof (jack_default_audio_sample_t) * nframes);
  806. #else /* USE_DYNSIMD */
  807. opt_copy (buffer, jack_output_port_buffer (input), nframes);
  808. #endif /* USE_DYNSIMD */
  809. for (node = jack_slist_next (node); node;
  810. node = jack_slist_next (node)) {
  811. input = (jack_port_t *) node->data;
  812. #ifndef USE_DYNSIMD
  813. n = nframes;
  814. dst = buffer;
  815. src = jack_output_port_buffer (input);
  816. while (n--) {
  817. *dst++ += *src++;
  818. }
  819. #else /* USE_DYNSIMD */
  820. opt_mix (buffer, jack_output_port_buffer (input), nframes);
  821. #endif /* USE_DYNSIMD */
  822. }
  823. }