Jack2  1.9.8
JackAudioAdapter.cpp
1 /*
2 Copyright (C) 2008 Grame
3 
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 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 
18 */
19 
20 #include "JackAudioAdapter.h"
21 #include "JackError.h"
22 #include "JackCompilerDeps.h"
23 #include "JackTools.h"
24 #include "JackTime.h"
25 #include "jslist.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 
30 using namespace std;
31 
32 namespace Jack
33 {
34 
35 int JackAudioAdapter::Process(jack_nframes_t frames, void* arg)
36 {
37  JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
38  jack_default_audio_sample_t* inputBuffer[adapter->fAudioAdapter->GetInputs()];
39  jack_default_audio_sample_t* outputBuffer[adapter->fAudioAdapter->GetOutputs()];
40 
41  // Always clear output
42  for (int i = 0; i < adapter->fAudioAdapter->GetInputs(); i++) {
43  inputBuffer[i] = (jack_default_audio_sample_t*)jack_port_get_buffer(adapter->fCapturePortList[i], frames);
44  memset(inputBuffer[i], 0, frames * sizeof(jack_default_audio_sample_t));
45  }
46 
47  for (int i = 0; i < adapter->fAudioAdapter->GetOutputs(); i++) {
48  outputBuffer[i] = (jack_default_audio_sample_t*)jack_port_get_buffer(adapter->fPlaybackPortList[i], frames);
49  }
50 
51  adapter->fAudioAdapter->PullAndPush(inputBuffer, outputBuffer, frames);
52  return 0;
53 }
54 
55 int JackAudioAdapter::BufferSize(jack_nframes_t buffer_size, void* arg)
56 {
57  JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
58  adapter->Reset();
59  adapter->fAudioAdapter->SetHostBufferSize(buffer_size);
60  return 0;
61 }
62 
63 int JackAudioAdapter::SampleRate(jack_nframes_t sample_rate, void* arg)
64 {
65  JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
66  adapter->Reset();
67  adapter->fAudioAdapter->SetHostSampleRate(sample_rate);
68  return 0;
69 }
70 
71 void JackAudioAdapter::Latency(jack_latency_callback_mode_t mode, void* arg)
72 {
73  JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
74 
75  if (mode == JackCaptureLatency) {
76  for (int i = 0; i < adapter->fAudioAdapter->GetInputs(); i++) {
78  range.min = range.max = adapter->fAudioAdapter->GetInputLatency(i);
79  jack_port_set_latency_range(adapter->fCapturePortList[i], JackCaptureLatency, &range);
80  }
81 
82  } else {
83  for (int i = 0; i < adapter->fAudioAdapter->GetOutputs(); i++) {
85  range.min = range.max = adapter->fAudioAdapter->GetOutputLatency(i);
86  jack_port_set_latency_range(adapter->fPlaybackPortList[i], JackPlaybackLatency, &range);
87  }
88  }
89 }
90 
91 JackAudioAdapter::JackAudioAdapter(jack_client_t* client, JackAudioAdapterInterface* audio_io, const JSList* params)
92  :fClient(client), fAudioAdapter(audio_io)
93 {
94  const JSList* node;
95  const jack_driver_param_t* param;
96  fAutoConnect = false;
97 
98  for (node = params; node; node = jack_slist_next(node)) {
99  param = (const jack_driver_param_t*)node->data;
100  switch (param->character) {
101  case 'c':
102  fAutoConnect = true;
103  break;
104  }
105  }
106 }
107 
108 JackAudioAdapter::~JackAudioAdapter()
109 {
110  // When called, Close has already been used for the client, thus ports are already unregistered.
111  delete fAudioAdapter;
112 }
113 
114 void JackAudioAdapter::FreePorts()
115 {
116  for (int i = 0; i < fAudioAdapter->GetInputs(); i++) {
117  if (fCapturePortList[i]) {
118  jack_port_unregister(fClient, fCapturePortList[i]);
119  }
120  }
121  for (int i = 0; i < fAudioAdapter->GetOutputs(); i++) {
122  if (fPlaybackPortList[i]) {
123  jack_port_unregister(fClient, fPlaybackPortList[i]);
124  }
125  }
126 
127  delete[] fCapturePortList;
128  delete[] fPlaybackPortList;
129 }
130 
131 void JackAudioAdapter::ConnectPorts()
132 {
133  const char** ports;
134 
135  ports = jack_get_ports(fClient, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
136  if (ports != NULL) {
137  for (int i = 0; i < fAudioAdapter->GetInputs() && ports[i]; i++) {
138  jack_connect(fClient, jack_port_name(fCapturePortList[i]), ports[i]);
139  }
140  jack_free(ports);
141  }
142 
143  ports = jack_get_ports(fClient, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
144  if (ports != NULL) {
145  for (int i = 0; i < fAudioAdapter->GetOutputs() && ports[i]; i++) {
146  jack_connect(fClient, ports[i], jack_port_name(fPlaybackPortList[i]));
147  }
148  jack_free(ports);
149  }
150 }
151 
152 void JackAudioAdapter::Reset()
153 {
154  fAudioAdapter->Reset();
155 }
156 
157 int JackAudioAdapter::Open()
158 {
159  char name[32];
160  jack_log("JackAudioAdapter::Open fCaptureChannels %d fPlaybackChannels %d", fAudioAdapter->GetInputs(), fAudioAdapter->GetOutputs());
161  fAudioAdapter->Create();
162 
163  //jack ports
164  fCapturePortList = new jack_port_t*[fAudioAdapter->GetInputs()];
165  fPlaybackPortList = new jack_port_t*[fAudioAdapter->GetOutputs()];
166 
167  for (int i = 0; i < fAudioAdapter->GetInputs(); i++) {
168  snprintf(name, sizeof(name), "capture_%d", i + 1);
169  if ((fCapturePortList[i] = jack_port_register(fClient, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, 0)) == NULL) {
170  goto fail;
171  }
172  }
173 
174  for (int i = 0; i < fAudioAdapter->GetOutputs(); i++) {
175  snprintf(name, sizeof(name), "playback_%d", i + 1);
176  if ((fPlaybackPortList[i] = jack_port_register(fClient, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, 0)) == NULL) {
177  goto fail;
178  }
179  }
180 
181  //callbacks and activation
182  if (jack_set_process_callback(fClient, Process, this) < 0) {
183  goto fail;
184  }
185  if (jack_set_buffer_size_callback(fClient, BufferSize, this) < 0) {
186  goto fail;
187  }
188  if (jack_set_sample_rate_callback(fClient, SampleRate, this) < 0) {
189  goto fail;
190  }
191  if (jack_set_latency_callback(fClient, Latency, this) < 0) {
192  goto fail;
193  }
194  if (jack_activate(fClient) < 0) {
195  goto fail;
196  }
197 
198  if (fAutoConnect) {
199  ConnectPorts();
200  }
201 
202  // Ring buffers are now allocated...
203  return fAudioAdapter->Open();
204  return 0;
205 
206 fail:
207  FreePorts();
208  fAudioAdapter->Destroy();
209  return -1;
210 }
211 
212 int JackAudioAdapter::Close()
213 {
214  fAudioAdapter->Close();
215  fAudioAdapter->Destroy();
216  return 0;
217 }
218 
219 } //namespace