Skip to content

Commit

Permalink
Simplify audiofilters.DistortionMode.LOFI sample processing with bi…
Browse files Browse the repository at this point in the history
…t mask.
  • Loading branch information
relic-se committed Oct 31, 2024
1 parent 3a16daf commit 1008dd5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
2 changes: 1 addition & 1 deletion shared-bindings/audiofilters/Distortion.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static mp_obj_t audiofilters_distortion_make_new(const mp_obj_type_t *type, size
if (bits_per_sample != 8 && bits_per_sample != 16) {
mp_raise_ValueError(MP_ERROR_TEXT("bits_per_sample must be 8 or 16"));
}

audiofilters_distortion_mode mode = DISTORTION_MODE_CLIP;
if (args[ARG_mode].u_obj != MP_OBJ_NULL) {
mode = validate_distortion_mode(args[ARG_mode].u_obj, MP_QSTR_mode);
Expand Down
16 changes: 4 additions & 12 deletions shared-module/audiofilters/Distortion.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void common_hal_audiofilters_distortion_stop(audiofilters_distortion_obj_t *self
}

static mp_float_t db_to_linear(mp_float_t value) {
return expf(value * MICROPY_FLOAT_CONST(0.11512925464970228420089957273422));
return expf(value * MICROPY_FLOAT_CONST(0.11512925464970228420089957273422));
}

audioio_get_buffer_result_t audiofilters_distortion_get_buffer(audiofilters_distortion_obj_t *self, bool single_channel_output, uint8_t channel,
Expand Down Expand Up @@ -274,15 +274,7 @@ audioio_get_buffer_result_t audiofilters_distortion_get_buffer(audiofilters_dist
}
} else {

// Pre-calculate drive-based constants if needed by effect mode
mp_float_t word_mult = 0;
switch (self->mode) {
case DISTORTION_MODE_LOFI:
word_mult = powf(2.0, 2.0 + (1.0 - drive) * 14); // goes from 16 to 2 bits
break;
default:
break;
}
uint32_t word_mask = 0xFFFFFFFF ^ ((1 << (uint32_t)roundf(drive * 14.0)) - 1); // LOFI mode bit mask

for (uint32_t i = 0; i < n; i++) {
int32_t sample_word = 0;
Expand All @@ -305,7 +297,7 @@ audioio_get_buffer_result_t audiofilters_distortion_get_buffer(audiofilters_dist
word = MIN(MAX(word, -32767), 32768); // Hard clip
} break;
case DISTORTION_MODE_LOFI: {
word = floorf(word / 32768.0 * word_mult + 0.5) / word_mult * 32767.0;
word = word & word_mask;
} break;
case DISTORTION_MODE_OVERDRIVE: {
mp_float_t x = word / 32768.0 * 0.686306;
Expand All @@ -319,7 +311,7 @@ audioio_get_buffer_result_t audiofilters_distortion_get_buffer(audiofilters_dist
} break;
}
word = word * post_gain;

if (MP_LIKELY(self->bits_per_sample == 16)) {
word_buffer[i] = (sample_word * (1.0 - mix)) + (word * mix);
if (!self->samples_signed) {
Expand Down

0 comments on commit 1008dd5

Please sign in to comment.