-
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
96c8621
commit 4b0734d
Showing
4 changed files
with
174 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,93 @@ | ||
#pragma once | ||
#include "HephAudioShared.h" | ||
#include "AudioEffect.h" | ||
|
||
/** @file */ | ||
|
||
namespace HephAudio | ||
{ | ||
/** | ||
* @brief applies linear fade-out. | ||
* | ||
*/ | ||
class LinearFadeOut : public AudioEffect | ||
{ | ||
public: | ||
using AudioEffect::Process; | ||
|
||
protected: | ||
/** | ||
* duration of the fade-out in seconds. | ||
* | ||
*/ | ||
double duration; | ||
|
||
/** | ||
* index of the first audio frame the effect will be applied to. | ||
* | ||
*/ | ||
size_t startIndex; | ||
|
||
/** | ||
* for real-time processing. | ||
* | ||
*/ | ||
size_t currentIndex; | ||
|
||
public: | ||
/** @copydoc default_constructor */ | ||
LinearFadeOut(); | ||
|
||
/** | ||
* @copydoc constructor | ||
* | ||
* @param duration @copydetails duration | ||
* | ||
*/ | ||
explicit LinearFadeOut(double duration); | ||
|
||
/** | ||
* @copydoc constructor | ||
* | ||
* @param duration @copydetails duration | ||
* @param startIndex @copydetails startIndex | ||
* | ||
*/ | ||
LinearFadeOut(double duration, size_t startIndex); | ||
|
||
/** @copydoc destructor */ | ||
virtual ~LinearFadeOut() = default; | ||
|
||
virtual std::string Name() const override; | ||
virtual void Process(AudioBuffer& buffer, size_t startIndex, size_t frameCount) override; | ||
|
||
/** | ||
* gets the duration in seconds. | ||
* | ||
*/ | ||
virtual double GetDuration() const; | ||
|
||
/** | ||
* sets the duration. | ||
* | ||
* @param duration @copydetails duration | ||
*/ | ||
virtual void SetDuration(double duration); | ||
|
||
/** | ||
* gets the start index. | ||
* | ||
*/ | ||
virtual size_t GetStartIndex() const; | ||
|
||
/** | ||
* sets the start index. | ||
* | ||
* @param startIndex @copydetails startIndex | ||
*/ | ||
virtual void SetStartIndex(size_t startIndex); | ||
|
||
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,77 @@ | ||
#include "AudioEffects/LinearFadeOut.h" | ||
#include "Exceptions/InvalidArgumentException.h" | ||
#include "HephMath.h" | ||
|
||
using namespace Heph; | ||
|
||
namespace HephAudio | ||
{ | ||
LinearFadeOut::LinearFadeOut() : LinearFadeOut(0.5) {} | ||
|
||
LinearFadeOut::LinearFadeOut(double duration) : LinearFadeOut(duration, 0) {} | ||
|
||
LinearFadeOut::LinearFadeOut(double duration, size_t startIndex) : AudioEffect(), currentIndex(0) | ||
{ | ||
this->SetDuration(duration); | ||
this->SetStartIndex(startIndex); | ||
} | ||
|
||
std::string LinearFadeOut::Name() const | ||
{ | ||
return "Linear Fade-out"; | ||
} | ||
|
||
void LinearFadeOut::Process(AudioBuffer& buffer, size_t startIndex, size_t frameCount) | ||
{ | ||
AudioEffect::Process(buffer, startIndex, frameCount); | ||
this->currentIndex += frameCount; | ||
} | ||
|
||
double LinearFadeOut::GetDuration() const | ||
{ | ||
return this->duration; | ||
} | ||
|
||
void LinearFadeOut::SetDuration(double duration) | ||
{ | ||
if (duration < 0) | ||
{ | ||
HEPH_RAISE_AND_THROW_EXCEPTION(this, InvalidArgumentException(HEPH_FUNC, "duration cannot be negative.")); | ||
} | ||
|
||
this->duration = duration; | ||
} | ||
|
||
size_t LinearFadeOut::GetStartIndex() const | ||
{ | ||
return this->startIndex; | ||
} | ||
|
||
void LinearFadeOut::SetStartIndex(size_t startIndex) | ||
{ | ||
this->startIndex = startIndex; | ||
} | ||
|
||
void LinearFadeOut::ProcessST(const AudioBuffer& inputBuffer, AudioBuffer& outputBuffer, size_t startIndex, size_t frameCount) | ||
{ | ||
startIndex += this->currentIndex; | ||
size_t endIndex = startIndex + frameCount; | ||
const AudioFormatInfo& formatInfo = outputBuffer.FormatInfo(); | ||
const double duration_sample = formatInfo.sampleRate * this->duration; | ||
|
||
if (endIndex > this->startIndex && startIndex < (this->startIndex + duration_sample)) | ||
{ | ||
startIndex = HEPH_MATH_MAX(startIndex, this->startIndex) - this->currentIndex; | ||
endIndex = HEPH_MATH_MIN(endIndex, (duration_sample + this->startIndex)) - this->currentIndex; | ||
|
||
for (size_t i = startIndex; i < endIndex; ++i) | ||
{ | ||
const double factor = ((this->startIndex + duration_sample) - (i + this->currentIndex)) / duration_sample; | ||
for (size_t j = 0; j < formatInfo.channelLayout.count; ++j) | ||
{ | ||
outputBuffer[i][j] *= factor; | ||
} | ||
} | ||
} | ||
} | ||
} |