Skip to content

Commit

Permalink
added cubic distortion class
Browse files Browse the repository at this point in the history
  • Loading branch information
ozguronsoy committed Nov 11, 2024
1 parent c1b6795 commit 65f2951
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 11 deletions.
2 changes: 1 addition & 1 deletion HephAudio/HeaderFiles/AudioEffects/ArctanDistortion.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace HephAudio
protected:
/**
* controls the amount of distortion.
* Must be non-negative where 0 means no distortion is applied.
* Must be non-negative.
*
*/
double factor;
Expand Down
56 changes: 56 additions & 0 deletions HephAudio/HeaderFiles/AudioEffects/CubicDistortion.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 cubic distortion.
*
*/
class CubicDistortion : public AudioEffect
{
protected:
/**
* controls the amount of distortion.
* In the range of [0, 10].
*
*/
double factor;

public:
/** @copydoc default_constructor */
CubicDistortion();

/**
* @copydoc constructor
*
* @param factor @copydetails factor
*
*/
explicit CubicDistortion(double factor);

/** @copydoc destructor */
virtual ~CubicDistortion() = default;

virtual std::string Name() const override;

/**
* gets the factor.
*
*/
virtual double GetFactor() const;

/**
* sets the facotr.
*
* @param factor @copydetails factor
*/
virtual void SetFactor(double factor);

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 @@ -14,6 +14,7 @@
<ProjectCapability Include="SourceItemsFromImports" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\CubicDistortion.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\ArctanDistortion.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\HardClipDistortion.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\Chorus.h" />
Expand Down Expand Up @@ -97,6 +98,7 @@
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\RmsNormalizer.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\CubicDistortion.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\ArctanDistortion.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\HardClipDistortion.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\RmsNormalizer.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 @@ -249,6 +249,7 @@
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\RmsNormalizer.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\HardClipDistortion.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\ArctanDistortion.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\CubicDistortion.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioProcessor.cpp">
Expand Down Expand Up @@ -414,5 +415,6 @@
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\RmsNormalizer.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\HardClipDistortion.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\ArctanDistortion.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\CubicDistortion.cpp" />
</ItemGroup>
</Project>
19 changes: 9 additions & 10 deletions HephAudio/SourceFiles/AudioEffects/ArctanDistortion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,17 @@ namespace HephAudio

void ArctanDistortion::ProcessST(const AudioBuffer& inputBuffer, AudioBuffer& outputBuffer, size_t startIndex, size_t frameCount)
{
if (this->factor > 1)
{
const size_t endIndex = startIndex + frameCount;
const AudioFormatInfo& formatInfo = outputBuffer.FormatInfo();
const size_t endIndex = startIndex + frameCount;
const AudioFormatInfo& formatInfo = outputBuffer.FormatInfo();

for (size_t i = startIndex; i < endIndex; ++i)
for (size_t i = startIndex; i < endIndex; ++i)
{
for (size_t j = 0; j < formatInfo.channelLayout.count; ++j)
{
for (size_t j = 0; j < formatInfo.channelLayout.count; ++j)
{
const double fltSample = HEPH_AUDIO_SAMPLE_TO_IEEE_FLT(outputBuffer[i][j]);
outputBuffer[i][j] = HEPH_AUDIO_SAMPLE_FROM_IEEE_FLT(atan(this->factor * fltSample) * (2.0 / HEPH_MATH_PI));
}
double fltSample = HEPH_AUDIO_SAMPLE_TO_IEEE_FLT(outputBuffer[i][j]);
fltSample = atan(this->factor * fltSample) * (2.0 / HEPH_MATH_PI);

outputBuffer[i][j] = HEPH_AUDIO_SAMPLE_FROM_IEEE_FLT(HEPH_MATH_MIN(HEPH_MATH_MAX(fltSample, -1.0), 1.0));
}
}
}
Expand Down
51 changes: 51 additions & 0 deletions HephAudio/SourceFiles/AudioEffects/CubicDistortion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "AudioEffects/CubicDistortion.h"
#include "Exceptions/InvalidArgumentException.h"

using namespace Heph;

namespace HephAudio
{
CubicDistortion::CubicDistortion() : CubicDistortion(1.0) {}

CubicDistortion::CubicDistortion(double factor) : AudioEffect()
{
this->SetFactor(factor);
}

std::string CubicDistortion::Name() const
{
return "Cubic Distortion";
}

double CubicDistortion::GetFactor() const
{
return this->factor - 1;
}

void CubicDistortion::SetFactor(double factor)
{
if (factor < 0 || factor > 10)
{
HEPH_RAISE_AND_THROW_EXCEPTION(this, InvalidArgumentException(HEPH_FUNC, "factor must be in the range of [0, 10]."));
}

this->factor = factor + 1;
}

void CubicDistortion::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)
{
double fltSample = this->factor * HEPH_AUDIO_SAMPLE_TO_IEEE_FLT(outputBuffer[i][j]);
fltSample -= fltSample * fltSample * fltSample / 3.0;

outputBuffer[i][j] = HEPH_AUDIO_SAMPLE_FROM_IEEE_FLT(HEPH_MATH_MIN(HEPH_MATH_MAX(fltSample, -1.0), 1.0));
}
}
}
}

0 comments on commit 65f2951

Please sign in to comment.