Merge branch 'master' into develop
[jack2.git] / common / JackTimedDriver.cpp
blob7fccfd01c89274e4d49feed3a21666ef4118e853
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "JackTimedDriver.h"
22 #include "JackEngineControl.h"
23 #include "JackTime.h"
24 #include "JackCompilerDeps.h"
25 #include <iostream>
26 #include <unistd.h>
27 #include <math.h>
29 namespace Jack
32 int JackTimedDriver::FirstCycle(jack_time_t cur_time_usec)
34 fAnchorTimeUsec = cur_time_usec;
35 return int((double(fEngineControl->fBufferSize) * 1000000) / double(fEngineControl->fSampleRate));
38 int JackTimedDriver::CurrentCycle(jack_time_t cur_time_usec)
40 return int(((double(fCycleCount) * double(fEngineControl->fBufferSize) * 1000000.) / double(fEngineControl->fSampleRate)) - (cur_time_usec - fAnchorTimeUsec));
43 int JackTimedDriver::Start()
45 fCycleCount = 0;
46 return JackAudioDriver::Start();
49 void JackTimedDriver::ProcessWait()
51 jack_time_t cur_time_usec = GetMicroSeconds();
52 int wait_time_usec;
54 if (fCycleCount++ == 0) {
55 wait_time_usec = FirstCycle(cur_time_usec);
56 } else {
57 wait_time_usec = CurrentCycle(cur_time_usec);
60 if (wait_time_usec < 0) {
61 NotifyXRun(cur_time_usec, float(cur_time_usec - fBeginDateUst));
62 fCycleCount = 0;
63 wait_time_usec = 0;
64 jack_error("JackTimedDriver::Process XRun = %ld usec", (cur_time_usec - fBeginDateUst));
67 //jack_log("JackTimedDriver::Process wait_time = %d", wait_time_usec);
68 JackSleep(wait_time_usec);
71 int JackWaiterDriver::ProcessNull()
73 JackDriver::CycleTakeBeginTime();
75 // Graph processing without Read/Write
76 if (fEngineControl->fSyncMode) {
77 ProcessGraphSync();
78 } else {
79 ProcessGraphAsync();
82 // Keep end cycle time
83 JackDriver::CycleTakeEndTime();
85 ProcessWait();
86 return 0;
89 void JackRestarterDriver::SetRestartDriver(JackDriver* driver)
91 fRestartDriver = driver;
94 int JackRestarterDriver::RestartWait()
96 if (!fRestartDriver) {
97 jack_error("JackRestartedDriver::RestartWait driver not set");
98 return -1;
100 return fRestartDriver->Start();
103 } // end of namespace