From 03064ebd5bb7f4374f93a048ab4355c03a965f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr?= Date: Mon, 11 Nov 2024 19:59:38 +0300 Subject: [PATCH] added overdrive class --- .../HeaderFiles/AudioEffects/Overdrive.h | 56 +++++++++++++++++++ HephAudio/HephAudio.vcxitems | 2 + HephAudio/HephAudio.vcxitems.filters | 2 + .../SourceFiles/AudioEffects/Overdrive.cpp | 50 +++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 HephAudio/HeaderFiles/AudioEffects/Overdrive.h create mode 100644 HephAudio/SourceFiles/AudioEffects/Overdrive.cpp diff --git a/HephAudio/HeaderFiles/AudioEffects/Overdrive.h b/HephAudio/HeaderFiles/AudioEffects/Overdrive.h new file mode 100644 index 00000000..77426d05 --- /dev/null +++ b/HephAudio/HeaderFiles/AudioEffects/Overdrive.h @@ -0,0 +1,56 @@ +#pragma once +#include "HephAudioShared.h" +#include "AudioEffect.h" + +/** @file */ + +namespace HephAudio +{ + /** + * @brief applies overdrive distortion. + * + */ + class Overdrive : public AudioEffect + { + protected: + /** + * controls the amount of distortion. + * In the range of [0, 100] + * + */ + double drive; + + public: + /** @copydoc default_constructor */ + Overdrive(); + + /** + * @copydoc constructor + * + * @param drive @copydetails drive + * + */ + explicit Overdrive(double drive); + + /** @copydoc destructor */ + virtual ~Overdrive() = default; + + virtual std::string Name() const override; + + /** + * gets the drive. + * + */ + virtual double GetDrive() const; + + /** + * sets the drive. + * + * @param drive @copydetails drive + */ + virtual void SetDrive(double drive); + + protected: + virtual void ProcessST(const AudioBuffer& inputBuffer, AudioBuffer& outputBuffer, size_t startIndex, size_t frameCount) override; + }; +} \ No newline at end of file diff --git a/HephAudio/HephAudio.vcxitems b/HephAudio/HephAudio.vcxitems index 11b05bb3..f2296583 100644 --- a/HephAudio/HephAudio.vcxitems +++ b/HephAudio/HephAudio.vcxitems @@ -96,8 +96,10 @@ + + diff --git a/HephAudio/HephAudio.vcxitems.filters b/HephAudio/HephAudio.vcxitems.filters index faf61ed7..c999dab9 100644 --- a/HephAudio/HephAudio.vcxitems.filters +++ b/HephAudio/HephAudio.vcxitems.filters @@ -250,6 +250,7 @@ + @@ -416,5 +417,6 @@ + \ No newline at end of file diff --git a/HephAudio/SourceFiles/AudioEffects/Overdrive.cpp b/HephAudio/SourceFiles/AudioEffects/Overdrive.cpp new file mode 100644 index 00000000..26a53963 --- /dev/null +++ b/HephAudio/SourceFiles/AudioEffects/Overdrive.cpp @@ -0,0 +1,50 @@ +#include "AudioEffects/Overdrive.h" +#include "Exceptions/InvalidArgumentException.h" +#include + +using namespace Heph; + +namespace HephAudio +{ + Overdrive::Overdrive() : Overdrive(0) {} + + Overdrive::Overdrive(double drive) : AudioEffect() + { + this->SetDrive(drive); + } + + std::string Overdrive::Name() const + { + return "Overdrive"; + } + + double Overdrive::GetDrive() const + { + return this->drive - 1; + } + + void Overdrive::SetDrive(double drive) + { + if (drive < 0 || drive > 100) + { + HEPH_RAISE_AND_THROW_EXCEPTION(this, InvalidArgumentException(HEPH_FUNC, "drive must be in the range of [0, 100].")); + } + + this->drive = drive + 1; + } + + void Overdrive::ProcessST(const AudioBuffer& inputBuffer, AudioBuffer& outputBuffer, size_t startIndex, size_t frameCount) + { + const size_t endIndex = startIndex + frameCount; + const AudioFormatInfo& formatInfo = outputBuffer.FormatInfo(); + + for (size_t i = startIndex; i < endIndex; ++i) + { + for (size_t j = 0; j < formatInfo.channelLayout.count; ++j) + { + const double fltSample = tanh(this->drive * sin(HEPH_AUDIO_SAMPLE_TO_IEEE_FLT(outputBuffer[i][j]))); + outputBuffer[i][j] = HEPH_AUDIO_SAMPLE_FROM_IEEE_FLT(HEPH_MATH_MIN(HEPH_MATH_MAX(fltSample, -1.0), 1.0)); + } + } + } +} \ No newline at end of file