2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 GramefClientControl.
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::Attach()
85 jack_port_id_t port_index
;
86 char name
[JACK_CLIENT_NAME_SIZE
+ JACK_PORT_NAME_SIZE
];
87 char alias
[JACK_CLIENT_NAME_SIZE
+ JACK_PORT_NAME_SIZE
];
88 unsigned long port_flags
= JackPortIsOutput
| JackPortIsPhysical
| JackPortIsTerminal
;
91 jack_log("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl
->fBufferSize
, fEngineControl
->fSampleRate
);
93 for (i
= 0; i
< fCaptureChannels
; i
++) {
94 snprintf(alias
, sizeof(alias
) - 1, "%s:%s:out%d", fAliasName
, fCaptureDriverName
, i
+ 1);
95 snprintf(name
, sizeof(name
) - 1, "%s:capture_%d", fClientControl
.fName
, i
+ 1);
96 if ((port_index
= fGraphManager
->AllocatePort(fClientControl
.fRefNum
, name
, JACK_DEFAULT_AUDIO_TYPE
, (JackPortFlags
)port_flags
, fEngineControl
->fBufferSize
)) == NO_PORT
) {
97 jack_error("driver: cannot register port for %s", name
);
100 port
= fGraphManager
->GetPort(port_index
);
101 port
->SetAlias(alias
);
102 port
->SetLatency(fEngineControl
->fBufferSize
+ fCaptureLatency
);
103 fCapturePortList
[i
] = port_index
;
104 jack_log("JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index
);
107 port_flags
= JackPortIsInput
| JackPortIsPhysical
| JackPortIsTerminal
;
109 for (i
= 0; i
< fPlaybackChannels
; i
++) {
110 snprintf(alias
, sizeof(alias
) - 1, "%s:%s:in%d", fAliasName
, fPlaybackDriverName
, i
+ 1);
111 snprintf(name
, sizeof(name
) - 1, "%s:playback_%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 // Add more latency if "async" mode is used...
119 port
->SetLatency(fEngineControl
->fBufferSize
+ ((fEngineControl
->fSyncMode
) ? 0 : fEngineControl
->fBufferSize
) + fPlaybackLatency
);
120 fPlaybackPortList
[i
] = port_index
;
121 jack_log("JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index
);
124 if (fWithMonitorPorts
) {
125 jack_log("Create monitor port ");
126 snprintf(name
, sizeof(name
) - 1, "%s:%s:monitor_%u", fAliasName
, fPlaybackDriverName
, i
+ 1);
127 if ((port_index
= fGraphManager
->AllocatePort(fClientControl
.fRefNum
, name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, fEngineControl
->fBufferSize
)) == NO_PORT
) {
128 jack_error("Cannot register monitor port for %s", name
);
131 port
= fGraphManager
->GetPort(port_index
);
132 port
->SetLatency(fEngineControl
->fBufferSize
);
133 fMonitorPortList
[i
] = port_index
;
141 int JackAudioDriver::Detach()
144 jack_log("JackAudioDriver::Detach");
146 for (i
= 0; i
< fCaptureChannels
; i
++) {
147 fGraphManager
->ReleasePort(fClientControl
.fRefNum
, fCapturePortList
[i
]);
150 for (i
= 0; i
< fPlaybackChannels
; i
++) {
151 fGraphManager
->ReleasePort(fClientControl
.fRefNum
, fPlaybackPortList
[i
]);
152 if (fWithMonitorPorts
)
153 fGraphManager
->ReleasePort(fClientControl
.fRefNum
, fMonitorPortList
[i
]);
159 int JackAudioDriver::Write()
161 for (int i
= 0; i
< fPlaybackChannels
; i
++) {
162 if (fGraphManager
->GetConnectionsNum(fPlaybackPortList
[i
]) > 0) {
163 float* buffer
= GetOutputBuffer(i
);
164 int size
= sizeof(float) * fEngineControl
->fBufferSize
;
166 if (fWithMonitorPorts
&& fGraphManager
->GetConnectionsNum(fMonitorPortList
[i
]) > 0)
167 memcpy(GetMonitorBuffer(i
), buffer
, size
);
173 int JackAudioDriver::ProcessNull()
175 // Keep begin cycle time
176 JackDriver::CycleTakeBeginTime();
178 if (fEngineControl
->fSyncMode
) {
184 // Keep end cycle time
185 JackDriver::CycleTakeEndTime();
186 WaitUntilNextCycle();
190 int JackAudioDriver::Process()
192 return (fEngineControl
->fSyncMode
) ? ProcessSync() : ProcessAsync();
196 The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
197 synchronize to the end of client graph execution.
200 int JackAudioDriver::ProcessAsync()
202 // Read input buffers for the current cycle
204 jack_error("JackAudioDriver::ProcessAsync: read error, skip cycle");
205 return 0; // Skip cycle, but continue processing...
208 // Write output buffers from the previous cycle
210 jack_error("JackAudioDriver::ProcessAsync: write error, skip cycle");
211 return 0; // Skip cycle, but continue processing...
217 fGraphManager
->ResumeRefNum(&fClientControl
, fSynchroTable
);
220 // Keep end cycle time
221 JackDriver::CycleTakeEndTime();
226 The driver SYNC mode: the server does synchronize to the end of client graph execution,
227 output buffers computed at the *current cycle* are used.
230 int JackAudioDriver::ProcessSync()
232 // Read input buffers for the current cycle
234 jack_error("ProcessSync: read error, skip cycle");
235 return 0; // Skip cycle, but continue processing...
241 fGraphManager
->ResumeRefNum(&fClientControl
, fSynchroTable
);
244 // Write output buffers for the current cycle
246 jack_error("JackAudioDriver::ProcessSync: write error, skip cycle");
247 return 0; // Skip cycle, but continue processing...
250 // Keep end cycle time
251 JackDriver::CycleTakeEndTime();
255 void JackAudioDriver::ProcessGraphAsync()
257 // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
258 if (!fEngine
->Process(fBeginDateUst
, fEndDateUst
))
259 jack_error("JackAudioDriver::ProcessAsync Process error");
260 fGraphManager
->ResumeRefNum(&fClientControl
, fSynchroTable
);
261 if (ProcessSlaves() < 0)
262 jack_error("JackAudioDriver::ProcessAsync ProcessSlaves error");
265 void JackAudioDriver::ProcessGraphSync()
267 // fBeginDateUst is set in the "low level" layer, fEndDateUst is from previous cycle
268 if (fEngine
->Process(fBeginDateUst
, fEndDateUst
)) {
269 fGraphManager
->ResumeRefNum(&fClientControl
, fSynchroTable
);
270 if (ProcessSlaves() < 0)
271 jack_error("JackAudioDriver::ProcessSync ProcessSlaves error, engine may now behave abnormally!!");
272 if (fGraphManager
->SuspendRefNum(&fClientControl
, fSynchroTable
, fEngineControl
->fTimeOutUsecs
) < 0)
273 jack_error("JackAudioDriver::ProcessSync SuspendRefNum error, engine may now behave abnormally!!");
274 } else { // Graph not finished: do not activate it
275 jack_error("JackAudioDriver::ProcessSync: error");
279 void JackAudioDriver::WaitUntilNextCycle()
281 int wait_time_usec
= (int((float(fEngineControl
->fBufferSize
) / (float(fEngineControl
->fSampleRate
))) * 1000000.0f
));
282 wait_time_usec
= int(wait_time_usec
- (GetMicroSeconds() - fBeginDateUst
));
283 if (wait_time_usec
> 0)
284 JackSleep(wait_time_usec
);
287 jack_default_audio_sample_t
* JackAudioDriver::GetInputBuffer(int port_index
)
289 assert(fCapturePortList
[port_index
]);
290 return (jack_default_audio_sample_t
*)fGraphManager
->GetBuffer(fCapturePortList
[port_index
], fEngineControl
->fBufferSize
);
293 jack_default_audio_sample_t
* JackAudioDriver::GetOutputBuffer(int port_index
)
295 assert(fPlaybackPortList
[port_index
]);
296 return (jack_default_audio_sample_t
*)fGraphManager
->GetBuffer(fPlaybackPortList
[port_index
], fEngineControl
->fBufferSize
);
299 jack_default_audio_sample_t
* JackAudioDriver::GetMonitorBuffer(int port_index
)
301 assert(fPlaybackPortList
[port_index
]);
302 return (jack_default_audio_sample_t
*)fGraphManager
->GetBuffer(fMonitorPortList
[port_index
], fEngineControl
->fBufferSize
);
305 } // end of namespace