LMMS
Loading...
Searching...
No Matches
juce_MidiDataConcatenator.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26//==============================================================================
34{
35public:
36 MidiDataConcatenator (int initialBufferSize)
37 : pendingSysexData ((size_t) initialBufferSize)
38 {
39 }
40
41 void reset()
42 {
46 }
47
48 template <typename UserDataType, typename CallbackType>
49 void pushMidiData (const void* inputData, int numBytes, double time,
50 UserDataType* input, CallbackType& callback)
51 {
52 auto d = static_cast<const uint8*> (inputData);
53
54 while (numBytes > 0)
55 {
56 auto nextByte = *d;
57
58 if (pendingSysexSize != 0 || nextByte == 0xf0)
59 {
60 processSysex (d, numBytes, time, input, callback);
62 continue;
63 }
64
65 ++d;
66 --numBytes;
67
68 if (isRealtimeMessage (nextByte))
69 {
70 callback.handleIncomingMidiMessage (input, MidiMessage (nextByte, time));
71 // These can be embedded in the middle of a normal message, so we won't
72 // reset the currentMessageLen here.
73 continue;
74 }
75
76 if (isInitialByte (nextByte))
77 {
78 currentMessage[0] = nextByte;
80 }
81 else if (currentMessageLen > 0 && currentMessageLen < 3)
82 {
84 }
85 else
86 {
87 // message is too long or invalid MIDI - abandon it and start again with the next byte
89 continue;
90 }
91
93
94 if (expectedLength == currentMessageLen)
95 {
96 callback.handleIncomingMidiMessage (input, MidiMessage (currentMessage, expectedLength, time));
97 currentMessageLen = 1; // reset, but leave the first byte to use as the running status byte
98 }
99 }
100 }
101
102private:
103 template <typename UserDataType, typename CallbackType>
104 void processSysex (const uint8*& d, int& numBytes, double time,
105 UserDataType* input, CallbackType& callback)
106 {
107 if (*d == 0xf0)
108 {
110 pendingSysexTime = time;
111 }
112
113 pendingSysexData.ensureSize ((size_t) (pendingSysexSize + numBytes), false);
114 auto totalMessage = static_cast<uint8*> (pendingSysexData.getData());
115 auto dest = totalMessage + pendingSysexSize;
116
117 do
118 {
119 if (pendingSysexSize > 0 && isStatusByte (*d))
120 {
121 if (*d == 0xf7)
122 {
123 *dest++ = *d++;
125 --numBytes;
126 break;
127 }
128
129 if (*d >= 0xfa || *d == 0xf8)
130 {
131 callback.handleIncomingMidiMessage (input, MidiMessage (*d, time));
132 ++d;
133 --numBytes;
134 }
135 else
136 {
138 int used = 0;
139 const MidiMessage m (d, numBytes, used, 0, time);
140
141 if (used > 0)
142 {
143 callback.handleIncomingMidiMessage (input, m);
144 numBytes -= used;
145 d += used;
146 }
147
148 break;
149 }
150 }
151 else
152 {
153 *dest++ = *d++;
155 --numBytes;
156 }
157 }
158 while (numBytes > 0);
159
160 if (pendingSysexSize > 0)
161 {
162 if (totalMessage [pendingSysexSize - 1] == 0xf7)
163 {
164 callback.handleIncomingMidiMessage (input, MidiMessage (totalMessage, pendingSysexSize, pendingSysexTime));
166 }
167 else
168 {
169 callback.handlePartialSysexMessage (input, totalMessage, pendingSysexSize, pendingSysexTime);
170 }
171 }
172 }
173
174 static bool isRealtimeMessage (uint8 byte) { return byte >= 0xf8 && byte <= 0xfe; }
175 static bool isStatusByte (uint8 byte) { return byte >= 0x80; }
176 static bool isInitialByte (uint8 byte) { return isStatusByte (byte) && byte != 0xf7; }
177
180
184
186};
187
188} // namespace juce
static int getMessageLengthFromFirstByte(uint8 firstByte) noexcept
Definition MidiMessage.cpp:82
Definition juce_MemoryBlock.h:33
static bool isRealtimeMessage(uint8 byte)
Definition juce_MidiDataConcatenator.h:174
int pendingSysexSize
Definition juce_MidiDataConcatenator.h:183
void pushMidiData(const void *inputData, int numBytes, double time, UserDataType *input, CallbackType &callback)
Definition juce_MidiDataConcatenator.h:49
uint8 currentMessage[3]
Definition juce_MidiDataConcatenator.h:178
MidiDataConcatenator(int initialBufferSize)
Definition juce_MidiDataConcatenator.h:36
static bool isStatusByte(uint8 byte)
Definition juce_MidiDataConcatenator.h:175
void processSysex(const uint8 *&d, int &numBytes, double time, UserDataType *input, CallbackType &callback)
Definition juce_MidiDataConcatenator.h:104
void reset()
Definition juce_MidiDataConcatenator.h:41
MemoryBlock pendingSysexData
Definition juce_MidiDataConcatenator.h:181
double pendingSysexTime
Definition juce_MidiDataConcatenator.h:182
static bool isInitialByte(uint8 byte)
Definition juce_MidiDataConcatenator.h:176
int currentMessageLen
Definition juce_MidiDataConcatenator.h:179
Definition juce_MidiMessage.h:35
unsigned * m
Definition inflate.c:1559
unsigned d
Definition inflate.c:940
#define JUCE_DECLARE_NON_COPYABLE(className)
Definition carla_juce.cpp:31
unsigned char uint8
Definition juce_MathsFunctions.h:37
RECT const char void(* callback)(const char *droppath))) SWELL_API_DEFINE(BOOL
Definition swell-functions.h:1004