Skip to content

Commit

Permalink
added overdrive class
Browse files Browse the repository at this point in the history
  • Loading branch information
ozguronsoy committed Nov 11, 2024
1 parent 65f2951 commit 03064eb
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
56 changes: 56 additions & 0 deletions HephAudio/HeaderFiles/AudioEffects/Overdrive.h
Original file line number Diff line number Diff line change
@@ -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;
};
}
2 changes: 2 additions & 0 deletions HephAudio/HephAudio.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\Vibrato.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\Normalizer.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\RmsNormalizer.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\Overdrive.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\Overdrive.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\CubicDistortion.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\ArctanDistortion.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\HardClipDistortion.cpp" />
Expand Down
2 changes: 2 additions & 0 deletions HephAudio/HephAudio.vcxitems.filters
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\HardClipDistortion.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\ArctanDistortion.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\CubicDistortion.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\Overdrive.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioProcessor.cpp">
Expand Down Expand Up @@ -416,5 +417,6 @@
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\HardClipDistortion.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\ArctanDistortion.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\CubicDistortion.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\Overdrive.cpp" />
</ItemGroup>
</Project>
50 changes: 50 additions & 0 deletions HephAudio/SourceFiles/AudioEffects/Overdrive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "AudioEffects/Overdrive.h"
#include "Exceptions/InvalidArgumentException.h"
#include <cmath>

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));
}
}
}
}

0 comments on commit 03064eb

Please sign in to comment.