Skip to content

Commit

Permalink
Implemented Gain processor to adjust signal volume without colouring
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Zinken committed Jul 24, 2022
1 parent 61a1337 commit 05b76c0
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 0 deletions.
1 change: 1 addition & 0 deletions mwengine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ set(MWENGINE_PROCESSORS ${CPP_SRC}/processors/bitcrusher.cpp
${CPP_SRC}/processors/flanger.cpp
${CPP_SRC}/processors/fm.cpp
${CPP_SRC}/processors/formantfilter.cpp
${CPP_SRC}/processors/gain.cpp
${CPP_SRC}/processors/glitcher.cpp
${CPP_SRC}/processors/limiter.cpp
${CPP_SRC}/processors/lowpassfilter.cpp
Expand Down
2 changes: 2 additions & 0 deletions mwengine/src/main/cpp/mwengine.i
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ using namespace MWEngine;
#include "processors/limiter.h"
#include "processors/fm.h"
#include "processors/formantfilter.h"
#include "processors/gain.h"
#include "processors/glitcher.h"
#include "processors/lowpassfilter.h"
#include "processors/lpfhpfilter.h"
Expand Down Expand Up @@ -115,6 +116,7 @@ typedef double SAMPLE_TYPE;
%include "processors/lpfhpfilter.h"
%include "processors/fm.h"
%include "processors/formantfilter.h"
%include "processors/gain.h"
%include "processors/glitcher.h"
%include "processors/phaser.h"
%include "processors/pitchshifter.h"
Expand Down
91 changes: 91 additions & 0 deletions mwengine/src/main/cpp/processors/gain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2022 Igor Zinken - http://www.igorski.nl
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "gain.h"
#include <utilities/utils.h>
#include <cmath>

namespace MWEngine {

/* constructor / destructor */

Gain::Gain()
{
setAmount( MAX_VOLUME );
}

Gain::Gain( float amount )
{
setAmount( amount );
}

Gain::~Gain()
{
// nowt...
}

/* public methods */

void Gain::process( AudioBuffer* sampleBuffer, bool isMonoSource )
{
// no gain specified ? do nothing
if ( _amount == MAX_VOLUME )
return;

int bufferSize = sampleBuffer->bufferSize;

for ( int i = 0, l = sampleBuffer->amountOfChannels; i < l; ++i )
{
SAMPLE_TYPE* channelBuffer = sampleBuffer->getBufferForChannel( i );

for ( int j = 0; j < bufferSize; ++j )
{
channelBuffer[ j ] = capSampleSafe( channelBuffer[ j ] * _amount );
}

// omit unnecessary cycles by copying the mono content
if ( isMonoSource )
{
sampleBuffer->applyMonoSource();
break;
}
}
}

bool Gain::isCacheable()
{
return true;
}

/* getters / setters */

float Gain::getAmount()
{
return ( float ) _amount;
}

void Gain::setAmount( float value )
{
_amount = std::max( MIN_GAIN, std::min( MAX_GAIN, ( SAMPLE_TYPE ) value ));
}

} // E.O namespace MWEngine
58 changes: 58 additions & 0 deletions mwengine/src/main/cpp/processors/gain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2022 Igor Zinken - https://www.igorski.nl
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef __MWENGINE__GAIN_H_INCLUDED__
#define __MWENGINE__GAIN_H_INCLUDED__

#include "baseprocessor.h"
#include <global.h>

namespace MWEngine {
class Gain : public BaseProcessor
{
public:
static constexpr SAMPLE_TYPE MIN_GAIN = SILENCE;
static constexpr SAMPLE_TYPE MAX_GAIN = 20;

Gain();
Gain( float amount );
~Gain();

std::string getType() {
return std::string( "Gain" );
}

float getAmount();
void setAmount( float value );

#ifndef SWIG
// internal to the engine
void process( AudioBuffer* sampleBuffer, bool isMonoSource );
bool isCacheable();
#endif

private:
SAMPLE_TYPE _amount;
};
} // E.O namespace MWEngine

#endif
1 change: 1 addition & 0 deletions mwengine/src/main/cpp/tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ using namespace MWEngine;
#include "processors/flanger_test.cpp"
#include "processors/fm_test.cpp"
#include "processors/formantfilter_test.cpp"
#include "processors/gain_test.cpp"
#include "processors/glitcher_test.cpp"
#include "processors/limiter_test.cpp"
#include "processors/lowpassfilter_test.cpp"
Expand Down
51 changes: 51 additions & 0 deletions mwengine/src/main/cpp/tests/processors/gain_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <processors/gain.h>
#include <global.h>

TEST( Gain, Constructors )
{
auto processor = new Gain();

EXPECT_EQ( MAX_VOLUME, processor->getAmount() )
<< "expected default gain to equal the maximum volume";

delete processor;

processor = new Gain( 0.5f );

EXPECT_EQ( 0.5f, processor->getAmount() )
<< "expected default gain to equal the amount provided to the constructor";

delete processor;
}

TEST( Gain, getType )
{
auto processor = new Gain();

std::string expectedType( "Gain" );
ASSERT_TRUE( 0 == expectedType.compare( processor->getType() ));

delete processor;
}

TEST( Gain, setAmount )
{
auto processor = new Gain();

processor->setAmount( 2.f );

EXPECT_EQ( 2.f, processor->getAmount() )
<< "expected gain amount to equal the value passed to the setter";

processor->setAmount( Gain::MIN_GAIN - 1 );

EXPECT_EQ( Gain::MIN_GAIN, processor->getAmount() )
<< "expected out of range gain amount to have been capped to the minimum supported value";

processor->setAmount( Gain::MAX_GAIN + 1 );

EXPECT_EQ( Gain::MAX_GAIN, processor->getAmount() )
<< "expected out of range gain amount to have been capped to the maximum supported value";

delete processor;
}

0 comments on commit 05b76c0

Please sign in to comment.