~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to arts/midi/audiomiditimer_impl.cc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    /*
 
2
 
 
3
    Copyright (C) 2001 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 "artsmidi.h"
 
24
#include "artsflow.h"
 
25
#include "stdsynthmodule.h"
 
26
#include "debug.h"
 
27
#include "miditimercommon.h"
 
28
#include "flowsystem.h"
 
29
 
 
30
using namespace std;
 
31
using namespace Arts;
 
32
 
 
33
namespace Arts {
 
34
 
 
35
class AudioMidiTimerCommon : public MidiTimerCommon,
 
36
                                                         public SynthModule_skel,
 
37
                                                         public StdSynthModule
 
38
                                                                        /* we are a SynthModule to get the timing */
 
39
{
 
40
protected:
 
41
        AudioMidiTimerCommon();
 
42
        virtual ~AudioMidiTimerCommon();
 
43
 
 
44
        long samples;   /* at most samplingRate, overflow goes in seconds */
 
45
        long seconds;
 
46
 
 
47
public:
 
48
        // allocation: share one AudioMidiTimerCommon for everbody who needs one
 
49
        static AudioMidiTimerCommon *subscribe();
 
50
 
 
51
        void unsubscribe() {
 
52
                if(--refCount == 0)
 
53
                {
 
54
                        stop();
 
55
                        delete this;
 
56
                }
 
57
        }
 
58
        void notify(const Notification& n);
 
59
        TimeStamp time();
 
60
        void calculateBlock(unsigned long samples);
 
61
};
 
62
 
 
63
};
 
64
 
 
65
static AudioMidiTimerCommon *AudioMidiTimerCommon_the = 0;
 
66
 
 
67
AudioMidiTimerCommon::AudioMidiTimerCommon()
 
68
{
 
69
        AudioMidiTimerCommon_the = this;
 
70
        samples = seconds = 0;
 
71
}
 
72
 
 
73
AudioMidiTimerCommon::~AudioMidiTimerCommon()
 
74
{
 
75
        AudioMidiTimerCommon_the = 0;
 
76
}
 
77
 
 
78
TimeStamp AudioMidiTimerCommon::time()
 
79
{
 
80
        return TimeStamp(seconds,
 
81
                                (long)((float)samples / samplingRateFloat * 1000000.0));
 
82
}
 
83
 
 
84
void AudioMidiTimerCommon::notify(const Notification &)
 
85
{
 
86
        processQueue();
 
87
}
 
88
 
 
89
void AudioMidiTimerCommon::calculateBlock(unsigned long s)
 
90
{
 
91
        samples += s;
 
92
        while(samples > samplingRate)
 
93
        {
 
94
                samples -= samplingRate;
 
95
                seconds++;
 
96
        }
 
97
        Notification n;
 
98
        n.receiver = this;
 
99
        n.ID = 0;
 
100
        n.data = 0;
 
101
        n.internal = 0;
 
102
        NotificationManager::the()->send(n);
 
103
}
 
104
 
 
105
AudioMidiTimerCommon *AudioMidiTimerCommon::subscribe()
 
106
{
 
107
        if(!AudioMidiTimerCommon_the)
 
108
        {
 
109
                new AudioMidiTimerCommon();
 
110
                AudioMidiTimerCommon_the->_node()->start();
 
111
        }
 
112
        AudioMidiTimerCommon_the->refCount++;
 
113
        return AudioMidiTimerCommon_the;
 
114
}
 
115
 
 
116
namespace Arts {
 
117
 
 
118
class AudioMidiTimer_impl : public AudioMidiTimer_skel {
 
119
protected:
 
120
        AudioMidiTimerCommon *timer;
 
121
public:
 
122
        AudioMidiTimer_impl()
 
123
        {
 
124
                timer = AudioMidiTimerCommon::subscribe();
 
125
        }
 
126
        ~AudioMidiTimer_impl()
 
127
        {
 
128
                timer->unsubscribe();
 
129
        }
 
130
        TimeStamp time()
 
131
        {
 
132
                return timer->time();
 
133
        }
 
134
        void queueEvent(MidiPort port, const MidiEvent& event)
 
135
        {
 
136
                timer->queueEvent(port, event);
 
137
        }
 
138
};
 
139
 
 
140
REGISTER_IMPLEMENTATION(AudioMidiTimer_impl);
 
141
}