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.

1037 lines
24KB

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