Advertisement
Guest User

jacktoalsa.c

a guest
Dec 25th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.71 KB | None | 0 0
  1. // License: GPLv3
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6. #include <jack/jack.h>
  7. #include <alsa/asoundlib.h>
  8.  
  9. #define JACK_CLIENT_NAME "jacktoalsa"
  10. #define ALSA_CARD "default"
  11. #define ALSA_CHANNELS 1
  12. #define ALSA_BIT_DEPTH SND_PCM_FORMAT_S32_LE
  13.  
  14. // jack
  15. jack_client_t *jack_client = NULL;
  16. jack_port_t *output_port_left;
  17. jack_port_t *output_port_right;
  18. jack_port_t *input_port_left;
  19. jack_port_t *input_port_right;
  20. uint32_t buffer_size = 0;
  21. uint32_t sample_rate = 0;
  22.  
  23. // alsa
  24. snd_pcm_t *playback_handle;
  25. bool alsa_init = false;
  26.  
  27. int jack_process(jack_nframes_t nframes, void *arg) {
  28.     if (alsa_init == false) return 0; // wait for alsa
  29.  
  30.     jack_default_audio_sample_t *out_l, *out_r, *in_l, *in_r;
  31.  
  32.     out_l = (jack_default_audio_sample_t *)
  33.         jack_port_get_buffer(output_port_left, nframes);
  34.     out_r = (jack_default_audio_sample_t *)
  35.         jack_port_get_buffer(output_port_right, nframes);
  36.     in_l = (jack_default_audio_sample_t *)
  37.         jack_port_get_buffer(input_port_left, nframes);
  38.     in_r = (jack_default_audio_sample_t *)
  39.         jack_port_get_buffer(input_port_right, nframes);
  40.  
  41.     int err;
  42.  
  43.     err = snd_pcm_writei(playback_handle, in_l, nframes);
  44.     //if (err == -EPIPE) {
  45.         //err = snd_pcm_recover(playback_handle, err, 1);
  46.         //snd_pcm_writei(playback_handle, in_r, nframes);
  47.     //}
  48.  
  49.     //fprintf(stdout, "%f %f\n", new_in_l[0], in_r[0]);
  50.  
  51.     return 0;
  52. }
  53.  
  54. int init_jack() {
  55.     fprintf(stdout, "JACK initialization...\n");
  56.  
  57.     jack_status_t status;
  58.  
  59.     fprintf(stdout, "JACK: opening client\n");
  60.     jack_client = jack_client_open( JACK_CLIENT_NAME,
  61.                                     JackNullOption,
  62.                                     &status,
  63.                                     NULL );
  64.  
  65.     if (jack_client == NULL) {
  66.         fprintf(stderr, "JACK: client open error\n");
  67.         return 1;
  68.     }
  69.  
  70.     if (status & JackNameNotUnique) {
  71.         fprintf(stderr, "JACK: client name already taken"
  72.                         " (" JACK_CLIENT_NAME " already started?)\n");
  73.         return 1;
  74.     }
  75.  
  76.     fprintf(stdout, "JACK: registering ports\n");
  77.     output_port_left = jack_port_register( jack_client, "out_left",
  78.         JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsPhysical, 0);
  79.     output_port_right = jack_port_register( jack_client, "out_right",
  80.         JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsPhysical, 0);
  81.     input_port_left = jack_port_register( jack_client, "in_left",
  82.         JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput | JackPortIsPhysical, 0);
  83.     input_port_right = jack_port_register( jack_client, "in_right",
  84.         JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput | JackPortIsPhysical, 0);
  85.  
  86.     if (output_port_left == NULL || output_port_right == NULL
  87.      || input_port_left == NULL || input_port_right == NULL) {
  88.         fprintf(stderr, "JACK: no more ports available\n");
  89.         return 1;
  90.     }
  91.  
  92.     fprintf(stdout, "JACK: binding process callback\n");
  93.     jack_set_process_callback(jack_client, jack_process, 0);
  94.  
  95.     fprintf(stdout, "JACK: activating client\n");
  96.     if (jack_activate(jack_client)) {
  97.         fprintf(stderr, "JACK: activating client error\n");
  98.         return 1;
  99.     }
  100.  
  101.     fprintf(stdout, "JACK: getting sample rate\n");
  102.     sample_rate = jack_get_sample_rate(jack_client);
  103.  
  104.     fprintf(stdout, "JACK: getting buffer size\n");
  105.     buffer_size = jack_get_buffer_size(jack_client);
  106.  
  107.     fprintf(stdout, "JACK is initialized\n");
  108.     return 0;
  109. }
  110.  
  111. int init_alsa() {
  112.     fprintf(stdout, "ALSA initialization...\n");
  113.  
  114.     int err;
  115.  
  116.     err = snd_pcm_open(&playback_handle, ALSA_CARD, SND_PCM_STREAM_PLAYBACK, 0);
  117.     if (err < 0)
  118.         fprintf(stderr, "ALSA: cannot open pcm playback \"%s\": %s\n",
  119.                          ALSA_CARD, snd_strerror(err) );
  120.  
  121.     fprintf(stdout, "ALSA: start snd_pcm_set_params\n");
  122.     err = snd_pcm_set_params( playback_handle, ALSA_BIT_DEPTH,
  123.                               SND_PCM_ACCESS_RW_INTERLEAVED,
  124.                               ALSA_CHANNELS, sample_rate, 1, 5000000);
  125.     if (err < 0)
  126.         fprintf(stderr, "ALSA: cannot set sound parameters: %s\n", snd_strerror(err));
  127.  
  128.     fprintf(stdout, "ALSA is initialized\n");
  129.     alsa_init = true;
  130.     return 0;
  131. }
  132.  
  133. int main() {
  134.     if (init_jack() != 0) {
  135.         fprintf(stderr, "Cannot initialize JACK\n");
  136.         return EXIT_FAILURE;
  137.     }
  138.  
  139.     if (init_alsa() != 0) {
  140.         fprintf(stderr, "Cannot initialize ALSA\n");
  141.         return EXIT_FAILURE;
  142.     }
  143.  
  144.     fprintf(stdout, "Start main loop...\n");
  145.     while (1) {} // main loop
  146.  
  147.     snd_pcm_drain(playback_handle);
  148.     snd_pcm_close(playback_handle);
  149.     jack_client_close(jack_client);
  150.  
  151.     return EXIT_SUCCESS;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement