jack2 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.

927 lines
28KB

  1. /*
  2. Copyright (C) 2010 Devin Anderson
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. /*
  16. * This program is used to measure MIDI latency and jitter. It writes MIDI
  17. * messages to one port and calculates how long it takes before it reads the
  18. * same MIDI message over another port. It was written to calculate the
  19. * latency and jitter of hardware and JACK hardware drivers, but might have
  20. * other practical applications.
  21. *
  22. * The latency results of the program include the latency introduced by the
  23. * JACK system. Because JACK has sample accurate MIDI, the same latency
  24. * imposed on audio is also imposed on MIDI going through the system. Make
  25. * sure you take this into account before complaining to me or (*especially*)
  26. * other JACK developers about reported MIDI latency.
  27. *
  28. * The jitter results are a little more interesting. The program attempts to
  29. * calculate 'average jitter' and 'peak jitter', as defined here:
  30. *
  31. * http://openmuse.org/transport/fidelity.html
  32. *
  33. * It also outputs a jitter plot, which gives you a more specific idea about
  34. * the MIDI jitter for the ports you're testing. This is useful for catching
  35. * extreme jitter values, and for analyzing the amount of truth in the
  36. * technical specifications for your MIDI interface(s). :)
  37. *
  38. * This program is loosely based on 'alsa-midi-latency-test' in the ALSA test
  39. * suite.
  40. *
  41. * To port this program to non-POSIX platforms, you'll have to include
  42. * implementations for semaphores and command-line argument handling.
  43. */
  44. #include <errno.h>
  45. #include <math.h>
  46. #include <signal.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <getopt.h>
  51. #include <jack/jack.h>
  52. #include <jack/midiport.h>
  53. #ifdef WIN32
  54. #include <windows.h>
  55. #include <unistd.h>
  56. #else
  57. #include <semaphore.h>
  58. #endif
  59. #define ABS(x) (((x) >= 0) ? (x) : (-(x)))
  60. #ifdef WIN32
  61. typedef HANDLE semaphore_t;
  62. #else
  63. typedef sem_t *semaphore_t;
  64. #endif
  65. const char *ERROR_MSG_TIMEOUT = "timed out while waiting for MIDI message";
  66. const char *ERROR_RESERVE = "could not reserve MIDI event on port buffer";
  67. const char *ERROR_SHUTDOWN = "the JACK server has been shutdown";
  68. const char *SOURCE_EVENT_RESERVE = "jack_midi_event_reserve";
  69. const char *SOURCE_PROCESS = "handle_process";
  70. const char *SOURCE_SHUTDOWN = "handle_shutdown";
  71. const char *SOURCE_SIGNAL_SEMAPHORE = "signal_semaphore";
  72. const char *SOURCE_WAIT_SEMAPHORE = "wait_semaphore";
  73. jack_client_t *client;
  74. semaphore_t connect_semaphore;
  75. volatile int connections_established;
  76. const char *error_message;
  77. const char *error_source;
  78. jack_nframes_t highest_latency;
  79. jack_time_t highest_latency_time;
  80. jack_latency_range_t in_latency_range;
  81. jack_port_t *in_port;
  82. semaphore_t init_semaphore;
  83. jack_nframes_t last_activity;
  84. jack_time_t last_activity_time;
  85. jack_time_t *latency_time_values;
  86. jack_nframes_t *latency_values;
  87. jack_nframes_t lowest_latency;
  88. jack_time_t lowest_latency_time;
  89. jack_midi_data_t *message_1;
  90. jack_midi_data_t *message_2;
  91. size_t messages_received;
  92. size_t messages_sent;
  93. size_t message_size;
  94. jack_latency_range_t out_latency_range;
  95. jack_port_t *out_port;
  96. semaphore_t process_semaphore;
  97. volatile sig_atomic_t process_state;
  98. char *program_name;
  99. jack_port_t *remote_in_port;
  100. jack_port_t *remote_out_port;
  101. size_t samples;
  102. const char *target_in_port_name;
  103. const char *target_out_port_name;
  104. int timeout;
  105. jack_nframes_t total_latency;
  106. jack_time_t total_latency_time;
  107. size_t unexpected_messages;
  108. size_t xrun_count;
  109. #ifdef WIN32
  110. char semaphore_error_msg[1024];
  111. #endif
  112. static void
  113. output_error(const char *source, const char *message);
  114. static void
  115. output_usage(void);
  116. static void
  117. set_process_error(const char *source, const char *message);
  118. static int
  119. signal_semaphore(semaphore_t semaphore);
  120. static jack_port_t *
  121. update_connection(jack_port_t *remote_port, int connected,
  122. jack_port_t *local_port, jack_port_t *current_port,
  123. const char *target_name);
  124. static int
  125. wait_semaphore(semaphore_t semaphore, int block);
  126. static semaphore_t
  127. create_semaphore(int id)
  128. {
  129. semaphore_t semaphore;
  130. #ifdef WIN32
  131. semaphore = CreateSemaphore(NULL, 0, 2, NULL);
  132. #elif defined (__APPLE__)
  133. char name[128];
  134. sprintf(name, "midi_sem_%d", id);
  135. semaphore = sem_open(name, O_CREAT, 0777, 0);
  136. if (semaphore == (sem_t *) SEM_FAILED) {
  137. semaphore = NULL;
  138. }
  139. #else
  140. semaphore = malloc(sizeof(semaphore_t));
  141. if (semaphore != NULL) {
  142. if (sem_init(semaphore, 0, 0)) {
  143. free(semaphore);
  144. semaphore = NULL;
  145. }
  146. }
  147. #endif
  148. return semaphore;
  149. }
  150. static void
  151. destroy_semaphore(semaphore_t semaphore, int id)
  152. {
  153. #ifdef WIN32
  154. CloseHandle(semaphore);
  155. #else
  156. sem_destroy(semaphore);
  157. #ifdef __APPLE__
  158. {
  159. char name[128];
  160. sprintf(name, "midi_sem_%d", id);
  161. sem_unlink(name);
  162. }
  163. #else
  164. free(semaphore);
  165. #endif
  166. #endif
  167. }
  168. static void
  169. die(const char *source, const char *error_message)
  170. {
  171. output_error(source, error_message);
  172. output_usage();
  173. exit(EXIT_FAILURE);
  174. }
  175. static const char *
  176. get_semaphore_error(void)
  177. {
  178. #ifdef WIN32
  179. DWORD error = GetLastError();
  180. if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error,
  181. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  182. semaphore_error_msg, 1024, NULL)) {
  183. snprintf(semaphore_error_msg, 1023, "Unknown OS error code '%d'",
  184. error);
  185. }
  186. return semaphore_error_msg;
  187. #else
  188. return strerror(errno);
  189. #endif
  190. }
  191. static void
  192. handle_info(const char *message)
  193. {
  194. /* Suppress info */
  195. }
  196. static void
  197. handle_port_connection_change(jack_port_id_t port_id_1,
  198. jack_port_id_t port_id_2, int connected,
  199. void *arg)
  200. {
  201. jack_port_t *port_1;
  202. jack_port_t *port_2;
  203. if ((remote_in_port != NULL) && (remote_out_port != NULL)) {
  204. return;
  205. }
  206. port_1 = jack_port_by_id(client, port_id_1);
  207. port_2 = jack_port_by_id(client, port_id_2);
  208. /* The 'update_connection' call is not RT-safe. It calls
  209. 'jack_port_get_connections' and 'jack_free'. This might be a problem
  210. with JACK 1, as this callback runs in the process thread in JACK 1. */
  211. if (port_1 == in_port) {
  212. remote_in_port = update_connection(port_2, connected, in_port,
  213. remote_in_port,
  214. target_in_port_name);
  215. } else if (port_2 == in_port) {
  216. remote_in_port = update_connection(port_1, connected, in_port,
  217. remote_in_port,
  218. target_in_port_name);
  219. } else if (port_1 == out_port) {
  220. remote_out_port = update_connection(port_2, connected, out_port,
  221. remote_out_port,
  222. target_out_port_name);
  223. } else if (port_2 == out_port) {
  224. remote_out_port = update_connection(port_1, connected, out_port,
  225. remote_out_port,
  226. target_out_port_name);
  227. }
  228. if ((remote_in_port != NULL) && (remote_out_port != NULL)) {
  229. connections_established = 1;
  230. if (! signal_semaphore(connect_semaphore)) {
  231. /* Sigh ... */
  232. die("post_semaphore", get_semaphore_error());
  233. }
  234. if (! signal_semaphore(init_semaphore)) {
  235. /* Sigh ... */
  236. die("post_semaphore", get_semaphore_error());
  237. }
  238. }
  239. }
  240. static int
  241. handle_process(jack_nframes_t frames, void *arg)
  242. {
  243. jack_midi_data_t *buffer;
  244. jack_midi_event_t event;
  245. jack_nframes_t event_count;
  246. jack_nframes_t event_time;
  247. jack_nframes_t frame;
  248. size_t i;
  249. jack_nframes_t last_frame_time;
  250. jack_midi_data_t *message;
  251. jack_time_t microseconds;
  252. void *port_buffer;
  253. jack_time_t time;
  254. jack_midi_clear_buffer(jack_port_get_buffer(out_port, frames));
  255. switch (process_state) {
  256. case 0:
  257. /* State: initializing */
  258. switch (wait_semaphore(init_semaphore, 0)) {
  259. case -1:
  260. set_process_error(SOURCE_WAIT_SEMAPHORE, get_semaphore_error());
  261. /* Fallthrough on purpose */
  262. case 0:
  263. return 0;
  264. }
  265. highest_latency = 0;
  266. lowest_latency = 0;
  267. messages_received = 0;
  268. messages_sent = 0;
  269. process_state = 1;
  270. total_latency = 0;
  271. total_latency_time = 0;
  272. unexpected_messages = 0;
  273. xrun_count = 0;
  274. jack_port_get_latency_range(remote_in_port, JackCaptureLatency,
  275. &in_latency_range);
  276. jack_port_get_latency_range(remote_out_port, JackPlaybackLatency,
  277. &out_latency_range);
  278. goto send_message;
  279. case 1:
  280. /* State: processing */
  281. port_buffer = jack_port_get_buffer(in_port, frames);
  282. event_count = jack_midi_get_event_count(port_buffer);
  283. last_frame_time = jack_last_frame_time(client);
  284. for (i = 0; i < event_count; i++) {
  285. jack_midi_event_get(&event, port_buffer, i);
  286. message = (messages_received % 2) ? message_2 : message_1;
  287. if ((event.size == message_size) &&
  288. (! memcmp(message, event.buffer,
  289. message_size * sizeof(jack_midi_data_t)))) {
  290. goto found_message;
  291. }
  292. unexpected_messages++;
  293. }
  294. microseconds = jack_frames_to_time(client, last_frame_time) -
  295. last_activity_time;
  296. if ((microseconds / 1000000) >= timeout) {
  297. set_process_error(SOURCE_PROCESS, ERROR_MSG_TIMEOUT);
  298. }
  299. break;
  300. found_message:
  301. event_time = last_frame_time + event.time;
  302. frame = event_time - last_activity;
  303. time = jack_frames_to_time(client, event_time) - last_activity_time;
  304. if ((! highest_latency) || (frame > highest_latency)) {
  305. highest_latency = frame;
  306. highest_latency_time = time;
  307. }
  308. if ((! lowest_latency) || (frame < lowest_latency)) {
  309. lowest_latency = frame;
  310. lowest_latency_time = time;
  311. }
  312. latency_time_values[messages_received] = time;
  313. latency_values[messages_received] = frame;
  314. total_latency += frame;
  315. total_latency_time += time;
  316. messages_received++;
  317. if (messages_received == samples) {
  318. process_state = 2;
  319. if (! signal_semaphore(process_semaphore)) {
  320. /* Sigh ... */
  321. die(SOURCE_SIGNAL_SEMAPHORE, get_semaphore_error());
  322. }
  323. break;
  324. }
  325. send_message:
  326. frame = (jack_nframes_t) ((((double) rand()) / RAND_MAX) * frames);
  327. if (frame >= frames) {
  328. frame = frames - 1;
  329. }
  330. port_buffer = jack_port_get_buffer(out_port, frames);
  331. buffer = jack_midi_event_reserve(port_buffer, frame, message_size);
  332. if (buffer == NULL) {
  333. set_process_error(SOURCE_EVENT_RESERVE, ERROR_RESERVE);
  334. break;
  335. }
  336. message = (messages_sent % 2) ? message_2 : message_1;
  337. memcpy(buffer, message, message_size * sizeof(jack_midi_data_t));
  338. last_activity = jack_last_frame_time(client) + frame;
  339. last_activity_time = jack_frames_to_time(client, last_activity);
  340. messages_sent++;
  341. case 2:
  342. /* State: finished - do nothing */
  343. case -1:
  344. /* State: error - do nothing */
  345. case -2:
  346. /* State: signalled - do nothing */
  347. ;
  348. }
  349. return 0;
  350. }
  351. static void
  352. handle_shutdown(void *arg)
  353. {
  354. set_process_error(SOURCE_SHUTDOWN, ERROR_SHUTDOWN);
  355. }
  356. static void
  357. handle_signal(int sig)
  358. {
  359. process_state = -2;
  360. if (! signal_semaphore(connect_semaphore)) {
  361. /* Sigh ... */
  362. die(SOURCE_SIGNAL_SEMAPHORE, get_semaphore_error());
  363. }
  364. if (! signal_semaphore(process_semaphore)) {
  365. /* Sigh ... */
  366. die(SOURCE_SIGNAL_SEMAPHORE, get_semaphore_error());
  367. }
  368. }
  369. static int
  370. handle_xrun(void *arg)
  371. {
  372. xrun_count++;
  373. return 0;
  374. }
  375. static void
  376. output_error(const char *source, const char *message)
  377. {
  378. fprintf(stderr, "%s: %s: %s\n", program_name, source, message);
  379. }
  380. static void
  381. output_usage(void)
  382. {
  383. fprintf(stderr, "Usage: %s [options] [out-port-name in-port-name]\n\n"
  384. "\t-h, --help print program usage\n"
  385. "\t-m, --message-size=size set size of MIDI messages to send "
  386. "(default: 3)\n"
  387. "\t-s, --samples=n number of MIDI messages to send "
  388. "(default: 1024)\n"
  389. "\t-t, --timeout=seconds message timeout (default: 5)\n\n",
  390. program_name);
  391. }
  392. static unsigned long
  393. parse_positive_number_arg(char *s, char *name)
  394. {
  395. char *end_ptr;
  396. unsigned long result;
  397. errno = 0;
  398. result = strtoul(s, &end_ptr, 10);
  399. if (errno) {
  400. die(name, strerror(errno));
  401. }
  402. if (*s == '\0') {
  403. die(name, "argument value cannot be empty");
  404. }
  405. if (*end_ptr != '\0') {
  406. die(name, "invalid value");
  407. }
  408. if (! result) {
  409. die(name, "must be a positive number");
  410. }
  411. return result;
  412. }
  413. static int
  414. register_signal_handler(void (*func)(int))
  415. {
  416. #ifdef WIN32
  417. if (signal(SIGABRT, func) == SIG_ERR) {
  418. return 0;
  419. }
  420. #else
  421. if (signal(SIGQUIT, func) == SIG_ERR) {
  422. return 0;
  423. }
  424. if (signal(SIGHUP, func) == SIG_ERR) {
  425. return 0;
  426. }
  427. #endif
  428. if (signal(SIGINT, func) == SIG_ERR) {
  429. return 0;
  430. }
  431. if (signal(SIGTERM, func) == SIG_ERR) {
  432. return 0;
  433. }
  434. return 1;
  435. }
  436. static void
  437. set_process_error(const char *source, const char *message)
  438. {
  439. error_source = source;
  440. error_message = message;
  441. process_state = -1;
  442. if (! signal_semaphore(process_semaphore)) {
  443. /* Sigh ... */
  444. output_error(source, message);
  445. die(SOURCE_SIGNAL_SEMAPHORE, get_semaphore_error());
  446. }
  447. }
  448. static int
  449. signal_semaphore(semaphore_t semaphore)
  450. {
  451. #ifdef WIN32
  452. return ReleaseSemaphore(semaphore, 1, NULL);
  453. #else
  454. return ! sem_post(semaphore);
  455. #endif
  456. }
  457. static jack_port_t *
  458. update_connection(jack_port_t *remote_port, int connected,
  459. jack_port_t *local_port, jack_port_t *current_port,
  460. const char *target_name)
  461. {
  462. if (connected) {
  463. if (current_port) {
  464. return current_port;
  465. }
  466. if (target_name) {
  467. if (strcmp(target_name, jack_port_name(remote_port))) {
  468. return NULL;
  469. }
  470. }
  471. return remote_port;
  472. }
  473. if (! strcmp(jack_port_name(remote_port), jack_port_name(current_port))) {
  474. const char **port_names;
  475. if (target_name) {
  476. return NULL;
  477. }
  478. port_names = jack_port_get_connections(local_port);
  479. if (port_names == NULL) {
  480. return NULL;
  481. }
  482. /* If a connected port is disconnected and other ports are still
  483. connected, then we take the first port name in the array and use it
  484. as our remote port. It's a dumb implementation. */
  485. current_port = jack_port_by_name(client, port_names[0]);
  486. jack_free(port_names);
  487. if (current_port == NULL) {
  488. /* Sigh */
  489. die("jack_port_by_name", "failed to get port by name");
  490. }
  491. }
  492. return current_port;
  493. }
  494. static int
  495. wait_semaphore(semaphore_t semaphore, int block)
  496. {
  497. #ifdef WIN32
  498. DWORD result = WaitForSingleObject(semaphore, block ? INFINITE : 0);
  499. switch (result) {
  500. case WAIT_OBJECT_0:
  501. return 1;
  502. case WAIT_TIMEOUT:
  503. return 0;
  504. }
  505. return -1;
  506. #else
  507. if (block) {
  508. while (sem_wait(semaphore)) {
  509. if (errno != EINTR) {
  510. return -1;
  511. }
  512. }
  513. } else {
  514. while (sem_trywait(semaphore)) {
  515. switch (errno) {
  516. case EAGAIN:
  517. return 0;
  518. case EINTR:
  519. continue;
  520. default:
  521. return -1;
  522. }
  523. }
  524. }
  525. return 1;
  526. #endif
  527. }
  528. int
  529. main(int argc, char **argv)
  530. {
  531. size_t jitter_plot[101];
  532. size_t latency_plot[101];
  533. int long_index = 0;
  534. struct option long_options[] = {
  535. {"help", 0, NULL, 'h'},
  536. {"message-size", 1, NULL, 'm'},
  537. {"samples", 1, NULL, 's'},
  538. {"timeout", 1, NULL, 't'}
  539. };
  540. size_t name_arg_count;
  541. char *option_string = "hm:s:t:";
  542. int show_usage = 0;
  543. connections_established = 0;
  544. error_message = NULL;
  545. message_size = 3;
  546. program_name = argv[0];
  547. remote_in_port = 0;
  548. remote_out_port = 0;
  549. samples = 1024;
  550. timeout = 5;
  551. for (;;) {
  552. char c = getopt_long(argc, argv, option_string, long_options,
  553. &long_index);
  554. switch (c) {
  555. case 'h':
  556. show_usage = 1;
  557. break;
  558. case 'm':
  559. message_size = parse_positive_number_arg(optarg, "message-size");
  560. break;
  561. case 's':
  562. samples = parse_positive_number_arg(optarg, "samples");
  563. break;
  564. case 't':
  565. timeout = parse_positive_number_arg(optarg, "timeout");
  566. break;
  567. default:
  568. {
  569. char *s = "'- '";
  570. s[2] = c;
  571. die(s, "invalid switch");
  572. }
  573. case -1:
  574. if (show_usage) {
  575. output_usage();
  576. exit(EXIT_SUCCESS);
  577. }
  578. goto parse_port_names;
  579. case 1:
  580. /* end of switch :) */
  581. ;
  582. }
  583. }
  584. parse_port_names:
  585. name_arg_count = argc - optind;
  586. switch (name_arg_count) {
  587. case 2:
  588. target_in_port_name = argv[optind + 1];
  589. target_out_port_name = argv[optind];
  590. break;
  591. case 0:
  592. target_in_port_name = 0;
  593. target_out_port_name = 0;
  594. break;
  595. default:
  596. output_usage();
  597. return EXIT_FAILURE;
  598. }
  599. latency_values = malloc(sizeof(jack_nframes_t) * samples);
  600. if (latency_values == NULL) {
  601. error_message = strerror(errno);
  602. error_source = "malloc";
  603. goto show_error;
  604. }
  605. latency_time_values = malloc(sizeof(jack_time_t) * samples);
  606. if (latency_time_values == NULL) {
  607. error_message = strerror(errno);
  608. error_source = "malloc";
  609. goto free_latency_values;
  610. }
  611. message_1 = malloc(message_size * sizeof(jack_midi_data_t));
  612. if (message_1 == NULL) {
  613. error_message = strerror(errno);
  614. error_source = "malloc";
  615. goto free_latency_time_values;
  616. }
  617. message_2 = malloc(message_size * sizeof(jack_midi_data_t));
  618. if (message_2 == NULL) {
  619. error_message = strerror(errno);
  620. error_source = "malloc";
  621. goto free_message_1;
  622. }
  623. switch (message_size) {
  624. case 1:
  625. message_1[0] = 0xf6;
  626. message_2[0] = 0xfe;
  627. break;
  628. case 2:
  629. message_1[0] = 0xc0;
  630. message_1[1] = 0x00;
  631. message_2[0] = 0xd0;
  632. message_2[1] = 0x7f;
  633. break;
  634. case 3:
  635. message_1[0] = 0x80;
  636. message_1[1] = 0x00;
  637. message_1[2] = 0x00;
  638. message_2[0] = 0x90;
  639. message_2[1] = 0x7f;
  640. message_2[2] = 0x7f;
  641. break;
  642. default:
  643. message_1[0] = 0xf0;
  644. memset(message_1 + 1, 0,
  645. (message_size - 2) * sizeof(jack_midi_data_t));
  646. message_1[message_size - 1] = 0xf7;
  647. message_2[0] = 0xf0;
  648. memset(message_2 + 1, 0x7f,
  649. (message_size - 2) * sizeof(jack_midi_data_t));
  650. message_2[message_size - 1] = 0xf7;
  651. }
  652. client = jack_client_open(program_name, JackNullOption, NULL);
  653. if (client == NULL) {
  654. error_message = "failed to open JACK client";
  655. error_source = "jack_client_open";
  656. goto free_message_2;
  657. }
  658. in_port = jack_port_register(client, "in", JACK_DEFAULT_MIDI_TYPE,
  659. JackPortIsInput, 0);
  660. if (in_port == NULL) {
  661. error_message = "failed to register MIDI-in port";
  662. error_source = "jack_port_register";
  663. goto close_client;
  664. }
  665. out_port = jack_port_register(client, "out", JACK_DEFAULT_MIDI_TYPE,
  666. JackPortIsOutput, 0);
  667. if (out_port == NULL) {
  668. error_message = "failed to register MIDI-out port";
  669. error_source = "jack_port_register";
  670. goto unregister_in_port;
  671. }
  672. if (jack_set_process_callback(client, handle_process, NULL)) {
  673. error_message = "failed to set process callback";
  674. error_source = "jack_set_process_callback";
  675. goto unregister_out_port;
  676. }
  677. if (jack_set_xrun_callback(client, handle_xrun, NULL)) {
  678. error_message = "failed to set xrun callback";
  679. error_source = "jack_set_xrun_callback";
  680. goto unregister_out_port;
  681. }
  682. if (jack_set_port_connect_callback(client, handle_port_connection_change,
  683. NULL)) {
  684. error_message = "failed to set port connection callback";
  685. error_source = "jack_set_port_connect_callback";
  686. goto unregister_out_port;
  687. }
  688. jack_on_shutdown(client, handle_shutdown, NULL);
  689. jack_set_info_function(handle_info);
  690. process_state = 0;
  691. connect_semaphore = create_semaphore(0);
  692. if (connect_semaphore == NULL) {
  693. error_message = get_semaphore_error();
  694. error_source = "create_semaphore";
  695. goto unregister_out_port;
  696. }
  697. init_semaphore = create_semaphore(1);
  698. if (init_semaphore == NULL) {
  699. error_message = get_semaphore_error();
  700. error_source = "create_semaphore";
  701. goto destroy_connect_semaphore;;
  702. }
  703. process_semaphore = create_semaphore(2);
  704. if (process_semaphore == NULL) {
  705. error_message = get_semaphore_error();
  706. error_source = "create_semaphore";
  707. goto destroy_init_semaphore;
  708. }
  709. if (jack_activate(client)) {
  710. error_message = "could not activate client";
  711. error_source = "jack_activate";
  712. goto destroy_process_semaphore;
  713. }
  714. if (name_arg_count) {
  715. if (jack_connect(client, jack_port_name(out_port),
  716. target_out_port_name)) {
  717. error_message = "could not connect MIDI out port";
  718. error_source = "jack_connect";
  719. goto deactivate_client;
  720. }
  721. if (jack_connect(client, target_in_port_name,
  722. jack_port_name(in_port))) {
  723. error_message = "could not connect MIDI in port";
  724. error_source = "jack_connect";
  725. goto deactivate_client;
  726. }
  727. }
  728. if (! register_signal_handler(handle_signal)) {
  729. error_message = strerror(errno);
  730. error_source = "register_signal_handler";
  731. goto deactivate_client;
  732. }
  733. printf("Waiting for connections ...\n");
  734. if (wait_semaphore(connect_semaphore, 1) == -1) {
  735. error_message = get_semaphore_error();
  736. error_source = "wait_semaphore";
  737. goto deactivate_client;
  738. }
  739. if (connections_established) {
  740. printf("Waiting for test completion ...\n\n");
  741. if (wait_semaphore(process_semaphore, 1) == -1) {
  742. error_message = get_semaphore_error();
  743. error_source = "wait_semaphore";
  744. goto deactivate_client;
  745. }
  746. }
  747. if (! register_signal_handler(SIG_DFL)) {
  748. error_message = strerror(errno);
  749. error_source = "register_signal_handler";
  750. goto deactivate_client;
  751. }
  752. if (process_state == 2) {
  753. double average_latency = ((double) total_latency) / samples;
  754. double average_latency_time = total_latency_time / samples;
  755. size_t i;
  756. double latency_plot_offset =
  757. floor(((double) lowest_latency_time) / 100.0) / 10.0;
  758. double sample_rate = (double) jack_get_sample_rate(client);
  759. jack_nframes_t total_jitter = 0;
  760. jack_time_t total_jitter_time = 0;
  761. for (i = 0; i <= 100; i++) {
  762. jitter_plot[i] = 0;
  763. latency_plot[i] = 0;
  764. }
  765. for (i = 0; i < samples; i++) {
  766. double latency_time_value = (double) latency_time_values[i];
  767. double latency_plot_time =
  768. (latency_time_value / 1000.0) - latency_plot_offset;
  769. double jitter_time = ABS(average_latency_time -
  770. latency_time_value);
  771. if (latency_plot_time >= 10.0) {
  772. (latency_plot[100])++;
  773. } else {
  774. (latency_plot[(int) (latency_plot_time * 10.0)])++;
  775. }
  776. if (jitter_time >= 10000.0) {
  777. (jitter_plot[100])++;
  778. } else {
  779. (jitter_plot[(int) (jitter_time / 100.0)])++;
  780. }
  781. total_jitter += ABS(average_latency -
  782. ((double) latency_values[i]));
  783. total_jitter_time += jitter_time;
  784. }
  785. printf("Reported out-port latency: %.2f-%.2f ms (%u-%u frames)\n"
  786. "Reported in-port latency: %.2f-%.2f ms (%u-%u frames)\n"
  787. "Average latency: %.2f ms (%.2f frames)\n"
  788. "Lowest latency: %.2f ms (%u frames)\n"
  789. "Highest latency: %.2f ms (%u frames)\n"
  790. "Peak MIDI jitter: %.2f ms (%u frames)\n"
  791. "Average MIDI jitter: %.2f ms (%.2f frames)\n",
  792. (out_latency_range.min / sample_rate) * 1000.0,
  793. (out_latency_range.max / sample_rate) * 1000.0,
  794. out_latency_range.min, out_latency_range.max,
  795. (in_latency_range.min / sample_rate) * 1000.0,
  796. (in_latency_range.max / sample_rate) * 1000.0,
  797. in_latency_range.min, in_latency_range.max,
  798. average_latency_time / 1000.0, average_latency,
  799. lowest_latency_time / 1000.0, lowest_latency,
  800. highest_latency_time / 1000.0, highest_latency,
  801. (highest_latency_time - lowest_latency_time) / 1000.0,
  802. highest_latency - lowest_latency,
  803. (total_jitter_time / 1000.0) / samples,
  804. ((double) total_jitter) / samples);
  805. printf("\nJitter Plot:\n");
  806. for (i = 0; i < 100; i++) {
  807. if (jitter_plot[i]) {
  808. printf("%.1f - %.1f ms: %zu\n", ((float) i) / 10.0,
  809. ((float) (i + 1)) / 10.0, jitter_plot[i]);
  810. }
  811. }
  812. if (jitter_plot[100]) {
  813. printf(" > 10 ms: %zu\n", jitter_plot[100]);
  814. }
  815. printf("\nLatency Plot:\n");
  816. for (i = 0; i < 100; i++) {
  817. if (latency_plot[i]) {
  818. printf("%.1f - %.1f ms: %zu\n",
  819. latency_plot_offset + (((float) i) / 10.0),
  820. latency_plot_offset + (((float) (i + 1)) / 10.0),
  821. latency_plot[i]);
  822. }
  823. }
  824. if (latency_plot[100]) {
  825. printf(" > %.1f ms: %zu\n", latency_plot_offset + 10.0,
  826. latency_plot[100]);
  827. }
  828. }
  829. deactivate_client:
  830. jack_deactivate(client);
  831. printf("\nMessages sent: %zu\nMessages received: %zu\n", messages_sent,
  832. messages_received);
  833. if (unexpected_messages) {
  834. printf("Unexpected messages received: %zu\n", unexpected_messages);
  835. }
  836. if (xrun_count) {
  837. printf("Xruns: %zu\n", xrun_count);
  838. }
  839. destroy_process_semaphore:
  840. destroy_semaphore(process_semaphore, 2);
  841. destroy_init_semaphore:
  842. destroy_semaphore(init_semaphore, 1);
  843. destroy_connect_semaphore:
  844. destroy_semaphore(connect_semaphore, 0);
  845. unregister_out_port:
  846. jack_port_unregister(client, out_port);
  847. unregister_in_port:
  848. jack_port_unregister(client, in_port);
  849. close_client:
  850. jack_client_close(client);
  851. free_message_2:
  852. free(message_2);
  853. free_message_1:
  854. free(message_1);
  855. free_latency_time_values:
  856. free(latency_time_values);
  857. free_latency_values:
  858. free(latency_values);
  859. if (error_message != NULL) {
  860. show_error:
  861. output_error(error_source, error_message);
  862. exit(EXIT_FAILURE);
  863. }
  864. return EXIT_SUCCESS;
  865. }