-
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
65f2951
commit 03064eb
Showing
4 changed files
with
110 additions
and
0 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,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; | ||
}; | ||
} |
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,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)); | ||
} | ||
} | ||
} | ||
} |