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.

340 lines
8.7KB

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