gusimplewhiteboard
wb_dominant_frequencies.c
Go to the documentation of this file.
1/*
2 * file wb_dominant_frequencies.c
3 *
4 * This file was generated by classgenerator from dominant_frequencies.gen.
5 * DO NOT CHANGE MANUALLY!
6 *
7 * Copyright © 2021 Rene Hexel. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials
19 * provided with the distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgement:
23 *
24 * This product includes software developed by Rene Hexel.
25 *
26 * 4. Neither the name of the author nor the names of contributors
27 * may be used to endorse or promote products derived from this
28 * software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
34 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * -----------------------------------------------------------------------
43 * This program is free software; you can redistribute it and/or
44 * modify it under the above terms or under the terms of the GNU
45 * General Public License as published by the Free Software Foundation;
46 * either version 2 of the License, or (at your option) any later version.
47 *
48 * This program is distributed in the hope that it will be useful,
49 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51 * GNU General Public License for more details.
52 *
53 * You should have received a copy of the GNU General Public License
54 * along with this program; if not, see http://www.gnu.org/licenses/
55 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
56 * Fifth Floor, Boston, MA 02110-1301, USA.
57 *
58 */
59
60#ifndef WHITEBOARD_POSTER_STRING_CONVERSION
61#define WHITEBOARD_POSTER_STRING_CONVERSION
62#endif // WHITEBOARD_POSTER_STRING_CONVERSION
63
64#include "gusimplewhiteboard.h"
66#include <stdio.h>
67#include <string.h>
68#include <stdlib.h>
69#include <ctype.h>
70#include <limits.h>
71
72/* Network byte order functions */
73#pragma clang diagnostic push
74#pragma clang diagnostic ignored "-Wunused-macros"
75#if defined(__linux)
76# include <endian.h>
77# include <byteswap.h>
78#elif defined(__APPLE__)
79# include <machine/endian.h> //Needed for __BYTE_ORDER
80# include <architecture/byte_order.h> //Needed for byte swap functions
81# define bswap_16(x) NXSwapShort(x)
82# define bswap_32(x) NXSwapInt(x)
83# define bswap_64(x) NXSwapLongLong(x)
84#elif defined(ESP8266)
85# define bswap_16(x) __builtin_bswap16(x)
86# define bswap_32(x) __builtin_bswap32(x)
87# define bswap_64(x) __builtin_bswap64(x)
88#else
89 //Manually define swap macros?
90#endif
91
92#if (!defined(__BYTE_ORDER) && !defined(__LITTLE_ENDIAN)) || (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN)
93# if !defined(htonll) && !defined(ntohll)
94# define htonll(x) bswap_64(x)
95# define ntohll(x) bswap_64(x)
96# endif
97# if !defined(htonl) && !defined(ntohl)
98# define htonl(x) bswap_32(x)
99# define ntohl(x) bswap_32(x)
100# endif
101# if !defined(htons) && !defined(ntohs)
102# define htons(x) bswap_16(x)
103# define ntohs(x) bswap_16(x)
104# endif
105#else
106# if !defined(htonll) && !defined(ntohll)
107# define htonll(x) (x)
108# define ntohll(x) (x)
109# endif
110# if !defined(htonl) && !defined(ntohl)
111# define htonl(x) (x)
112# define ntohl(x) (x)
113# endif
114# if !defined(htons) && !defined(ntohs)
115# define htons(x) (x)
116# define ntohs(x) (x)
117# endif
118#endif
119#pragma clang diagnostic pop
120
121
122
126const char* wb_dominant_frequencies_description(const struct wb_dominant_frequencies* self, char* descString, size_t bufferSize)
127{
128#pragma clang diagnostic push
129#pragma clang diagnostic ignored "-Wunused-variable"
130 size_t len = 0;
131 if (len >= bufferSize) {
132 return descString;
133 }
134 len = gu_strlcat(descString, "rms={", bufferSize);
135 if (len >= bufferSize) {
136 return descString;
137 }
138 char rms_buffer[RMS_LEVELS_DESC_BUFFER_SIZE];
139 char* rms_p = rms_buffer;
140 const char* rms_description = wb_r_m_s_levels_description(&self->rms, rms_p, RMS_LEVELS_DESC_BUFFER_SIZE);
141 len = gu_strlcat(descString, rms_p, bufferSize);
142 if (len >= bufferSize) {
143 return descString;
144 }
145 len = gu_strlcat(descString, "}", bufferSize);
146 if (len >= bufferSize) {
147 return descString;
148 }
149 len = gu_strlcat(descString, ", ", bufferSize);
150 if (len >= bufferSize) {
151 return descString;
152 }
153 len = gu_strlcat(descString, "frequencies={", bufferSize);
154 for (int frequencies_index = 0; frequencies_index < DOMINANT_FREQUENCIES_FREQUENCIES_ARRAY_SIZE; frequencies_index++) {
155 if (len >= bufferSize) {
156 return descString;
157 }
158 if (frequencies_index > 0) {
159 len = gu_strlcat(descString, ", ", bufferSize);
160 }
161 len = gu_strlcat(descString, "{", bufferSize);
162 if (len >= bufferSize) {
163 return descString;
164 }
165 char frequencies_1_buffer[MICROPHONE_FREQUENCIES_DESC_BUFFER_SIZE];
166 char* frequencies_1_p = frequencies_1_buffer;
167 const char* frequencies_1_description = wb_microphone_frequencies_description(&self->frequencies[frequencies_index], frequencies_1_p, MICROPHONE_FREQUENCIES_DESC_BUFFER_SIZE);
168 len = gu_strlcat(descString, frequencies_1_p, bufferSize);
169 if (len >= bufferSize) {
170 return descString;
171 }
172 len = gu_strlcat(descString, "}", bufferSize);
173 }
174 if (len >= bufferSize) {
175 return descString;
176 }
177 len = gu_strlcat(descString, "}", bufferSize);
178 return descString;
179#pragma clang diagnostic pop
180}
181
185const char* wb_dominant_frequencies_to_string(const struct wb_dominant_frequencies* self, char* toString, size_t bufferSize)
186{
187#pragma clang diagnostic push
188#pragma clang diagnostic ignored "-Wunused-variable"
189 size_t len = 0;
190 if (len >= bufferSize) {
191 return toString;
192 }
193 len = gu_strlcat(toString, "{", bufferSize);
194 if (len >= bufferSize) {
195 return toString;
196 }
197 char rms_buffer[RMS_LEVELS_TO_STRING_BUFFER_SIZE];
198 char* rms_p = rms_buffer;
199 const char* rms_to_string = wb_r_m_s_levels_to_string(&self->rms, rms_p, RMS_LEVELS_TO_STRING_BUFFER_SIZE);
200 len = gu_strlcat(toString, rms_p, bufferSize);
201 if (len >= bufferSize) {
202 return toString;
203 }
204 len = gu_strlcat(toString, "}", bufferSize);
205 if (len >= bufferSize) {
206 return toString;
207 }
208 len = gu_strlcat(toString, ", ", bufferSize);
209 if (len >= bufferSize) {
210 return toString;
211 }
212 len = gu_strlcat(toString, "{", bufferSize);
213 for (int frequencies_index = 0; frequencies_index < DOMINANT_FREQUENCIES_FREQUENCIES_ARRAY_SIZE; frequencies_index++) {
214 if (len >= bufferSize) {
215 return toString;
216 }
217 if (frequencies_index > 0) {
218 len = gu_strlcat(toString, ", ", bufferSize);
219 }
220 len = gu_strlcat(toString, "{", bufferSize);
221 if (len >= bufferSize) {
222 return toString;
223 }
224 char frequencies_1_buffer[MICROPHONE_FREQUENCIES_TO_STRING_BUFFER_SIZE];
225 char* frequencies_1_p = frequencies_1_buffer;
226 const char* frequencies_1_to_string = wb_microphone_frequencies_to_string(&self->frequencies[frequencies_index], frequencies_1_p, MICROPHONE_FREQUENCIES_TO_STRING_BUFFER_SIZE);
227 len = gu_strlcat(toString, frequencies_1_p, bufferSize);
228 if (len >= bufferSize) {
229 return toString;
230 }
231 len = gu_strlcat(toString, "}", bufferSize);
232 }
233 if (len >= bufferSize) {
234 return toString;
235 }
236 len = gu_strlcat(toString, "}", bufferSize);
237 return toString;
238#pragma clang diagnostic pop
239}
240
245{
246 size_t temp_length = strlen(str);
247 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
248 if (length < 1 || length > DOMINANT_FREQUENCIES_DESC_BUFFER_SIZE) {
249 return self;
250 }
251 char var_str_buffer[DOMINANT_FREQUENCIES_DESC_BUFFER_SIZE + 1];
252 char* var_str = &var_str_buffer[0];
253 char key_buffer[12];
254 char* key = &key_buffer[0];
255 int bracecount = 0;
256 int lastBrace = -1;
257 int startVar = 0;
258 int index = 0;
259 int startKey = 0;
260 int endKey = -1;
261 int varIndex = 0;
262 if (index == 0 && str[0] == '{') {
263 index = 1;
264 }
265 startVar = index;
266 startKey = startVar;
267 do {
268 for (int i = index; i < length; i++) {
269 index = i + 1;
270 if (bracecount == 0 && str[i] == '=') {
271 endKey = i - 1;
272 startVar = index;
273 continue;
274 }
275 if (bracecount == 0 && isspace(str[i])) {
276 startVar = index;
277 if (endKey == -1) {
278 startKey = index;
279 }
280 continue;
281 }
282 if (bracecount == 0 && str[i] == ',') {
283 index = i - 1;
284 break;
285 }
286 if (str[i] == '{') {
287 bracecount++;
288 if (bracecount == 1) {
289 lastBrace = i;
290 }
291 continue;
292 }
293 if (str[i] == '}') {
294 bracecount--;
295 if (bracecount < 0) {
296 index = i - 1;
297 break;
298 }
299 }
300 if (i == length - 1) {
301 index = i;
302 }
303 }
304 if (endKey >= startKey && endKey - startKey < length) {
305 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
306 key[(endKey - startKey) + 1] = 0;
307 } else {
308 key[0] = 0;
309 }
310 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
311 var_str[(index - startVar) + 1] = 0;
312 bracecount = 0;
313 index += 2;
314 startVar = index;
315 startKey = startVar;
316 endKey = -1;
317 if (strlen(key) > 0) {
318 if (0 == strcmp("rms", key)) {
319 varIndex = 0;
320 } else if (0 == strcmp("frequencies", key)) {
321 varIndex = 1;
322 } else {
323 varIndex = -1;
324 }
325 }
326 switch (varIndex) {
327 case -1: { break; }
328 case 0:
329 {
330 wb_r_m_s_levels_from_string(&self->rms, var_str);
331 break;
332 }
333 case 1:
334 {
335 int restartIndex = index;
336 index = lastBrace + 1;
337 startVar = index;
338 startKey = startVar;
339 endKey = -1;
340 bracecount = 0;
341 for (int frequencies_0_index = 0; frequencies_0_index < DOMINANT_FREQUENCIES_FREQUENCIES_ARRAY_SIZE; frequencies_0_index++) {
342 for (int i = index; i < length; i++) {
343 index = i + 1;
344 if (bracecount == 0 && str[i] == '=') {
345 endKey = i - 1;
346 startVar = index;
347 continue;
348 }
349 if (bracecount == 0 && isspace(str[i])) {
350 startVar = index;
351 if (endKey == -1) {
352 startKey = index;
353 }
354 continue;
355 }
356 if (bracecount == 0 && str[i] == ',') {
357 index = i - 1;
358 break;
359 }
360 if (str[i] == '{') {
361 bracecount++;
362 continue;
363 }
364 if (str[i] == '}') {
365 bracecount--;
366 if (bracecount < 0) {
367 index = i - 1;
368 break;
369 }
370 }
371 if (i == length - 1) {
372 index = i;
373 }
374 }
375 if (endKey >= startKey && endKey - startKey < length) {
376 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
377 key[(endKey - startKey) + 1] = 0;
378 } else {
379 key[0] = 0;
380 }
381 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
382 var_str[(index - startVar) + 1] = 0;
383 bracecount = 0;
384 index += 2;
385 startVar = index;
386 startKey = startVar;
387 endKey = -1;
388 struct wb_microphone_frequencies frequencies_0 = {};
389 wb_microphone_frequencies_from_string(&frequencies_0, var_str);
390 self->frequencies[frequencies_0_index] = frequencies_0;
391 }
392 index = restartIndex;
393 break;
394 }
395 }
396 if (varIndex >= 0) {
397 varIndex++;
398 }
399 } while(index < length);
400 return self;
401}
402
403/*#ifdef WHITEBOARD_SERIALISATION*/
404
409{
410 uint16_t bit_offset = 0;
411 //Class generator does not support array network compression.
412 //Copying into the buffer, uncompressed
413 do { //limit declaration scope
414 uint32_t len = DOMINANT_NUMFREQi;
415 uint32_t bytes = len * sizeof(struct wb_microphone_frequencies);
416 const char *buf = (const char *)&self->frequencies[0];
417 uint32_t c;
418 int8_t b;
419 for (c = 0; c < bytes; c++) {
420 for (b = 7; b >= 0; b--) {
421 do {
422 uint16_t byte = bit_offset / 8;
423 uint16_t bit = 7 - (bit_offset % 8);
424 unsigned long newbit = !!((buf[c] >> b) & 1U);
425 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
426 bit_offset = bit_offset + 1;
427 } while(false);
428 }
429 }
430 } while(false);
431 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
432 (void)self;
433 (void)dst;
434 return bit_offset;
435}
436
441{
442 uint16_t bit_offset = 0;
443 //Class generator does not support array network compression.
444 //Copying into the buffer, uncompressed
445 do { //limit declaration scope
446 uint32_t len = DOMINANT_NUMFREQi;
447 uint32_t bytes = len * sizeof(struct wb_microphone_frequencies);
448 char *buf = (char *)malloc(bytes);
449 uint32_t c;
450 int8_t b;
451 for (c = 0; c < bytes; c++) {
452 for (b = 7; b >= 0; b--) {
453 do {
454 uint16_t byte = bit_offset / 8;
455 uint16_t bit = 7 - (bit_offset % 8);
456 char dataByte = src[byte];
457 unsigned char bitValue = (dataByte >> bit) & 1U;
458 buf[c] ^= (-bitValue ^ buf[c]) & (1UL << b);
459 bit_offset = bit_offset + 1;
460 } while(false);
461 }
462 }
463 memcpy(&dst->frequencies[0], &buf[0], bytes);
464 free(buf);
465 } while(false);
466 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
467 (void)src;
468 (void)dst;
469 return bit_offset;
470}
471
472/*#endif // WHITEBOARD_SERIALISATION*/
Frequency quadruple for rear and front stereo channels.
struct wb_microphone_frequencies frequencies[static_cast< int >(((GU_SIMPLE_WHITEBOARD_BUFSIZE - sizeof(struct wb_r_m_s_levels))/sizeof(struct wb_microphone_frequencies)))]
dominant frequencies
WHITEBOARD_POSTER_STRING_CONVERSION.
size_t wb_dominant_frequencies_from_network_serialised(const char *src, struct wb_dominant_frequencies *dst)
Convert from a compressed, serialised, network byte order byte stream.
const char * wb_dominant_frequencies_description(const struct wb_dominant_frequencies *self, char *descString, size_t bufferSize)
Convert to a description string.
const char * wb_dominant_frequencies_to_string(const struct wb_dominant_frequencies *self, char *toString, size_t bufferSize)
Convert to a string.
struct wb_dominant_frequencies * wb_dominant_frequencies_from_string(struct wb_dominant_frequencies *self, const char *str)
Convert from a string.
size_t wb_dominant_frequencies_to_network_serialised(const struct wb_dominant_frequencies *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
#define DOMINANT_NUMFREQi
#define DOMINANT_FREQUENCIES_FREQUENCIES_ARRAY_SIZE
WHITEBOARD_POSTER_STRING_CONVERSION.
#define DOMINANT_FREQUENCIES_DESC_BUFFER_SIZE
struct wb_microphone_frequencies * wb_microphone_frequencies_from_string(struct wb_microphone_frequencies *self, const char *str)
Convert from a string.
const char * wb_microphone_frequencies_to_string(const struct wb_microphone_frequencies *self, char *toString, size_t bufferSize)
Convert to a string.
const char * wb_microphone_frequencies_description(const struct wb_microphone_frequencies *self, char *descString, size_t bufferSize)
Convert to a description string.
#define MICROPHONE_FREQUENCIES_TO_STRING_BUFFER_SIZE
#define MICROPHONE_FREQUENCIES_DESC_BUFFER_SIZE
const char * wb_r_m_s_levels_to_string(const struct wb_r_m_s_levels *self, char *toString, size_t bufferSize)
Convert to a string.
struct wb_r_m_s_levels * wb_r_m_s_levels_from_string(struct wb_r_m_s_levels *self, const char *str)
Convert from a string.
const char * wb_r_m_s_levels_description(const struct wb_r_m_s_levels *self, char *descString, size_t bufferSize)
Convert to a description string.
#define RMS_LEVELS_DESC_BUFFER_SIZE
#define RMS_LEVELS_TO_STRING_BUFFER_SIZE