2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 Grame.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 (at your option) any later version.
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "JackSystemDeps.h"
22 #include "JackAudioDriver.h"
24 #include "JackError.h"
25 #include "JackEngineControl.h"
27 #include "JackGraphManager.h"
28 #include "JackLockedEngine.h"
29 #include "JackException.h"
35 JackAudioDriver::JackAudioDriver(const char* name
, const char* alias
, JackLockedEngine
* engine
, JackSynchro
* table
)
36 : JackDriver(name
, alias
, engine
, table
),
39 fWithMonitorPorts(false)
42 JackAudioDriver::~JackAudioDriver()
45 int JackAudioDriver::SetBufferSize(jack_nframes_t buffer_size
)
47 fEngineControl
->fBufferSize
= buffer_size
;
48 fGraphManager
->SetBufferSize(buffer_size
);
49 fEngineControl
->fPeriodUsecs
= jack_time_t(1000000.f
/ fEngineControl
->fSampleRate
* fEngineControl
->fBufferSize
); // in microsec
50 if (!fEngineControl
->fTimeOut
)
51 fEngineControl
->fTimeOutUsecs
= jack_time_t(2.f
* fEngineControl
->fPeriodUsecs
);
55 int JackAudioDriver::SetSampleRate(jack_nframes_t sample_rate
)
57 fEngineControl
->fSampleRate
= sample_rate
;
58 fEngineControl
->fPeriodUsecs
= jack_time_t(1000000.f
/ fEngineControl
->fSampleRate
* fEngineControl
->fBufferSize
); // in microsec
59 if (!fEngineControl
->fTimeOut
)
60 fEngineControl
->fTimeOutUsecs
= jack_time_t(2.f
* fEngineControl
->fPeriodUsecs
);
64 int JackAudioDriver::Open(jack_nframes_t buffer_size
,
65 jack_nframes_t samplerate
,
71 const char* capture_driver_name
,
72 const char* playback_driver_name
,
73 jack_nframes_t capture_latency
,
74 jack_nframes_t playback_latency
)
76 fCaptureChannels
= inchannels
;
77 fPlaybackChannels
= outchannels
;
78 fWithMonitorPorts
= monitor
;
79 return JackDriver::Open(buffer_size
, samplerate
, capturing
, playing
, inchannels
, outchannels
, monitor
, capture_driver_name
, playback_driver_name
, capture_latency
, playback_latency
);
82 int JackAudioDriver::Open(bool capturing
,
87 const char* capture_driver_name
,
88 const char* playback_driver_name
,
89 jack_nframes_t capture_latency
,
90 jack_nframes_t playback_latency
)
92 fCaptureChannels
= inchannels
;
93 fPlaybackChannels
= outchannels
;
94 fWithMonitorPorts
= monitor
;
95 return JackDriver::Open(capturing
, playing
, inchannels
, outchannels
, monitor
, capture_driver_name
, playback_driver_name
, capture_latency
, playback_latency
);
98 int JackAudioDriver::Attach()
101 jack_port_id_t port_index
;
102 char name
[JACK_CLIENT_NAME_SIZE
+ JACK_PORT_NAME_SIZE
];
103 char alias
[JACK_CLIENT_NAME_SIZE
+ JACK_PORT_NAME_SIZE
];
104 unsigned long port_flags
= JackPortIsOutput
| JackPortIsPhysical
| JackPortIsTerminal
;
107 jack_log("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl
->fBufferSize
, fEngineControl
->fSampleRate
);
109 for (i
= 0; i
< fCaptureChannels
; i
++) {
110 snprintf(alias
, sizeof(alias
) - 1, "%s:%s:out%d", fAliasName
, fCaptureDriverName
, i
+ 1);
111 snprintf(name
, sizeof(name
) - 1, "%s:capture_%d", fClientControl
.fName
, i
+ 1);
112 if ((port_index
= fGraphManager
->AllocatePort(fClientControl
.fRefNum
, name
, JACK_DEFAULT_AUDIO_TYPE
, (JackPortFlags
)port_flags
, fEngineControl
->fBufferSize
)) == NO_PORT
) {
113 jack_error("driver: cannot register port for %s", name
);
116 port
= fGraphManager
->GetPort(port_index
);
117 port
->SetAlias(alias
);
118 port
->SetLatency(fEngineControl
->fBufferSize
+ fCaptureLatency
);
119 fCapturePortList
[i
] = port_index
;
120 jack_log("JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index
);
123 port_flags
= JackPortIsInput
| JackPortIsPhysical
| JackPortIsTerminal
;
125 for (i
= 0; i
< fPlaybackChannels
; i
++) {
126 snprintf(alias
, sizeof(alias
) - 1, "%s:%s:in%d", fAliasName
, fPlaybackDriverName
, i
+ 1);
127 snprintf(name
, sizeof(name
) - 1, "%s:playback_%d", fClientControl
.fName
, i
+ 1);
128 if ((port_index
= fGraphManager
->AllocatePort(fClientControl
.fRefNum
, name
, JACK_DEFAULT_AUDIO_TYPE
, (JackPortFlags
)port_flags
, fEngineControl
->fBufferSize
)) == NO_PORT
) {
129 jack_error("driver: cannot register port for %s", name
);
132 port
= fGraphManager
->GetPort(port_index
);
133 port
->SetAlias(alias
);
134 // Add more latency if "async" mode is used...
135 port
->SetLatency(fEngineControl
->fBufferSize
+ ((fEngineControl
->fSyncMode
) ? 0 : fEngineControl
->fBufferSize
) + fPlaybackLatency
);
136 fPlaybackPortList
[i
] = port_index
;
137 jack_log("JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index
);
140 if (fWithMonitorPorts
) {
141 jack_log("Create monitor port ");
142 snprintf(name
, sizeof(name
) - 1, "%s:monitor_%u", fClientControl
.fName
, i
+ 1);
143 if ((port_index
= fGraphManager
->AllocatePort(fClientControl
.fRefNum
, name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, fEngineControl
->fBufferSize
)) == NO_PORT
) {
144 jack_error("Cannot register monitor port for %s", name
);
147 port
= fGraphManager
->GetPort(port_index
);
148 port
->SetAlias(alias
);
149 port
->SetLatency(fEngineControl
->fBufferSize
);
150 fMonitorPortList
[i
] = port_index
;
158 int JackAudioDriver::Detach()
161 jack_log("JackAudioDriver::Detach");
163 for (i
= 0; i
< fCaptureChannels
; i
++) {
164 fGraphManager
->ReleasePort(fClientControl
.fRefNum
, fCapturePortList
[i
]);
167 for (i
= 0; i
< fPlaybackChannels
; i
++) {
168 fGraphManager
->ReleasePort(fClientControl
.fRefNum
, fPlaybackPortList
[i
]);
169 if (fWithMonitorPorts
)
170 fGraphManager
->ReleasePort(fClientControl
.fRefNum
, fMonitorPortList
[i
]);
176 int JackAudioDriver::Write()
178 for (int i
= 0; i
< fPlaybackChannels
; i
++) {
179 if (fGraphManager
->GetConnectionsNum(fPlaybackPortList
[i
]) > 0) {
180 float* buffer
= GetOutputBuffer(i
);
181 int size
= sizeof(float) * fEngineControl
->fBufferSize
;
183 if (fWithMonitorPorts
&& fGraphManager
->GetConnectionsNum(fMonitorPortList
[i
]) > 0)
184 memcpy(GetMonitorBuffer(i
), buffer
, size
);
190 int JackAudioDriver::ProcessNull()
192 // Keep begin cycle time
193 JackDriver::CycleTakeBeginTime();
195 if (fEngineControl
->fSyncMode
) {
201 // Keep end cycle time
202 JackDriver::CycleTakeEndTime();
203 WaitUntilNextCycle();
207 int JackAudioDriver::Process()
209 return (fEngineControl
->fSyncMode
) ? ProcessSync() : ProcessAsync();
213 The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
214 synchronize to the end of client graph execution.
217 int JackAudioDriver::ProcessAsync()
219 // Read input buffers for the current cycle
221 jack_error("JackAudioDriver::ProcessAsync: read error, skip cycle");
222 return 0; // Skip cycle, but continue processing...
225 // Write output buffers from the previous cycle
227 jack_error("JackAudioDriver::ProcessAsync: write error, skip cycle");
228 return 0; // Skip cycle, but continue processing...
234 fGraphManager
->ResumeRefNum(&fClientControl
, fSynchroTable
);
237 // Keep end cycle time
238 JackDriver::CycleTakeEndTime();
243 The driver SYNC mode: the server does synchronize to the end of client graph execution,
244 output buffers computed at the *current cycle* are used.
247 int JackAudioDriver::ProcessSync()
249 // Read input buffers for the current cycle
251 jack_error("JackAudioDriver::ProcessSync: read error, skip cycle");
252 return 0; // Skip cycle, but continue processing...
258 fGraphManager
->ResumeRefNum(&fClientControl
, fSynchroTable
);
261 // Write output buffers for the current cycle
263 jack_error("JackAudioDriver::ProcessSync: write error, skip cycle");
264 return 0; // Skip cycle, but continue processing...
267 // Keep end cycle time
268 JackDriver::CycleTakeEndTime();
272 void JackAudioDriver::ProcessGraphAsync()
274 // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
275 if (!fEngine
->Process(fBeginDateUst
, fEndDateUst
))
276 jack_error("JackAudioDriver::ProcessAsync Process error");
277 fGraphManager
->ResumeRefNum(&fClientControl
, fSynchroTable
);
278 if (ProcessSlaves() < 0)
279 jack_error("JackAudioDriver::ProcessAsync ProcessSlaves error");
282 void JackAudioDriver::ProcessGraphSync()
284 // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
285 if (fEngine
->Process(fBeginDateUst
, fEndDateUst
)) {
286 fGraphManager
->ResumeRefNum(&fClientControl
, fSynchroTable
);
287 if (ProcessSlaves() < 0)
288 jack_error("JackAudioDriver::ProcessSync ProcessSlaves error, engine may now behave abnormally!!");
289 if (fGraphManager
->SuspendRefNum(&fClientControl
, fSynchroTable
, DRIVER_TIMEOUT_FACTOR
* fEngineControl
->fTimeOutUsecs
) < 0)
290 jack_error("JackAudioDriver::ProcessSync SuspendRefNum error, engine may now behave abnormally!!");
291 } else { // Graph not finished: do not activate it
292 jack_error("JackAudioDriver::ProcessSync: error");
296 void JackAudioDriver::WaitUntilNextCycle()
298 int wait_time_usec
= (int((float(fEngineControl
->fBufferSize
) / (float(fEngineControl
->fSampleRate
))) * 1000000.0f
));
299 wait_time_usec
= int(wait_time_usec
- (GetMicroSeconds() - fBeginDateUst
));
300 if (wait_time_usec
> 0)
301 JackSleep(wait_time_usec
);
304 jack_default_audio_sample_t
* JackAudioDriver::GetInputBuffer(int port_index
)
306 assert(fCapturePortList
[port_index
]);
307 return (jack_default_audio_sample_t
*)fGraphManager
->GetBuffer(fCapturePortList
[port_index
], fEngineControl
->fBufferSize
);
310 jack_default_audio_sample_t
* JackAudioDriver::GetOutputBuffer(int port_index
)
312 assert(fPlaybackPortList
[port_index
]);
313 return (jack_default_audio_sample_t
*)fGraphManager
->GetBuffer(fPlaybackPortList
[port_index
], fEngineControl
->fBufferSize
);
316 jack_default_audio_sample_t
* JackAudioDriver::GetMonitorBuffer(int port_index
)
318 assert(fPlaybackPortList
[port_index
]);
319 return (jack_default_audio_sample_t
*)fGraphManager
->GetBuffer(fMonitorPortList
[port_index
], fEngineControl
->fBufferSize
);
322 } // end of namespace