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.

808 lines
24KB

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