-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventprocessorclient.cpp
114 lines (101 loc) · 4.13 KB
/
eventprocessorclient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
Copyright 2011 Arne Jacobs <[email protected]>
This file is part of elektrocillin.
Elektrocillin is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Elektrocillin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Elektrocillin. If not, see <http://www.gnu.org/licenses/>.
*/
#include "eventprocessorclient.h"
EventProcessorClient::EventProcessorClient(const QString &clientName, AudioProcessor *audioProcessor, MidiProcessor *midiProcessor, EventProcessor *eventProcessor_, size_t ringBufferSize) :
MidiProcessorClient(clientName, audioProcessor, midiProcessor),
eventProcessor(eventProcessor_),
ringBuffer(ringBufferSize)
{}
EventProcessorClient::EventProcessorClient(const QString &clientName, const QStringList &audioInputPortNames, const QStringList &audioOutputPortNames, const QStringList &midiInputPortNames, const QStringList &midiOutputPortNames, size_t ringBufferSize) :
MidiProcessorClient(clientName, audioInputPortNames, audioOutputPortNames, midiInputPortNames, midiOutputPortNames),
eventProcessor(0),
ringBuffer(ringBufferSize)
{}
EventProcessor * EventProcessorClient::getEventProcessor()
{
return eventProcessor;
}
bool EventProcessorClient::postEvent(RingBufferEvent *event)
{
if (isActive()) {
jack_nframes_t time = getEstimatedCurrentTime();
return ringBuffer.sendEvent(event, time);
} else {
return false;
}
}
bool EventProcessorClient::postEvents(const QVector<RingBufferEvent*> &events)
{
if (isActive()) {
int writtenEvents = 0;
jack_nframes_t time = getEstimatedCurrentTime();
for (int i = 0; i < events.size(); i++) {
if (ringBuffer.sendEvent(events[i], time)) {
writtenEvents++;
}
}
return (writtenEvents == events.size());
} else {
return false;
}
}
bool EventProcessorClient::process(jack_nframes_t nframes)
{
// get port buffers:
getAudioPortBuffers(nframes);
getMidiPortBuffers(nframes);
// process all events:
processEvents(0, nframes, nframes);
return true;
}
bool EventProcessorClient::processEvents(jack_nframes_t start, jack_nframes_t end, jack_nframes_t nframes)
{
jack_nframes_t lastFrameTime = getLastFrameTime();
for (jack_nframes_t currentFrame = start; currentFrame < end; ) {
// get the next event from the ring buffer, if there is any:
if (ringBuffer.hasEvents()) {
jack_nframes_t eventTime = ringBuffer.peekEventTime();
// adjust time relative to the beginning of this frame:
if (eventTime + nframes < lastFrameTime) {
// if time is too early, this is in the buffer for too long, adjust time accordingly:
eventTime = 0;
} else {
eventTime = eventTime + nframes - lastFrameTime;
}
if (eventTime < end) {
// process everything up to the event's time stamp:
processMidi(currentFrame, eventTime);
currentFrame = eventTime;
// process the event:
RingBufferEvent *event = ringBuffer.readEvent();
processEvent(event, eventTime);
// have the event deleted in the creator thread:
ringBuffer.returnEvent(event);
} else {
processMidi(currentFrame, end);
currentFrame = end;
}
} else {
processMidi(currentFrame, end);
currentFrame = end;
}
}
return true;
}
bool EventProcessorClient::processEvent(const RingBufferEvent *event, jack_nframes_t time)
{
Q_ASSERT(eventProcessor);
return getEventProcessor()->processEvent(event, time);
}