jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
3.8KB

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <jack/jack.h>
  6. #include <glib.h>
  7. #include <jack/port.h>
  8. #include <jack/cycles.h>
  9. jack_port_t *input_port;
  10. jack_port_t *output_port;
  11. volatile char *buf;
  12. unsigned long stompsize;
  13. int do_stomp = 0;
  14. pthread_mutex_t foolock = PTHREAD_MUTEX_INITIALIZER;
  15. int
  16. process (jack_nframes_t nframes, void *arg)
  17. {
  18. unsigned long i;
  19. unsigned long long now, then;
  20. jack_default_audio_sample_t *out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
  21. jack_default_audio_sample_t *in = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port, nframes);
  22. now = get_cycles ();
  23. if (pthread_mutex_trylock (&foolock) == 0) {
  24. then = get_cycles ();
  25. if (do_stomp) {
  26. for (i = 0; i < stompsize; ++i) {
  27. buf[i]++;
  28. }
  29. } else {
  30. for (i = 0; i < stompsize; ++i) {
  31. buf[0]++;
  32. }
  33. }
  34. then = get_cycles ();
  35. pthread_mutex_unlock (&foolock);
  36. }
  37. memcpy (out, in, sizeof (jack_default_audio_sample_t) * nframes);
  38. return 0;
  39. }
  40. int
  41. bufsize (jack_nframes_t nframes, void *arg)
  42. {
  43. printf ("the maximum buffer size is now %lu\n", nframes);
  44. return 0;
  45. }
  46. int
  47. srate (jack_nframes_t nframes, void *arg)
  48. {
  49. printf ("the sample rate is now %lu/sec\n", nframes);
  50. return 0;
  51. }
  52. void
  53. jack_shutdown (void *arg)
  54. {
  55. printf ("shutdown by JACK\n");
  56. exit (1);
  57. }
  58. void *
  59. other_thread (void *arg)
  60. {
  61. while (1) {
  62. pthread_mutex_lock (&foolock);
  63. usleep (3000);
  64. pthread_mutex_unlock (&foolock);
  65. usleep (3000);
  66. }
  67. return 0;
  68. }
  69. int
  70. main (int argc, char *argv[])
  71. {
  72. jack_client_t *client;
  73. pthread_t other;
  74. if (argc < 2) {
  75. fprintf (stderr, "usage: jack_simple_client <name>\n");
  76. return 1;
  77. }
  78. /* try to become a client of the JACK server */
  79. if ((client = jack_client_new (argv[1])) == 0) {
  80. fprintf (stderr, "jack server not running?\n");
  81. return 1;
  82. }
  83. stompsize = atoi (argv[2]);
  84. buf = (char *) malloc (sizeof (char) * stompsize);
  85. do_stomp = atoi (argv[3]);
  86. pthread_create (&other, NULL, other_thread, NULL);
  87. /* tell the JACK server to call `process()' whenever
  88. there is work to be done.
  89. */
  90. jack_set_process_callback (client, process, 0);
  91. /* tell the JACK server to call `bufsize()' whenever
  92. the maximum number of frames that will be passed
  93. to `process()' changes
  94. */
  95. jack_set_buffer_size_callback (client, bufsize, 0);
  96. /* tell the JACK server to call `srate()' whenever
  97. the sample rate of the system changes.
  98. */
  99. jack_set_sample_rate_callback (client, srate, 0);
  100. /* tell the JACK server to call `jack_shutdown()' if
  101. it ever shuts down, either entirely, or if it
  102. just decides to stop calling us.
  103. */
  104. jack_on_shutdown (client, jack_shutdown, 0);
  105. /* display the current sample rate. once the client is activated
  106. (see below), you should rely on your own sample rate
  107. callback (see above) for this value.
  108. */
  109. printf ("engine sample rate: %lu\n", jack_get_sample_rate (client));
  110. /* create two ports */
  111. input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  112. output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  113. /* tell the JACK server that we are ready to roll */
  114. if (jack_activate (client)) {
  115. fprintf (stderr, "cannot activate client");
  116. return 1;
  117. }
  118. /* connect the ports. Note: you can't do this before
  119. the client is activated (this may change in the future).
  120. */
  121. if (jack_connect (client, "alsa_pcm:in_1", jack_port_name (input_port))) {
  122. fprintf (stderr, "cannot connect input ports\n");
  123. }
  124. if (jack_connect (client, jack_port_name (output_port), "alsa_pcm:out_1")) {
  125. fprintf (stderr, "cannot connect output ports\n");
  126. }
  127. /* Since this is just a toy, run for a few seconds, then finish */
  128. sleep (10);
  129. jack_client_close (client);
  130. printf ("finished OK\n");
  131. exit (0);
  132. }