Distortion effect

apiel

Member
I have been trying to implement the nice distortion effect of ToneJs https://tonejs.github.io/docs/14.7.77/Distortion
I managed to kind of getting something similar but it is still not as nice. Maybe you have some idea to get even better sound?

Code:
// https://github.com/apiel/kick/blob/main/effect/AudioEffectDistortion.h

#ifndef AudioEffectDistortion_h_
#define AudioEffectDistortion_h_

#include <Arduino.h>
#include <Audio.h>
#include <arm_math.h>

#define WAVESHAPE_SIZE 4097

class AudioEffectDistortion : public AudioEffectWaveshaper {
   public:
    float amount = 50;

    AudioEffectDistortion(void) { distortion(50); };

    void update() {
        if (bypassed || amount == 0) {
            audio_block_t *block;
            block = AudioStream::receiveReadOnly();
            if (!block) return;
            AudioStream::transmit(block);
            AudioStream::release(block);
            return;
        }
        AudioEffectWaveshaper::update();
    }

    void distortion(float _amount) {
        amount = _amount;
        if (amount > 0) {
            float deg = PI / 180;
            for (u_int16_t i = 0; i < WAVESHAPE_SIZE; i++) {
                float x = (float)i * 2.0 / (float)WAVESHAPE_SIZE - 1.0;
                waveshapeData[i] =
                    (3.0 + amount) * x * 20.0 * deg / (PI + amount * abs(x));
            }
            shape(waveshapeData, WAVESHAPE_SIZE);
        }
    }

    void enable() { bypassed = false; }
    void bypass() { bypassed = true; }
    void toggle() { bypassed = !bypassed; }

   private:
    bool bypassed = false;
    float waveshapeData[WAVESHAPE_SIZE];
};

#undef WAVESHAPE_SIZE

#endif
 
Back
Top