-
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
c1b6795
commit 65f2951
Showing
6 changed files
with
121 additions
and
11 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
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 @@ | ||
#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; | ||
}; | ||
} |
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,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)); | ||
} | ||
} | ||
} | ||
} |