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:%s:monitor_%u", fAliasName, fPlaybackDriverName, 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->SetLatency(fEngineControl->fBufferSize);
00149 fMonitorPortList[i] = port_index;
00150 }
00151 }
00152 }
00153
00154 return 0;
00155 }
00156
00157 int JackAudioDriver::Detach()
00158 {
00159 int i;
00160 jack_log("JackAudioDriver::Detach");
00161
00162 for (i = 0; i < fCaptureChannels; i++) {
00163 fGraphManager->ReleasePort(fClientControl.fRefNum, fCapturePortList[i]);
00164 }
00165
00166 for (i = 0; i < fPlaybackChannels; i++) {
00167 fGraphManager->ReleasePort(fClientControl.fRefNum, fPlaybackPortList[i]);
00168 if (fWithMonitorPorts)
00169 fGraphManager->ReleasePort(fClientControl.fRefNum, fMonitorPortList[i]);
00170 }
00171
00172 return 0;
00173 }
00174
00175 int JackAudioDriver::Write()
00176 {
00177 for (int i = 0; i < fPlaybackChannels; i++) {
00178 if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
00179 float* buffer = GetOutputBuffer(i);
00180 int size = sizeof(float) * fEngineControl->fBufferSize;
00181
00182 if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
00183 memcpy(GetMonitorBuffer(i), buffer, size);
00184 }
00185 }
00186 return 0;
00187 }
00188
00189 int JackAudioDriver::ProcessNull()
00190 {
00191
00192 JackDriver::CycleTakeBeginTime();
00193
00194 if (fEngineControl->fSyncMode) {
00195 ProcessGraphSync();
00196 } else {
00197 ProcessGraphAsync();
00198 }
00199
00200
00201 JackDriver::CycleTakeEndTime();
00202 WaitUntilNextCycle();
00203 return 0;
00204 }
00205
00206 int JackAudioDriver::Process()
00207 {
00208 return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
00209 }
00210
00211
00212
00213
00214
00215
00216 int JackAudioDriver::ProcessAsync()
00217 {
00218
00219 if (Read() < 0) {
00220 jack_error("JackAudioDriver::ProcessAsync: read error, skip cycle");
00221 return 0;
00222 }
00223
00224
00225 if (Write() < 0) {
00226 jack_error("JackAudioDriver::ProcessAsync: write error, skip cycle");
00227 return 0;
00228 }
00229
00230 if (fIsMaster) {
00231 ProcessGraphAsync();
00232 } else {
00233 fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
00234 }
00235
00236
00237 JackDriver::CycleTakeEndTime();
00238 return 0;
00239 }
00240
00241
00242
00243
00244
00245
00246 int JackAudioDriver::ProcessSync()
00247 {
00248
00249 if (Read() < 0) {
00250 jack_error("JackAudioDriver::ProcessSync: read error, skip cycle");
00251 return 0;
00252 }
00253
00254 if (fIsMaster) {
00255 ProcessGraphSync();
00256 } else {
00257 fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
00258 }
00259
00260
00261 if (Write() < 0) {
00262 jack_error("JackAudioDriver::ProcessSync: write error, skip cycle");
00263 return 0;
00264 }
00265
00266
00267 JackDriver::CycleTakeEndTime();
00268 return 0;
00269 }
00270
00271 void JackAudioDriver::ProcessGraphAsync()
00272 {
00273
00274 if (!fEngine->Process(fBeginDateUst, fEndDateUst))
00275 jack_error("JackAudioDriver::ProcessAsync Process error");
00276 fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
00277 if (ProcessSlaves() < 0)
00278 jack_error("JackAudioDriver::ProcessAsync ProcessSlaves error");
00279 }
00280
00281 void JackAudioDriver::ProcessGraphSync()
00282 {
00283
00284 if (fEngine->Process(fBeginDateUst, fEndDateUst)) {
00285 fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
00286 if (ProcessSlaves() < 0)
00287 jack_error("JackAudioDriver::ProcessSync ProcessSlaves error, engine may now behave abnormally!!");
00288 if (fGraphManager->SuspendRefNum(&fClientControl, fSynchroTable, DRIVER_TIMEOUT_FACTOR * fEngineControl->fTimeOutUsecs) < 0)
00289 jack_error("JackAudioDriver::ProcessSync SuspendRefNum error, engine may now behave abnormally!!");
00290 } else {
00291 jack_error("JackAudioDriver::ProcessSync: error");
00292 }
00293 }
00294
00295 void JackAudioDriver::WaitUntilNextCycle()
00296 {
00297 int wait_time_usec = (int((float(fEngineControl->fBufferSize) / (float(fEngineControl->fSampleRate))) * 1000000.0f));
00298 wait_time_usec = int(wait_time_usec - (GetMicroSeconds() - fBeginDateUst));
00299 if (wait_time_usec > 0)
00300 JackSleep(wait_time_usec);
00301 }
00302
00303 jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
00304 {
00305 assert(fCapturePortList[port_index]);
00306 return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
00307 }
00308
00309 jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
00310 {
00311 assert(fPlaybackPortList[port_index]);
00312 return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
00313 }
00314
00315 jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
00316 {
00317 assert(fPlaybackPortList[port_index]);
00318 return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize);
00319 }
00320
00321 }