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.

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