00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "JackSystemDeps.h"
00022 #include "JackAudioDriver.h"
00023 #include "JackTime.h"
00024 #include "JackError.h"
00025 #include "JackEngineControl.h"
00026 #include "JackPort.h"
00027 #include "JackGraphManager.h"
00028 #include "JackLockedEngine.h"
00029 #include "JackException.h"
00030 #include <assert.h>
00031
00032 namespace Jack
00033 {
00034
00035 JackAudioDriver::JackAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
00036 : JackDriver(name, alias, engine, table),
00037 fCaptureChannels(0),
00038 fPlaybackChannels(0),
00039 fWithMonitorPorts(false)
00040 {}
00041
00042 JackAudioDriver::~JackAudioDriver()
00043 {}
00044
00045 int JackAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
00046 {
00047 fEngineControl->fBufferSize = buffer_size;
00048 fGraphManager->SetBufferSize(buffer_size);
00049 fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize);
00050 if (!fEngineControl->fTimeOut)
00051 fEngineControl->fTimeOutUsecs = jack_time_t(2.f * fEngineControl->fPeriodUsecs);
00052 return 0;
00053 }
00054
00055 int JackAudioDriver::SetSampleRate(jack_nframes_t sample_rate)
00056 {
00057 fEngineControl->fSampleRate = sample_rate;
00058 fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize);
00059 if (!fEngineControl->fTimeOut)
00060 fEngineControl->fTimeOutUsecs = jack_time_t(2.f * fEngineControl->fPeriodUsecs);
00061 return 0;
00062 }
00063
00064 int JackAudioDriver::Open(jack_nframes_t buffer_size,
00065 jack_nframes_t samplerate,
00066 bool capturing,
00067 bool playing,
00068 int inchannels,
00069 int outchannels,
00070 bool monitor,
00071 const char* capture_driver_name,
00072 const char* playback_driver_name,
00073 jack_nframes_t capture_latency,
00074 jack_nframes_t playback_latency)
00075 {
00076 fCaptureChannels = inchannels;
00077 fPlaybackChannels = outchannels;
00078 fWithMonitorPorts = monitor;
00079 return JackDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
00080 }
00081
00082 int JackAudioDriver::Open(bool capturing,
00083 bool playing,
00084 int inchannels,
00085 int outchannels,
00086 bool monitor,
00087 const char* capture_driver_name,
00088 const char* playback_driver_name,
00089 jack_nframes_t capture_latency,
00090 jack_nframes_t playback_latency)
00091 {
00092 fCaptureChannels = inchannels;
00093 fPlaybackChannels = outchannels;
00094 fWithMonitorPorts = monitor;
00095 return JackDriver::Open(capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
00096 }
00097
00098 int JackAudioDriver::Attach()
00099 {
00100 JackPort* port;
00101 jack_port_id_t port_index;
00102 char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
00103 char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
00104 unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
00105 int i;
00106
00107 jack_log("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
00108
00109 for (i = 0; i < fCaptureChannels; i++) {
00110 snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
00111 snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl.fName, i + 1);
00112 if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
00113 jack_error("driver: cannot register port for %s", name);
00114 return -1;
00115 }
00116 port = fGraphManager->GetPort(port_index);
00117 port->SetAlias(alias);
00118 port->SetLatency(fEngineControl->fBufferSize + fCaptureLatency);
00119 fCapturePortList[i] = port_index;
00120 jack_log("JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
00121 }
00122
00123 port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
00124
00125 for (i = 0; i < fPlaybackChannels; i++) {
00126 snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
00127 snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl.fName, i + 1);
00128 if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
00129 jack_error("driver: cannot register port for %s", name);
00130 return -1;
00131 }
00132 port = fGraphManager->GetPort(port_index);
00133 port->SetAlias(alias);
00134
00135 port->SetLatency(fEngineControl->fBufferSize + ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize) + fPlaybackLatency);
00136 fPlaybackPortList[i] = port_index;
00137 jack_log("JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
00138
00139
00140 if (fWithMonitorPorts) {
00141 jack_log("Create monitor port ");
00142 snprintf(name, sizeof(name) - 1, "%s:monitor_%u", fClientControl.fName, i + 1);
00143 if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize)) == NO_PORT) {
00144 jack_error("Cannot register monitor port for %s", name);
00145 return -1;
00146 } else {
00147 port = fGraphManager->GetPort(port_index);
00148 port->SetAlias(alias);
00149 port->SetLatency(fEngineControl->fBufferSize);
00150 fMonitorPortList[i] = port_index;
00151 }
00152 }
00153 }
00154
00155 return 0;
00156 }
00157
00158 int JackAudioDriver::Detach()
00159 {
00160 int i;
00161 jack_log("JackAudioDriver::Detach");
00162
00163 for (i = 0; i < fCaptureChannels; i++) {
00164 fGraphManager->ReleasePort(fClientControl.fRefNum, fCapturePortList[i]);
00165 }
00166
00167 for (i = 0; i < fPlaybackChannels; i++) {
00168 fGraphManager->ReleasePort(fClientControl.fRefNum, fPlaybackPortList[i]);
00169 if (fWithMonitorPorts)
00170 fGraphManager->ReleasePort(fClientControl.fRefNum, fMonitorPortList[i]);
00171 }
00172
00173 return 0;
00174 }
00175
00176 int JackAudioDriver::Write()
00177 {
00178 for (int i = 0; i < fPlaybackChannels; i++) {
00179 if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
00180 float* buffer = GetOutputBuffer(i);
00181 int size = sizeof(float) * fEngineControl->fBufferSize;
00182
00183 if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
00184 memcpy(GetMonitorBuffer(i), buffer, size);
00185 }
00186 }
00187 return 0;
00188 }
00189
00190 int JackAudioDriver::ProcessNull()
00191 {
00192
00193 JackDriver::CycleTakeBeginTime();
00194
00195 if (fEngineControl->fSyncMode) {
00196 ProcessGraphSync();
00197 } else {
00198 ProcessGraphAsync();
00199 }
00200
00201
00202 JackDriver::CycleTakeEndTime();
00203 WaitUntilNextCycle();
00204 return 0;
00205 }
00206
00207 int JackAudioDriver::Process()
00208 {
00209 return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
00210 }
00211
00212
00213
00214
00215
00216
00217 int JackAudioDriver::ProcessAsync()
00218 {
00219
00220 if (Read() < 0) {
00221 jack_error("JackAudioDriver::ProcessAsync: read error, skip cycle");
00222 return 0;
00223 }
00224
00225
00226 if (Write() < 0) {
00227 jack_error("JackAudioDriver::ProcessAsync: write error, skip cycle");
00228 return 0;
00229 }
00230
00231 if (fIsMaster) {
00232 ProcessGraphAsync();
00233 } else {
00234 fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
00235 }
00236
00237
00238 JackDriver::CycleTakeEndTime();
00239 return 0;
00240 }
00241
00242
00243
00244
00245
00246
00247 int JackAudioDriver::ProcessSync()
00248 {
00249
00250 if (Read() < 0) {
00251 jack_error("JackAudioDriver::ProcessSync: read error, skip cycle");
00252 return 0;
00253 }
00254
00255 if (fIsMaster) {
00256 ProcessGraphSync();
00257 } else {
00258 fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
00259 }
00260
00261
00262 if (Write() < 0) {
00263 jack_error("JackAudioDriver::ProcessSync: write error, skip cycle");
00264 return 0;
00265 }
00266
00267
00268 JackDriver::CycleTakeEndTime();
00269 return 0;
00270 }
00271
00272 void JackAudioDriver::ProcessGraphAsync()
00273 {
00274
00275 if (!fEngine->Process(fBeginDateUst, fEndDateUst))
00276 jack_error("JackAudioDriver::ProcessAsync Process error");
00277 fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
00278 if (ProcessSlaves() < 0)
00279 jack_error("JackAudioDriver::ProcessAsync ProcessSlaves error");
00280 }
00281
00282 void JackAudioDriver::ProcessGraphSync()
00283 {
00284
00285 if (fEngine->Process(fBeginDateUst, fEndDateUst)) {
00286 fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
00287 if (ProcessSlaves() < 0)
00288 jack_error("JackAudioDriver::ProcessSync ProcessSlaves error, engine may now behave abnormally!!");
00289 if (fGraphManager->SuspendRefNum(&fClientControl, fSynchroTable, DRIVER_TIMEOUT_FACTOR * fEngineControl->fTimeOutUsecs) < 0)
00290 jack_error("JackAudioDriver::ProcessSync SuspendRefNum error, engine may now behave abnormally!!");
00291 } else {
00292 jack_error("JackAudioDriver::ProcessSync: error");
00293 }
00294 }
00295
00296 void JackAudioDriver::WaitUntilNextCycle()
00297 {
00298 int wait_time_usec = (int((float(fEngineControl->fBufferSize) / (float(fEngineControl->fSampleRate))) * 1000000.0f));
00299 wait_time_usec = int(wait_time_usec - (GetMicroSeconds() - fBeginDateUst));
00300 if (wait_time_usec > 0)
00301 JackSleep(wait_time_usec);
00302 }
00303
00304 jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
00305 {
00306 assert(fCapturePortList[port_index]);
00307 return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
00308 }
00309
00310 jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
00311 {
00312 assert(fPlaybackPortList[port_index]);
00313 return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
00314 }
00315
00316 jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
00317 {
00318 assert(fPlaybackPortList[port_index]);
00319 return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize);
00320 }
00321
00322 }