3
Copyright (C) 2001-2002 Stefan Westerfeld
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public
8
License as published by the Free Software Foundation; either
9
version 2 of the License, or (at your option) any later version.
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU Library General Public License
17
along with this library; see the file COPYING.LIB. If not, write to
18
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
Boston, MA 02111-1307, USA.
23
#include "audiosync_impl.h"
24
#include "midisyncgroup_impl.h"
25
#include "audiotimer.h"
26
#include "audiosubsys.h"
27
#include "timestampmath.h"
29
#undef AUDIO_DEBUG_DRIFT
35
static list<AudioSync_impl *> audioSyncImplList;
38
void AudioSync_impl::AudioSyncEvent::execute()
40
list<SynthModule>::iterator i;
42
for(i = startModules.begin(); i != startModules.end(); i++)
45
for(i = stopModules.begin(); i != stopModules.end(); i++)
49
AudioSync_impl::AudioSync_impl()
50
: newEvent(new AudioSyncEvent), syncGroup(0)
52
syncOffset = TimeStamp(0,0);
54
timer = AudioTimer::subscribe();
55
timer->addCallback(this);
57
audioSyncImplList.push_back(this);
60
AudioSync_impl::~AudioSync_impl()
64
while(!events.empty())
66
delete events.front();
72
syncGroup->audioSyncDied(this);
75
audioSyncImplList.remove(this);
76
timer->removeCallback(this);
80
TimeStamp AudioSync_impl::time()
83
return syncGroup->time();
88
TimeStamp AudioSync_impl::playTime()
91
return syncGroup->playTime();
93
return audioPlayTime();
96
TimeStamp AudioSync_impl::audioTime()
101
TimeStamp AudioSync_impl::audioPlayTime()
103
double delay = AudioSubSystem::the()->outputDelay();
105
TimeStamp time = audioTime();
106
timeStampDec(time,timeStampFromDouble(delay));
110
TimeStamp AudioSync_impl::clientTime()
112
TimeStamp time = audioTime();
113
timeStampDec(time, syncOffset);
117
void AudioSync_impl::queueStart(SynthModule synthModule)
119
newEvent->startModules.push_back(synthModule);
122
void AudioSync_impl::queueStop(SynthModule synthModule)
124
newEvent->stopModules.push_back(synthModule);
127
void AudioSync_impl::execute()
130
newEvent->startModules.clear();
131
newEvent->stopModules.clear();
134
void AudioSync_impl::executeAt(const TimeStamp& timeStamp)
136
newEvent->time = timeStamp;
138
timeStampInc(newEvent->time, syncOffset);
140
events.push_back(newEvent);
142
newEvent = new AudioSyncEvent;
145
void AudioSync_impl::updateTime()
147
TimeStamp now = audioTime();
148
list<AudioSyncEvent*>::iterator i;
151
while(i != events.end())
153
AudioSyncEvent *event = *i;
154
TimeStamp& eventTime = event->time;
156
if( now.sec > eventTime.sec
157
|| ((now.sec == eventTime.sec) && (now.usec > eventTime.usec)))
170
void AudioSync_impl::setSyncGroup(MidiSyncGroup_impl *newSyncGroup)
172
syncGroup = newSyncGroup;
175
void AudioSync_impl::synchronizeTo(const TimeStamp& time)
177
#ifdef AUDIO_DEBUG_DRIFT
178
TimeStamp drift = syncOffset; // debug drift
181
syncOffset = audioPlayTime();
182
timeStampDec(syncOffset, time);
184
#ifdef AUDIO_DEBUG_DRIFT
185
timeStampDec(drift, syncOffset); // debug drift
186
printf("SYNC DRIFT %30s %30s: %f\n",
187
"AudioSync", "AudioSync", timeStampToDouble(drift));
191
AudioSync_impl *AudioSync_impl::find(AudioSync audioSync)
193
list<AudioSync_impl *>::iterator i;
195
for(i = audioSyncImplList.begin(); i != audioSyncImplList.end(); i++)
197
if((*i)->_isEqual(audioSync._base()))
203
namespace Arts { REGISTER_IMPLEMENTATION(AudioSync_impl); }