FastLED 3.9.15
Loading...
Searching...
No Matches
beat.h
Go to the documentation of this file.
1#pragma once
2
5
6#include "fl/stl/int.h"
7#include "fl/stl/chrono.h" // fl::millis()
9#include "fl/math/qfx.h" // accum88
10#include "fl/math/trig8.h" // sin8, sin16
11#include "fl/math/scale8.h" // scale8, scale16
12#include "fl/stl/noexcept.h"
13
14namespace fl {
15
18
26LIB8STATIC u16 beat88(accum88 beats_per_minute_88, u32 timebase = 0) FL_NOEXCEPT {
27 // BPM is 'beats per minute', or 'beats per 60000ms'.
28 // To avoid using the (slower) division operator, we
29 // want to convert 'beats per 60000ms' to 'beats per 65536ms',
30 // and then use a simple, fast bit-shift to divide by 65536.
31 //
32 // The ratio 65536:60000 is 279.620266667:256; we'll call it 280:256.
33 // The conversion is accurate to about 0.05%, more or less,
34 // e.g. if you ask for "120 BPM", you'll get about "119.93".
35 return (((fl::millis()) - timebase) * beats_per_minute_88 * 280) >> 16;
36}
37
41LIB8STATIC u16 beat16(accum88 beats_per_minute, u32 timebase = 0) FL_NOEXCEPT {
42 // Convert simple 8-bit BPM's to full Q8.8 accum88's if needed
43 if (beats_per_minute < 256)
44 beats_per_minute <<= 8;
45 return beat88(beats_per_minute, timebase);
46}
47
51LIB8STATIC u8 beat8(accum88 beats_per_minute, u32 timebase = 0) FL_NOEXCEPT {
52 return beat16(beats_per_minute, timebase) >> 8;
53}
54
63LIB8STATIC u16 beatsin88(accum88 beats_per_minute_88, u16 lowest = 0,
64 u16 highest = 65535, u32 timebase = 0,
65 u16 phase_offset = 0) FL_NOEXCEPT {
66 u16 beat = beat88(beats_per_minute_88, timebase);
67 u16 beatsin = (sin16(beat + phase_offset) + 32768);
68 u16 rangewidth = highest - lowest;
69 u16 scaledbeat = scale16(beatsin, rangewidth);
70 u16 result = lowest + scaledbeat;
71 if (result > highest) {
72 result = highest;
73 }
74 return result;
75}
76
84LIB8STATIC u16 beatsin16(accum88 beats_per_minute, u16 lowest = 0,
85 u16 highest = 65535, u32 timebase = 0,
86 u16 phase_offset = 0) FL_NOEXCEPT {
87 u16 beat = beat16(beats_per_minute, timebase);
88 u16 beatsin = (sin16(beat + phase_offset) + 32768);
89 u16 rangewidth = highest - lowest;
90 u16 scaledbeat = scale16(beatsin, rangewidth);
91 u16 result = lowest + scaledbeat;
92 if (result > highest) {
93 result = highest;
94 }
95 return result;
96}
97
105LIB8STATIC u8 beatsin8(accum88 beats_per_minute, u8 lowest = 0,
106 u8 highest = 255, u32 timebase = 0,
107 u8 phase_offset = 0) FL_NOEXCEPT {
108 u8 beat = beat8(beats_per_minute, timebase);
109 u8 beatsin = sin8(beat + phase_offset);
110 u8 rangewidth = highest - lowest;
111 u8 scaledbeat = scale8(beatsin, rangewidth);
112 u8 result = lowest + scaledbeat;
113 if (result > highest) {
114 result = highest;
115 }
116 return result;
117}
118
120
121} // namespace fl
FastLED chrono implementation - duration types for time measurements.
Defines static inlining macros for lib8tion functions.
Legacy compatibility header for 8-bit scaling functions.
Legacy compatibility header for trigonometry functions.
LIB8STATIC u16 beat16(accum88 beats_per_minute, u32 timebase=0) FL_NOEXCEPT
Generates a 16-bit "sawtooth" wave at a given BPM.
Definition beat.h:41
LIB8STATIC u16 beatsin88(accum88 beats_per_minute_88, u16 lowest=0, u16 highest=65535, u32 timebase=0, u16 phase_offset=0) FL_NOEXCEPT
Generates a 16-bit sine wave at a given BPM that oscillates within a given range.
Definition beat.h:63
LIB8STATIC u8 beatsin8(accum88 beats_per_minute, u8 lowest=0, u8 highest=255, u32 timebase=0, u8 phase_offset=0) FL_NOEXCEPT
Generates an 8-bit sine wave at a given BPM that oscillates within a given range.
Definition beat.h:105
LIB8STATIC u8 beat8(accum88 beats_per_minute, u32 timebase=0) FL_NOEXCEPT
Generates an 8-bit "sawtooth" wave at a given BPM.
Definition beat.h:51
LIB8STATIC u16 beat88(accum88 beats_per_minute_88, u32 timebase=0) FL_NOEXCEPT
Generates a 16-bit "sawtooth" wave at a given BPM, with BPM specified in Q8.8 fixed-point format.
Definition beat.h:26
LIB8STATIC u16 beatsin16(accum88 beats_per_minute, u16 lowest=0, u16 highest=65535, u32 timebase=0, u16 phase_offset=0) FL_NOEXCEPT
Generates a 16-bit sine wave at a given BPM that oscillates within a given range.
Definition beat.h:84
#define LIB8STATIC
Define a LIB8TION member function as static inline with an "unused" attribute.
Definition lib8static.h:12
u16 accum88
ANSI: unsigned short _Accum. 8 bits int, 8 bits fraction.
Definition s16x16x4.h:182
unsigned char u8
Definition stdint.h:131
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT