Skip to content

Commit

Permalink
make control back to use slope per beat
Browse files Browse the repository at this point in the history
  • Loading branch information
anokta committed Feb 18, 2024
1 parent 3f37dff commit 3f39d50
Show file tree
Hide file tree
Showing 41 changed files with 407 additions and 279 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ auto instrument = musician.CreateInstrument<barely::SynthInstrument>(/*frame_rat

// Set the instrument gain to 0.5.
instrument.SetControl(barely::SynthInstrument::Control::kGain, /*value=*/0.5,
/*slope_per_second=*/0.0);
/*slope_per_beat=*/0.0);

// Set the instrument A3 note pitch on with a 0.25 intensity.
//
Expand All @@ -59,9 +59,9 @@ const bool is_note_on = instrument.IsNoteOn(a3_pitch); // will return true.
// Add a low-pass effect to the instrument.
auto effect = instrument.CreateEffect<barely::LowPassEffect>();

// Set the effect cutoff frequency to increase by 100 hertz per second.
// Set the effect cutoff frequency to increase by 100 hertz per beat.
effect.SetControl(barely::LowPassEffect::Control::kCutoffFrequency, /*value=*/0.0,
/*slope_per_second=*/100.0);
/*slope_per_beat=*/100.0);

// Update the musician timestamp in seconds.
//
Expand Down
15 changes: 8 additions & 7 deletions barelymusician/barelymusician.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ struct BarelyInstrument : public Observable<Instrument> {
// Constructs `BarelyInstrument` with `musician`, `definition`, and `frame_rate`.
BarelyInstrument(const Observable<Musician>& musician, BarelyInstrumentDefinition definition,
int32_t frame_rate) noexcept
: Observable<Instrument>(definition, frame_rate, musician.GetTimestamp()),
: Observable<Instrument>(definition, frame_rate, musician.GetTempo(),
musician.GetTimestamp()),
musician_(musician.Observe()) {
musician_->AddInstrument(*this);
}
Expand Down Expand Up @@ -209,10 +210,10 @@ bool BarelyEffect_ResetControl(BarelyEffectHandle effect, int32_t index) {
}

bool BarelyEffect_SetControl(BarelyEffectHandle effect, int32_t index, double value,
double slope_per_second) {
double slope_per_beat) {
if (!effect || !effect->instrument()) return false;

return effect->instrument()->SetEffectControl(*effect, index, value, slope_per_second);
return effect->instrument()->SetEffectControl(*effect, index, value, slope_per_beat);
}

bool BarelyEffect_SetControlEvent(BarelyEffectHandle effect,
Expand Down Expand Up @@ -355,10 +356,10 @@ bool BarelyInstrument_SetAllNotesOff(BarelyInstrumentHandle instrument) {
}

bool BarelyInstrument_SetControl(BarelyInstrumentHandle instrument, int32_t index, double value,
double slope_per_second) {
double slope_per_beat) {
if (!instrument) return false;

return instrument->SetControl(index, value, slope_per_second);
return instrument->SetControl(index, value, slope_per_beat);
}

bool BarelyInstrument_SetControlEvent(BarelyInstrumentHandle instrument,
Expand All @@ -379,10 +380,10 @@ bool BarelyInstrument_SetData(BarelyInstrumentHandle instrument, const void* dat
}

bool BarelyInstrument_SetNoteControl(BarelyInstrumentHandle instrument, double pitch, int32_t index,
double value, double slope_per_second) {
double value, double slope_per_beat) {
if (!instrument) return false;

return instrument->SetNoteControl(pitch, index, value, slope_per_second);
return instrument->SetNoteControl(pitch, index, value, slope_per_beat);
}

bool BarelyInstrument_SetNoteControlEvent(BarelyInstrumentHandle instrument,
Expand Down
42 changes: 21 additions & 21 deletions barelymusician/barelymusician.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@
///
/// // Set a control value.
/// instrument.SetControl(barely::SynthInstrument::Control::kGain, /*value=*/0.5,
/// /*slope_per_second=*/0.0);
/// /*slope_per_beat=*/0.0);
///
/// // Create a low-pass effect.
/// auto effect = instrument.CreateEffect<barely::LowPassEffect>();
///
/// // Set the low-pass cutoff frequency to increase by 100 hertz per second.
/// // Set the low-pass cutoff frequency to increase by 100 hertz per beat.
/// effect.SetControl(barely::LowPassEffect::Control::kCutoffFrequency, /*value=*/0.0,
/// /*slope_per_second=*/100.0);
/// /*slope_per_beat=*/100.0);
///
/// // Process.
/// //
Expand Down Expand Up @@ -147,15 +147,15 @@
/// BarelyInstrument_IsNoteOn(instrument, /*pitch=*/-1.0, &is_note_on);
///
/// // Set a control value.
/// BarelyInstrument_SetControl(instrument, /*index=*/0, /*value=*/0.5, /*slope_per_second=*/0.0);
/// BarelyInstrument_SetControl(instrument, /*index=*/0, /*value=*/0.5, /*slope_per_beat=*/0.0);
///
/// // Create a low-pass effect.
/// BarelyEffectHandle effect;
/// BarelyEffect_Create(instrument, BarelyLowPassEffect_GetDefinition(), /*process_order=*/0,
/// &effect);
///
/// // Set the low-pass cutoff frequency to increase by 100 hertz per second.
/// BarelyEffect_SetControl(effect, /*index=*/0, /*value=*/0.0, /*slope_per_second=*/100.0);
/// // Set the low-pass cutoff frequency to increase by 100 hertz per beat.
/// BarelyEffect_SetControl(effect, /*index=*/0, /*value=*/0.0, /*slope_per_beat=*/100.0);
///
/// // Process.
/// //
Expand Down Expand Up @@ -642,10 +642,10 @@ BARELY_EXPORT bool BarelyEffect_ResetControl(BarelyEffectHandle effect, int32_t
/// @param effect Effect handle.
/// @param index Control index.
/// @param value Control value.
/// @param slope_per_second Control slope in value change per second.
/// @param slope_per_beat Control slope in value change per beat.
/// @return True if successful, false otherwise.
BARELY_EXPORT bool BarelyEffect_SetControl(BarelyEffectHandle effect, int32_t index, double value,
double slope_per_second);
double slope_per_beat);

/// Sets the control event of an effect.
///
Expand Down Expand Up @@ -793,10 +793,10 @@ BARELY_EXPORT bool BarelyInstrument_SetAllNotesOff(BarelyInstrumentHandle instru
/// @param instrument Instrument handle.
/// @param index Control index.
/// @param value Control value.
/// @param slope_per_second Control slope in value change per second.
/// @param slope_per_beat Control slope in value change per beat.
/// @return True if successful, false otherwise.
BARELY_EXPORT bool BarelyInstrument_SetControl(BarelyInstrumentHandle instrument, int32_t index,
double value, double slope_per_second);
double value, double slope_per_beat);

/// Sets the control event of an instrument.
///
Expand All @@ -823,11 +823,11 @@ BARELY_EXPORT bool BarelyInstrument_SetData(BarelyInstrumentHandle instrument, c
/// @param pitch Note pitch.
/// @param index Note control index.
/// @param value Note control value.
/// @param slope_per_second Note control slope in value change per second.
/// @param slope_per_beat Note control slope in value change per beat.
/// @return True if successful, false otherwise.
BARELY_EXPORT bool BarelyInstrument_SetNoteControl(BarelyInstrumentHandle instrument, double pitch,
int32_t index, double value,
double slope_per_second);
double slope_per_beat);

/// Sets the note control event of an instrument.
///
Expand Down Expand Up @@ -1719,15 +1719,15 @@ class Effect : protected Wrapper<BarelyEffectHandle> {
///
/// @param index Control index.
/// @param value Control value.
/// @param slope_per_second Control slope in value change per second.
/// @param slope_per_beat Control slope in value change per beat.
template <typename IndexType, typename ValueType>
void SetControl(IndexType index, ValueType value, double slope_per_second = 0.0) noexcept {
void SetControl(IndexType index, ValueType value, double slope_per_beat = 0.0) noexcept {
static_assert(std::is_integral<IndexType>::value || std::is_enum<IndexType>::value,
"IndexType is not supported");
static_assert(std::is_arithmetic<ValueType>::value || std::is_enum<ValueType>::value,
"ValueType is not supported");
[[maybe_unused]] const bool success = BarelyEffect_SetControl(
Get(), static_cast<int>(index), static_cast<double>(value), slope_per_second);
Get(), static_cast<int>(index), static_cast<double>(value), slope_per_beat);
assert(success);
}

Expand Down Expand Up @@ -1972,15 +1972,15 @@ class Instrument : protected Wrapper<BarelyInstrumentHandle> {
///
/// @param index Control index.
/// @param value Control value.
/// @param slope_per_second Control slope in value change per second.
/// @param slope_per_beat Control slope in value change per beat.
template <typename IndexType, typename ValueType>
void SetControl(IndexType index, ValueType value, double slope_per_second = 0.0) noexcept {
void SetControl(IndexType index, ValueType value, double slope_per_beat = 0.0) noexcept {
static_assert(std::is_integral<IndexType>::value || std::is_enum<IndexType>::value,
"IndexType is not supported");
static_assert(std::is_arithmetic<ValueType>::value || std::is_enum<ValueType>::value,
"ValueType is not supported");
[[maybe_unused]] const bool success = BarelyInstrument_SetControl(
Get(), static_cast<int>(index), static_cast<double>(value), slope_per_second);
Get(), static_cast<int>(index), static_cast<double>(value), slope_per_beat);
assert(success);
}

Expand Down Expand Up @@ -2032,16 +2032,16 @@ class Instrument : protected Wrapper<BarelyInstrumentHandle> {
/// @param pitch Note pitch.
/// @param index Note control index.
/// @param value Note control value.
/// @param slope_per_second Note control slope in value change per second.
/// @param slope_per_beat Note control slope in value change per beat.
template <typename IndexType, typename ValueType>
void SetNoteControl(double pitch, IndexType index, ValueType value,
double slope_per_second = 0.0) noexcept {
double slope_per_beat = 0.0) noexcept {
static_assert(std::is_integral<IndexType>::value || std::is_enum<IndexType>::value,
"IndexType is not supported");
static_assert(std::is_arithmetic<ValueType>::value || std::is_enum<ValueType>::value,
"ValueType is not supported");
[[maybe_unused]] const bool success = BarelyInstrument_SetNoteControl(
Get(), pitch, static_cast<int>(index), static_cast<double>(value), slope_per_second);
Get(), pitch, static_cast<int>(index), static_cast<double>(value), slope_per_beat);
assert(success);
}

Expand Down
8 changes: 8 additions & 0 deletions barelymusician/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ cc_library(
":message",
":message_queue",
":mutable",
":seconds",
"//barelymusician:barelymusician_headeronly",
"//barelymusician/common:find_or_null",
],
Expand Down Expand Up @@ -68,6 +69,7 @@ cc_library(
":instrument",
":mutable",
":performer",
":seconds",
],
)

Expand All @@ -91,6 +93,12 @@ cc_library(
],
)

cc_library(
name = "seconds",
srcs = ["seconds.cpp"],
hdrs = ["seconds.h"],
)

cc_library(
name = "task",
srcs = ["task.cpp"],
Expand Down
19 changes: 19 additions & 0 deletions barelymusician/internal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ target_link_libraries(
barely_internal_message
barely_internal_message_queue
barely_internal_mutable
barely_internal_seconds
barelymusician_headeronly
barely_find_or_null
)
Expand Down Expand Up @@ -71,6 +72,7 @@ target_link_libraries(
barely_internal_instrument
barely_internal_mutable
barely_internal_performer
barely_internal_seconds
)

add_library(
Expand All @@ -94,6 +96,12 @@ target_link_libraries(
barelymusician_headeronly
)

add_library(
barely_internal_seconds
seconds.cpp
seconds.h
)

add_library(
barely_internal_task
task.cpp
Expand Down Expand Up @@ -210,6 +218,17 @@ if (ENABLE_TESTS)
)
gtest_discover_tests(barely_internal_performer_test)

add_executable(
barely_internal_seconds_test
seconds_test.cpp
)
target_link_libraries(
barely_internal_seconds_test
barely_internal_seconds
gtest_main
)
gtest_discover_tests(barely_internal_seconds_test)

add_executable(
barely_internal_task_test
task_test.cpp
Expand Down
20 changes: 10 additions & 10 deletions barelymusician/internal/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,33 @@ Control::Control(ControlDefinition definition) noexcept

const ControlDefinition& Control::GetDefinition() const noexcept { return definition_; }

double Control::GetSlopePerSecond() const noexcept { return slope_per_second_; }
double Control::GetSlopePerBeat() const noexcept { return slope_per_beat_; }

double Control::GetValue() const noexcept { return value_; }

bool Control::Reset() noexcept {
if (value_ != definition_.default_value || slope_per_second_ != 0.0) {
if (value_ != definition_.default_value || slope_per_beat_ != 0.0) {
value_ = definition_.default_value;
slope_per_second_ = 0.0;
slope_per_beat_ = 0.0;
return true;
}
return false;
}

bool Control::Set(double value, double slope_per_second) noexcept {
bool Control::Set(double value, double slope_per_beat) noexcept {
value = Clamp(value);
if (value_ != value || slope_per_second_ != slope_per_second) {
if (value_ != value || slope_per_beat_ != slope_per_beat) {
value_ = value;
slope_per_second_ = slope_per_second;
slope_per_beat_ = slope_per_beat;
return true;
}
return false;
}

bool Control::Update(double elapsed_seconds) noexcept {
assert(elapsed_seconds > 0.0);
if (slope_per_second_ != 0.0) {
if (const double value = Clamp(value_ + slope_per_second_ * elapsed_seconds); value_ != value) {
bool Control::Update(double duration) noexcept {
assert(duration > 0.0);
if (slope_per_beat_ != 0.0) {
if (const double value = Clamp(value_ + slope_per_beat_ * duration); value_ != value) {
value_ = value;
return true;
}
Expand Down
18 changes: 9 additions & 9 deletions barelymusician/internal/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class Control {

/// Returns the slope per beat.
///
/// @return Control slope in value change per second.
[[nodiscard]] double GetSlopePerSecond() const noexcept;
/// @return Control slope in value change per beat.
[[nodiscard]] double GetSlopePerBeat() const noexcept;

/// Returns the value.
///
Expand All @@ -43,15 +43,15 @@ class Control {
/// Sets the value.
///
/// @param value Control value.
/// @param slope_per_second Control slope in value change per second.
/// @param slope_per_beat Control slope in value change per beat.
/// @return True if changed, false otherwise.
bool Set(double value, double slope_per_second) noexcept;
bool Set(double value, double slope_per_beat) noexcept;

/// Updates the value by elapsed seconds.
/// Updates the value.
///
/// @param elapsed_seconds Elapsed seconds.
/// @param duration Duration in beats.
/// @return True if changed, false otherwise.
bool Update(double elapsed_seconds) noexcept;
bool Update(double duration) noexcept;

private:
// Clamps a given `value`.
Expand All @@ -63,8 +63,8 @@ class Control {
// Value.
double value_ = 0.0;

// Slope in value change per second.
double slope_per_second_ = 0.0;
// Slope in value change per beat.
double slope_per_beat_ = 0.0;
};

/// Builds the corresponding controls for a given array of control `definitions`.
Expand Down
Loading

0 comments on commit 3f39d50

Please sign in to comment.