Skip to content

Commit

Permalink
added linear fade-out class
Browse files Browse the repository at this point in the history
  • Loading branch information
ozguronsoy committed Nov 12, 2024
1 parent 96c8621 commit 4b0734d
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
93 changes: 93 additions & 0 deletions HephAudio/HeaderFiles/AudioEffects/LinearFadeOut.h
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;
};
}
2 changes: 2 additions & 0 deletions HephAudio/HephAudio.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\RmsNormalizer.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\Overdrive.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\LinearFadeIn.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\LinearFadeOut.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\LinearFadeOut.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\LinearFadeIn.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\Overdrive.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\CubicDistortion.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 @@ -252,6 +252,7 @@
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\CubicDistortion.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\Overdrive.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\LinearFadeIn.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)HeaderFiles\AudioEffects\LinearFadeOut.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioProcessor.cpp">
Expand Down Expand Up @@ -420,5 +421,6 @@
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\CubicDistortion.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\Overdrive.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\LinearFadeIn.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEffects\LinearFadeOut.cpp" />
</ItemGroup>
</Project>
77 changes: 77 additions & 0 deletions HephAudio/SourceFiles/AudioEffects/LinearFadeOut.cpp
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;
}
}
}
}
}

0 comments on commit 4b0734d

Please sign in to comment.