Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // License: GPLv3
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <jack/jack.h>
- #include <alsa/asoundlib.h>
- #define JACK_CLIENT_NAME "jacktoalsa"
- #define ALSA_CARD "default"
- #define ALSA_CHANNELS 1
- #define ALSA_BIT_DEPTH SND_PCM_FORMAT_S32_LE
- // jack
- jack_client_t *jack_client = NULL;
- jack_port_t *output_port_left;
- jack_port_t *output_port_right;
- jack_port_t *input_port_left;
- jack_port_t *input_port_right;
- uint32_t buffer_size = 0;
- uint32_t sample_rate = 0;
- // alsa
- snd_pcm_t *playback_handle;
- bool alsa_init = false;
- int jack_process(jack_nframes_t nframes, void *arg) {
- if (alsa_init == false) return 0; // wait for alsa
- jack_default_audio_sample_t *out_l, *out_r, *in_l, *in_r;
- out_l = (jack_default_audio_sample_t *)
- jack_port_get_buffer(output_port_left, nframes);
- out_r = (jack_default_audio_sample_t *)
- jack_port_get_buffer(output_port_right, nframes);
- in_l = (jack_default_audio_sample_t *)
- jack_port_get_buffer(input_port_left, nframes);
- in_r = (jack_default_audio_sample_t *)
- jack_port_get_buffer(input_port_right, nframes);
- int err;
- err = snd_pcm_writei(playback_handle, in_l, nframes);
- //if (err == -EPIPE) {
- //err = snd_pcm_recover(playback_handle, err, 1);
- //snd_pcm_writei(playback_handle, in_r, nframes);
- //}
- //fprintf(stdout, "%f %f\n", new_in_l[0], in_r[0]);
- return 0;
- }
- int init_jack() {
- fprintf(stdout, "JACK initialization...\n");
- jack_status_t status;
- fprintf(stdout, "JACK: opening client\n");
- jack_client = jack_client_open( JACK_CLIENT_NAME,
- JackNullOption,
- &status,
- NULL );
- if (jack_client == NULL) {
- fprintf(stderr, "JACK: client open error\n");
- return 1;
- }
- if (status & JackNameNotUnique) {
- fprintf(stderr, "JACK: client name already taken"
- " (" JACK_CLIENT_NAME " already started?)\n");
- return 1;
- }
- fprintf(stdout, "JACK: registering ports\n");
- output_port_left = jack_port_register( jack_client, "out_left",
- JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsPhysical, 0);
- output_port_right = jack_port_register( jack_client, "out_right",
- JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsPhysical, 0);
- input_port_left = jack_port_register( jack_client, "in_left",
- JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput | JackPortIsPhysical, 0);
- input_port_right = jack_port_register( jack_client, "in_right",
- JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput | JackPortIsPhysical, 0);
- if (output_port_left == NULL || output_port_right == NULL
- || input_port_left == NULL || input_port_right == NULL) {
- fprintf(stderr, "JACK: no more ports available\n");
- return 1;
- }
- fprintf(stdout, "JACK: binding process callback\n");
- jack_set_process_callback(jack_client, jack_process, 0);
- fprintf(stdout, "JACK: activating client\n");
- if (jack_activate(jack_client)) {
- fprintf(stderr, "JACK: activating client error\n");
- return 1;
- }
- fprintf(stdout, "JACK: getting sample rate\n");
- sample_rate = jack_get_sample_rate(jack_client);
- fprintf(stdout, "JACK: getting buffer size\n");
- buffer_size = jack_get_buffer_size(jack_client);
- fprintf(stdout, "JACK is initialized\n");
- return 0;
- }
- int init_alsa() {
- fprintf(stdout, "ALSA initialization...\n");
- int err;
- err = snd_pcm_open(&playback_handle, ALSA_CARD, SND_PCM_STREAM_PLAYBACK, 0);
- if (err < 0)
- fprintf(stderr, "ALSA: cannot open pcm playback \"%s\": %s\n",
- ALSA_CARD, snd_strerror(err) );
- fprintf(stdout, "ALSA: start snd_pcm_set_params\n");
- err = snd_pcm_set_params( playback_handle, ALSA_BIT_DEPTH,
- SND_PCM_ACCESS_RW_INTERLEAVED,
- ALSA_CHANNELS, sample_rate, 1, 5000000);
- if (err < 0)
- fprintf(stderr, "ALSA: cannot set sound parameters: %s\n", snd_strerror(err));
- fprintf(stdout, "ALSA is initialized\n");
- alsa_init = true;
- return 0;
- }
- int main() {
- if (init_jack() != 0) {
- fprintf(stderr, "Cannot initialize JACK\n");
- return EXIT_FAILURE;
- }
- if (init_alsa() != 0) {
- fprintf(stderr, "Cannot initialize ALSA\n");
- return EXIT_FAILURE;
- }
- fprintf(stdout, "Start main loop...\n");
- while (1) {} // main loop
- snd_pcm_drain(playback_handle);
- snd_pcm_close(playback_handle);
- jack_client_close(jack_client);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement