43 lines
834 B
C++
43 lines
834 B
C++
|
|
#include "Instrument.hpp"
|
|
|
|
#include "string"
|
|
|
|
Instrument::Instrument(ConfigService* config, LoggerService* logger) :
|
|
config_(config), logger_(logger) {
|
|
|
|
}
|
|
|
|
void Instrument::noteOn(float frequency, float velocity) {
|
|
|
|
phaseIncrement_ = 2.0f * pi * frequency / sampleRate_;
|
|
|
|
envelope_ += 0.01f; // so it triggers as active
|
|
|
|
active_ = true;
|
|
}
|
|
|
|
void Instrument::noteOff() {
|
|
active_ = false;
|
|
}
|
|
|
|
bool Instrument::isActive() {
|
|
return (envelope_ > 0.0f);
|
|
}
|
|
|
|
float Instrument::process(bool& scopeTrigger) {
|
|
|
|
if(active_ && envelope_ < 1.0f) envelope_ += 0.01f;
|
|
if(!active_ && envelope_ > 0.0f) envelope_ -= 0.01f;
|
|
|
|
phase_ += phaseIncrement_;
|
|
if(phase_ > 2.0f * pi) {
|
|
phase_ -= 2.0f * pi;
|
|
scopeTrigger = true;
|
|
}
|
|
|
|
if(!isActive()) return 0.0f;
|
|
return sin(phase_) * envelope_;
|
|
|
|
}
|