Skip to content
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

Include synthio.Biquad methods within audiofilters.Filter #9754

Closed
wants to merge 4 commits into from

Conversation

relic-se
Copy link

This is a minor modification to the previously merged PR #9744.

This modification adds the following methods to the audiofilters.Filter class:

  • audiofilters.Filter.low_pass_filter
  • audiofilters.Filter.high_pass_filter
  • audiofilters.Filter.bass_pass_filter

These new methods help facilitate the generation of synthio.Biquad objects for the filter effect without requiring an instance of synthio.Synthesizer. It also ensures that the sample rate used for the filter calculations matches the effect.

Here is a modified version of the example provided in #9744 with this change in place:

import board
import audiobusio
import audiofilters
import audiocore
import digitalio
import adafruit_debouncer

Q = 2.0

audio = audiobusio.I2SOut(bit_clock=board.GP2, word_select=board.GP3, data=board.GP4)

wave_file = open("StreetChicken.wav", "rb")
wave = audiocore.WaveFile(wave_file)

effect = audiofilters.Filter(buffer_size=1024, channel_count=wave.channel_count, sample_rate=wave.sample_rate, mix=1.0)
effect.play(wave, loop=True)
audio.play(effect)

button_up_pin = digitalio.DigitalInOut(board.GP0)
button_up_pin.direction = digitalio.Direction.INPUT
button_up = adafruit_debouncer.Debouncer(button_up_pin)

button_down_pin = digitalio.DigitalInOut(board.GP1)
button_down_pin.direction = digitalio.Direction.INPUT
button_down = adafruit_debouncer.Debouncer(button_down_pin)

position = 1.0
def update_filter():
    frequency = (min(max(position, 0.0), 1.0) ** 2) * (wave.sample_rate / 2 - 25) + 25
    print("{}hz".format(frequency))
    effect.low_pass_filter(frequency, Q)
update_filter()

while True:
    button_up.update()
    if button_up.rose:
        position = min(position + 0.025, 1.0)
        update_filter()
        
    button_down.update()
    if button_down.rose:
        position = max(position - 0.025, 0.0)
        update_filter()

The only consideration I have is whether or not to automatically update the filter internally or return the synthio.Biquad object to set manually. This is how that implementation would look like:

effect.filter = effect.low_pass_filter(frequency, Q)

@relic-se relic-se marked this pull request as draft October 23, 2024 20:45
@relic-se
Copy link
Author

I've changed this PR to a draft, because I am currently contemplating the possibility of implementing multiple synthio.Biquad objects on a single audiofilters.Filter effect. Doing so would require that the synthio.Biquad objects generated by the newly added methods (ie: audiofilters.Filter.low_pass_filter) would not automatically update the audiofilters.Filter.filter property.

@relic-se
Copy link
Author

I've decided to have the new methods simply return the synthio.Biquad object rather than update the filter property, because that will work better for future updates to this class (support for multiple biquads).

I've also added a deprecation notice in correspondence with #9756. However, this does cause an error in the docs because that PR hasn't been merged yet and synthio.BlockBiquad doesn't exist in this branch.

I don't plan on doing any more work on this PR. This addition may be entirely unnecessary at this point, so it is alright by me to reject the PR.

@relic-se relic-se marked this pull request as ready for review October 29, 2024 13:34
@relic-se
Copy link
Author

PR no longer needed with merge of #9756

@relic-se relic-se closed this Nov 11, 2024
@relic-se relic-se deleted the audiofilters branch November 13, 2024 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant