JackAudioDriver.cpp

00001 /*
00002 Copyright (C) 2001 Paul Davis 
00003 Copyright (C) 2004-2006 Grame
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 This program is distributed in the hope that it will be useful,
00009 but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 (at your option) any later version.
00012 
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 */
00020 
00021 #ifdef WIN32 
00022 #pragma warning (disable : 4786)
00023 #endif
00024 
00025 #include "JackAudioDriver.h"
00026 #include "JackTime.h"
00027 #include "JackError.h"
00028 #include "JackEngineControl.h"
00029 #include "JackClientControl.h"
00030 #include "JackPort.h"
00031 #include "JackGraphManager.h"
00032 #include "JackEngine.h"
00033 #include <assert.h>
00034 
00035 namespace Jack
00036 {
00037 
00038 JackAudioDriver::JackAudioDriver(const char* name, JackEngine* engine, JackSynchro** table)
00039         : JackDriver(name, engine, table),
00040         fCaptureChannels(0),
00041         fPlaybackChannels(0),
00042         fWithMonitorPorts(false)
00043 {}
00044 
00045 JackAudioDriver::~JackAudioDriver()
00046 {}
00047 
00048 // DB: This is here because audio driver is the only one, who can change buffer size.
00049 // It can be moved into JackDriver, but then it would be called twice
00050 // because of JackServer::SetBufferSize() implementation.
00051 // Initial values are set in JackDriver::Open(...). Yes it is a duplicate code (bad).
00052 int JackAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
00053 {
00054     fEngineControl->fBufferSize = buffer_size;
00055     fEngineControl->fPeriodUsecs = jack_time_t(1000000.f / fEngineControl->fSampleRate * fEngineControl->fBufferSize); // in microsec
00056     return 0;
00057 }
00058 
00059 int JackAudioDriver::Open(jack_nframes_t nframes,
00060                           jack_nframes_t samplerate,
00061                           int capturing,
00062                           int playing,
00063                           int inchannels,
00064                           int outchannels,
00065                           bool monitor,
00066                           const char* capture_driver_name,
00067                           const char* playback_driver_name,
00068                           jack_nframes_t capture_latency,
00069                           jack_nframes_t playback_latency)
00070 {
00071     fCaptureChannels = inchannels;
00072     fPlaybackChannels = outchannels;
00073     fWithMonitorPorts = monitor;
00074     return JackDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
00075 }
00076 
00077 int JackAudioDriver::Attach()
00078 {
00079     JackPort* port;
00080     jack_port_id_t port_index;
00081     char buf[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
00082     unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
00083     int i;
00084 
00085     JackLog("JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld\n", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
00086 
00087     for (i = 0; i < fCaptureChannels; i++) {
00088         snprintf(buf, sizeof(buf) - 1, "%s:%s:out%d", fClientControl->fName, fCaptureDriverName, i + 1);
00089         if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, (JackPortFlags)port_flags)) == NO_PORT) {
00090             jack_error("driver: cannot register port for %s", buf);
00091             return -1;
00092         }
00093             port = fGraphManager->GetPort(port_index);
00094             port->Rename("system:capture_%d", i + 1);
00095         port->SetLatency(fEngineControl->fBufferSize + fCaptureLatency);
00096         fCapturePortList[i] = port_index;
00097         JackLog("JackAudioDriver::Attach fCapturePortList[i] %ld = \n", port_index);
00098     }
00099 
00100     port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
00101 
00102     for (i = 0; i < fPlaybackChannels; i++) {
00103         snprintf(buf, sizeof(buf) - 1, "%s:%s:in%d", fClientControl->fName, fPlaybackDriverName, i + 1);
00104         if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, (JackPortFlags)port_flags)) == NO_PORT) {
00105             jack_error("driver: cannot register port for %s", buf);
00106             return -1;
00107         }
00108             port = fGraphManager->GetPort(port_index);
00109             port->Rename("system:playback_%d", i + 1);
00110         port->SetLatency(fEngineControl->fBufferSize + fPlaybackLatency);
00111         fPlaybackPortList[i] = port_index;
00112         JackLog("JackAudioDriver::Attach fPlaybackPortList[i] %ld = \n", port_index);
00113         
00114         // Monitor ports
00115         if (fWithMonitorPorts) {
00116             JackLog("Create monitor port \n");
00117             snprintf(buf, sizeof(buf) - 1, "%s:%s:monitor_%u", fClientControl->fName, fPlaybackDriverName, i + 1);
00118             if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JackPortIsOutput)) == NO_PORT) {
00119                 jack_error("Cannot register monitor port for %s", buf);
00120                 return -1;
00121             } else {
00122                 port = fGraphManager->GetPort(port_index);
00123                 port->SetLatency(fEngineControl->fBufferSize);
00124                 fMonitorPortList[i] = port_index;
00125             }
00126         }
00127     }
00128 
00129     return 0;
00130 }
00131 
00132 int JackAudioDriver::Detach()
00133 {
00134     int i;
00135     JackLog("JackAudioDriver::Detach\n");
00136 
00137     for (i = 0; i < fCaptureChannels; i++) {
00138         fGraphManager->ReleasePort(fClientControl->fRefNum, fCapturePortList[i]);
00139     }
00140 
00141     for (i = 0; i < fPlaybackChannels; i++) {
00142         fGraphManager->ReleasePort(fClientControl->fRefNum, fPlaybackPortList[i]);
00143         if (fWithMonitorPorts)
00144             fGraphManager->ReleasePort(fClientControl->fRefNum, fMonitorPortList[i]);
00145     }
00146 
00147     return 0;
00148 }
00149 
00150 int JackAudioDriver::Write()
00151 {
00152     for (int i = 0; i < fPlaybackChannels; i++) {
00153         if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
00154                 float* buffer = GetOutputBuffer(i);
00155             int size = sizeof(float) * fEngineControl->fBufferSize;
00156             // Monitor ports
00157             if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0)
00158                 memcpy(GetMonitorBuffer(i), buffer, size);
00159         }
00160     }
00161     return 0;
00162 }
00163 
00164 int JackAudioDriver::Process()
00165 {
00166     return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
00167 }
00168 
00169 /*
00170 The driver ASYNC mode: output buffers computed at the *previous cycle* are used, the server does not
00171 synchronize to the end of client graph execution.
00172 */
00173 
00174 int JackAudioDriver::ProcessAsync()
00175 {
00176     if (Read() < 0) { // Read input buffers for the current cycle
00177         jack_error("ProcessAsync: read error");
00178         return 0;
00179     }
00180 
00181     if (Write() < 0) { // Write output buffers from the previous cycle
00182         jack_error("ProcessAsync: write error");
00183         return 0;
00184     }
00185 
00186     if (fIsMaster) {
00187         fEngine->Process(fLastWaitUst); // fLastWaitUst is set in the "low level" layer
00188         fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
00189         ProcessSlaves();
00190     } else {
00191         fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
00192     }
00193     return 0;
00194 }
00195 
00196 /*
00197 The driver SYNC mode: the server does synchronize to the end of client graph execution,
00198 output buffers computed at the *current cycle* are used.
00199 */
00200 
00201 int JackAudioDriver::ProcessSync()
00202 {
00203     if (Read() < 0) { // Read input buffers for the current cycle
00204         jack_error("ProcessSync: read error");
00205         return 0;
00206     }
00207 
00208     if (fIsMaster) {
00209 
00210         if (fEngine->Process(fLastWaitUst)) { // fLastWaitUst is set in the "low level" layer
00211                         fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
00212                         if (ProcessSlaves() < 0) 
00213                                 jack_error("JackAudioDriver::ProcessSync ProcessSlaves error, engine may now behave abnormally!!");
00214                         if (fGraphManager->SuspendRefNum(fClientControl, fSynchroTable, fEngineControl->fTimeOutUsecs) < 0) 
00215                                 jack_error("JackAudioDriver::ProcessSync SuspendRefNum error, engine may now behave abnormally!!");
00216                 } else { // Graph not finished: do not activate it
00217                         jack_error("JackAudioDriver::ProcessSync: error");
00218                 }
00219 
00220                 if (Write() < 0)  // Write output buffers for the current cycle
00221                         jack_error("Process: write error");
00222 
00223     } else {
00224         fGraphManager->ResumeRefNum(fClientControl, fSynchroTable);
00225     }
00226     return 0;
00227 }
00228 
00229 void JackAudioDriver::NotifyXRun(jack_time_t callback_usecs)
00230 {
00231     fEngine->NotifyXRun(callback_usecs);
00232 }
00233 
00234 jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(int port_index)
00235 {
00236     assert(fCapturePortList[port_index]);
00237     return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize);
00238 }
00239 
00240 jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(int port_index)
00241 {
00242     assert(fPlaybackPortList[port_index]);
00243     return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize);
00244 }
00245 
00246 jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(int port_index)
00247 {
00248     assert(fPlaybackPortList[port_index]);
00249     return (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize);
00250 }
00251 
00252 } // end of namespace

Generated on Mon Oct 15 17:11:13 2007 for Jackdmp by  doxygen 1.5.1