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.

344 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. $Id$
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <unistd.h>
  24. #include <sndfile.h>
  25. #include <pthread.h>
  26. #include <getopt.h>
  27. #include <jack/jack.h>
  28. #include "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. } 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. 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. void *
  55. disk_thread (void *arg)
  56. {
  57. thread_info_t *info = (thread_info_t *) arg;
  58. static jack_nframes_t total_captured = 0;
  59. jack_nframes_t samples_per_frame = info->channels;
  60. size_t bytes_per_frame = samples_per_frame * sample_size;
  61. void *framebuf = malloc (bytes_per_frame);
  62. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  63. pthread_mutex_lock (&disk_thread_lock);
  64. info->status = 0;
  65. while (1) {
  66. /* Write the data one frame at a time. This is
  67. * inefficient, but makes things simpler. */
  68. while (info->can_capture &&
  69. (ringbuffer_read_space (rb) >= bytes_per_frame)) {
  70. ringbuffer_read (rb, framebuf, bytes_per_frame);
  71. if (sf_writef_float (info->sf, framebuf, 1) != 1) {
  72. char errstr[256];
  73. sf_error_str (0, errstr, sizeof (errstr) - 1);
  74. fprintf (stderr,
  75. "cannot write sndfile (%s)\n",
  76. errstr);
  77. info->status = EIO; /* write failed */
  78. goto done;
  79. }
  80. if (++total_captured >= info->duration) {
  81. printf ("disk thread finished\n");
  82. goto done;
  83. }
  84. }
  85. /* wait until process() signals more data */
  86. pthread_cond_wait (&data_ready, &disk_thread_lock);
  87. }
  88. done:
  89. pthread_mutex_unlock (&disk_thread_lock);
  90. free (framebuf);
  91. return 0;
  92. }
  93. int
  94. process (jack_nframes_t nframes, void *arg)
  95. {
  96. int chn;
  97. size_t i;
  98. thread_info_t *info = (thread_info_t *) arg;
  99. /* Do nothing until we're ready to begin. */
  100. if ((!info->can_process) || (!info->can_capture))
  101. return 0;
  102. for (chn = 0; chn < nports; chn++)
  103. in[chn] = jack_port_get_buffer (ports[chn], nframes);
  104. /* Sndfile requires interleaved data. It is simpler here to
  105. * just queue interleaved samples to a single ringbuffer. */
  106. for (i = 0; i < nframes; i++) {
  107. for (chn = 0; chn < nports; chn++) {
  108. if (ringbuffer_write (rb, (void *) (in[chn]+i),
  109. sample_size)
  110. < sample_size)
  111. overruns++;
  112. }
  113. }
  114. /* Tell the disk thread there is work to do. If it is already
  115. * running, the lock will not be available. We can't wait
  116. * here in the process() thread, but we don't need to signal
  117. * in that case, because the disk thread will read all the
  118. * data queued before waiting again. */
  119. if (pthread_mutex_trylock (&disk_thread_lock) == 0) {
  120. pthread_cond_signal (&data_ready);
  121. pthread_mutex_unlock (&disk_thread_lock);
  122. }
  123. return 0;
  124. }
  125. void
  126. jack_shutdown (void *arg)
  127. {
  128. fprintf (stderr, "JACK shutdown\n");
  129. // exit (0);
  130. abort();
  131. }
  132. void
  133. setup_disk_thread (thread_info_t *info)
  134. {
  135. SF_INFO sf_info;
  136. int short_mask;
  137. sf_info.samplerate = jack_get_sample_rate (info->client);
  138. sf_info.channels = info->channels;
  139. switch (info->bitdepth) {
  140. case 8: short_mask = SF_FORMAT_PCM_U8;
  141. break;
  142. case 16: short_mask = SF_FORMAT_PCM_16;
  143. break;
  144. case 24: short_mask = SF_FORMAT_PCM_24;
  145. break;
  146. case 32: short_mask = SF_FORMAT_PCM_32;
  147. break;
  148. default: short_mask = SF_FORMAT_PCM_16;
  149. break;
  150. }
  151. sf_info.format = SF_FORMAT_WAV|short_mask;
  152. if ((info->sf = sf_open (info->path, SFM_WRITE, &sf_info)) == NULL) {
  153. char errstr[256];
  154. sf_error_str (0, errstr, sizeof (errstr) - 1);
  155. fprintf (stderr, "cannot open sndfile \"%s\" for output (%s)\n", info->path, errstr);
  156. jack_client_close (info->client);
  157. exit (1);
  158. }
  159. info->duration *= sf_info.samplerate;
  160. info->can_capture = 0;
  161. pthread_create (&info->thread_id, NULL, disk_thread, info);
  162. }
  163. void
  164. run_disk_thread (thread_info_t *info)
  165. {
  166. info->can_capture = 1;
  167. pthread_join (info->thread_id, NULL);
  168. sf_close (info->sf);
  169. if (overruns > 0) {
  170. fprintf (stderr,
  171. "jackrec failed with %ld overruns.\n", overruns);
  172. fprintf (stderr, " try a bigger buffer than -B %ld.\n",
  173. info->rb_size);
  174. info->status = EPIPE;
  175. }
  176. if (info->status) {
  177. unlink (info->path);
  178. }
  179. }
  180. void
  181. setup_ports (int sources, char *source_names[], 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 = 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. 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_new ("jackrec")) == 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. ringbuffer_free (rb);
  284. exit (0);
  285. }