Skip to content

Commit

Permalink
added hard clipping distortion
Browse files Browse the repository at this point in the history
  • Loading branch information
ozguronsoy committed Nov 10, 2024
1 parent 62dd11b commit 088f483
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 3 deletions.
55 changes: 55 additions & 0 deletions HephAudio/HeaderFiles/AudioEffects/HardClipDistortion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once
#include "HephAudioShared.h"
#include "AudioEffect.h"

/** @file */

namespace HephAudio
{
/**
* @brief applies hard-clipping distortion.
*
*/
class HardClipDistortion : public AudioEffect
{
protected:
/**
* threshold value.
*
*/
heph_audio_sample_t clippingLevel;

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

/**
* @copydoc constructor
*
* @param clippingLevel threshold value in decibels.
*
*/
explicit HardClipDistortion(double clippingLevel);

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

virtual std::string Name() const override;

/**
* gets the clipping level in decibels.
*
*/
virtual double GetClippingLevel() const;

/**
* sets the clipping level.
*
* @param clippingLevel threshold value in decibels.
*/
virtual void SetClippingLevel(double clippingLevel);

protected:
virtual void ProcessST(const AudioBuffer& inputBuffer, AudioBuffer& outputBuffer, size_t startIndex, size_t frameCount) override;
};
}
2 changes: 1 addition & 1 deletion HephAudio/HeaderFiles/AudioEffects/PanningEffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ namespace HephAudio
virtual void SetFactor(double factor) = 0;

protected:
virtual void ProcessST(const AudioBuffer& inputBuffer, AudioBuffer& outputBuffer, size_t startIndex, size_t endIndex) override;
virtual void ProcessST(const AudioBuffer& inputBuffer, AudioBuffer& outputBuffer, size_t startIndex, size_t frameCount) override;
};
}
1 change: 1 addition & 0 deletions HephAudio/HeaderFiles/AudioEffects/SineLawPanning.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace HephAudio

/**
* @copydoc constructor
*
* @param factor determines how the channels' volume will be changed.
* Ranges between -1 and 1.<br>
* -1 means only the left channel will be audible and 1 means only the right channel will be audible.
Expand Down
3 changes: 1 addition & 2 deletions HephAudio/HeaderFiles/AudioEffects/Tremolo.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ namespace HephAudio
virtual ~Tremolo() = default;

virtual std::string Name() const override;

virtual void Process(AudioBuffer& buffer, size_t startIndex, size_t frameCount) override;

protected:
virtual void ProcessST(const AudioBuffer& inputBuffer, AudioBuffer& outputBuffer, size_t startIndex, size_t endIndex) override;
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\HardClipDistortion.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\Chorus.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\Flanger.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\DoubleBufferedAudioEffect.h" />
Expand Down Expand Up @@ -95,6 +96,7 @@
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\RmsNormalizer.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\HardClipDistortion.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\RmsNormalizer.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\Normalizer.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\Chorus.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 @@ -247,6 +247,7 @@
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\Chorus.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\Normalizer.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\RmsNormalizer.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\HardClipDistortion.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioProcessor.cpp">
Expand Down Expand Up @@ -410,5 +411,6 @@
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\Chorus.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\Normalizer.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\RmsNormalizer.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\HardClipDistortion.cpp" />
</ItemGroup>
</Project>
56 changes: 56 additions & 0 deletions HephAudio/SourceFiles/AudioEffects/HardClipDistortion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "AudioEffects/HardClipDistortion.h"
#include "Exceptions/InvalidArgumentException.h"

using namespace Heph;

namespace HephAudio
{
HardClipDistortion::HardClipDistortion() : HardClipDistortion(0) {}

HardClipDistortion::HardClipDistortion(double clippingLevel) : AudioEffect()
{
this->SetClippingLevel(clippingLevel);
}

std::string HardClipDistortion::Name() const
{
return "Hard Clipping Distortion";
}

double HardClipDistortion::GetClippingLevel() const
{
return HephAudio::GainToDecibel(this->clippingLevel / (double)HEPH_AUDIO_SAMPLE_MAX);
}

void HardClipDistortion::SetClippingLevel(double clippingLevel)
{
if (clippingLevel > 0)
{
HEPH_RAISE_AND_THROW_EXCEPTION(this, InvalidArgumentException(HEPH_FUNC, "clippingLevel must be negative or zero."));
}

this->clippingLevel = HephAudio::DecibelToGain(clippingLevel) * HEPH_AUDIO_SAMPLE_MAX;
}

void HardClipDistortion::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)
{
heph_audio_sample_t& sample = outputBuffer[i][j];
if (sample > this->clippingLevel)
{
sample = this->clippingLevel;
}
else if (sample < -this->clippingLevel)
{
sample = -this->clippingLevel;
}
}
}
}
}

0 comments on commit 088f483

Please sign in to comment.