PipeWire: spa/control/ump-utils.h Source File

PipeWire 1.4.2 |

Loading...
Searching...
No Matches
ump-utils.h
Go to the documentation of this file.
1/* Simple Plugin API */
2/* SPDX-FileCopyrightText: Copyright © 2024 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5
6#ifndef SPA_CONTROL_UMP_UTILS_H
7#define SPA_CONTROL_UMP_UTILS_H
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13#include <errno.h>
14#include <spa/utils/defs.h>
15
16#ifndef SPA_API_CONTROL_UMP_UTILS
17 #ifdef SPA_API_IMPL
18 #define SPA_API_CONTROL_UMP_UTILS SPA_API_IMPL
19 #else
20 #define SPA_API_CONTROL_UMP_UTILS static inline
21 #endif
22#endif
27
28 SPA_API_CONTROL_UMP_UTILS size_t spa_ump_message_size(uint8_t message_type)
29{
30 static const uint32_t ump_sizes[] = {
31 [0x0] = 1, /* Utility messages */
32 [0x1] = 1, /* System messages */
33 [0x2] = 1, /* MIDI 1.0 messages */
34 [0x3] = 2, /* 7bit SysEx messages */
35 [0x4] = 2, /* MIDI 2.0 messages */
36 [0x5] = 4, /* 8bit data message */
37 [0x6] = 1,
38 [0x7] = 1,
39 [0x8] = 2,
40 [0x9] = 2,
41 [0xa] = 2,
42 [0xb] = 3,
43 [0xc] = 3,
44 [0xd] = 4, /* Flexible data messages */
45 [0xe] = 4,
46 [0xf] = 4, /* Stream messages */
47 };
48 return ump_sizes[message_type & 0xf];
49}
50
51 SPA_API_CONTROL_UMP_UTILS int spa_ump_to_midi(uint32_t *ump, size_t ump_size,
52 uint8_t *midi, size_t midi_maxsize)
53{
54 int size = 0;
55
56 if (ump_size < 4)
57 return 0;
58 if (midi_maxsize < 8)
59 return -ENOSPC;
60
61 switch (ump[0] >> 28) {
62 case 0x1: /* System Real Time and System Common Messages (except System Exclusive) */
63 midi[size++] = (ump[0] >> 16) & 0xff;
64 if (midi[0] >= 0xf1 && midi[0] <= 0xf3) {
65 midi[size++] = (ump[0] >> 8) & 0x7f;
66 if (midi[0] == 0xf2)
67 midi[size++] = ump[0] & 0x7f;
68 }
69 break;
70 case 0x2: /* MIDI 1.0 Channel Voice Messages */
71 midi[size++] = (ump[0] >> 16);
72 midi[size++] = (ump[0] >> 8);
73 if (midi[0] < 0xc0 || midi[0] > 0xdf)
74 midi[size++] = (ump[0]);
75 break;
76 case 0x3: /* Data Messages (including System Exclusive) */
77 {
78 uint8_t status, i, bytes;
79
80 if (ump_size < 8)
81 return 0;
82
83 status = (ump[0] >> 20) & 0xf;
84 bytes = SPA_CLAMP((ump[0] >> 16) & 0xf, 0u, 6u);
85
86 if (status == 0 || status == 1)
87 midi[size++] = 0xf0;
88 for (i = 0 ; i < bytes; i++)
89 /* ump[0] >> 8 | ump[0] | ump[1] >> 24 | ump[1] >>16 ... */
90 midi[size++] = ump[(i+2)/4] >> ((5-i)%4 * 8);
91 if (status == 0 || status == 3)
92 midi[size++] = 0xf7;
93 break;
94 }
95 case 0x4: /* MIDI 2.0 Channel Voice Messages */
96 if (ump_size < 8)
97 return 0;
98 midi[size++] = (ump[0] >> 16) | 0x80;
99 if (midi[0] < 0xc0 || midi[0] > 0xdf)
100 midi[size++] = (ump[0] >> 8) & 0x7f;
101 midi[size++] = (ump[1] >> 25);
102 break;
103
104 case 0x0: /* Utility Messages */
105 case 0x5: /* Data Messages */
106 default:
107 return 0;
108 }
109 return size;
110}
111
112 SPA_API_CONTROL_UMP_UTILS int spa_ump_from_midi(uint8_t **midi, size_t *midi_size,
113 uint32_t *ump, size_t ump_maxsize, uint8_t group, uint64_t *state)
114{
115 int size = 0;
116 uint32_t i, prefix = group << 24, to_consume = 0, bytes;
117 uint8_t status, *m = (*midi), end;
118
119 if (*midi_size < 1)
120 return 0;
121 if (ump_maxsize < 16)
122 return -ENOSPC;
123
124 status = m[0];
125
126 /* SysEx */
127 if (*state == 0) {
128 if (status == 0xf0)
129 *state = 1; /* sysex start */
130 else if (status == 0xf7)
131 *state = 2; /* sysex continue */
132 }
133 if (*state & 3) {
134 prefix |= 0x30000000;
135 if (status & 0x80) {
136 m++;
137 to_consume++;
138 }
139 bytes = SPA_CLAMP(*midi_size - to_consume, 0u, 7u);
140 if (bytes > 0) {
141 end = m[bytes-1];
142 if (end & 0x80) {
143 bytes--; /* skip terminator */
144 to_consume++;
145 }
146 else
147 end = 0xf0; /* pretend there is a continue terminator */
148
149 bytes = SPA_CLAMP(bytes, 0u, 6u);
150 to_consume += bytes;
151
152 if (end == 0xf7) {
153 if (*state == 2) {
154 /* continue and done */
155 prefix |= 0x3 << 20;
156 *state = 0;
157 }
158 } else if (*state == 1) {
159 /* first packet but not finished */
160 prefix |= 0x1 << 20;
161 *state = 2; /* sysex continue */
162 } else {
163 /* continue and not finished */
164 prefix |= 0x2 << 20;
165 }
166 ump[size++] = prefix | bytes << 16;
167 ump[size++] = 0;
168 for (i = 0 ; i < bytes; i++)
169 /* ump[0] |= (m[0] & 0x7f) << 8
170 * ump[0] |= (m[1] & 0x7f)
171 * ump[1] |= (m[2] & 0x7f) << 24
172 * ... */
173 ump[(i+2)/4] |= (m[i] & 0x7f) << ((5-i)%4 * 8);
174 }
175 } else {
176 /* regular messages */
177 switch (status) {
178 case 0x80 ... 0x8f:
179 case 0x90 ... 0x9f:
180 case 0xa0 ... 0xaf:
181 case 0xb0 ... 0xbf:
182 case 0xe0 ... 0xef:
183 to_consume = 3;
184 prefix |= 0x20000000;
185 break;
186 case 0xc0 ... 0xdf:
187 to_consume = 2;
188 prefix |= 0x20000000;
189 break;
190 case 0xf2:
191 to_consume = 3;
192 prefix = 0x10000000;
193 break;
194 case 0xf1: case 0xf3:
195 to_consume = 2;
196 prefix = 0x10000000;
197 break;
198 case 0xf4 ... 0xff:
199 to_consume = 1;
200 prefix = 0x10000000;
201 break;
202 default:
203 return -EIO;
204 }
205 if (*midi_size < to_consume) {
206 to_consume = *midi_size;
207 } else {
208 prefix |= status << 16;
209 if (to_consume > 1)
210 prefix |= (m[1] & 0x7f) << 8;
211 if (to_consume > 2)
212 prefix |= (m[2] & 0x7f);
213 ump[size++] = prefix;
214 }
215 }
216 (*midi_size) -= to_consume;
217 (*midi) += to_consume;
218
219 return size * 4;
220}
221
225
226#ifdef __cplusplus
227} /* extern "C" */
228#endif
229
230#endif /* SPA_CONTROL_UMP_UTILS_H */
defs.h
spa/utils/defs.h
spa_ump_to_midi
SPA_API_CONTROL_UMP_UTILS int spa_ump_to_midi(uint32_t *ump, size_t ump_size, uint8_t *midi, size_t midi_maxsize)
Definition ump-utils.h:58
spa_ump_message_size
SPA_API_CONTROL_UMP_UTILS size_t spa_ump_message_size(uint8_t message_type)
Definition ump-utils.h:35
spa_ump_from_midi
SPA_API_CONTROL_UMP_UTILS int spa_ump_from_midi(uint8_t **midi, size_t *midi_size, uint32_t *ump, size_t ump_maxsize, uint8_t group, uint64_t *state)
Definition ump-utils.h:119
SPA_CLAMP
#define SPA_CLAMP(v, low, high)
Definition defs.h:177
SPA_API_CONTROL_UMP_UTILS
#define SPA_API_CONTROL_UMP_UTILS
Definition ump-utils.h:27