-
-
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.
+ might need to support some sort of note start/reset in effect definition
- Loading branch information
Showing
13 changed files
with
247 additions
and
9 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
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,75 @@ | ||
#include "barelymusician/effects/lfo_effect.h" | ||
|
||
#include <array> | ||
#include <cassert> | ||
|
||
#include "barelymusician/barelymusician.h" | ||
#include "barelymusician/dsp/oscillator.h" | ||
#include "barelymusician/effects/custom_effect.h" | ||
|
||
BarelyEffectDefinition BarelyLfoEffect_GetDefinition() { | ||
return barely::LfoEffect::GetDefinition(); | ||
} | ||
|
||
namespace barely { | ||
|
||
EffectDefinition LfoEffect::GetDefinition() noexcept { | ||
static const std::array<ControlDefinition, static_cast<int>(Control::kCount)> | ||
control_definitions = { | ||
// Oscillator type. | ||
ControlDefinition{static_cast<double>(OscillatorType::kSine), 0.0, | ||
static_cast<double>(OscillatorType::kNoise)}, | ||
// Oscillator frequency. | ||
ControlDefinition{1.0, 0.0, 32.0}, | ||
// Intensity. | ||
ControlDefinition{1.0, 0.0, 1.0}, | ||
}; | ||
return CustomEffect::GetDefinition<LfoEffect>(control_definitions); | ||
} | ||
|
||
LfoEffect::LfoEffect(int frame_rate) noexcept : lfo_(frame_rate) { assert(frame_rate > 0); } | ||
|
||
void LfoEffect::Process(double* output_samples, int output_channel_count, | ||
int output_frame_count) noexcept { | ||
for (int frame = 0; frame < output_frame_count; ++frame) { | ||
const double gain = intensity_.first * lfo_.Next(); | ||
for (int channel = 0; channel < output_channel_count; ++channel) { | ||
auto& sample = output_samples[output_channel_count * frame + channel]; | ||
sample *= gain; | ||
} | ||
if (frequency_.second != 0.0) { | ||
frequency_.first += frequency_.second; | ||
lfo_.SetFrequency(frequency_.first); | ||
} | ||
if (intensity_.second != 0.0) { | ||
intensity_.first += intensity_.second; | ||
} | ||
} | ||
} | ||
|
||
void LfoEffect::SetControl(int index, double value, double slope_per_frame) noexcept { | ||
switch (static_cast<Control>(index)) { | ||
case Control::kOscillatorType: | ||
lfo_.SetType(static_cast<OscillatorType>(static_cast<int>(value))); | ||
break; | ||
case Control::kOscillatorFrequency: | ||
if (value != frequency_.first) { | ||
frequency_.first = value; | ||
lfo_.SetFrequency(value); | ||
} | ||
frequency_.second = slope_per_frame; | ||
break; | ||
case Control::kIntensity: | ||
if (value != intensity_.first) { | ||
intensity_.first = value; | ||
lfo_.SetFrequency(value); | ||
} | ||
intensity_.second = slope_per_frame; | ||
break; | ||
default: | ||
assert(false); | ||
break; | ||
} | ||
} | ||
|
||
} // namespace barely |
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,72 @@ | ||
#ifndef BARELYMUSICIAN_EFFECTS_LFO_EFFECT_H_ | ||
#define BARELYMUSICIAN_EFFECTS_LFO_EFFECT_H_ | ||
|
||
// NOLINTBEGIN | ||
#include "barelymusician/barelymusician.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif // __cplusplus | ||
|
||
/// Returns the low-frequency oscillator effect definition. | ||
/// | ||
/// @return Effect definition. | ||
BARELY_EXPORT BarelyEffectDefinition BarelyLfoEffect_GetDefinition(); | ||
|
||
#ifdef __cplusplus | ||
} // extern "C" | ||
#endif // __cplusplus | ||
// NOLINTEND | ||
|
||
#ifdef __cplusplus | ||
|
||
#include "barelymusician/dsp/oscillator.h" | ||
#include "barelymusician/effects/custom_effect.h" | ||
|
||
namespace barely { | ||
|
||
/// Simple amplitude low-frequency oscillator effect. | ||
class LfoEffect : public CustomEffect { | ||
public: | ||
/// Control enum. | ||
enum class Control : int { | ||
/// Oscillator type. | ||
kOscillatorType = 0, | ||
/// Oscillator frequency. | ||
kOscillatorFrequency = 1, | ||
/// Intensity. | ||
kIntensity = 2, | ||
/// Number of controls. | ||
kCount, | ||
}; | ||
|
||
/// Returns the effect definition. | ||
/// | ||
/// @return Effect definition. | ||
static EffectDefinition GetDefinition() noexcept; | ||
|
||
protected: | ||
/// Constructs new `LfoEffect`. | ||
explicit LfoEffect(int frame_rate) noexcept; | ||
|
||
/// Implements `CustomEffect`. | ||
void Process(double* output_samples, int output_channel_count, | ||
int output_frame_count) noexcept final; | ||
void SetControl(int index, double value, double slope_per_frame) noexcept final; | ||
void SetData(const void* /*data*/, int /*size*/) noexcept final {} | ||
|
||
private: | ||
// Low-frequency oscillator. | ||
Oscillator lfo_; | ||
|
||
// Frequency-slope pair. | ||
std::pair<double, double> frequency_ = {220.0, 0.0}; | ||
|
||
// Intensity-slope pair. | ||
std::pair<double, double> intensity_ = {1.0, 0.0}; | ||
}; | ||
|
||
} // namespace barely | ||
#endif // __cplusplus | ||
|
||
#endif // BARELYMUSICIAN_EFFECTS_LFO_EFFECT_H_ |
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
40 changes: 40 additions & 0 deletions
40
platforms/unity/Assets/BarelyMusician/Scripts/Effects/LfoEffect.cs
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,40 @@ | ||
using UnityEngine; | ||
|
||
namespace Barely { | ||
/// A representation of a simple amplitude low-frequency oscillator effect. | ||
public class LfoEffect : Effect { | ||
/// Type. | ||
public OscillatorType Type { | ||
get { return (OscillatorType)GetControl(0); } | ||
set { SetControl(0, (double)value, 0.0); } | ||
} | ||
|
||
/// Frequency. | ||
public double Frequency { | ||
get { return GetControl(1); } | ||
set { SetControl(1, value, 0.0); } | ||
} | ||
|
||
/// Intensity. | ||
public double Intensity { | ||
get { return GetControl(2); } | ||
set { SetControl(2, value, 0.0); } | ||
} | ||
|
||
/// Sets the oscillator frequency with a slope. | ||
/// | ||
/// @param frequency Oscillator frequency. | ||
/// @param slopePerBeat Slope in value change per beat. | ||
public void SetFrequency(double frequency, double slopePerBeat) { | ||
SetControl(0, frequency, slopePerBeat); | ||
} | ||
|
||
/// Sets the intensity with a slope. | ||
/// | ||
/// @param intensity Intensity. | ||
/// @param slopePerBeat Slope in value change per beat. | ||
public void SetIntensity(double intensity, double slopePerBeat) { | ||
SetControl(0, intensity, slopePerBeat); | ||
} | ||
} | ||
} // namespace Barely |
11 changes: 11 additions & 0 deletions
11
platforms/unity/Assets/BarelyMusician/Scripts/Effects/LfoEffect.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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