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.

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