30#define CHIRP_C2 1.0f / (expf(CHIRP_C1) - 1)
35 bool exponential_chirp,
bool fade_in)
40 chirp->length_s = length_s;
42 chirp->total_length_s = length_s + 2 / f0_hz;
44 chirp->total_length_s = length_s;
47 chirp->start_time_s = current_time_s;
48 chirp->exponential_chirp = exponential_chirp;
49 chirp->fade_in = fade_in;
51 chirp->current_frequency_hz = 0;
52 chirp->current_value = 0;
53 chirp->percentage_done = 0;
58 chirp->current_time_s = current_time_s;
59 chirp->start_time_s = current_time_s;
60 chirp->current_frequency_hz = 0;
61 chirp->current_value = 0;
62 chirp->percentage_done = 0;
67 float t = current_time_s -
chirp->start_time_s;
68 return (t >= 0) && (t <=
chirp->total_length_s);
74 chirp->current_value = 0.0f;
78 float t = current_time_s -
chirp->start_time_s;
79 chirp->current_time_s = current_time_s;
81 if (
chirp->total_length_s <= 0) {
82 chirp->total_length_s = 0.01;
84 chirp->percentage_done = t /
chirp->total_length_s;
86 if (
chirp->fade_in && t < 2 / chirp->f0_hz) {
91 return chirp->current_value;
96 t -= 2 /
chirp->f0_hz;
99 if (
chirp->exponential_chirp) {
105 float theta = 2 * M_PI * (
chirp->f0_hz * t
108 chirp->current_value = sinf(theta);
112 chirp->current_frequency_hz = k * t;
113 chirp->current_value = sinf(2 * M_PI * t * (
chirp->f0_hz +
chirp->current_frequency_hz / 2));
116 return chirp->current_value;
bool chirp_is_running(struct chirp_t *chirp, float current_time_s)
Return if the current_time is within the chirp manoeuvre.
void chirp_init(struct chirp_t *chirp, float f0_hz, float f1_hz, float length_s, float current_time_s, bool exponential_chirp, bool fade_in)
Allocate and initialize a new chirp struct.
float chirp_update(struct chirp_t *chirp, float current_time_s)
Calculate the value at current_time_s and update the struct with current frequency and value.
void chirp_reset(struct chirp_t *chirp, float current_time_s)
Reset the time of the chirp.
Initialize with chirp_init.
static struct chirp_t chirp