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