circuitpython/shared-module/synthio/LFO.c

142 lines
4.7 KiB
C
Raw Normal View History

2023-05-15 11:49:42 -04:00
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2023 Jeff Epler for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "shared-bindings/synthio/LFO.h"
#include "shared-module/synthio/LFO.h"
#include <math.h>
#define ONE (MICROPY_FLOAT_CONST(1.))
#define ZERO (MICROPY_FLOAT_CONST(0.))
#define ALMOST_ONE (MICROPY_FLOAT_CONST(32767.) / 32768)
mp_float_t common_hal_synthio_lfo_tick(mp_obj_t self_in) {
synthio_lfo_obj_t *lfo = MP_OBJ_TO_PTR(self_in);
2023-05-15 11:49:42 -04:00
mp_float_t rate = synthio_block_slot_get(&lfo->rate) * synthio_global_rate_scale;
2023-05-20 20:43:15 -04:00
mp_float_t phase_offset = synthio_block_slot_get(&lfo->phase_offset);
2023-05-15 11:49:42 -04:00
2023-05-20 20:43:15 -04:00
mp_float_t accum = lfo->accum + rate + phase_offset;
2023-05-15 11:49:42 -04:00
if (lfo->once) {
if (rate > 0) {
if (accum > ALMOST_ONE) {
accum = ALMOST_ONE;
2023-05-15 11:49:42 -04:00
}
} else if (rate < 0 && accum < ZERO) {
2023-05-20 20:43:15 -04:00
accum = ZERO;
2023-05-15 11:49:42 -04:00
}
2023-05-20 20:43:15 -04:00
} else {
accum = accum - MICROPY_FLOAT_C_FUN(floor)(accum);
2023-05-15 11:49:42 -04:00
}
lfo->accum = accum - phase_offset;
2023-05-15 11:49:42 -04:00
int len = lfo->waveform_bufinfo.len;
mp_float_t scaled_accum = accum * (len - lfo->once);
size_t idx = (size_t)MICROPY_FLOAT_C_FUN(floor)(scaled_accum);
2023-05-20 20:43:15 -04:00
assert(idx < lfo->waveform_bufinfo.len);
2023-05-15 11:49:42 -04:00
int16_t *waveform = lfo->waveform_bufinfo.buf;
mp_float_t value = waveform[idx];
if (lfo->interpolate) {
mp_float_t frac = scaled_accum - idx;
size_t idxp1 = idx + 1;
if (idxp1 == lfo->waveform_bufinfo.len) {
idxp1 = lfo->once ? idx : 0;
}
value = value * (1 - frac) + waveform[idxp1] * frac;
}
mp_float_t scale = synthio_block_slot_get(&lfo->scale);
mp_float_t offset = synthio_block_slot_get(&lfo->offset);
value = MICROPY_FLOAT_C_FUN(ldexp)(value, -15) * scale + offset;
2023-05-15 11:49:42 -04:00
return value;
}
mp_obj_t common_hal_synthio_lfo_get_waveform_obj(synthio_lfo_obj_t *self) {
return self->waveform_obj;
}
mp_obj_t common_hal_synthio_lfo_get_rate_obj(synthio_lfo_obj_t *self) {
return self->rate.obj;
}
void common_hal_synthio_lfo_set_rate_obj(synthio_lfo_obj_t *self, mp_obj_t arg) {
synthio_block_assign_slot(arg, &self->rate, MP_QSTR_rate);
2023-05-15 11:49:42 -04:00
}
mp_obj_t common_hal_synthio_lfo_get_scale_obj(synthio_lfo_obj_t *self) {
return self->scale.obj;
}
void common_hal_synthio_lfo_set_scale_obj(synthio_lfo_obj_t *self, mp_obj_t arg) {
synthio_block_assign_slot(arg, &self->scale, MP_QSTR_scale);
2023-05-15 11:49:42 -04:00
}
2023-05-20 20:43:15 -04:00
mp_obj_t common_hal_synthio_lfo_get_phase_offset_obj(synthio_lfo_obj_t *self) {
return self->phase_offset.obj;
}
void common_hal_synthio_lfo_set_phase_offset_obj(synthio_lfo_obj_t *self, mp_obj_t arg) {
synthio_block_assign_slot(arg, &self->phase_offset, MP_QSTR_phase_offset);
}
2023-05-15 11:49:42 -04:00
mp_obj_t common_hal_synthio_lfo_get_offset_obj(synthio_lfo_obj_t *self) {
return self->offset.obj;
}
void common_hal_synthio_lfo_set_offset_obj(synthio_lfo_obj_t *self, mp_obj_t arg) {
synthio_block_assign_slot(arg, &self->offset, MP_QSTR_offset);
2023-05-15 11:49:42 -04:00
}
bool common_hal_synthio_lfo_get_once(synthio_lfo_obj_t *self) {
return self->once;
}
void common_hal_synthio_lfo_set_once(synthio_lfo_obj_t *self, bool arg) {
self->once = arg;
}
bool common_hal_synthio_lfo_get_interpolate(synthio_lfo_obj_t *self) {
return self->interpolate;
}
void common_hal_synthio_lfo_set_interpolate(synthio_lfo_obj_t *self, bool arg) {
self->interpolate = arg;
}
2023-05-15 11:49:42 -04:00
mp_float_t common_hal_synthio_lfo_get_value(synthio_lfo_obj_t *self) {
return self->base.value;
2023-05-15 11:49:42 -04:00
}
mp_float_t common_hal_synthio_lfo_get_phase(synthio_lfo_obj_t *self) {
return self->accum;
}
void common_hal_synthio_lfo_retrigger(synthio_lfo_obj_t *self) {
self->accum = 0;
}