File: jackaudio.c

package info (click to toggle)
lebiniou 3.67.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,500 kB
  • sloc: ansic: 28,670; makefile: 1,276; sh: 602; awk: 432; xml: 261; javascript: 23
file content (193 lines) | stat: -rw-r--r-- 5,018 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 *  Copyright 1994-2022 Olivier Girondel
 *
 *  This file is part of lebiniou.
 *
 *  lebiniou is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  lebiniou is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with lebiniou. If not, see <http://www.gnu.org/licenses/>.
 */

#include <jack/jack.h>
#include "context.h"
#include "settings.h"
#include "pthread_utils.h"


uint32_t options = BO_NONE;
uint32_t version = 0;

/* JACK data */
static jack_port_t **input_ports;
static jack_client_t *client;
static const char *source_names[2] = { NULL, NULL };
static const char **ports;

// default input size if not defined in the configuration file
#define INSIZE 1024
static uint16_t insize = INSIZE;
// buffer jack samples here before copying to input->data
static double *data[2];
// number of jack frames to be read to reach input->size
static uint8_t chunks;


static void
jack_shutdown(void *arg)
{
  Context_t *ctx = (Context_t *)arg;
  printf("[!] JACK: server shut down, exiting\n");
  ctx->running = 0;
}


static int
process(jack_nframes_t nframes, void *arg)
{
  int chn;
  uint32_t i;
  Context_t *ctx = (Context_t *)arg;
  jack_default_audio_sample_t *in;
  static uint8_t chunk = 0;
  static uint16_t idx = 0;

  if (!ctx->input->mute) {
    uint16_t idx2 = idx;

    for (chn = 0; chn < 2; chn++) {
      in = jack_port_get_buffer(input_ports[chn], nframes);
      if (NULL != in) {
        for (i = 0; i < nframes; i++, idx++) {
          data[chn][idx] = in[i];
        }
        if (chn == 0) {
          idx = idx2;
        }
      }
    }

    chunk++;
#ifdef DEBUG_JACKAUDIO
    printf("[i] JACK: chunk= %d\n", chunk);
#endif

    if (chunk == chunks) {
#ifdef DEBUG_JACKAUDIO
      printf("[i] JACK: setting input, idx= %d\n", idx);
#endif
      if (!xpthread_mutex_lock(&ctx->input->mutex)) {
        for (i = 0; i < ctx->input->size; i++) {
          ctx->input->data[A_LEFT][i]  = data[0][i];
          ctx->input->data[A_RIGHT][i] = data[1][i];
        }
        Input_set(ctx->input, A_STEREO);
        xpthread_mutex_unlock(&ctx->input->mutex);
        idx = chunk = 0;
      }
    }
  }

  return 0;
}


static void
setup_ports(void)
{
  input_ports = xcalloc(2, sizeof(jack_port_t *));

  for (int i = 0; i < 2; i++) {
    char name[64];

    sprintf(name, "input_%d", i);

    if ((input_ports[i] = jack_port_register(client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == 0) {
      fprintf(stderr, "[!] JACK: cannot register input port \"%s\" !\n", name);
      jack_client_close(client);
      exit(1);
    } else {
      printf("[i] JACK: registered %s\n", name);
    }
  }

  ports = jack_get_ports(client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
  if (NULL == ports) {
    xerror("JACK: no physical capture ports\n");
  }
}


int8_t
create(Context_t *ctx)
{
  if ((client = jack_client_open(PACKAGE, JackNullOption, NULL)) == 0) {
    xerror("JACK server not running ?\n");
  }

  jack_set_process_callback(client, process, ctx);
  jack_on_shutdown(client, jack_shutdown, ctx);

  json_t *input = Settings_get_input();
  if (NULL != input) {
    source_names[0] = json_string_value(json_object_get(input, "jackaudioLeft"));
    source_names[1] = json_string_value(json_object_get(input, "jackaudioRight"));
  } else {
    source_names[0] = JACKAUDIO_DEFAULT_LEFT;
    source_names[1] = JACKAUDIO_DEFAULT_RIGHT;
  }

  printf("[i] JACK: left  capture from %s\n", source_names[0]);
  printf("[i] JACK: right capture from %s\n", source_names[1]);

  setup_ports();

  jack_nframes_t jack_size = jack_get_buffer_size(client);
  printf("[i] JACK: buffer size: %d\n", jack_size);
  if (jack_size >= insize) {
    chunks = 1;
    insize = jack_size;
  } else {
    chunks = insize / jack_size;
  }
  printf("[i] JACK: %d chunks to read\n", chunks);

  ctx->input = Input_new(insize);
  data[0] = xcalloc(insize, sizeof(double));
  data[1] = xcalloc(insize, sizeof(double));

  if (jack_activate(client)) {
    xerror("JACK: cannot activate client\n");
  }

  for (uint8_t i = 0; i < 2; i++) {
    if (jack_connect(client, ports[i], jack_port_name(input_ports[i]))) {
      jack_client_close(client);
      xerror("JACK: can not connect input port %s to %s\n", jack_port_name(input_ports[i]), source_names[i]);
    } else {
      printf("[i] JACK: connected %s to %s\n", source_names[i], jack_port_name(input_ports[i]));
    }
  }
  jack_free(ports);

  return 1;
}


void
destroy(Context_t *ctx)
{
  jack_client_close(client);
  Input_delete(ctx->input);
  xfree(data[0]);
  xfree(data[1]);
  xfree(input_ports);
}