Plaquette
 
Loading...
Searching...
No Matches
pq_wave_utils.h
1/*
2 * pq_wave_utils.h
3 *
4 * Utility functions for waves.
5 *
6 * (c) 2025 Sofian Audry :: info(@)sofianaudry(.)com
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21#ifndef PQ_WAVE_UTILS_H_
22#define PQ_WAVE_UTILS_H_
23
24#include "pq_phase_utils.h"
25#include "pq_fixed32_trig.h"
26
27namespace pq {
28
29inline q0_32u_t squareWave(const q0_32u_t& t, const q0_32u_t& skew)
30{
31 return (t <= skew) ? FIXED_32_MAX : 0;
32}
33
34inline q0_32u_t triangleWave(const q0_32u_t& t, const q0_32u_t& skew)
35{
36 return (t <= skew) ?
37 fixed32Divide(t, skew) :
38 fixed32Divide(FIXED_32_MAX - t, FIXED_32_MAX - skew);
39}
40
41inline q0_32u_t sineWave(const q0_32u_t& t, const q0_32u_t& skew)
42{
43#if defined(PQ_ARCH_32BITS)
44 // Phasse time remapped and rescaled to 16 bits for use with trigonometric library.
45 q0_32u_t phase32;
46
47 // Special case: skew == 0.5 (default and most common). More efficient.
48 if (skew == HALF_FIXED_32_MAX)
49 {
50 phase32 = t;
51 }
52 // Rising part of sine wave.
53 else if (t < skew)
54 {
55 phase32 = fixed32Divide(t, skew) / 2;
56 }
57 // Falling part of sine wave.
58 else
59 {
60 phase32 = fixed32Divide(t - skew, FIXED_32_MAX - skew) / 2 + HALF_FIXED_32_MAX;
61 }
62
63 return static_cast<uint32_t>(HALF_FIXED_32_MAX - cos32(phase32));
64#else
65 // Phasse time remapped and rescaled to 16 bits for use with trigonometric library.
66 uint16_t phase16;
67
68 // Special case: skew == 0.5 (default and most common). More efficient.
69 if (skew == HALF_FIXED_32_MAX)
70 {
71 phase16 = static_cast<uint16_t>(t >> 16);
72 }
73 // Rising part of sine wave.
74 else if (t < skew)
75 {
76 phase16 = static_cast<uint16_t>((static_cast<uint64_t>(t) << 15) / skew);
77 }
78 // Falling part of sine wave.
79 else if (t > skew)
80 {
81 phase16 = static_cast<uint16_t>((static_cast<uint64_t>(t - skew) << 15) / (FIXED_32_MAX - skew)) + HALF_FIXED_16_MAX;
82 }
83 // Peak of sine wave.
84 else
85 { // t == _skew32
86 phase16 = 32768;
87 }
88
89 // Convert to [0, 1] with wave shape similar to triangle wave.
90 return static_cast<uint32_t>(static_cast<uint16_t>(HALF_FIXED_16_MAX-1) - cos16(phase16)) << 16;
91#endif
92}
93
94}
95
96#endif