JACK example clients
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.

345 lines
8.8KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2003 Jack O'Quin
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. * 2002/08/23 - modify for libsndfile 1.0.0 <andy@alsaplayer.org>
  16. * 2003/05/26 - use ringbuffers - joq
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <sndfile.h>
  24. #include <pthread.h>
  25. #include <getopt.h>
  26. #include <jack/jack.h>
  27. #include <jack/ringbuffer.h>
  28. typedef struct _thread_info {
  29. pthread_t thread_id;
  30. SNDFILE *sf;
  31. jack_nframes_t duration;
  32. jack_nframes_t rb_size;
  33. jack_client_t *client;
  34. unsigned int channels;
  35. int bitdepth;
  36. char *path;
  37. volatile int can_capture;
  38. volatile int can_process;
  39. volatile int status;
  40. } jack_thread_info_t;
  41. /* JACK data */
  42. unsigned int nports;
  43. jack_port_t **ports;
  44. jack_default_audio_sample_t **in;
  45. jack_nframes_t nframes;
  46. const size_t sample_size = sizeof(jack_default_audio_sample_t);
  47. /* Synchronization between process thread and disk thread. */
  48. #define DEFAULT_RB_SIZE 16384 /* ringbuffer size in frames */
  49. jack_ringbuffer_t *rb;
  50. pthread_mutex_t disk_thread_lock = PTHREAD_MUTEX_INITIALIZER;
  51. pthread_cond_t data_ready = PTHREAD_COND_INITIALIZER;
  52. long overruns = 0;
  53. void *
  54. disk_thread (void *arg)
  55. {
  56. jack_thread_info_t *info = (jack_thread_info_t *) arg;
  57. static jack_nframes_t total_captured = 0;
  58. jack_nframes_t samples_per_frame = info->channels;
  59. size_t bytes_per_frame = samples_per_frame * sample_size;
  60. void *framebuf = malloc (bytes_per_frame);
  61. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  62. pthread_mutex_lock (&disk_thread_lock);
  63. info->status = 0;
  64. while (1) {
  65. /* Write the data one frame at a time. This is
  66. * inefficient, but makes things simpler. */
  67. while (info->can_capture &&
  68. (jack_ringbuffer_read_space (rb) >= bytes_per_frame)) {
  69. jack_ringbuffer_read (rb, framebuf, bytes_per_frame);
  70. if (sf_writef_float (info->sf, framebuf, 1) != 1) {
  71. char errstr[256];
  72. sf_error_str (0, errstr, sizeof (errstr) - 1);
  73. fprintf (stderr,
  74. "cannot write sndfile (%s)\n",
  75. errstr);
  76. info->status = EIO; /* write failed */
  77. goto done;
  78. }
  79. if (++total_captured >= info->duration) {
  80. printf ("disk thread finished\n");
  81. goto done;
  82. }
  83. }
  84. /* wait until process() signals more data */
  85. pthread_cond_wait (&data_ready, &disk_thread_lock);
  86. }
  87. done:
  88. pthread_mutex_unlock (&disk_thread_lock);
  89. free (framebuf);
  90. return 0;
  91. }
  92. int
  93. process (jack_nframes_t nframes, void *arg)
  94. {
  95. int chn;
  96. size_t i;
  97. jack_thread_info_t *info = (jack_thread_info_t *) arg;
  98. /* Do nothing until we're ready to begin. */
  99. if ((!info->can_process) || (!info->can_capture))
  100. return 0;
  101. for (chn = 0; chn < nports; chn++)
  102. in[chn] = jack_port_get_buffer (ports[chn], nframes);
  103. /* Sndfile requires interleaved data. It is simpler here to
  104. * just queue interleaved samples to a single ringbuffer. */
  105. for (i = 0; i < nframes; i++) {
  106. for (chn = 0; chn < nports; chn++) {
  107. if (jack_ringbuffer_write (rb, (void *) (in[chn]+i),
  108. sample_size)
  109. < sample_size)
  110. overruns++;
  111. }
  112. }
  113. /* Tell the disk thread there is work to do. If it is already
  114. * running, the lock will not be available. We can't wait
  115. * here in the process() thread, but we don't need to signal
  116. * in that case, because the disk thread will read all the
  117. * data queued before waiting again. */
  118. if (pthread_mutex_trylock (&disk_thread_lock) == 0) {
  119. pthread_cond_signal (&data_ready);
  120. pthread_mutex_unlock (&disk_thread_lock);
  121. }
  122. return 0;
  123. }
  124. void
  125. jack_shutdown (void *arg)
  126. {
  127. fprintf (stderr, "JACK shutdown\n");
  128. // exit (0);
  129. abort();
  130. }
  131. void
  132. setup_disk_thread (jack_thread_info_t *info)
  133. {
  134. SF_INFO sf_info;
  135. int short_mask;
  136. sf_info.samplerate = jack_get_sample_rate (info->client);
  137. sf_info.channels = info->channels;
  138. switch (info->bitdepth) {
  139. case 8: short_mask = SF_FORMAT_PCM_U8;
  140. break;
  141. case 16: short_mask = SF_FORMAT_PCM_16;
  142. break;
  143. case 24: short_mask = SF_FORMAT_PCM_24;
  144. break;
  145. case 32: short_mask = SF_FORMAT_PCM_32;
  146. break;
  147. default: short_mask = SF_FORMAT_PCM_16;
  148. break;
  149. }
  150. sf_info.format = SF_FORMAT_WAV|short_mask;
  151. if ((info->sf = sf_open (info->path, SFM_WRITE, &sf_info)) == NULL) {
  152. char errstr[256];
  153. sf_error_str (0, errstr, sizeof (errstr) - 1);
  154. fprintf (stderr, "cannot open sndfile \"%s\" for output (%s)\n", info->path, errstr);
  155. jack_client_close (info->client);
  156. exit (1);
  157. }
  158. if (info->duration == 0) {
  159. info->duration = JACK_MAX_FRAMES;
  160. } else {
  161. info->duration *= sf_info.samplerate;
  162. }
  163. info->can_capture = 0;
  164. pthread_create (&info->thread_id, NULL, disk_thread, info);
  165. }
  166. void
  167. run_disk_thread (jack_thread_info_t *info)
  168. {
  169. info->can_capture = 1;
  170. pthread_join (info->thread_id, NULL);
  171. sf_close (info->sf);
  172. if (overruns > 0) {
  173. fprintf (stderr,
  174. "jackrec failed with %ld overruns.\n", overruns);
  175. fprintf (stderr, " try a bigger buffer than -B %"
  176. PRIu32 ".\n", info->rb_size);
  177. info->status = EPIPE;
  178. }
  179. }
  180. void
  181. setup_ports (int sources, char *source_names[], jack_thread_info_t *info)
  182. {
  183. unsigned int i;
  184. size_t in_size;
  185. /* Allocate data structures that depend on the number of ports. */
  186. nports = sources;
  187. ports = (jack_port_t **) malloc (sizeof (jack_port_t *) * nports);
  188. in_size = nports * sizeof (jack_default_audio_sample_t *);
  189. in = (jack_default_audio_sample_t **) malloc (in_size);
  190. rb = jack_ringbuffer_create (nports * sample_size * info->rb_size);
  191. /* When JACK is running realtime, jack_activate() will have
  192. * called mlockall() to lock our pages into memory. But, we
  193. * still need to touch any newly allocated pages before
  194. * process() starts using them. Otherwise, a page fault could
  195. * create a delay that would force JACK to shut us down. */
  196. memset(in, 0, in_size);
  197. memset(rb->buf, 0, rb->size);
  198. for (i = 0; i < nports; i++) {
  199. char name[64];
  200. sprintf (name, "input%d", i+1);
  201. if ((ports[i] = jack_port_register (info->client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == 0) {
  202. fprintf (stderr, "cannot register input port \"%s\"!\n", name);
  203. jack_client_close (info->client);
  204. exit (1);
  205. }
  206. }
  207. for (i = 0; i < nports; i++) {
  208. if (jack_connect (info->client, source_names[i], jack_port_name (ports[i]))) {
  209. fprintf (stderr, "cannot connect input port %s to %s\n", jack_port_name (ports[i]), source_names[i]);
  210. jack_client_close (info->client);
  211. exit (1);
  212. }
  213. }
  214. info->can_process = 1; /* process() can start, now */
  215. }
  216. int
  217. main (int argc, char *argv[])
  218. {
  219. jack_client_t *client;
  220. jack_thread_info_t thread_info;
  221. int c;
  222. int longopt_index = 0;
  223. extern int optind, opterr;
  224. int show_usage = 0;
  225. char *optstring = "d:f:b:B:h";
  226. struct option long_options[] = {
  227. { "help", 0, 0, 'h' },
  228. { "duration", 1, 0, 'd' },
  229. { "file", 1, 0, 'f' },
  230. { "bitdepth", 1, 0, 'b' },
  231. { "bufsize", 1, 0, 'B' },
  232. { 0, 0, 0, 0 }
  233. };
  234. memset (&thread_info, 0, sizeof (thread_info));
  235. thread_info.rb_size = DEFAULT_RB_SIZE;
  236. opterr = 0;
  237. while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
  238. switch (c) {
  239. case 1:
  240. /* getopt signals end of '-' options */
  241. break;
  242. case 'h':
  243. show_usage++;
  244. break;
  245. case 'd':
  246. thread_info.duration = atoi (optarg);
  247. break;
  248. case 'f':
  249. thread_info.path = optarg;
  250. break;
  251. case 'b':
  252. thread_info.bitdepth = atoi (optarg);
  253. break;
  254. case 'B':
  255. thread_info.rb_size = atoi (optarg);
  256. break;
  257. default:
  258. fprintf (stderr, "error\n");
  259. show_usage++;
  260. break;
  261. }
  262. }
  263. if (show_usage || thread_info.path == NULL || optind == argc) {
  264. fprintf (stderr, "usage: jackrec -f filename [ -d second ] [ -b bitdepth ] [ -B bufsize ] port1 [ port2 ... ]\n");
  265. exit (1);
  266. }
  267. if ((client = jack_client_open ("jackrec", JackNullOption, NULL)) == 0) {
  268. fprintf (stderr, "jack server not running?\n");
  269. exit (1);
  270. }
  271. thread_info.client = client;
  272. thread_info.channels = argc - optind;
  273. thread_info.can_process = 0;
  274. setup_disk_thread (&thread_info);
  275. jack_set_process_callback (client, process, &thread_info);
  276. jack_on_shutdown (client, jack_shutdown, &thread_info);
  277. if (jack_activate (client)) {
  278. fprintf (stderr, "cannot activate client");
  279. }
  280. setup_ports (argc - optind, &argv[optind], &thread_info);
  281. run_disk_thread (&thread_info);
  282. jack_client_close (client);
  283. jack_ringbuffer_free (rb);
  284. exit (0);
  285. }