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 int i;
00105
00106 jack_log("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
00107
00108 for (i = 0; i < fCaptureChannels; i++) {
00109 snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
00110 snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl.fName, i + 1);
00111 if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize)) == NO_PORT) {
00112 jack_error("driver: cannot register port for %s", name);
00113 return -1;
00114 }
00115 port = fGraphManager->GetPort(port_index);
00116 port->SetAlias(alias);
00117 port->SetLatency(fEngineControl->fBufferSize + fCaptureLatency);
00118 fCapturePortList[i] = port_index;
00119 jack_log("JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
00120 }
00121
00122 for (i = 0; i < fPlaybackChannels; i++) {
00123 snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
00124 snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl.fName, i + 1);
00125 if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize)) == NO_PORT) {
00126 jack_error("driver: cannot register port for %s", name);
00127 return -1;
00128 }
00129 port = fGraphManager->GetPort(port_index);
00130 port->SetAlias(alias);
00131
00132 port->SetLatency(fEngineControl->fBufferSize + ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize) + fPlaybackLatency);
00133 fPlaybackPortList[i] = port_index;
00134 jack_log("JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
00135
00136
00137 if (fWithMonitorPorts) {
00138 jack_log("Create monitor port ");
00139 snprintf(name, sizeof(name) - 1, "%s:monitor_%u", fClientControl.fName, i + 1);
00140 if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize)) == NO_PORT) {
00141 jack_error("Cannot register monitor port for %s", name);
00142 return -1;
00143 } else {
00144 port = fGraphManager->GetPort(port_index);
00145 port->SetAlias(alias);
00146 port->SetLatency(fEngineControl->fBufferSize);
00147 fMonitorPortList[i] = port_index;
00148 }
00149 }
00150 }
00151
00152 return 0;
00153 }
00154
00155 int JackAudioDriver::Detach()
00156 {
00157 int i;
00158 jack_log("JackAudioDriver::Detach");
00159
00160 for (i = 0; i < fCaptureChannels; i++) {
00161 fGraphManager->ReleasePort(fClientControl.fRefNum, fCapturePortList[i]);
00162 }
00163
00164 for (i = 0; i < fPlaybackChannels; i++) {
00165 fGraphManager->ReleasePort(fClientControl.fRefNum, fPlaybackPortList[i]);
00166 if (fWithMonitorPorts)
00167 fGraphManager->ReleasePort(fClientControl.fRefNum, fMonitorPortList[i]);
00168 }
00169
00170 return 0;
00171 }
00172
00173 int JackAudioDriver::Write()
00174 {
00175 for (int i = 0; i < fPlaybackChannels; i++) {
00176 if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
00177 float* buffer = GetOutputBuffer(i);
00178 int size = sizeof(float) * fEngineControl->fBufferSize;
00179
00180 if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
00181 memcpy(GetMonitorBuffer(i), buffer, size);
00182 }
00183 }
00184 return 0;
00185 }
00186
00187 int JackAudioDriver::ProcessNull()
00188 {
00189
00190 JackDriver::CycleTakeBeginTime();
00191
00192 if (fEngineControl->fSyncMode) {
00193 ProcessGraphSync();
00194 } else {
00195 ProcessGraphAsync();
00196 }
00197
00198
00199 JackDriver::CycleTakeEndTime();
00200 WaitUntilNextCycle();
00201 return 0;
00202 }
00203
00204 int JackAudioDriver::Process()
00205 {
00206 return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
00207 }
00208
00209
00210
00211
00212
00213
00214 int JackAudioDriver::ProcessAsync()
00215 {
00216
00217 if (Read() < 0) {
00218 jack_error("JackAudioDriver::ProcessAsync: read error, skip cycle");
00219 return 0;
00220 }
00221
00222
00223 if (Write() < 0) {
00224 jack_error("JackAudioDriver::ProcessAsync: write error, skip cycle");
00225 return 0;
00226 }
00227
00228 if (fIsMaster) {
00229 ProcessGraphAsync();
00230 } else {
00231 fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
00232 }
00233
00234
00235 JackDriver::CycleTakeEndTime();
00236 return 0;
00237 }
00238
00239
00240
00241
00242
00243
00244 int JackAudioDriver::ProcessSync()
00245 {
00246
00247 if (Read() < 0) {
00248 jack_error("JackAudioDriver::ProcessSync: read error, skip cycle");
00249 return 0;
00250 }
00251
00252 if (fIsMaster) {
00253 ProcessGraphSync();
00254 } else {
00255 fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
00256 }
00257
00258
00259 if (Write() < 0) {
00260 jack_error("JackAudioDriver::ProcessSync: write error, skip cycle");
00261 return 0;
00262 }
00263
00264
00265 JackDriver::CycleTakeEndTime();
00266 return 0;
00267 }
00268
00269 void JackAudioDriver::ProcessGraphAsync()
00270 {
00271
00272 if (!fEngine->Process(fBeginDateUst, fEndDateUst))
00273 jack_error("JackAudioDriver::ProcessAsync Process error");
00274 fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
00275 if (ProcessSlaves() < 0)
00276 jack_error("JackAudioDriver::ProcessAsync ProcessSlaves error");
00277 }
00278
00279 void JackAudioDriver::ProcessGraphSync()
00280 {
00281
00282 if (fEngine->Process(fBeginDateUst, fEndDateUst)) {
00283 fGraphManager->ResumeRefNum(&fClientControl, fSynchroTable);
00284 if (ProcessSlaves() < 0)
00285 jack_error("JackAudioDriver::ProcessSync ProcessSlaves error, engine may now behave abnormally!!");
00286 if (fGraphManager->SuspendRefNum(&fClientControl, fSynchroTable, DRIVER_TIMEOUT_FACTOR * fEngineControl->fTimeOutUsecs) < 0)
00287 jack_error("JackAudioDriver::ProcessSync SuspendRefNum error, engine may now behave abnormally!!");
00288 } else {
00289 jack_error("JackAudioDriver::ProcessSync: error");
00290 }
00291 }
00292
00293 void JackAudioDriver::WaitUntilNextCycle()
00294 {
00295 int wait_time_usec = (int((float(fEngineControl->fBufferSize) / (float(fEngineControl->fSampleRate))) * 1000000.0f));
00296 wait_time_usec = int(wait_time_usec - (GetMicroSeconds() - fBeginDateUst));
00297 if (wait_time_usec > 0)
00298 JackSleep(wait_time_usec);
00299 }
00300
00301 jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
00302 {
00303 assert(fCapturePortList[port_index]);
00304 return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
00305 }
00306
00307 jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
00308 {
00309 assert(fPlaybackPortList[port_index]);
00310 return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
00311 }
00312
00313 jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
00314 {
00315 assert(fPlaybackPortList[port_index]);
00316 return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize);
00317 }
00318
00319 }