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.

350 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 shut down, exiting ...\n");
  136. exit(1);
  137. }
  138. static void
  139. setup_disk_thread (jack_thread_info_t *info)
  140. {
  141. SF_INFO sf_info;
  142. int short_mask;
  143. sf_info.samplerate = jack_get_sample_rate (info->client);
  144. sf_info.channels = info->channels;
  145. switch (info->bitdepth) {
  146. case 8: short_mask = SF_FORMAT_PCM_U8;
  147. break;
  148. case 16: short_mask = SF_FORMAT_PCM_16;
  149. break;
  150. case 24: short_mask = SF_FORMAT_PCM_24;
  151. break;
  152. case 32: short_mask = SF_FORMAT_PCM_32;
  153. break;
  154. default: short_mask = SF_FORMAT_PCM_16;
  155. break;
  156. }
  157. sf_info.format = SF_FORMAT_WAV|short_mask;
  158. if ((info->sf = sf_open (info->path, SFM_WRITE, &sf_info)) == NULL) {
  159. char errstr[256];
  160. sf_error_str (0, errstr, sizeof (errstr) - 1);
  161. fprintf (stderr, "cannot open sndfile \"%s\" for output (%s)\n", info->path, errstr);
  162. jack_client_close (info->client);
  163. exit (1);
  164. }
  165. info->duration *= sf_info.samplerate;
  166. info->can_capture = 0;
  167. pthread_create (&info->thread_id, NULL, disk_thread, info);
  168. }
  169. static void
  170. run_disk_thread (jack_thread_info_t *info)
  171. {
  172. info->can_capture = 1;
  173. pthread_join (info->thread_id, NULL);
  174. sf_close (info->sf);
  175. if (overruns > 0) {
  176. fprintf (stderr,
  177. "jackrec failed with %ld overruns.\n", overruns);
  178. fprintf (stderr, " try a bigger buffer than -B %"
  179. PRIu32 ".\n", info->rb_size);
  180. info->status = EPIPE;
  181. }
  182. }
  183. static void
  184. setup_ports (int sources, char *source_names[], jack_thread_info_t *info)
  185. {
  186. unsigned int i;
  187. size_t in_size;
  188. /* Allocate data structures that depend on the number of ports. */
  189. nports = sources;
  190. ports = (jack_port_t **) malloc (sizeof (jack_port_t *) * nports);
  191. in_size = nports * sizeof (jack_default_audio_sample_t *);
  192. in = (jack_default_audio_sample_t **) malloc (in_size);
  193. rb = jack_ringbuffer_create (nports * sample_size * info->rb_size);
  194. /* When JACK is running realtime, jack_activate() will have
  195. * called mlockall() to lock our pages into memory. But, we
  196. * still need to touch any newly allocated pages before
  197. * process() starts using them. Otherwise, a page fault could
  198. * create a delay that would force JACK to shut us down. */
  199. memset(in, 0, in_size);
  200. memset(rb->buf, 0, rb->size);
  201. for (i = 0; i < nports; i++) {
  202. char name[64];
  203. sprintf (name, "input%d", i+1);
  204. if ((ports[i] = jack_port_register (info->client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == 0) {
  205. fprintf (stderr, "cannot register input port \"%s\"!\n", name);
  206. jack_client_close (info->client);
  207. exit (1);
  208. }
  209. }
  210. for (i = 0; i < nports; i++) {
  211. if (jack_connect (info->client, source_names[i], jack_port_name (ports[i]))) {
  212. fprintf (stderr, "cannot connect input port %s to %s\n", jack_port_name (ports[i]), source_names[i]);
  213. jack_client_close (info->client);
  214. exit (1);
  215. }
  216. }
  217. info->can_process = 1; /* process() can start, now */
  218. }
  219. int
  220. main (int argc, char *argv[])
  221. {
  222. jack_thread_info_t thread_info;
  223. int c;
  224. int longopt_index = 0;
  225. extern int optind, opterr;
  226. int show_usage = 0;
  227. char *optstring = "d:f:b:B:h";
  228. struct option long_options[] = {
  229. { "help", 0, 0, 'h' },
  230. { "duration", 1, 0, 'd' },
  231. { "file", 1, 0, 'f' },
  232. { "bitdepth", 1, 0, 'b' },
  233. { "bufsize", 1, 0, 'B' },
  234. { 0, 0, 0, 0 }
  235. };
  236. memset (&thread_info, 0, sizeof (thread_info));
  237. thread_info.rb_size = DEFAULT_RB_SIZE;
  238. opterr = 0;
  239. while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
  240. switch (c) {
  241. case 1:
  242. /* getopt signals end of '-' options */
  243. break;
  244. case 'h':
  245. show_usage++;
  246. break;
  247. case 'd':
  248. thread_info.duration = atoi (optarg);
  249. break;
  250. case 'f':
  251. thread_info.path = optarg;
  252. break;
  253. case 'b':
  254. thread_info.bitdepth = atoi (optarg);
  255. break;
  256. case 'B':
  257. thread_info.rb_size = atoi (optarg);
  258. break;
  259. default:
  260. fprintf (stderr, "error\n");
  261. show_usage++;
  262. break;
  263. }
  264. }
  265. if (show_usage || thread_info.path == NULL || optind == argc) {
  266. fprintf (stderr, "usage: jackrec -f filename [ -d second ] [ -b bitdepth ] [ -B bufsize ] port1 [ port2 ... ]\n");
  267. exit (1);
  268. }
  269. if ((client = jack_client_open ("jackrec", JackNullOption, NULL)) == 0) {
  270. fprintf (stderr, "JACK server not running?\n");
  271. exit (1);
  272. }
  273. thread_info.client = client;
  274. thread_info.channels = argc - optind;
  275. thread_info.can_process = 0;
  276. setup_disk_thread (&thread_info);
  277. jack_set_process_callback (client, process, &thread_info);
  278. jack_on_shutdown (client, jack_shutdown, &thread_info);
  279. if (jack_activate (client)) {
  280. fprintf (stderr, "cannot activate client");
  281. }
  282. setup_ports (argc - optind, &argv[optind], &thread_info);
  283. /* install a signal handler to properly quits jack client */
  284. signal(SIGQUIT, signal_handler);
  285. signal(SIGTERM, signal_handler);
  286. signal(SIGHUP, signal_handler);
  287. signal(SIGINT, signal_handler);
  288. run_disk_thread (&thread_info);
  289. jack_client_close (client);
  290. jack_ringbuffer_free (rb);
  291. exit (0);
  292. }