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.

373 lines
8.1KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. $Id$
  15. */
  16. #include <stdio.h>
  17. #include <errno.h>
  18. #include <unistd.h>
  19. #include <sndfile.h>
  20. #include <pthread.h>
  21. #include <glib.h>
  22. #include <getopt.h>
  23. #include <jack/jack.h>
  24. typedef struct _thread_info {
  25. pthread_t thread_id;
  26. SNDFILE *sf;
  27. nframes_t duration;
  28. jack_client_t *client;
  29. unsigned int channels;
  30. int bitdepth;
  31. int can_capture;
  32. char *path;
  33. int status;
  34. int can_process;
  35. } thread_info_t;
  36. unsigned int nports;
  37. jack_port_t **ports;
  38. pthread_mutex_t buffer_lock = PTHREAD_MUTEX_INITIALIZER;
  39. pthread_cond_t data_ready = PTHREAD_COND_INITIALIZER;
  40. typedef struct _sample_buffer {
  41. nframes_t nframes;
  42. sample_t **data;
  43. } sample_buffer_t;
  44. sample_buffer_t *
  45. sample_buffer_new (nframes_t nframes, unsigned int nchans)
  46. {
  47. sample_buffer_t *buf;
  48. unsigned int i;
  49. buf = (sample_buffer_t *) malloc (sizeof (sample_buffer_t));
  50. buf->nframes = nframes;
  51. buf->data = (sample_t **) malloc (sizeof (sample_t *) * nchans);
  52. for (i = 0; i < nchans; i++) {
  53. buf->data[i] = (sample_t *) malloc (sizeof (sample_t) * nframes);
  54. }
  55. return buf;
  56. }
  57. GSList *pending_writes = NULL;
  58. GSList *free_buffers = NULL;
  59. sample_buffer_t *
  60. get_free_buffer (nframes_t nframes, unsigned int nchans)
  61. {
  62. sample_buffer_t *buf;
  63. if (free_buffers == NULL) {
  64. buf = sample_buffer_new (nframes, nchans);
  65. } else {
  66. buf = (sample_buffer_t *) free_buffers->data;
  67. free_buffers = g_slist_next (free_buffers);
  68. }
  69. return buf;
  70. }
  71. sample_buffer_t *
  72. get_write_buffer ()
  73. {
  74. sample_buffer_t *buf;
  75. if (pending_writes == NULL) {
  76. return NULL;
  77. }
  78. buf = (sample_buffer_t *) pending_writes->data;
  79. pending_writes = g_slist_next (pending_writes);
  80. return buf;
  81. }
  82. void
  83. put_write_buffer (sample_buffer_t *buf)
  84. {
  85. pending_writes = g_slist_append (pending_writes, buf);
  86. }
  87. void
  88. put_free_buffer (sample_buffer_t *buf)
  89. {
  90. free_buffers = g_slist_prepend (free_buffers, buf);
  91. }
  92. void *
  93. disk_thread (void *arg)
  94. {
  95. sample_buffer_t *buf;
  96. thread_info_t *info = (thread_info_t *) arg;
  97. int i;
  98. unsigned int chn;
  99. nframes_t total_captured = 0;
  100. int done = 0;
  101. double *fbuf;
  102. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  103. pthread_mutex_lock (&buffer_lock);
  104. /* preload the buffer cache */
  105. for (i = 0; i < 8; i++) {
  106. buf = sample_buffer_new (jack_get_buffer_size (info->client), info->channels);
  107. put_free_buffer (buf);
  108. }
  109. info->status = 0;
  110. while (!done) {
  111. pthread_cond_wait (&data_ready, &buffer_lock);
  112. while ((buf = get_write_buffer ()) != 0) {
  113. pthread_mutex_unlock (&buffer_lock);
  114. /* grrr ... libsndfile doesn't do float data yet, only double */
  115. if (info->can_capture) {
  116. fbuf = (double *) malloc (sizeof (double) * buf->nframes * info->channels);
  117. for (chn = 0; chn < info->channels; chn++) {
  118. for (i = 0; i < buf->nframes; i++) {
  119. fbuf[chn+(i*info->channels)] = buf->data[chn][i];
  120. }
  121. }
  122. if (sf_writef_double (info->sf, fbuf, buf->nframes, 1) != buf->nframes) {
  123. char errstr[256];
  124. sf_error_str (0, errstr, sizeof (errstr) - 1);
  125. fprintf (stderr, "cannot write data to sndfile (%s)\n", errstr);
  126. info->status = -1;
  127. done = 1;
  128. break;
  129. }
  130. free (fbuf);
  131. total_captured += buf->nframes;
  132. if (total_captured >= info->duration) {
  133. printf ("disk thread finished\n");
  134. done = 1;
  135. break;
  136. }
  137. }
  138. pthread_mutex_lock (&buffer_lock);
  139. put_free_buffer (buf);
  140. }
  141. }
  142. pthread_mutex_unlock (&buffer_lock);
  143. return 0;
  144. }
  145. int
  146. process (nframes_t nframes, void *arg)
  147. {
  148. thread_info_t *info = (thread_info_t *) arg;
  149. sample_t *in;
  150. sample_buffer_t *buf;
  151. unsigned int i;
  152. if (!info->can_process) {
  153. return 0;
  154. }
  155. /* we don't like taking locks, but until we have a lock
  156. free ringbuffer written in C, this is what has to be done
  157. */
  158. pthread_mutex_lock (&buffer_lock);
  159. buf = get_free_buffer (nframes, nports);
  160. for (i = 0; i < nports; i++) {
  161. in = (sample_t *) jack_port_get_buffer (ports[i], nframes);
  162. memcpy (buf->data[i], in, sizeof (sample_t) * nframes);
  163. }
  164. put_write_buffer (buf);
  165. /* tell the disk thread that there is work to do */
  166. pthread_cond_signal (&data_ready);
  167. pthread_mutex_unlock (&buffer_lock);
  168. return 0;
  169. }
  170. void
  171. jack_shutdown (void *arg)
  172. {
  173. fprintf (stderr, "JACK shutdown\n");
  174. exit (0);
  175. }
  176. void
  177. setup_disk_thread (thread_info_t *info)
  178. {
  179. SF_INFO sf_info;
  180. sf_info.samplerate = jack_get_sample_rate (info->client);
  181. sf_info.channels = info->channels;
  182. sf_info.format = SF_FORMAT_WAV|SF_FORMAT_PCM;
  183. sf_info.pcmbitwidth = info->bitdepth;
  184. if ((info->sf = sf_open_write (info->path, &sf_info)) == NULL) {
  185. char errstr[256];
  186. sf_error_str (0, errstr, sizeof (errstr) - 1);
  187. fprintf (stderr, "cannot open sndfile \"%s\" for output (%s)\n", info->path, errstr);
  188. jack_client_close (info->client);
  189. exit (1);
  190. }
  191. info->duration *= sf_info.samplerate;
  192. info->can_capture = 0;
  193. pthread_create (&info->thread_id, NULL, disk_thread, info);
  194. }
  195. void
  196. run_disk_thread (thread_info_t *info)
  197. {
  198. info->can_capture = 1;
  199. pthread_join (info->thread_id, NULL);
  200. sf_close (info->sf);
  201. if (info->status) {
  202. unlink (info->path);
  203. }
  204. }
  205. void
  206. setup_ports (int sources, char *source_names[], thread_info_t *info)
  207. {
  208. int i;
  209. nports = sources;
  210. ports = (jack_port_t **) malloc (sizeof (jack_port_t *) * nports);
  211. for (i = 0; i < nports; i++) {
  212. char name[64];
  213. sprintf (name, "input%d", i+1);
  214. if ((ports[i] = jack_port_register (info->client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == 0) {
  215. fprintf (stderr, "cannot register input port \"%s\"!\n", name);
  216. jack_client_close (info->client);
  217. exit (1);
  218. }
  219. }
  220. for (i = 0; i < nports; i++) {
  221. if (jack_connect (info->client, source_names[i], jack_port_name (ports[i]))) {
  222. fprintf (stderr, "cannot connect input port %s to %s\n", jack_port_name (ports[i]), source_names[i]);
  223. jack_client_close (info->client);
  224. exit (1);
  225. }
  226. }
  227. info->can_process = 1;
  228. }
  229. int
  230. main (int argc, char *argv[])
  231. {
  232. jack_client_t *client;
  233. thread_info_t thread_info;
  234. int c;
  235. int longopt_index = 0;
  236. extern int optind, opterr;
  237. int show_usage = 0;
  238. char *optstring = "d:f:b:h";
  239. struct option long_options[] = {
  240. { "help", 1, 0, 'h' },
  241. { "duration", 1, 0, 'd' },
  242. { "file", 1, 0, 'f' },
  243. { "bitdepth", 1, 0, 'b' },
  244. { 0, 0, 0, 0 }
  245. };
  246. memset (&thread_info, 0, sizeof (thread_info));
  247. opterr = 0;
  248. while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
  249. switch (c) {
  250. case 1:
  251. /* getopt signals end of '-' options */
  252. break;
  253. case 'h':
  254. show_usage++;
  255. break;
  256. case 'd':
  257. thread_info.duration = atoi (optarg);
  258. break;
  259. case 'f':
  260. thread_info.path = optarg;
  261. break;
  262. case 'b':
  263. thread_info.bitdepth = atoi (optarg);
  264. break;
  265. default:
  266. fprintf (stderr, "error\n");
  267. show_usage++;
  268. break;
  269. }
  270. }
  271. if (show_usage || thread_info.path == NULL || optind == argc) {
  272. fprintf (stderr, "usage: jackrec -f filename [ -d second ] [ -b bitdepth ] port1 [ port2 ... ]\n");
  273. exit (1);
  274. }
  275. if ((client = jack_client_new ("jackrec")) == 0) {
  276. fprintf (stderr, "jack server not running?\n");
  277. exit (1);
  278. }
  279. thread_info.client = client;
  280. thread_info.channels = argc - optind;
  281. thread_info.can_process = 0;
  282. setup_disk_thread (&thread_info);
  283. jack_set_process_callback (client, process, &thread_info);
  284. jack_on_shutdown (client, jack_shutdown, NULL);
  285. if (jack_activate (client)) {
  286. fprintf (stderr, "cannot activate client");
  287. }
  288. setup_ports (argc - optind, &argv[optind], &thread_info);
  289. run_disk_thread (&thread_info);
  290. jack_client_close (client);
  291. exit (0);
  292. }