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.

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