forked from Tracktion/choc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
choc_AudioMIDIBlockDispatcher.h
321 lines (265 loc) · 13.4 KB
/
choc_AudioMIDIBlockDispatcher.h
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// ██████ ██ ██ ██████ ██████
// ██ ██ ██ ██ ██ ██ ** Classy Header-Only Classes **
// ██ ███████ ██ ██ ██
// ██ ██ ██ ██ ██ ██ https://github.com/Tracktion/choc
// ██████ ██ ██ ██████ ██████
//
// CHOC is (C)2022 Tracktion Corporation, and is offered under the terms of the ISC license:
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or
// without fee is hereby granted, provided that the above copyright notice and this permission
// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef CHOC_AUDIOMIDIDISPATCHER_HEADER_INCLUDED
#define CHOC_AUDIOMIDIDISPATCHER_HEADER_INCLUDED
#include <functional>
#include "../containers/choc_Span.h"
#include "../containers/choc_VariableSizeFIFO.h"
#include "../platform/choc_HighResolutionSteadyClock.h"
#include "../memory/choc_Endianness.h"
#include "choc_SampleBuffers.h"
#include "choc_MIDI.h"
namespace choc::audio
{
//==============================================================================
/**
Collects and synchronises incoming live audio and MIDI data, splitting it
into suitably-aligned chunks for a callback to handle.
This takes care of sub-dividing audio blocks between the times that MIDI messages
fall, so that a client gets callbacks where all the MIDI messages lie at the
start of the block, and don't need to worry about the timing of events within
the block.
*/
struct AudioMIDIBlockDispatcher
{
AudioMIDIBlockDispatcher() = default;
//==============================================================================
/// Prepares the dispatcher for running at the given sample-rate.
/// This must be called before any other methods are used.
void reset (double sampleRate, size_t midiFIFOCapacity = 1024);
//==============================================================================
using MIDIDeviceID = const char*;
using MIDIEventTime = HighResolutionSteadyClock::time_point;
/// Adds an incoming MIDI event to the queue. This can be called from any thread.
template <typename StorageType>
void addMIDIEvent (MIDIDeviceID, const choc::midi::Message<StorageType>&);
/// Adds an incoming MIDI event to the queue. This can be called from any thread.
void addMIDIEvent (MIDIDeviceID, const void* data, uint32_t size);
/// Adds a juce::MidiMessage to the queue. This can be called from any thread.
template <typename JUCECompatibleMIDIMessage>
void addMIDIEvent (MIDIDeviceID, const JUCECompatibleMIDIMessage&);
/// This struct holds a timestamped MIDI message with details about its source.
struct MIDIMessage
{
/// A global timestamp for this message.
/// NB: depending on where the event came from, this could be null.
MIDIEventTime time;
/// An ID for the device that was the source of this event. This is a
/// `const char*`, so may be either a null-terminated name, or a nullptr.
MIDIDeviceID sourceDeviceID;
/// The MIDI message itself.
choc::midi::MessageView message;
};
//==============================================================================
/// Before calling processInChunks(), this must be called to provide the audio buffers.
void setAudioBuffers (choc::buffer::ChannelArrayView<const float> input,
choc::buffer::ChannelArrayView<float> output);
/// Before calling processInChunks(), this must be called to provide the audio buffers.
void setAudioBuffers (const float* const* inputData, int numInputChannels,
float* const* outputData, int numOutputChannels, int numFrames);
/// A function prototype which accepts a time-stamped MIDI event.
using HandleMIDIMessageFn = std::function<void(uint32_t frame, choc::midi::MessageView)>;
/// Before calling processInChunks(), this may be called to receive MIDI output events.
void setMidiOutputCallback (HandleMIDIMessageFn);
//==============================================================================
/// This struct is given to a client callback to process.
struct Block
{
choc::buffer::ChannelArrayView<const float> audioInput;
choc::buffer::ChannelArrayView<float> audioOutput;
choc::span<MIDIMessage> midiMessages;
const HandleMIDIMessageFn& onMidiOutputMessage;
};
/// After calling setAudioBuffers() to provide the audio channel data, call this
/// to invoke a sequence of chunk callbacks on the function provided.
/// The callback function provided must take a Block object as its parameter.
template <typename Callback>
void processInChunks (Callback&&);
/// This clears the output buffers which were configured via a call to setAudioBuffers().
void clearOutputBuffers();
/// This governs the granularity at which MIDI events are time-stamped, and hence
/// determines the smallest chunk size into which the callbacks will be split.
/// Smaller sizes will give more accurate MIDI timing at the expense of extra callback
/// overhead when there are a lot of messages. In practice, 32 frames is less than
/// a millisecond so probably good enough for most live situations.
uint32_t midiTimingGranularityFrames = 32;
private:
//==============================================================================
choc::buffer::ChannelArrayView<float> nextOutputBlock;
choc::buffer::ChannelArrayView<const float> nextInputBlock;
HandleMIDIMessageFn midiOutputMessageCallback;
using DurationType = std::chrono::duration<double, std::ratio<1, 1>>;
static constexpr int32_t maxCatchUpFrames = 20000;
DurationType frameDuration;
MIDIEventTime lastBlockTime;
std::vector<uint32_t> midiMessageTimes;
std::vector<MIDIMessage> midiMessages;
uint32_t chunkFrameOffset = 0;
choc::fifo::VariableSizeFIFO midiFIFO;
void fetchMIDIBlockFromFIFO (choc::fifo::VariableSizeFIFO::BatchReadOperation&, uint32_t);
};
//==============================================================================
// _ _ _ _
// __| | ___ | |_ __ _ (_)| | ___
// / _` | / _ \| __| / _` || || |/ __|
// | (_| || __/| |_ | (_| || || |\__ \ _ _ _
// \__,_| \___| \__| \__,_||_||_||___/(_)(_)(_)
//
// Code beyond this point is implementation detail...
//
//==============================================================================
inline void AudioMIDIBlockDispatcher::reset (double sampleRate, size_t midiFIFOCapacity)
{
midiMessageTimes.clear();
midiMessageTimes.reserve (midiFIFOCapacity);
midiMessages.clear();
midiMessages.reserve (midiFIFOCapacity);
midiFIFO.reset (static_cast<uint32_t> (midiFIFOCapacity * (sizeof (MIDIEventTime) + sizeof (MIDIDeviceID) + 3u)));
frameDuration = DurationType (1.0 / sampleRate);
}
inline void AudioMIDIBlockDispatcher::addMIDIEvent (MIDIDeviceID deviceID, const void* data, uint32_t size)
{
midiFIFO.push (sizeof (MIDIEventTime) + sizeof (MIDIDeviceID) + size,
[time = HighResolutionSteadyClock::now(), deviceID, data, size] (void* dest)
{
auto d = static_cast<char*> (dest);
choc::memory::writeNativeEndian (d, time);
d += sizeof (MIDIEventTime);
choc::memory::writeNativeEndian (d, deviceID);
d += sizeof (MIDIDeviceID);
memcpy (d, data, size);
});
}
template <typename StorageType>
inline void AudioMIDIBlockDispatcher::addMIDIEvent (MIDIDeviceID deviceID, const choc::midi::Message<StorageType>& message)
{
addMIDIEvent (deviceID, message.data(), message.size());
}
template <typename JUCECompatibleMIDIMessage>
void AudioMIDIBlockDispatcher::addMIDIEvent (MIDIDeviceID deviceID, const JUCECompatibleMIDIMessage& message)
{
addMIDIEvent (deviceID, message.getRawData(), static_cast<uint32_t> (message.getRawDataSize()));
}
inline void AudioMIDIBlockDispatcher::setAudioBuffers (choc::buffer::ChannelArrayView<const float> input,
choc::buffer::ChannelArrayView<float> output)
{
CHOC_ASSERT (input.getNumFrames() == output.getNumFrames());
nextInputBlock = input;
nextOutputBlock = output;
}
inline void AudioMIDIBlockDispatcher::setAudioBuffers (const float* const* inputData, int numInputChannels,
float* const* outputData, int numOutputChannels, int numFrames)
{
setAudioBuffers (choc::buffer::createChannelArrayView (inputData,
static_cast<choc::buffer::ChannelCount> (numInputChannels),
static_cast<choc::buffer::FrameCount> (numFrames)),
choc::buffer::createChannelArrayView (outputData,
static_cast<choc::buffer::ChannelCount> (numOutputChannels),
static_cast<choc::buffer::FrameCount> (numFrames)));
}
inline void AudioMIDIBlockDispatcher::setMidiOutputCallback (AudioMIDIBlockDispatcher::HandleMIDIMessageFn callback)
{
if (! callback)
{
midiOutputMessageCallback = {};
return;
}
midiOutputMessageCallback = [this, cb = std::move (callback)] (uint32_t frame, choc::midi::MessageView m)
{
cb (frame + chunkFrameOffset, m);
};
}
template <typename Callback>
void AudioMIDIBlockDispatcher::processInChunks (Callback&& process)
{
chunkFrameOffset = 0;
const auto numFrames = nextOutputBlock.getNumFrames();
CHOC_ASSERT (numFrames == nextInputBlock.getNumFrames());
choc::fifo::VariableSizeFIFO::BatchReadOperation midiFIFOBatchOp (midiFIFO);
fetchMIDIBlockFromFIFO (midiFIFOBatchOp, numFrames);
if (auto totalNumMIDIMessages = static_cast<uint32_t> (midiMessageTimes.size()))
{
auto frameRange = nextOutputBlock.getFrameRange();
uint32_t midiStart = 0;
while (frameRange.start < frameRange.end)
{
auto chunkToDo = frameRange;
auto endOfMIDI = midiStart;
while (endOfMIDI < totalNumMIDIMessages)
{
auto eventTime = midiMessageTimes[endOfMIDI];
if (eventTime > chunkToDo.start)
{
chunkToDo.end = eventTime;
break;
}
++endOfMIDI;
}
process (Block { nextInputBlock.getFrameRange (chunkToDo),
nextOutputBlock.getFrameRange (chunkToDo),
choc::span<const MIDIMessage> (midiMessages.data() + midiStart,
midiMessages.data() + endOfMIDI),
midiOutputMessageCallback });
chunkFrameOffset += chunkToDo.size();
frameRange.start = chunkToDo.end;
midiStart = endOfMIDI;
}
}
else
{
process (Block { nextInputBlock, nextOutputBlock, {}, midiOutputMessageCallback });
}
CHOC_ASSERT(chunkFrameOffset <= numFrames);
}
inline void AudioMIDIBlockDispatcher::clearOutputBuffers()
{
nextOutputBlock.clear();
}
inline void AudioMIDIBlockDispatcher::fetchMIDIBlockFromFIFO (choc::fifo::VariableSizeFIFO::BatchReadOperation& midiFIFOBatchOp, uint32_t numFramesNeeded)
{
midiMessages.clear();
midiMessageTimes.clear();
auto blockStartTime = lastBlockTime;
lastBlockTime = HighResolutionSteadyClock::now();
while (midiFIFOBatchOp.pop ([&] (const void* d, uint32_t totalSize)
{
auto data = static_cast<const char*> (d);
auto dataEnd = data + totalSize;
auto eventTime = choc::memory::readNativeEndian<MIDIEventTime> (data);
auto timeWithinBlock = DurationType (eventTime - blockStartTime);
auto frameIndex = static_cast<int32_t> (timeWithinBlock.count() / frameDuration.count());
if (frameIndex < 0)
{
if (frameIndex < -maxCatchUpFrames)
return;
frameIndex = 0;
}
else if (frameIndex > static_cast<int32_t> (numFramesNeeded))
{
frameIndex = static_cast<int32_t> (numFramesNeeded) - 1;
}
auto snappedIndex = static_cast<uint32_t> (frameIndex) % midiTimingGranularityFrames;
midiMessageTimes.push_back (snappedIndex);
data += sizeof (MIDIEventTime);
auto deviceID = choc::memory::readNativeEndian<MIDIDeviceID> (data);
data += sizeof (MIDIDeviceID);
midiMessages.push_back ({ eventTime, deviceID, { data, static_cast<size_t> (dataEnd - data) }});
})) {}
}
} // namespace choc::audio
#endif // CHOC_AUDIOMIDIDISPATCHER_HEADER_INCLUDED