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.

778 lines
23KB

  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 <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <getopt.h>
  50. #include <jack/jack.h>
  51. #include <jack/midiport.h>
  52. #ifdef WIN32
  53. #include <windows.h>
  54. #include <unistd.h>
  55. #else
  56. #include <semaphore.h>
  57. #endif
  58. #include <signal.h>
  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_RESERVE = "could not reserve MIDI event on port buffer";
  66. const char *ERROR_SHUTDOWN = "the JACK server has been shutdown";
  67. const char *ERROR_TIMEOUT1 = "timed out while waiting for MIDI message";
  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. const char *error_message;
  75. const char *error_source;
  76. jack_nframes_t highest_latency;
  77. jack_time_t highest_latency_time;
  78. jack_latency_range_t in_latency_range;
  79. jack_port_t *in_port;
  80. semaphore_t init_semaphore;
  81. jack_nframes_t last_activity;
  82. jack_time_t last_activity_time;
  83. jack_time_t *latency_time_values;
  84. jack_nframes_t *latency_values;
  85. jack_nframes_t lowest_latency;
  86. jack_time_t lowest_latency_time;
  87. jack_midi_data_t *message_1;
  88. jack_midi_data_t *message_2;
  89. size_t messages_received;
  90. size_t messages_sent;
  91. size_t message_size;
  92. jack_latency_range_t out_latency_range;
  93. jack_port_t *out_port;
  94. semaphore_t process_semaphore;
  95. int process_state;
  96. char *program_name;
  97. jack_port_t *remote_in_port;
  98. jack_port_t *remote_out_port;
  99. size_t samples;
  100. int timeout;
  101. jack_nframes_t total_latency;
  102. jack_time_t total_latency_time;
  103. size_t unexpected_messages;
  104. size_t xrun_count;
  105. static void signal_handler(int sig)
  106. {
  107. jack_client_close(client);
  108. fprintf(stderr, "signal received, exiting ...\n");
  109. exit(0);
  110. }
  111. #ifdef WIN32
  112. char semaphore_error_msg[1024];
  113. #endif
  114. static void
  115. output_error(const char *source, const char *message);
  116. static void
  117. output_usage();
  118. static void
  119. set_process_error(const char *source, const char *message);
  120. static int
  121. signal_semaphore(semaphore_t semaphore);
  122. static int
  123. wait_semaphore(semaphore_t semaphore, int block);
  124. static semaphore_t
  125. create_semaphore(int id)
  126. {
  127. semaphore_t semaphore;
  128. #ifdef WIN32
  129. semaphore = CreateSemaphore(NULL, 0, 1, NULL);
  130. #elif defined (__APPLE__)
  131. char name[128];
  132. sprintf(name, "midi_sem_%d", id);
  133. semaphore = sem_open(name, O_CREAT, 0777, 0);
  134. if (semaphore == (sem_t *) SEM_FAILED) {
  135. semaphore = NULL;
  136. }
  137. #else
  138. semaphore = malloc(sizeof(semaphore_t));
  139. if (semaphore != NULL) {
  140. if (sem_init(semaphore, 0, 0)) {
  141. free(semaphore);
  142. semaphore = NULL;
  143. }
  144. }
  145. #endif
  146. return semaphore;
  147. }
  148. static void
  149. destroy_semaphore(semaphore_t semaphore, int id)
  150. {
  151. #ifdef WIN32
  152. CloseHandle(semaphore);
  153. #else
  154. sem_destroy(semaphore);
  155. #ifdef __APPLE__
  156. {
  157. char name[128];
  158. sprintf(name, "midi_sem_%d", id);
  159. sem_unlink(name);
  160. }
  161. #else
  162. free(semaphore);
  163. #endif
  164. #endif
  165. }
  166. static void
  167. die(const char *source, const char *error_message)
  168. {
  169. output_error(source, error_message);
  170. output_usage();
  171. exit(EXIT_FAILURE);
  172. }
  173. static const char *
  174. get_semaphore_error()
  175. {
  176. #ifdef WIN32
  177. DWORD error = GetLastError();
  178. if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error,
  179. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  180. semaphore_error_msg, 1024, NULL)) {
  181. snprintf(semaphore_error_msg, 1023, "Unknown OS error code '%d'",
  182. error);
  183. }
  184. return semaphore_error_msg;
  185. #else
  186. return strerror(errno);
  187. #endif
  188. }
  189. static void
  190. handle_info(const char *message)
  191. {
  192. /* Suppress info */
  193. }
  194. static int
  195. handle_process(jack_nframes_t frames, void *arg)
  196. {
  197. jack_midi_data_t *buffer;
  198. jack_midi_event_t event;
  199. jack_nframes_t event_count;
  200. jack_nframes_t event_time;
  201. jack_nframes_t frame;
  202. size_t i;
  203. jack_nframes_t last_frame_time;
  204. jack_midi_data_t *message;
  205. jack_time_t microseconds;
  206. void *port_buffer;
  207. jack_time_t time;
  208. jack_midi_clear_buffer(jack_port_get_buffer(out_port, frames));
  209. switch (process_state) {
  210. case 0:
  211. /* State: initializing */
  212. switch (wait_semaphore(init_semaphore, 0)) {
  213. case -1:
  214. set_process_error(SOURCE_WAIT_SEMAPHORE, get_semaphore_error());
  215. // Fallthrough on purpose
  216. case 0:
  217. return 0;
  218. }
  219. highest_latency = 0;
  220. lowest_latency = 0;
  221. messages_received = 0;
  222. messages_sent = 0;
  223. process_state = 1;
  224. total_latency = 0;
  225. total_latency_time = 0;
  226. unexpected_messages = 0;
  227. xrun_count = 0;
  228. jack_port_get_latency_range(remote_in_port, JackCaptureLatency,
  229. &in_latency_range);
  230. jack_port_get_latency_range(remote_out_port, JackPlaybackLatency,
  231. &out_latency_range);
  232. goto send_message;
  233. case 1:
  234. /* State: processing */
  235. port_buffer = jack_port_get_buffer(in_port, frames);
  236. event_count = jack_midi_get_event_count(port_buffer);
  237. last_frame_time = jack_last_frame_time(client);
  238. for (i = 0; i < event_count; i++) {
  239. jack_midi_event_get(&event, port_buffer, i);
  240. message = (messages_received % 2) ? message_2 : message_1;
  241. if ((event.size == message_size) &&
  242. (! memcmp(message, event.buffer,
  243. message_size * sizeof(jack_midi_data_t)))) {
  244. goto found_message;
  245. }
  246. unexpected_messages++;
  247. }
  248. microseconds = jack_frames_to_time(client, last_frame_time) -
  249. last_activity_time;
  250. if ((microseconds / 1000000) >= timeout) {
  251. set_process_error(SOURCE_PROCESS, ERROR_TIMEOUT1);
  252. }
  253. break;
  254. found_message:
  255. event_time = last_frame_time + event.time;
  256. frame = event_time - last_activity;
  257. time = jack_frames_to_time(client, event_time) - last_activity_time;
  258. if ((! highest_latency) || (frame > highest_latency)) {
  259. highest_latency = frame;
  260. highest_latency_time = time;
  261. }
  262. if ((! lowest_latency) || (frame < lowest_latency)) {
  263. lowest_latency = frame;
  264. lowest_latency_time = time;
  265. }
  266. latency_time_values[messages_received] = time;
  267. latency_values[messages_received] = frame;
  268. total_latency += frame;
  269. total_latency_time += time;
  270. messages_received++;
  271. if (messages_received == samples) {
  272. process_state = 2;
  273. if (! signal_semaphore(process_semaphore)) {
  274. // Sigh ...
  275. die(SOURCE_SIGNAL_SEMAPHORE, get_semaphore_error());
  276. }
  277. break;
  278. }
  279. send_message:
  280. frame = (jack_nframes_t) ((((double) rand()) / RAND_MAX) * frames);
  281. if (frame >= frames) {
  282. frame = frames - 1;
  283. }
  284. port_buffer = jack_port_get_buffer(out_port, frames);
  285. buffer = jack_midi_event_reserve(port_buffer, frame, message_size);
  286. if (buffer == NULL) {
  287. set_process_error(SOURCE_EVENT_RESERVE, ERROR_RESERVE);
  288. break;
  289. }
  290. message = (messages_sent % 2) ? message_2 : message_1;
  291. memcpy(buffer, message, message_size * sizeof(jack_midi_data_t));
  292. last_activity = jack_last_frame_time(client) + frame;
  293. last_activity_time = jack_frames_to_time(client, last_activity);
  294. messages_sent++;
  295. case 2:
  296. /* State: finished - do nothing */
  297. case -1:
  298. /* State: error - do nothing */
  299. ;
  300. }
  301. return 0;
  302. }
  303. static void
  304. handle_shutdown(void *arg)
  305. {
  306. set_process_error(SOURCE_SHUTDOWN, ERROR_SHUTDOWN);
  307. }
  308. static int
  309. handle_xrun(void *arg)
  310. {
  311. xrun_count++;
  312. return 0;
  313. }
  314. static void
  315. output_error(const char *source, const char *message)
  316. {
  317. fprintf(stderr, "%s: %s: %s\n", program_name, source, message);
  318. }
  319. static void
  320. output_usage()
  321. {
  322. fprintf(stderr, "Usage: %s [options] out-port-name in-port-name\n\n"
  323. "\t-h, --help print program usage\n"
  324. "\t-m, --message-size=size set size of MIDI messages to send\n"
  325. "\t-s, --samples=n number of MIDI messages to send\n"
  326. "\t-t, --timeout=seconds wait time before giving up on message\n"
  327. "\n", program_name);
  328. }
  329. static unsigned long
  330. parse_positive_number_arg(char *s, char *name)
  331. {
  332. char *end_ptr;
  333. unsigned long result;
  334. errno = 0;
  335. result = strtoul(s, &end_ptr, 10);
  336. if (errno) {
  337. die(name, strerror(errno));
  338. }
  339. if (*s == '\0') {
  340. die(name, "argument value cannot be empty");
  341. }
  342. if (*end_ptr != '\0') {
  343. die(name, "invalid value");
  344. }
  345. if (! result) {
  346. die(name, "must be a positive number");
  347. }
  348. return result;
  349. }
  350. static void
  351. set_process_error(const char *source, const char *message)
  352. {
  353. error_source = source;
  354. error_message = message;
  355. process_state = -1;
  356. if (! signal_semaphore(process_semaphore)) {
  357. // Sigh
  358. output_error(source, message);
  359. die(SOURCE_SIGNAL_SEMAPHORE, get_semaphore_error());
  360. }
  361. }
  362. static int
  363. signal_semaphore(semaphore_t semaphore)
  364. {
  365. #ifdef WIN32
  366. return ReleaseSemaphore(semaphore, 1, NULL);
  367. #else
  368. return ! sem_post(semaphore);
  369. #endif
  370. }
  371. static int
  372. wait_semaphore(semaphore_t semaphore, int block)
  373. {
  374. #ifdef WIN32
  375. DWORD result = WaitForSingleObject(semaphore, block ? INFINITE : 0);
  376. switch (result) {
  377. case WAIT_OBJECT_0:
  378. return 1;
  379. case WAIT_TIMEOUT:
  380. return 0;
  381. }
  382. return -1;
  383. #else
  384. if (block) {
  385. while (sem_wait(semaphore)) {
  386. if (errno != EINTR) {
  387. return -1;
  388. }
  389. }
  390. } else {
  391. while (sem_trywait(semaphore)) {
  392. switch (errno) {
  393. case EAGAIN:
  394. return 0;
  395. case EINTR:
  396. continue;
  397. default:
  398. return -1;
  399. }
  400. }
  401. }
  402. return 1;
  403. #endif
  404. }
  405. int
  406. main(int argc, char **argv)
  407. {
  408. size_t jitter_plot[101];
  409. size_t latency_plot[101];
  410. int long_index = 0;
  411. struct option long_options[] = {
  412. {"help", 0, NULL, 'h'},
  413. {"message-size", 1, NULL, 'm'},
  414. {"samples", 1, NULL, 's'},
  415. {"timeout", 1, NULL, 't'}
  416. };
  417. char *option_string = "hm:s:t:";
  418. int show_usage = 0;
  419. error_message = NULL;
  420. message_size = 3;
  421. program_name = argv[0];
  422. samples = 1024;
  423. timeout = 5;
  424. for (;;) {
  425. char c = getopt_long(argc, argv, option_string, long_options,
  426. &long_index);
  427. switch (c) {
  428. case 'h':
  429. show_usage = 1;
  430. break;
  431. case 'm':
  432. message_size = parse_positive_number_arg(optarg, "message-size");
  433. break;
  434. case 's':
  435. samples = parse_positive_number_arg(optarg, "samples");
  436. break;
  437. case 't':
  438. timeout = parse_positive_number_arg(optarg, "timeout");
  439. break;
  440. default:
  441. {
  442. char *s = "'- '";
  443. s[2] = c;
  444. die(s, "invalid switch");
  445. }
  446. case -1:
  447. if (show_usage) {
  448. output_usage();
  449. exit(EXIT_SUCCESS);
  450. }
  451. goto parse_port_names;
  452. case 1:
  453. /* end of switch :) */
  454. ;
  455. }
  456. }
  457. parse_port_names:
  458. if ((argc - optind) != 2) {
  459. output_usage();
  460. return EXIT_FAILURE;
  461. }
  462. latency_values = malloc(sizeof(jack_nframes_t) * samples);
  463. if (latency_values == NULL) {
  464. error_message = strerror(errno);
  465. error_source = "malloc";
  466. goto show_error;
  467. }
  468. latency_time_values = malloc(sizeof(jack_time_t) * samples);
  469. if (latency_time_values == NULL) {
  470. error_message = strerror(errno);
  471. error_source = "malloc";
  472. goto free_latency_values;
  473. }
  474. message_1 = malloc(message_size * sizeof(jack_midi_data_t));
  475. if (message_1 == NULL) {
  476. error_message = strerror(errno);
  477. error_source = "malloc";
  478. goto free_latency_time_values;
  479. }
  480. message_2 = malloc(message_size * sizeof(jack_midi_data_t));
  481. if (message_2 == NULL) {
  482. error_message = strerror(errno);
  483. error_source = "malloc";
  484. goto free_message_1;
  485. }
  486. switch (message_size) {
  487. case 1:
  488. message_1[0] = 0xf6;
  489. message_2[0] = 0xfe;
  490. break;
  491. case 2:
  492. message_1[0] = 0xc0;
  493. message_1[1] = 0x00;
  494. message_2[0] = 0xd0;
  495. message_2[1] = 0x7f;
  496. break;
  497. case 3:
  498. message_1[0] = 0x80;
  499. message_1[1] = 0x00;
  500. message_1[2] = 0x00;
  501. message_2[0] = 0x90;
  502. message_2[1] = 0x7f;
  503. message_2[2] = 0x7f;
  504. break;
  505. default:
  506. message_1[0] = 0xf0;
  507. memset(message_1 + 1, 0,
  508. (message_size - 2) * sizeof(jack_midi_data_t));
  509. message_1[message_size - 1] = 0xf7;
  510. message_2[0] = 0xf0;
  511. memset(message_2 + 1, 0x7f,
  512. (message_size - 2) * sizeof(jack_midi_data_t));
  513. message_2[message_size - 1] = 0xf7;
  514. }
  515. /* install a signal handler to properly quits jack client */
  516. #ifdef WIN32
  517. signal(SIGINT, signal_handler);
  518. signal(SIGABRT, signal_handler);
  519. signal(SIGTERM, signal_handler);
  520. #else
  521. signal(SIGQUIT, signal_handler);
  522. signal(SIGTERM, signal_handler);
  523. signal(SIGHUP, signal_handler);
  524. signal(SIGINT, signal_handler);
  525. #endif
  526. client = jack_client_open(program_name, JackNullOption, NULL);
  527. if (client == NULL) {
  528. error_message = "failed to open JACK client";
  529. error_source = "jack_client_open";
  530. goto free_message_2;
  531. }
  532. remote_in_port = jack_port_by_name(client, argv[optind + 1]);
  533. if (remote_in_port == NULL) {
  534. error_message = "invalid port name";
  535. error_source = argv[optind + 1];
  536. goto close_client;
  537. }
  538. remote_out_port = jack_port_by_name(client, argv[optind]);
  539. if (remote_out_port == NULL) {
  540. error_message = "invalid port name";
  541. error_source = argv[optind];
  542. goto close_client;
  543. }
  544. in_port = jack_port_register(client, "in", JACK_DEFAULT_MIDI_TYPE,
  545. JackPortIsInput, 0);
  546. if (in_port == NULL) {
  547. error_message = "failed to register MIDI-in port";
  548. error_source = "jack_port_register";
  549. goto close_client;
  550. }
  551. out_port = jack_port_register(client, "out", JACK_DEFAULT_MIDI_TYPE,
  552. JackPortIsOutput, 0);
  553. if (out_port == NULL) {
  554. error_message = "failed to register MIDI-out port";
  555. error_source = "jack_port_register";
  556. goto unregister_in_port;
  557. }
  558. if (jack_set_process_callback(client, handle_process, NULL)) {
  559. error_message = "failed to set process callback";
  560. error_source = "jack_set_process_callback";
  561. goto unregister_out_port;
  562. }
  563. if (jack_set_xrun_callback(client, handle_xrun, NULL)) {
  564. error_message = "failed to set xrun callback";
  565. error_source = "jack_set_xrun_callback";
  566. goto unregister_out_port;
  567. }
  568. jack_on_shutdown(client, handle_shutdown, NULL);
  569. jack_set_info_function(handle_info);
  570. process_state = 0;
  571. init_semaphore = create_semaphore(0);
  572. if (init_semaphore == NULL) {
  573. error_message = get_semaphore_error();
  574. error_source = "create_semaphore";
  575. goto unregister_out_port;
  576. }
  577. process_semaphore = create_semaphore(1);
  578. if (process_semaphore == NULL) {
  579. error_message = get_semaphore_error();
  580. error_source = "create_semaphore";
  581. goto destroy_init_semaphore;
  582. }
  583. if (jack_activate(client)) {
  584. error_message = "could not activate client";
  585. error_source = "jack_activate";
  586. goto destroy_process_semaphore;
  587. }
  588. if (jack_connect(client, jack_port_name(out_port),
  589. jack_port_name(remote_out_port))) {
  590. error_message = "could not connect MIDI out port";
  591. error_source = "jack_connect";
  592. goto deactivate_client;
  593. }
  594. if (jack_connect(client, jack_port_name(remote_in_port),
  595. jack_port_name(in_port))) {
  596. error_message = "could not connect MIDI in port";
  597. error_source = "jack_connect";
  598. goto deactivate_client;
  599. }
  600. if (! signal_semaphore(init_semaphore)) {
  601. error_message = get_semaphore_error();
  602. error_source = "post_semaphore";
  603. goto deactivate_client;
  604. }
  605. if (wait_semaphore(process_semaphore, 1) == -1) {
  606. error_message = get_semaphore_error();
  607. error_source = "wait_semaphore";
  608. goto deactivate_client;
  609. }
  610. if (process_state == 2) {
  611. double average_latency = ((double) total_latency) / samples;
  612. double average_latency_time = total_latency_time / samples;
  613. size_t i;
  614. double latency_plot_offset =
  615. floor(((double) lowest_latency_time) / 100.0) / 10.0;
  616. double sample_rate = (double) jack_get_sample_rate(client);
  617. jack_nframes_t total_jitter = 0;
  618. jack_time_t total_jitter_time = 0;
  619. for (i = 0; i <= 100; i++) {
  620. jitter_plot[i] = 0;
  621. latency_plot[i] = 0;
  622. }
  623. for (i = 0; i < samples; i++) {
  624. double latency_time_value = (double) latency_time_values[i];
  625. double latency_plot_time =
  626. (latency_time_value / 1000.0) - latency_plot_offset;
  627. double jitter_time = ABS(average_latency_time -
  628. latency_time_value);
  629. if (latency_plot_time >= 10.0) {
  630. (latency_plot[100])++;
  631. } else {
  632. (latency_plot[(int) (latency_plot_time * 10.0)])++;
  633. }
  634. if (jitter_time >= 10000.0) {
  635. (jitter_plot[100])++;
  636. } else {
  637. (jitter_plot[(int) (jitter_time / 100.0)])++;
  638. }
  639. total_jitter += ABS(average_latency -
  640. ((double) latency_values[i]));
  641. total_jitter_time += jitter_time;
  642. }
  643. printf("Reported out-port latency: %.2f-%.2f ms (%u-%u frames)\n"
  644. "Reported in-port latency: %.2f-%.2f ms (%u-%u frames)\n"
  645. "Average latency: %.2f ms (%.2f frames)\n"
  646. "Lowest latency: %.2f ms (%u frames)\n"
  647. "Highest latency: %.2f ms (%u frames)\n"
  648. "Peak MIDI jitter: %.2f ms (%u frames)\n"
  649. "Average MIDI jitter: %.2f ms (%.2f frames)\n",
  650. (out_latency_range.min / sample_rate) * 1000.0,
  651. (out_latency_range.max / sample_rate) * 1000.0,
  652. out_latency_range.min, out_latency_range.max,
  653. (in_latency_range.min / sample_rate) * 1000.0,
  654. (in_latency_range.max / sample_rate) * 1000.0,
  655. in_latency_range.min, in_latency_range.max,
  656. average_latency_time / 1000.0, average_latency,
  657. lowest_latency_time / 1000.0, lowest_latency,
  658. highest_latency_time / 1000.0, highest_latency,
  659. (highest_latency_time - lowest_latency_time) / 1000.0,
  660. highest_latency - lowest_latency,
  661. (total_jitter_time / 1000.0) / samples,
  662. ((double) total_jitter) / samples);
  663. printf("\nJitter Plot:\n");
  664. for (i = 0; i < 100; i++) {
  665. if (jitter_plot[i]) {
  666. printf("%.1f - %.1f ms: %u\n", ((float) i) / 10.0,
  667. ((float) (i + 1)) / 10.0, jitter_plot[i]);
  668. }
  669. }
  670. if (jitter_plot[100]) {
  671. printf(" > 10 ms: %u\n", jitter_plot[100]);
  672. }
  673. printf("\nLatency Plot:\n");
  674. for (i = 0; i < 100; i++) {
  675. if (latency_plot[i]) {
  676. printf("%.1f - %.1f ms: %u\n",
  677. latency_plot_offset + (((float) i) / 10.0),
  678. latency_plot_offset + (((float) (i + 1)) / 10.0),
  679. latency_plot[i]);
  680. }
  681. }
  682. if (latency_plot[100]) {
  683. printf(" > %.1f ms: %u\n", latency_plot_offset + 10.0,
  684. latency_plot[100]);
  685. }
  686. }
  687. printf("\nMessages sent: %d\n"
  688. "Messages received: %d\n",
  689. messages_sent, messages_received);
  690. if (unexpected_messages) {
  691. printf("Unexpected messages received: %d\n", unexpected_messages);
  692. }
  693. if (xrun_count) {
  694. printf("Xruns: %d (messages may have been lost)\n", xrun_count);
  695. }
  696. deactivate_client:
  697. jack_deactivate(client);
  698. destroy_process_semaphore:
  699. destroy_semaphore(process_semaphore, 1);
  700. destroy_init_semaphore:
  701. destroy_semaphore(init_semaphore, 0);
  702. unregister_out_port:
  703. jack_port_unregister(client, out_port);
  704. unregister_in_port:
  705. jack_port_unregister(client, in_port);
  706. close_client:
  707. jack_client_close(client);
  708. free_message_2:
  709. free(message_2);
  710. free_message_1:
  711. free(message_1);
  712. free_latency_time_values:
  713. free(latency_time_values);
  714. free_latency_values:
  715. free(latency_values);
  716. if (error_message != NULL) {
  717. show_error:
  718. output_error(error_source, error_message);
  719. exit(EXIT_FAILURE);
  720. }
  721. return EXIT_SUCCESS;
  722. }