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.

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