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.

351 lines
9.1KB

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