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.

988 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_audio_port_mixdown (jack_port_t *port,
  32. jack_nframes_t nframes);
  33. extern void jack_midi_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. .mixdown = jack_audio_port_mixdown,
  40. };
  41. jack_port_functions_t jack_builtin_midi_functions = {
  42. .mixdown = jack_midi_port_mixdown,
  43. };
  44. jack_port_functions_t jack_builtin_NULL_functions = {
  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_t *
  301. jack_port_new (const jack_client_t *client, jack_port_id_t port_id,
  302. jack_control_t *control)
  303. {
  304. jack_port_shared_t *shared = &control->ports[port_id];
  305. jack_port_type_id_t ptid = shared->ptype_id;
  306. jack_port_t *port = (jack_port_t *) malloc (sizeof (jack_port_t));
  307. port->mix_buffer = NULL;
  308. port->client_segment_base = NULL;
  309. port->shared = shared;
  310. port->type_info = &client->engine->port_types[ptid];
  311. pthread_mutex_init (&port->connection_lock, NULL);
  312. port->connections = 0;
  313. port->tied = NULL;
  314. if (client->control->id == port->shared->client_id) {
  315. /* It's our port, so initialize the pointers to port
  316. * functions within this address space. These builtin
  317. * definitions can be overridden by the client.
  318. */
  319. if (ptid == JACK_AUDIO_PORT_TYPE) {
  320. port->fptr = jack_builtin_audio_functions;
  321. port->shared->has_mixdown = TRUE;
  322. } else if (ptid == JACK_MIDI_PORT_TYPE) {
  323. port->fptr = jack_builtin_midi_functions;
  324. port->shared->has_mixdown = TRUE;
  325. } else { /* no other builtin functions */
  326. port->fptr = jack_builtin_NULL_functions;
  327. port->shared->has_mixdown = FALSE;
  328. }
  329. }
  330. /* set up a base address so that port->offset can be used to
  331. compute the correct location. we don't store the location
  332. directly, because port->client_segment_base and/or
  333. port->offset can change if the buffer size or port counts
  334. are changed.
  335. */
  336. port->client_segment_base =
  337. (void **) &client->port_segment[ptid].attached_at;
  338. return port;
  339. }
  340. jack_port_t *
  341. jack_port_register (jack_client_t *client,
  342. const char *port_name,
  343. const char *port_type,
  344. unsigned long flags,
  345. unsigned long buffer_size)
  346. {
  347. jack_request_t req;
  348. jack_port_t *port = 0;
  349. int length ;
  350. req.type = RegisterPort;
  351. length = strlen ((const char *) client->control->name)
  352. + 1 + strlen (port_name);
  353. if ( length >= sizeof (req.x.port_info.name) ) {
  354. jack_error ("\"%s:%s\" is too long to be used as a JACK port name.\n"
  355. "Please use %lu characters or less.",
  356. client->control->name ,
  357. port_name,
  358. sizeof (req.x.port_info.name) - 1);
  359. return NULL ;
  360. }
  361. strcpy ((char *) req.x.port_info.name,
  362. (const char *) client->control->name);
  363. strcat ((char *) req.x.port_info.name, ":");
  364. strcat ((char *) req.x.port_info.name, port_name);
  365. snprintf (req.x.port_info.type, sizeof (req.x.port_info.type),
  366. "%s", port_type);
  367. req.x.port_info.flags = flags;
  368. req.x.port_info.buffer_size = buffer_size;
  369. req.x.port_info.client_id = client->control->id;
  370. if (jack_client_deliver_request (client, &req)) {
  371. jack_error ("cannot deliver port registration request");
  372. return NULL;
  373. }
  374. if ((port = jack_port_new (client, req.x.port_info.port_id,
  375. client->engine)) == NULL) {
  376. jack_error ("cannot allocate client side port structure");
  377. return NULL;
  378. }
  379. client->ports = jack_slist_prepend (client->ports, port);
  380. return port;
  381. }
  382. int
  383. jack_port_unregister (jack_client_t *client, jack_port_t *port)
  384. {
  385. jack_request_t req;
  386. req.type = UnRegisterPort;
  387. req.x.port_info.port_id = port->shared->id;
  388. req.x.port_info.client_id = client->control->id;
  389. return jack_client_deliver_request (client, &req);
  390. }
  391. int
  392. jack_port_lock (jack_client_t *client, jack_port_t *port)
  393. {
  394. if (port) {
  395. port->shared->locked = 1;
  396. return 0;
  397. }
  398. return -1;
  399. }
  400. int
  401. jack_port_unlock (jack_client_t *client, jack_port_t *port)
  402. {
  403. if (port) {
  404. port->shared->locked = 0;
  405. return 0;
  406. }
  407. return -1;
  408. }
  409. /* LOCAL (in-client) connection querying only */
  410. int
  411. jack_port_connected (const jack_port_t *port)
  412. {
  413. return jack_slist_length (port->connections);
  414. }
  415. int
  416. jack_port_connected_to (const jack_port_t *port, const char *portname)
  417. {
  418. JSList *node;
  419. int ret = FALSE;
  420. /* XXX this really requires a cross-process lock
  421. so that ports/connections cannot go away
  422. while we are checking for them. that's hard,
  423. and has a non-trivial performance impact
  424. for jackd.
  425. */
  426. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  427. for (node = port->connections; node; node = jack_slist_next (node)) {
  428. jack_port_t *other_port = (jack_port_t *) node->data;
  429. if (strcmp (other_port->shared->name, portname) == 0) {
  430. ret = TRUE;
  431. break;
  432. }
  433. }
  434. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  435. return ret;
  436. }
  437. const char **
  438. jack_port_get_connections (const jack_port_t *port)
  439. {
  440. const char **ret = NULL;
  441. JSList *node;
  442. unsigned int n;
  443. /* XXX this really requires a cross-process lock
  444. so that ports/connections cannot go away
  445. while we are checking for them. that's hard,
  446. and has a non-trivial performance impact
  447. for jackd.
  448. */
  449. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  450. if (port->connections != NULL) {
  451. ret = (const char **)
  452. malloc (sizeof (char *)
  453. * (jack_slist_length (port->connections) + 1));
  454. for (n = 0, node = port->connections; node;
  455. node = jack_slist_next (node), ++n) {
  456. ret[n] = ((jack_port_t *) node->data)->shared->name;
  457. }
  458. ret[n] = NULL;
  459. }
  460. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  461. return ret;
  462. }
  463. /* SERVER-SIDE (all) connection querying */
  464. const char **
  465. jack_port_get_all_connections (const jack_client_t *client,
  466. const jack_port_t *port)
  467. {
  468. const char **ret;
  469. jack_request_t req;
  470. jack_port_t *tmp;
  471. unsigned int i;
  472. int need_free = FALSE;
  473. if (port == NULL) {
  474. return NULL;
  475. }
  476. req.type = GetPortConnections;
  477. req.x.port_info.name[0] = '\0';
  478. req.x.port_info.type[0] = '\0';
  479. req.x.port_info.flags = 0;
  480. req.x.port_info.buffer_size = 0;
  481. req.x.port_info.client_id = 0;
  482. req.x.port_info.port_id = port->shared->id;
  483. jack_client_deliver_request (client, &req);
  484. if (req.status != 0 || req.x.port_connections.nports == 0) {
  485. return NULL;
  486. }
  487. if (client->request_fd < 0) {
  488. /* internal client */
  489. return req.x.port_connections.ports;
  490. }
  491. ret = (const char **)
  492. malloc (sizeof (char *) * (req.x.port_connections.nports + 1));
  493. for (i = 0; i < req.x.port_connections.nports; ++i ) {
  494. jack_port_id_t port_id;
  495. if (read (client->request_fd, &port_id, sizeof (port_id))
  496. != sizeof (port_id)) {
  497. jack_error ("cannot read port id from server");
  498. return 0;
  499. }
  500. tmp = jack_port_by_id_int (client, port_id, &need_free);
  501. ret[i] = tmp->shared->name;
  502. if (need_free) {
  503. free (tmp);
  504. need_free = FALSE;
  505. }
  506. }
  507. ret[i] = NULL;
  508. return ret;
  509. }
  510. jack_port_t *
  511. jack_port_by_id_int (const jack_client_t *client, jack_port_id_t id, int* free)
  512. {
  513. JSList *node;
  514. for (node = client->ports; node; node = jack_slist_next (node)) {
  515. if (((jack_port_t *) node->data)->shared->id == id) {
  516. *free = FALSE;
  517. return (jack_port_t *) node->data;
  518. }
  519. }
  520. if (id >= client->engine->port_max)
  521. return NULL;
  522. if (client->engine->ports[id].in_use) {
  523. *free = TRUE;
  524. return jack_port_new (client, id, client->engine);
  525. }
  526. return NULL;
  527. }
  528. jack_port_t *
  529. jack_port_by_id (jack_client_t *client, jack_port_id_t id)
  530. {
  531. JSList *node;
  532. jack_port_t* port;
  533. int need_free = FALSE;
  534. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  535. port = node->data;
  536. if (port->shared->id == id) { // Found port, return the cached structure
  537. return port;
  538. }
  539. }
  540. // Otherwise possibly allocate a new port structure, keep it in the ports_ext list for later use
  541. port = jack_port_by_id_int (client,id,&need_free);
  542. if (port != NULL && need_free)
  543. client->ports_ext =
  544. jack_slist_prepend (client->ports_ext, port);
  545. return port;
  546. }
  547. jack_port_t *
  548. jack_port_by_name_int (jack_client_t *client, const char *port_name)
  549. {
  550. unsigned long i, limit;
  551. jack_port_shared_t *port;
  552. limit = client->engine->port_max;
  553. port = &client->engine->ports[0];
  554. for (i = 0; i < limit; i++) {
  555. if (port[i].in_use && strcmp (port[i].name, port_name) == 0) {
  556. return jack_port_new (client, port[i].id,
  557. client->engine);
  558. }
  559. }
  560. return NULL;
  561. }
  562. jack_port_t *
  563. jack_port_by_name (jack_client_t *client, const char *port_name)
  564. {
  565. JSList *node;
  566. jack_port_t* port;
  567. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  568. port = node->data;
  569. if (strcmp (port->shared->name, port_name) == 0) {
  570. /* Found port, return the cached structure. */
  571. return port;
  572. }
  573. }
  574. /* Otherwise allocate a new port structure, keep it in the
  575. * ports_ext list for later use. */
  576. port = jack_port_by_name_int (client, port_name);
  577. if (port != NULL)
  578. client->ports_ext =
  579. jack_slist_prepend (client->ports_ext, port);
  580. return port;
  581. }
  582. jack_nframes_t
  583. jack_port_get_latency (jack_port_t *port)
  584. {
  585. return port->shared->latency;
  586. }
  587. jack_nframes_t
  588. jack_port_get_total_latency (jack_client_t *client, jack_port_t *port)
  589. {
  590. return port->shared->total_latency;
  591. }
  592. void
  593. jack_port_set_latency (jack_port_t *port, jack_nframes_t nframes)
  594. {
  595. port->shared->latency = nframes;
  596. }
  597. void *
  598. jack_port_get_buffer (jack_port_t *port, jack_nframes_t nframes)
  599. {
  600. JSList *node, *next;
  601. /* Output port. The buffer was assigned by the engine
  602. when the port was registered.
  603. */
  604. if (port->shared->flags & JackPortIsOutput) {
  605. if (port->tied) {
  606. return jack_port_get_buffer (port->tied, nframes);
  607. }
  608. return jack_output_port_buffer (port);
  609. }
  610. /* Input port. Since this can only be called from the
  611. process() callback, and since no connections can be
  612. made/broken during this phase (enforced by the jack
  613. server), there is no need to take the connection lock here
  614. */
  615. if ((node = port->connections) == NULL) {
  616. /* no connections; return a zero-filled buffer */
  617. return jack_zero_filled_buffer;
  618. }
  619. if ((next = jack_slist_next (node)) == NULL) {
  620. /* one connection: use zero-copy mode - just pass
  621. the buffer of the connected (output) port.
  622. */
  623. return jack_port_get_buffer (((jack_port_t *) node->data),
  624. nframes);
  625. }
  626. /* Multiple connections. Use a local buffer and mix the
  627. incoming data into that buffer. We have already
  628. established the existence of a mixdown function during the
  629. connection process.
  630. */
  631. if (port->mix_buffer == NULL) {
  632. port->mix_buffer =
  633. jack_pool_alloc (
  634. port->type_info->buffer_scale_factor
  635. * sizeof (jack_default_audio_sample_t)
  636. * nframes);
  637. }
  638. port->fptr.mixdown (port, nframes);
  639. return (void *) port->mix_buffer;
  640. }
  641. int
  642. jack_port_tie (jack_port_t *src, jack_port_t *dst)
  643. {
  644. if (dst->shared->client_id != src->shared->client_id) {
  645. jack_error ("cannot tie ports not owned by the same client");
  646. return -1;
  647. }
  648. if (dst->shared->flags & JackPortIsOutput) {
  649. jack_error ("cannot tie an input port");
  650. return -1;
  651. }
  652. dst->tied = src;
  653. return 0;
  654. }
  655. int
  656. jack_port_untie (jack_port_t *port)
  657. {
  658. if (port->tied == NULL) {
  659. jack_error ("port \"%s\" is not tied", port->shared->name);
  660. return -1;
  661. }
  662. port->tied = NULL;
  663. return 0;
  664. }
  665. int
  666. jack_port_request_monitor (jack_port_t *port, int onoff)
  667. {
  668. if (onoff) {
  669. port->shared->monitor_requests++;
  670. } else if (port->shared->monitor_requests) {
  671. port->shared->monitor_requests--;
  672. }
  673. if ((port->shared->flags & JackPortIsOutput) == 0) {
  674. JSList *node;
  675. /* this port is for input, so recurse over each of the
  676. connected ports.
  677. */
  678. pthread_mutex_lock (&port->connection_lock);
  679. for (node = port->connections; node;
  680. node = jack_slist_next (node)) {
  681. /* drop the lock because if there is a feedback loop,
  682. we will deadlock. XXX much worse things will
  683. happen if there is a feedback loop !!!
  684. */
  685. pthread_mutex_unlock (&port->connection_lock);
  686. jack_port_request_monitor ((jack_port_t *) node->data,
  687. onoff);
  688. pthread_mutex_lock (&port->connection_lock);
  689. }
  690. pthread_mutex_unlock (&port->connection_lock);
  691. }
  692. return 0;
  693. }
  694. int
  695. jack_port_request_monitor_by_name (jack_client_t *client,
  696. const char *port_name, int onoff)
  697. {
  698. jack_port_t *port;
  699. unsigned long i, limit;
  700. jack_port_shared_t *ports;
  701. limit = client->engine->port_max;
  702. ports = &client->engine->ports[0];
  703. for (i = 0; i < limit; i++) {
  704. if (ports[i].in_use &&
  705. strcmp (ports[i].name, port_name) == 0) {
  706. port = jack_port_new (client, ports[i].id,
  707. client->engine);
  708. return jack_port_request_monitor (port, onoff);
  709. free (port);
  710. return 0;
  711. }
  712. }
  713. return -1;
  714. }
  715. int
  716. jack_ensure_port_monitor_input (jack_port_t *port, int yn)
  717. {
  718. if (yn) {
  719. if (port->shared->monitor_requests == 0) {
  720. port->shared->monitor_requests++;
  721. }
  722. } else {
  723. if (port->shared->monitor_requests > 0) {
  724. port->shared->monitor_requests = 0;
  725. }
  726. }
  727. return 0;
  728. }
  729. int
  730. jack_port_monitoring_input (jack_port_t *port)
  731. {
  732. return port->shared->monitor_requests > 0;
  733. }
  734. const char *
  735. jack_port_name (const jack_port_t *port)
  736. {
  737. return port->shared->name;
  738. }
  739. const char *
  740. jack_port_short_name (const jack_port_t *port)
  741. {
  742. /* we know there is always a colon, because we put
  743. it there ...
  744. */
  745. return strchr (port->shared->name, ':') + 1;
  746. }
  747. int
  748. jack_port_is_mine (const jack_client_t *client, const jack_port_t *port)
  749. {
  750. return port->shared->client_id == client->control->id;
  751. }
  752. int
  753. jack_port_flags (const jack_port_t *port)
  754. {
  755. return port->shared->flags;
  756. }
  757. const char *
  758. jack_port_type (const jack_port_t *port)
  759. {
  760. return port->type_info->type_name;
  761. }
  762. int
  763. jack_port_set_name (jack_port_t *port, const char *new_name)
  764. {
  765. char *colon;
  766. int len;
  767. colon = strchr (port->shared->name, ':');
  768. len = sizeof (port->shared->name) -
  769. ((int) (colon - port->shared->name)) - 2;
  770. snprintf (colon+1, len, "%s", new_name);
  771. return 0;
  772. }
  773. /* AUDIO PORT SUPPORT */
  774. static inline float f_max(float x, float a)
  775. {
  776. x -= a;
  777. x += fabs (x);
  778. x *= 0.5;
  779. x += a;
  780. return (x);
  781. }
  782. static void
  783. jack_audio_port_mixdown (jack_port_t *port, jack_nframes_t nframes)
  784. {
  785. JSList *node;
  786. jack_port_t *input;
  787. #ifndef ARCH_X86
  788. jack_nframes_t n;
  789. jack_default_audio_sample_t *dst, *src;
  790. #endif
  791. jack_default_audio_sample_t *buffer;
  792. /* by the time we've called this, we've already established
  793. the existence of more than one connection to this input
  794. port and allocated a mix_buffer.
  795. */
  796. /* no need to take connection lock, since this is called
  797. from the process() callback, and the jack server
  798. ensures that no changes to connections happen
  799. during this time.
  800. */
  801. node = port->connections;
  802. input = (jack_port_t *) node->data;
  803. buffer = port->mix_buffer;
  804. #ifndef USE_DYNSIMD
  805. memcpy (buffer, jack_output_port_buffer (input),
  806. sizeof (jack_default_audio_sample_t) * nframes);
  807. #else /* USE_DYNSIMD */
  808. opt_copy (buffer, jack_output_port_buffer (input), nframes);
  809. #endif /* USE_DYNSIMD */
  810. for (node = jack_slist_next (node); node;
  811. node = jack_slist_next (node)) {
  812. input = (jack_port_t *) node->data;
  813. #ifndef USE_DYNSIMD
  814. n = nframes;
  815. dst = buffer;
  816. src = jack_output_port_buffer (input);
  817. while (n--) {
  818. *dst++ += *src++;
  819. }
  820. #else /* USE_DYNSIMD */
  821. opt_mix (buffer, jack_output_port_buffer (input), nframes);
  822. #endif /* USE_DYNSIMD */
  823. }
  824. }