~ubuntu-branches/ubuntu/breezy/kdemultimedia/breezy

« back to all changes in this revision

Viewing changes to arts/midi/audiosync_impl.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-03-24 04:48:58 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050324044858-8ff88o9jxej6ii3d
Tags: 4:3.4.0-0ubuntu3
Add kubuntu_02_hide_arts_menu_entries.diff to hide artsbuilder and artscontrol k-menu entries

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    /*
 
2
 
 
3
    Copyright (C) 2001-2002 Stefan Westerfeld
 
4
                            stefan@space.twc.de
 
5
 
 
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.
 
10
  
 
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.
 
15
   
 
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.
 
20
 
 
21
    */
 
22
 
 
23
#include "audiosync_impl.h"
 
24
#include "midisyncgroup_impl.h"
 
25
#include "audiotimer.h"
 
26
#include "audiosubsys.h"
 
27
#include "timestampmath.h"
 
28
 
 
29
#undef AUDIO_DEBUG_DRIFT
 
30
 
 
31
using namespace std;
 
32
using namespace Arts;
 
33
 
 
34
namespace Arts {
 
35
        static list<AudioSync_impl *> audioSyncImplList;
 
36
}
 
37
 
 
38
void AudioSync_impl::AudioSyncEvent::execute()
 
39
{
 
40
        list<SynthModule>::iterator i;
 
41
 
 
42
        for(i = startModules.begin(); i != startModules.end(); i++)
 
43
                i->start();
 
44
 
 
45
        for(i = stopModules.begin(); i != stopModules.end(); i++)
 
46
                i->stop();
 
47
}
 
48
 
 
49
AudioSync_impl::AudioSync_impl()
 
50
        : newEvent(new AudioSyncEvent), syncGroup(0)
 
51
{
 
52
        syncOffset = TimeStamp(0,0);
 
53
 
 
54
        timer = AudioTimer::subscribe();
 
55
        timer->addCallback(this);
 
56
 
 
57
        audioSyncImplList.push_back(this);
 
58
}
 
59
 
 
60
AudioSync_impl::~AudioSync_impl()
 
61
{
 
62
        delete newEvent;
 
63
 
 
64
        while(!events.empty())
 
65
        {
 
66
                delete events.front();
 
67
                events.pop_front();
 
68
        }
 
69
 
 
70
        if(syncGroup)
 
71
        {
 
72
                syncGroup->audioSyncDied(this);
 
73
                syncGroup = 0;
 
74
        }
 
75
        audioSyncImplList.remove(this);
 
76
        timer->removeCallback(this);
 
77
        timer->unsubscribe();
 
78
}
 
79
 
 
80
TimeStamp AudioSync_impl::time()
 
81
{
 
82
        if(syncGroup)
 
83
                return syncGroup->time();
 
84
        else
 
85
                return audioTime();
 
86
}
 
87
 
 
88
TimeStamp AudioSync_impl::playTime()
 
89
{
 
90
        if(syncGroup)
 
91
                return syncGroup->playTime();
 
92
        else
 
93
                return audioPlayTime();
 
94
}
 
95
 
 
96
TimeStamp AudioSync_impl::audioTime()
 
97
{
 
98
        return timer->time();
 
99
}
 
100
 
 
101
TimeStamp AudioSync_impl::audioPlayTime()
 
102
{
 
103
        double delay = AudioSubSystem::the()->outputDelay();
 
104
 
 
105
        TimeStamp time = audioTime();
 
106
        timeStampDec(time,timeStampFromDouble(delay));
 
107
        return time;
 
108
}
 
109
 
 
110
TimeStamp AudioSync_impl::clientTime()
 
111
{
 
112
        TimeStamp time = audioTime();
 
113
        timeStampDec(time, syncOffset);
 
114
        return time;
 
115
}
 
116
 
 
117
void AudioSync_impl::queueStart(SynthModule synthModule)
 
118
{
 
119
        newEvent->startModules.push_back(synthModule);
 
120
}
 
121
 
 
122
void AudioSync_impl::queueStop(SynthModule synthModule)
 
123
{
 
124
        newEvent->stopModules.push_back(synthModule);
 
125
}
 
126
 
 
127
void AudioSync_impl::execute()
 
128
{
 
129
        newEvent->execute();
 
130
        newEvent->startModules.clear();
 
131
        newEvent->stopModules.clear();
 
132
}
 
133
 
 
134
void AudioSync_impl::executeAt(const TimeStamp& timeStamp)
 
135
{
 
136
        newEvent->time = timeStamp;
 
137
        if(syncGroup)
 
138
                timeStampInc(newEvent->time, syncOffset);
 
139
 
 
140
        events.push_back(newEvent);
 
141
 
 
142
        newEvent = new AudioSyncEvent;
 
143
}
 
144
 
 
145
void AudioSync_impl::updateTime()
 
146
{
 
147
        TimeStamp now = audioTime();
 
148
        list<AudioSyncEvent*>::iterator i;
 
149
 
 
150
        i = events.begin();
 
151
        while(i != events.end())
 
152
        {       
 
153
                AudioSyncEvent *event = *i;
 
154
                TimeStamp& eventTime = event->time;
 
155
 
 
156
                if( now.sec > eventTime.sec
 
157
                                || ((now.sec == eventTime.sec) && (now.usec > eventTime.usec)))
 
158
                {
 
159
                        event->execute();
 
160
                        delete event;
 
161
                        i = events.erase(i);
 
162
                }
 
163
                else
 
164
                {
 
165
                        i++;
 
166
                }
 
167
        }
 
168
}
 
169
 
 
170
void AudioSync_impl::setSyncGroup(MidiSyncGroup_impl *newSyncGroup)
 
171
{
 
172
        syncGroup = newSyncGroup;
 
173
}
 
174
 
 
175
void AudioSync_impl::synchronizeTo(const TimeStamp& time)
 
176
{
 
177
#ifdef AUDIO_DEBUG_DRIFT
 
178
        TimeStamp drift = syncOffset;                   // debug drift
 
179
#endif
 
180
 
 
181
        syncOffset = audioPlayTime();
 
182
        timeStampDec(syncOffset, time);
 
183
 
 
184
#ifdef AUDIO_DEBUG_DRIFT
 
185
        timeStampDec(drift, syncOffset);                // debug drift
 
186
        printf("SYNC DRIFT %30s %30s: %f\n",
 
187
                                "AudioSync", "AudioSync", timeStampToDouble(drift));
 
188
#endif
 
189
}
 
190
 
 
191
AudioSync_impl *AudioSync_impl::find(AudioSync audioSync)
 
192
{
 
193
        list<AudioSync_impl *>::iterator i;
 
194
 
 
195
        for(i = audioSyncImplList.begin(); i != audioSyncImplList.end(); i++)
 
196
        {
 
197
                if((*i)->_isEqual(audioSync._base()))
 
198
                        return (*i);
 
199
        }
 
200
        return 0;
 
201
}
 
202
 
 
203
namespace Arts { REGISTER_IMPLEMENTATION(AudioSync_impl); }