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.

967 lines
22KB

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