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.

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