Deluge Firmware 1.3.0
Build date: 2025.04.16
Loading...
Searching...
No Matches
waves.h
1#pragma once
2
3#include "util/lookuptables/lookuptables.h"
4
5#define CONG (jcong = 69069 * jcong + 1234567)
6
7extern uint32_t z, w, jcong;
8
9/* Basic waveform generation functions.
10 *
11 * Extracted from functions.h.
12 */
13
14// input must not have any extra bits set than numBitsInInput specifies
15[[gnu::always_inline]] inline int32_t interpolateTableSigned(uint32_t input, int32_t numBitsInInput,
16 const int16_t* table, int32_t numBitsInTableSize = 8) {
17 int32_t whichValue = input >> (numBitsInInput - numBitsInTableSize);
18 int32_t rshiftAmount = numBitsInInput - 16 - numBitsInTableSize;
19 uint32_t rshifted;
20 if (rshiftAmount >= 0)
21 rshifted = input >> rshiftAmount;
22 else
23 rshifted = input << (-rshiftAmount);
24 int32_t strength2 = rshifted & 65535;
25 int32_t strength1 = 65536 - strength2;
26 return (int32_t)table[whichValue] * strength1 + (int32_t)table[whichValue + 1] * strength2;
27}
28
29[[gnu::always_inline]] inline int32_t getSine(uint32_t phase, uint8_t numBitsInInput = 32) {
30 return interpolateTableSigned(phase, numBitsInInput, sineWaveSmall, 8);
31}
32
33[[gnu::always_inline]] inline int32_t getSquare(uint32_t phase, uint32_t phaseWidth = 2147483648u) {
34 return ((phase >= phaseWidth) ? (-2147483648) : 2147483647);
35}
36
37[[gnu::always_inline]] inline int32_t getSquareSmall(uint32_t phase, uint32_t phaseWidth = 2147483648u) {
38 return ((phase >= phaseWidth) ? (-1073741824) : 1073741823);
39}
40
41/* Should be faster, but isn't...
42 * inline int32_t getTriangleSmall(int32_t phase) {
43 int32_t multiplier = (phase >> 31) | 1;
44 phase *= multiplier;
45 */
46
47[[gnu::always_inline]] inline int32_t getTriangleSmall(uint32_t phase) {
48 if (phase >= 2147483648u)
49 phase = -phase;
50 return phase - 1073741824;
51}
52
53[[gnu::always_inline]] inline int32_t getTriangle(uint32_t phase) {
54 int32_t slope = 2;
55 int32_t offset = 0x80000000u;
56 if (phase >= 0x80000000u) {
57 slope = -2;
58 offset = 0x80000000u - 1;
59 }
60 return slope * phase + offset;
61}