forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
audiofilters: Add Distortion effect and implement LFO ticking #9776
Open
relic-se
wants to merge
30
commits into
adafruit:main
Choose a base branch
from
relic-se:audiofilters_distortion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 19 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
16575fc
Added `Distortion` and `DistortionMode` classes to `audiofilters` mod…
relic-se 46ebae1
Remove separate DistortionMode code files.
relic-se 3a16daf
Remove `audiofilters.DistortionMode.ATAN`
relic-se 1008dd5
Simplify `audiofilters.DistortionMode.LOFI` sample processing with bi…
relic-se 37b6b70
Fix error with null sample handling in `audiofilters.Distortion`.
relic-se 064c3f3
Merge branch 'adafruit:main' into audiofilters_distortion
relic-se a7060f0
Merge branch 'adafruit:main' into audiofilters_distortion
relic-se 6481b4e
Merge branch 'adafruit:main' into audiofilters_distortion
relic-se 31c9095
Implement `synthio_block_slot_get_limited`.
relic-se 155f197
Convert default float values from null checks to MP_ROM_INT.
relic-se 89f2ae1
Remove unnecessary kwarg setters.
relic-se 5c981f0
Use `MICROPY_FLOAT_CONST` and `MICROPY_FLOAT_C_FUN` within floating p…
relic-se 222ce2c
Apply similar updates to audiofilters.Filter and audiodelays.Echo: MI…
relic-se 0410d22
Added `soft_clip` property to toggle between hard clipping (default) …
relic-se 57022f9
Implemented soft clipping and continued optimization of distortion al…
relic-se 48ca21d
Add Distortion to unix port and make type conversions explicit.
relic-se 4257c62
Variable number of samples within `shared_bindings_synthio_lfo_tick`.
relic-se 0e64e1c
Implement block ticking within audio effects.
relic-se 5fbbeed
Call `shared_bindings_synthio_lfo_tick` on audioeffects in `SYNTHIO_M…
relic-se e18e5b2
Merge branch 'adafruit:main' into audiofilters_distortion
relic-se cef94d7
Remove unnecessary deinit check.
relic-se b796f0d
Only calculate lofi bit mask when necessary.
relic-se 84f8e31
Update `shared_bindings_synthio_lfo_tick` within `audiomixer` to use …
relic-se d84cdbc
Update Distortion class docstring.
relic-se 8c40c56
Move `shared_bindings_synthio_lfo_tick` to avoid error if using unsig…
relic-se 05db339
Remove truncation of `delay_ms` within buffer processing loop.
relic-se ec5b1e8
Add `mix_down_sample` to echo output.
relic-se 48f272e
Fix build errors.
relic-se 99b4fae
Remove unnecessary copies of `mix_down_sample`.
relic-se e049337
Allow variable mix down sample scale.
relic-se File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// This file is part of the CircuitPython project: https://circuitpython.org | ||
// | ||
// SPDX-FileCopyrightText: Copyright (c) 2024 Cooper Dalrymple | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#pragma once | ||
|
||
#include "shared-module/audiofilters/Distortion.h" | ||
|
||
extern const mp_obj_type_t audiofilters_distortion_type; | ||
extern const mp_obj_type_t audiofilters_distortion_mode_type; | ||
|
||
void common_hal_audiofilters_distortion_construct(audiofilters_distortion_obj_t *self, | ||
mp_obj_t drive, mp_obj_t pre_gain, mp_obj_t post_gain, | ||
audiofilters_distortion_mode mode, bool soft_clip, mp_obj_t mix, | ||
uint32_t buffer_size, uint8_t bits_per_sample, bool samples_signed, | ||
uint8_t channel_count, uint32_t sample_rate); | ||
|
||
void common_hal_audiofilters_distortion_deinit(audiofilters_distortion_obj_t *self); | ||
bool common_hal_audiofilters_distortion_deinited(audiofilters_distortion_obj_t *self); | ||
|
||
uint32_t common_hal_audiofilters_distortion_get_sample_rate(audiofilters_distortion_obj_t *self); | ||
uint8_t common_hal_audiofilters_distortion_get_channel_count(audiofilters_distortion_obj_t *self); | ||
uint8_t common_hal_audiofilters_distortion_get_bits_per_sample(audiofilters_distortion_obj_t *self); | ||
|
||
mp_obj_t common_hal_audiofilters_distortion_get_drive(audiofilters_distortion_obj_t *self); | ||
void common_hal_audiofilters_distortion_set_drive(audiofilters_distortion_obj_t *self, mp_obj_t arg); | ||
|
||
mp_obj_t common_hal_audiofilters_distortion_get_pre_gain(audiofilters_distortion_obj_t *self); | ||
void common_hal_audiofilters_distortion_set_pre_gain(audiofilters_distortion_obj_t *self, mp_obj_t arg); | ||
|
||
mp_obj_t common_hal_audiofilters_distortion_get_post_gain(audiofilters_distortion_obj_t *self); | ||
void common_hal_audiofilters_distortion_set_post_gain(audiofilters_distortion_obj_t *self, mp_obj_t arg); | ||
|
||
audiofilters_distortion_mode common_hal_audiofilters_distortion_get_mode(audiofilters_distortion_obj_t *self); | ||
void common_hal_audiofilters_distortion_set_mode(audiofilters_distortion_obj_t *self, audiofilters_distortion_mode mode); | ||
|
||
bool common_hal_audiofilters_distortion_get_soft_clip(audiofilters_distortion_obj_t *self); | ||
void common_hal_audiofilters_distortion_set_soft_clip(audiofilters_distortion_obj_t *self, bool soft_clip); | ||
|
||
mp_obj_t common_hal_audiofilters_distortion_get_mix(audiofilters_distortion_obj_t *self); | ||
void common_hal_audiofilters_distortion_set_mix(audiofilters_distortion_obj_t *self, mp_obj_t arg); | ||
|
||
bool common_hal_audiofilters_distortion_get_playing(audiofilters_distortion_obj_t *self); | ||
void common_hal_audiofilters_distortion_play(audiofilters_distortion_obj_t *self, mp_obj_t sample, bool loop); | ||
void common_hal_audiofilters_distortion_stop(audiofilters_distortion_obj_t *self); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may want to check that delay >= 0. Never did in the original code (my bad) but a LFO could go negative and weird things happen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Casting it to an unsigned integer does force it to be >= 0, but a negative result might cause it to wrap around. If it's 0, I believe it will do a minimum of 1 sample of delay. I'll look into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe it should be
synthio_block_slot_get_limited(1, some_calculated_maximum)
then?Technically under the C99 standard, conversion from a negative floating point value to an unsigned integer value is undefined behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've taken a different approach here. Storing this value and
current_delay_ms
asuint32_t
was an arbitrary choice, so I've instead reverted both back to floats. I've added in another pre-calculated variable for the length of one audio frame (sample) in milliseconds, and I'm checking against that determine if we need to recalculate delay buffer parameters. The minimum value of the delay is handled withinrecalculate_delay
. Before, delays could only be manipulated by an LFO in millisecond intervals because of this, whereas now they can be controlled with sample-level precision by an LFO. This could be useful for chorus/phaser type effects.As for
some_calculated_maximum
, it's difficult to sum that up because that maximum value depends on thefreq_shift
property. If using the standard delay, it is limited tomax_effect_ms
, but withfreq_shift
, I believe it ismax_effect_ms*256
because of the 8 sub-bits in the buffer pointer. You lose a lot of audio quality when going that long, though.I'm tempted to move this logic into
recalculate_delay
if it needs more refinement.