-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Gain processor to adjust signal volume without colouring
- Loading branch information
Igor Zinken
committed
Jul 24, 2022
1 parent
61a1337
commit 05b76c0
Showing
6 changed files
with
204 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
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,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 |
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,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 |
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 <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; | ||
} |