Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
functions.h
1/*
2 * Copyright © 2014-2023 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include "const_functions.h"
21#include "definitions_cxx.hpp"
22#include "fatfs/ff.h"
23#include "gui/colour/colour.h" // IWYU pragma: export todo: this probably shouldn't be exported from here
24#include "util/cfunctions.h" // IWYU pragma: export - minimal set of functions which need c linkage
25#include "util/d_string.h"
26#include "util/db_shift.h"
27#include "util/fast_pythag.h"
28#include "util/fixedpoint.h"
29#include "util/lookuptables/lookuptables.h"
30#include "util/name_compare.h" // IWYU pragma: export - strcmpspecial and friends used to live here
31#include "util/waves.h"
32#include <bit>
33#include <cstdint>
34#include <cstring>
35#include <string>
36
37class UI;
38
39extern UI* getCurrentUI();
40
41extern const uint8_t modButtonX[];
42extern const uint8_t modButtonY[];
43
44extern uint8_t subModeToReturnTo;
45
46extern int32_t paramRanges[];
47extern int32_t paramNeutralValues[];
48
49void functionsInit();
50
51char const* getThingName(OutputType outputType);
52char const* getOutputTypeName(OutputType outputType, int32_t channel);
53
54// bits must be *less* than 32! I.e. 31 or less
55[[gnu::always_inline]] inline int32_t signed_saturate_operand_unknown(int32_t val, int32_t bits) {
56
57 // Despite having this switch at the per-audio-sample level, it doesn't introduce any slowdown compared to just
58 // always saturating by the same amount!
59 switch (bits) {
60 case 31:
61 return signed_saturate<31>(val);
62 case 30:
63 return signed_saturate<30>(val);
64 case 29:
65 return signed_saturate<29>(val);
66 case 28:
67 return signed_saturate<28>(val);
68 case 27:
69 return signed_saturate<27>(val);
70 case 26:
71 return signed_saturate<26>(val);
72 case 25:
73 return signed_saturate<25>(val);
74 case 24:
75 return signed_saturate<24>(val);
76 case 23:
77 return signed_saturate<23>(val);
78 case 22:
79 return signed_saturate<22>(val);
80 case 21:
81 return signed_saturate<21>(val);
82 case 20:
83 return signed_saturate<20>(val);
84 case 19:
85 return signed_saturate<19>(val);
86 case 18:
87 return signed_saturate<18>(val);
88 case 17:
89 return signed_saturate<17>(val);
90 case 16:
91 return signed_saturate<16>(val);
92 case 15:
93 return signed_saturate<15>(val);
94 case 14:
95 return signed_saturate<14>(val);
96 case 13:
97 return signed_saturate<13>(val);
98 default:
99 return signed_saturate<12>(val);
100 }
101}
102
103template <uint8_t lshift>
104[[gnu::always_inline]] inline int32_t lshiftAndSaturate(int32_t val) {
105 return signed_saturate<32 - lshift>(val) << lshift;
106}
107
108// lshift must be greater than 0! Not 0
109[[gnu::always_inline]] inline int32_t lshiftAndSaturateUnknown(int32_t val, uint8_t lshift) {
110 return signed_saturate_operand_unknown(val, 32 - lshift) << lshift;
111}
112
113[[gnu::always_inline]] constexpr uint32_t charsToIntegerConstant(char a, char b, char c, char d) {
114 return (static_cast<uint32_t>(a)) | (static_cast<uint32_t>(b) << 8) | (static_cast<uint32_t>(c) << 16)
115 | (static_cast<uint32_t>(d) << 24);
116}
117
118[[gnu::always_inline]] constexpr uint16_t charsToIntegerConstant(char a, char b) {
119 return (static_cast<uint16_t>(a)) | (static_cast<uint16_t>(b) << 8);
120}
127[[gnu::always_inline]] constexpr void asterixToInt(char* str, int32_t i) {
128 while (*str != 0) {
129 // if double asterix (two digits)
130 if (*str == '*' && *(str + 1) == '*') {
131 intToString(i, str, 2);
132 break;
133 }
134 // if single asterix (single digit)
135 else if (*str == '*') {
136 *str = (char)('0' + i);
137 break;
138 }
139 str++;
140 }
141}
142
143void replace_char(char* str, const char* in_str, char find, char replace);
144
146[[gnu::always_inline]] constexpr void padStringTo(std::string& str, const size_t num) {
147 if (num > str.size()) {
148 str.insert(0, num - str.size(), ' ');
149 }
150}
151
152int32_t stringToInt(char const* string);
153int32_t stringToUIntOrError(char const* mem);
154int32_t memToUIntOrError(char const* mem, char const* const memEnd);
155void getInstrumentPresetFilename(char const* filePrefix, int16_t presetNumber, int8_t presetSubslotNumber,
156 char* fileName);
157char const* oscTypeToString(OscType osctype);
158OscType stringToOscType(char const* string);
159
160char const* lfoTypeToString(LFOType oscType);
161LFOType stringToLFOType(char const* string);
162
163char const* synthModeToString(SynthMode synthMode);
164SynthMode stringToSynthMode(char const* string);
165
166char const* polyphonyModeToString(PolyphonyMode synthMode);
167PolyphonyMode stringToPolyphonyMode(char const* string);
168
169char const* fxTypeToString(ModFXType fxType);
170ModFXType stringToFXType(char const* string);
171
172char const* modFXParamToString(ModFXParam fxType);
173ModFXParam stringToModFXParam(char const* string);
174
175char const* filterTypeToString(FilterType fxType);
176FilterType stringToFilterType(char const* string);
177
178ArpMode oldModeToArpMode(OldArpMode oldMode);
179ArpNoteMode oldModeToArpNoteMode(OldArpMode oldMode);
180ArpOctaveMode oldModeToArpOctaveMode(OldArpMode oldMode);
181
182char const* oldArpModeToString(OldArpMode mode);
183OldArpMode stringToOldArpMode(char const* string);
184
185char const* arpModeToString(ArpMode mode);
186ArpMode stringToArpMode(char const* string);
187
188char const* arpNoteModeToString(ArpNoteMode mode);
189ArpNoteMode stringToArpNoteMode(char const* string);
190
191char const* arpOctaveModeToString(ArpOctaveMode mode);
192ArpOctaveMode stringToArpOctaveMode(char const* string);
193
194char const* arpMpeModSourceToString(ArpMpeModSource modSource);
195ArpMpeModSource stringToArpMpeModSource(char const* string);
196
197char const* inputChannelToString(AudioInputChannel inputChannel);
198AudioInputChannel stringToInputChannel(char const* string);
199
200char const* sequenceDirectionModeToString(SequenceDirection sequenceDirectionMode);
201SequenceDirection stringToSequenceDirectionMode(char const* string);
202char const* launchStyleToString(LaunchStyle launchStyle);
203LaunchStyle stringToLaunchStyle(char const* string);
204
205char const* getInstrumentFolder(OutputType outputType);
206
207int32_t getExp(int32_t presetValue, int32_t adjustment);
208
209bool isAudioFilename(char const* filename);
210bool isAiffFilename(char const* filename);
211
212char const* getFileNameFromEndOfPath(char const* filePathChars);
213
214int32_t lookupReleaseRate(int32_t input);
215int32_t getParamFromUserValue(uint8_t p, int8_t userValue);
216int32_t getLookupIndexFromValue(int32_t value, const int32_t* table, int32_t maxIndex);
217int32_t instantTan(int32_t input);
218
219int32_t combineHitStrengths(int32_t strength1, int32_t strength2);
220
221int32_t cableToLinearParamShortcut(int32_t sourceValue);
222int32_t cableToExpParamShortcut(int32_t sourceValue);
223
224class Sound;
225struct StereoSample;
226int32_t getFinalParameterValueVolume(int32_t paramNeutralValue, int32_t patchedValue);
227int32_t getFinalParameterValueLinear(int32_t paramNeutralValue, int32_t patchedValue);
228int32_t getFinalParameterValueHybrid(int32_t paramNeutralValue, int32_t patchedValue);
229int32_t getFinalParameterValueExp(int32_t paramNeutralValue, int32_t patchedValue);
230int32_t getFinalParameterValueExpWithDumbEnvelopeHack(int32_t paramNeutralValue, int32_t patchedValue, int32_t p);
231
232char const* getSourceDisplayNameForOLED(PatchSource s);
233
234char const* sourceToString(PatchSource source);
235PatchSource stringToSource(char const* string);
236char const* sourceToStringShort(PatchSource source);
237
238int32_t quickLog(uint32_t input);
239
240int32_t interpolateTable(uint32_t input, int32_t numBitsInInput, const uint16_t* table, int32_t numBitsInTableSize = 8);
241uint32_t interpolateTableInverse(int32_t tableValueBig, int32_t numBitsInLookupOutput, const uint16_t* table,
242 int32_t numBitsInTableSize = 8);
243
244// input must not have any extra bits set than numBitsInInput specifies
245// Output of this function (unlike the regular 1d one) is only +- 1073741824
246[[gnu::always_inline]] inline int32_t interpolateTableSigned2d(uint32_t inputX, uint32_t inputY,
247 int32_t numBitsInInputX, int32_t numBitsInInputY,
248 const int16_t* table, int32_t numBitsInTableSizeX,
249 int32_t numBitsInTableSizeY) {
250
251 int32_t whichValue = inputY >> (numBitsInInputY - numBitsInTableSizeY);
252
253 int32_t tableSizeOneRow = (1 << numBitsInTableSizeX) + 1;
254
255 int32_t value1 =
256 interpolateTableSigned(inputX, numBitsInInputX, &table[whichValue * tableSizeOneRow], numBitsInTableSizeX);
257 int32_t value2 = interpolateTableSigned(inputX, numBitsInInputX, &table[(whichValue + 1) * tableSizeOneRow],
258 numBitsInTableSizeX);
259
260 int32_t lshiftAmount = 31 + numBitsInTableSizeY - numBitsInInputY;
261
262 uint32_t strength2;
263
264 if (lshiftAmount >= 0)
265 strength2 = (inputY << lshiftAmount) & 2147483647;
266 else
267 strength2 = (inputY >> (0 - lshiftAmount)) & 2147483647;
268
269 uint32_t strength1 = 2147483647 - strength2;
270 return multiply_32x32_rshift32(value1, strength1) + multiply_32x32_rshift32(value2, strength2);
271}
272
273template <unsigned saturationAmount>
274[[gnu::always_inline]] inline int32_t getTanH(int32_t input) {
275 uint32_t workingValue;
276
277 if (saturationAmount)
278 workingValue = (uint32_t)lshiftAndSaturate<saturationAmount>(input) + 2147483648u;
279 else
280 workingValue = (uint32_t)input + 2147483648u;
281
282 return interpolateTableSigned(workingValue, 32, tanHSmall, 8) >> (saturationAmount + 2);
283}
284
285[[gnu::always_inline]] inline int32_t getTanHUnknown(int32_t input, uint32_t saturationAmount) {
286 uint32_t workingValue;
287
288 if (saturationAmount)
289 workingValue = (uint32_t)lshiftAndSaturateUnknown(input, saturationAmount) + 2147483648u;
290 else
291 workingValue = (uint32_t)input + 2147483648u;
292
293 return interpolateTableSigned(workingValue, 32, tanHSmall, 8) >> (saturationAmount + 2);
294}
295
296[[gnu::always_inline]] inline int32_t getTanHAntialiased(int32_t input, uint32_t* lastWorkingValue,
297 uint32_t saturationAmount) {
298
299 uint32_t workingValue = (uint32_t)lshiftAndSaturateUnknown(input, saturationAmount) + 2147483648u;
300
301 int32_t toReturn = interpolateTableSigned2d(workingValue, *lastWorkingValue, 32, 32, &tanH2d[0][0], 7, 6)
302 >> (saturationAmount + 1);
303 *lastWorkingValue = workingValue;
304 return toReturn;
305}
306
307int32_t getDecay8(uint32_t input, uint8_t numBitsInInput);
308int32_t getDecay4(uint32_t input, uint8_t numBitsInInput);
309
310#define znew (z = 36969 * (z & 65535) + (z >> 16))
311#define wnew (w = 18000 * (w & 65535) + (w >> 16))
312#define MWC (int32_t)((znew << 16) + wnew)
313
314[[gnu::always_inline]] inline uint8_t getRandom255() {
315 return CONG >> 24;
316}
317
318[[gnu::always_inline]] inline int32_t getNoise() {
319 return CONG;
320}
321
323inline q31_t sampleTriangleDistribution() {
324 auto u1 = getNoise();
325 auto u2 = getNoise();
326 auto s = add_saturate(u1, u2);
327 return s;
328}
329
330void seedRandom();
331
332int32_t random(int32_t upperLimit);
333bool shouldDoPanning(int32_t panAmount, int32_t* amplitudeL, int32_t* amplitudeR);
334
335uint32_t getOscInitialPhaseForZero(OscType waveType);
336int32_t doLanczos(int32_t* data, int32_t pos, uint32_t posWithinPos, int32_t memoryNumElements);
337int32_t doLanczosCircular(int32_t* data, int32_t pos, uint32_t posWithinPos, int32_t memoryNumElements);
338
339// intensity is out of 65536 now
340// occupancyMask is out of 64 now
341inline RGB drawSquare(const RGB& squareColour, int32_t intensity, const RGB& square, uint8_t* occupancyMask,
342 int32_t occupancyFromWhichColourCame) {
343
344 int32_t modifiedIntensity = intensity; //(intensity * occupancyFromWhichColourCame) >> 6; // Out of 65536
345
346 // Make new colour being drawn into this square marginalise the colour already in the square
347 int32_t colourRemainingAmount = 65536;
348
349 // We know how much colour we want to add to this square, so constrain any existing colour to the remaining "space"
350 // that it may still occupy
351 int32_t maxOldOccupancy = (65536 - modifiedIntensity) >> 10;
352
353 // If the square has more colour in it than it's allowed to retain, then plan to reduce it
354 if (*occupancyMask > maxOldOccupancy) {
355 colourRemainingAmount = (maxOldOccupancy << 16) / *occupancyMask; // out of 65536
356 }
357
358 // Add the new colour, reducing the old if that's what we're doing
359 int32_t newOccupancyMaskValue =
360 rshift_round(*occupancyMask * colourRemainingAmount, 16) + rshift_round(modifiedIntensity, 10);
361 *occupancyMask = std::min<int32_t>(64, newOccupancyMaskValue);
362
363 return RGB::blend2(square, squareColour, colourRemainingAmount, modifiedIntensity);
364}
365
366[[gnu::always_inline]] inline int32_t increaseMagnitude(int32_t number, int32_t magnitude) {
367 if (magnitude >= 0) {
368 return number << magnitude;
369 }
370 return number >> (-magnitude);
371}
372
373[[gnu::always_inline]] inline int32_t increaseMagnitudeAndSaturate(int32_t number, int32_t magnitude) {
374 if (magnitude > 0) {
375 return lshiftAndSaturateUnknown(number, magnitude);
376 }
377 return number >> (-magnitude);
378}
379
380int32_t howMuchMoreMagnitude(uint32_t to, uint32_t from);
383constexpr size_t kNoteCodeStringBufferSize = 13;
386void noteCodeToString(int32_t noteCode, char* buffer, int32_t* getLengthWithoutDot, bool appendOctaveNo,
387 bool useSharps);
388void concatenateLines(const char* lines[], size_t numLines, char* resultString);
389double ConvertFromIeeeExtended(unsigned char* bytes /* LCN */);
390int32_t divide_round_negative(int32_t dividend, int32_t divisor);
391
392[[gnu::always_inline]] inline uint32_t swapEndianness32(uint32_t input) {
393 int32_t out;
394 asm("rev %0, %1" : "=r"(out) : "r"(input));
395 return out;
396}
397
398[[gnu::always_inline]] inline uint32_t swapEndianness2x16(uint32_t input) {
399 int32_t out;
400 asm("rev16 %0, %1" : "=r"(out) : "r"(input));
401 return out;
402}
403
404[[gnu::always_inline]] inline int32_t getMagnitudeOld(uint32_t input) {
405 return 32 - clz(input);
406}
407
408[[gnu::always_inline]] inline int32_t getMagnitude(uint32_t input) {
409 return 31 - clz(input);
410}
411
412[[gnu::always_inline]] inline bool isPowerOfTwo(uint32_t input) {
413 return (input == 1 << getMagnitude(input));
414}
415
416int32_t getWhichKernel(int32_t phaseIncrement);
417
418int32_t memcasecmp(char const* first, char const* second, int32_t size);
419int32_t getHowManyCharsAreTheSame(char const* a, char const* b);
420void dimColour(uint8_t colour[3]);
421bool charCaseEqual(char firstChar, char secondChar);
422bool shouldAbortLoading();
423int32_t getNoteMagnitudeFfromNoteLength(uint32_t noteLength, int32_t tickMagnitude);
425void getNoteLengthNameFromMagnitude(StringBuf& buf, int32_t magnitude, char const* durrationSuffix = "-notes",
426 bool clarifyPerColumn = false);
427bool doesFilenameFitPrefixFormat(char const* fileName, char const* filePrefix, int32_t prefixLength);
428Error fresultToDelugeErrorCode(FRESULT result);
429namespace FatFS {
430enum class Error;
431}
432Error fatfsErrorToDelugeError(FatFS::Error result);
433
434[[gnu::always_inline]] inline void writeInt16(char** address, uint16_t number) {
435 *(uint16_t*)*address = number;
436 *address += 2;
437}
438
439[[gnu::always_inline]] inline void writeInt32(char** address, uint32_t number) {
440 *(uint32_t*)*address = number;
441 *address += 4;
442}
443
444extern char miscStringBuffer[];
445
446constexpr size_t kShortStringBufferSize = 64;
447extern char shortStringBuffer[];
448
450 float l;
451 float r;
452};
453
454float sigmoidLikeCurve(const float x, const float xMax, const float softening);
This class represents the colour format most used by the Deluge globally.
Definition rgb.h:14
static constexpr RGB blend2(RGB sourceA, RGB sourceB, uint32_t indexA, uint32_t indexB)
Generate a new colour made from blending two source colours with individual proportions.
Definition rgb.h:181
Definition sound.h:78
Definition d_stringbuf.h:16
Definition ui.h:92
Definition functions.h:449
Definition stereo_sample.h:25