-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62dd11b
commit 088f483
Showing
7 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |