Skip to content

Commit 1141d8d

Browse files
committed
revert move tempo to engine controls
1 parent 50369ac commit 1141d8d

20 files changed

Lines changed: 156 additions & 96 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Example usage
3232
barely::Engine engine(/*sample_rate=*/48000, /*max_frame_count=*/512);
3333

3434
// Set the global tempo.
35-
engine.SetControl(barely::EngineControlType::kTempo, 124.0f);
35+
engine.SetTempo(/*tempo=*/124.0);
3636

3737
// Create a new instrument.
3838
auto instrument = engine.CreateInstrument();

examples/demo/instrument_demo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ constexpr double kLookahead = 0.05;
3636
constexpr float kDelayTime = 0.5f;
3737
constexpr float kDelayFeedback = 0.2;
3838
constexpr float kDelayLowPassFrequency = 1000.0f;
39-
constexpr float kTempo = 128.0f;
39+
constexpr double kTempo = 128.0;
4040

4141
// Instrument settings.
4242
constexpr float kGain = 0.125f;
@@ -78,7 +78,7 @@ int main(int /*argc*/, char* /*argv*/[]) {
7878
engine.SetControl(EngineControlType::kDelayTime, kDelayTime);
7979
engine.SetControl(EngineControlType::kDelayFeedback, kDelayFeedback);
8080
engine.SetControl(EngineControlType::kDelayLowPassFrequency, kDelayLowPassFrequency);
81-
engine.SetControl(EngineControlType::kTempo, kTempo);
81+
engine.SetTempo(kTempo);
8282

8383
auto instrument = engine.CreateInstrument({{
8484
{InstrumentControlType::kGain, kGain},

examples/demo/metronome_demo.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
namespace {
1414

1515
using ::barely::Engine;
16-
using ::barely::EngineControlType;
1716
using ::barely::InstrumentControlType;
1817
using ::barely::TaskEventType;
1918
using ::barely::examples::AudioClock;
@@ -39,8 +38,8 @@ constexpr float kBarPitch = 1.0f;
3938
constexpr float kBeatPitch = 0.0f;
4039

4140
constexpr int kBeatCount = 4;
42-
constexpr float kInitialTempo = 120.0f;
43-
constexpr float kTempoIncrement = 10.0f;
41+
constexpr double kInitialTempo = 120.0;
42+
constexpr double kTempoIncrement = 10.0;
4443

4544
} // namespace
4645

@@ -52,7 +51,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[]) {
5251
AudioOutput audio_output(kSampleRate, kChannelCount, kFrameCount);
5352

5453
Engine engine(kSampleRate, kFrameCount);
55-
engine.SetControl(EngineControlType::kTempo, kInitialTempo);
54+
engine.SetTempo(kInitialTempo);
5655

5756
// Create the metronome instrument.
5857
auto instrument = engine.CreateInstrument({{
@@ -98,7 +97,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[]) {
9897
return;
9998
}
10099
// Adjust tempo.
101-
float tempo = engine.GetControl<float>(EngineControlType::kTempo);
100+
double tempo = engine.GetTempo();
102101
switch (std::toupper(key)) {
103102
case ' ':
104103
if (metronome.IsPlaying()) {
@@ -132,10 +131,9 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[]) {
132131
default:
133132
return;
134133
}
135-
tempo = std::clamp(tempo, 0.0f, static_cast<float>(kSampleRate));
136-
engine.SetControl(EngineControlType::kTempo, tempo);
137-
ConsoleLog() << "Tempo set to " << engine.GetControl<float>(EngineControlType::kTempo)
138-
<< " bpm";
134+
tempo = std::clamp(tempo, 0.0, static_cast<double>(kSampleRate));
135+
engine.SetTempo(tempo);
136+
ConsoleLog() << "Tempo set to " << engine.GetTempo() << " bpm";
139137
};
140138
input_manager.SetKeyDownCallback(key_down_callback);
141139

examples/demo/midi_demo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ constexpr float kInstrumentGain = 0.1f;
5050
// Midi file name.
5151
constexpr char kMidiFileName[] = "midi/sample.mid";
5252

53-
constexpr float kTempo = 132.0f;
53+
constexpr double kTempo = 132.0;
5454

5555
// Builds the score for the given `midi_events`.
5656
bool BuildScore(const smf::MidiEventList& midi_events, int ticks_per_beat, Instrument& instrument,
@@ -102,7 +102,7 @@ int main(int /*argc*/, char* argv[]) {
102102
AudioOutput audio_output(kSampleRate, kChannelCount, kFrameCount);
103103

104104
Engine engine(kSampleRate, kFrameCount);
105-
engine.SetControl(barely::EngineControlType::kTempo, kTempo);
105+
engine.SetTempo(kTempo);
106106

107107
std::vector<std::tuple<Instrument, Performer, std::vector<Task>, size_t>> tracks;
108108
tracks.reserve(track_count);

examples/demo/musician_demo.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
namespace {
2525

2626
using ::barely::Engine;
27-
using ::barely::EngineControlType;
2827
using ::barely::Instrument;
2928
using ::barely::InstrumentControlType;
3029
using ::barely::NoteEventType;
@@ -62,7 +61,7 @@ constexpr int kFrameCount = 1024;
6261
constexpr double kLookahead = 0.1;
6362

6463
// Performer settings.
65-
constexpr float kTempo = 124.0f;
64+
constexpr double kTempo = 124.0;
6665
constexpr int kBeatCount = 3;
6766

6867
// Number of semitones in an octave.
@@ -222,7 +221,7 @@ int main(int /*argc*/, char* argv[]) {
222221
AudioOutput audio_output(kSampleRate, kChannelCount, kFrameCount);
223222

224223
Engine engine(kSampleRate, kFrameCount);
225-
engine.SetControl(barely::EngineControlType::kTempo, kTempo);
224+
engine.SetTempo(kTempo);
226225

227226
// Note event callback.
228227
const auto set_note_Event_callback_fn = [&](size_t index, Instrument& instrument) {
@@ -394,19 +393,15 @@ int main(int /*argc*/, char* argv[]) {
394393
}
395394
break;
396395
case '1':
397-
engine.SetControl(EngineControlType::kTempo,
398-
engine.GenerateRandomNumber(0.5f, 0.75f) *
399-
engine.GetControl<float>(EngineControlType::kTempo));
400-
ConsoleLog() << "Tempo changed to " << engine.GetControl<float>(EngineControlType::kTempo);
396+
engine.SetTempo(engine.GenerateRandomNumber(0.5, 0.75) * engine.GetTempo());
397+
ConsoleLog() << "Tempo changed to " << engine.GetTempo();
401398
break;
402399
case '2':
403-
engine.SetControl(EngineControlType::kTempo,
404-
engine.GenerateRandomNumber(1.5f, 2.0f) *
405-
engine.GetControl<float>(EngineControlType::kTempo));
406-
ConsoleLog() << "Tempo changed to " << engine.GetControl<float>(EngineControlType::kTempo);
400+
engine.SetTempo(engine.GenerateRandomNumber(1.5, 2.0) * engine.GetTempo());
401+
ConsoleLog() << "Tempo changed to " << engine.GetTempo();
407402
break;
408403
case 'R':
409-
engine.SetControl(EngineControlType::kTempo, kTempo);
404+
engine.SetTempo(kTempo);
410405
ConsoleLog() << "Tempo reset to " << kTempo;
411406
break;
412407
case 'D':

examples/demo/repeater_demo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int main(int /*argc*/, char* /*argv*/[]) {
7070
AudioOutput audio_output(kSampleRate, kChannelCount, kFrameCount);
7171

7272
Engine engine(kSampleRate, kFrameCount);
73-
engine.SetControl(barely::EngineControlType::kTempo, kInitialTempo);
73+
engine.SetTempo(kInitialTempo);
7474

7575
auto instrument = engine.CreateInstrument({{
7676
{InstrumentControlType::kGain, kGain},

examples/demo/sequencer_demo.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace {
1515

1616
using ::barely::Engine;
17-
using ::barely::EngineControlType;
1817
using ::barely::InstrumentControlType;
1918
using ::barely::NoteEventType;
2019
using ::barely::Task;
@@ -37,8 +36,8 @@ constexpr float kOscShape = 1.0f;
3736
constexpr float kAttack = 0.0f;
3837
constexpr float kRelease = 0.1f;
3938

40-
constexpr float kInitialTempo = 120.0f;
41-
constexpr float kTempoIncrement = 10.0f;
39+
constexpr double kInitialTempo = 120.0;
40+
constexpr double kTempoIncrement = 10.0;
4241

4342
} // namespace
4443

@@ -50,7 +49,7 @@ int main(int /*argc*/, char* /*argv*/[]) {
5049
AudioOutput audio_output(kSampleRate, kChannelCount, kFrameCount);
5150

5251
Engine engine(kSampleRate, kFrameCount);
53-
engine.SetControl(EngineControlType::kTempo, kInitialTempo);
52+
engine.SetTempo(kInitialTempo);
5453

5554
auto instrument = engine.CreateInstrument({{
5655
{InstrumentControlType::kGain, kGain},
@@ -129,7 +128,7 @@ int main(int /*argc*/, char* /*argv*/[]) {
129128
return;
130129
}
131130
// Adjust tempo.
132-
double tempo = engine.GetControl<float>(EngineControlType::kTempo);
131+
double tempo = engine.GetTempo();
133132
switch (std::toupper(key)) {
134133
case ' ':
135134
if (performer.IsPlaying()) {
@@ -165,9 +164,8 @@ int main(int /*argc*/, char* /*argv*/[]) {
165164
default:
166165
return;
167166
}
168-
engine.SetControl(EngineControlType::kTempo, tempo);
169-
ConsoleLog() << "Tempo set to " << engine.GetControl<float>(EngineControlType::kTempo)
170-
<< " bpm";
167+
engine.SetTempo(tempo);
168+
ConsoleLog() << "Tempo set to " << engine.GetTempo() << " bpm";
171169
};
172170
input_manager.SetKeyDownCallback(key_down_callback);
173171

examples/demo/trigger_demo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ constexpr float kOscShape = 1.0f;
4141
constexpr float kAttack = 0.0f;
4242
constexpr float kRelease = 0.1f;
4343

44-
constexpr float kInitialTempo = 120.0f;
44+
constexpr double kInitialTempo = 120.0;
4545

4646
} // namespace
4747

@@ -53,7 +53,7 @@ int main(int /*argc*/, char* /*argv*/[]) {
5353
AudioOutput audio_output(kSampleRate, kChannelCount, kFrameCount);
5454

5555
Engine engine(kSampleRate, kFrameCount);
56-
engine.SetControl(barely::EngineControlType::kTempo, kInitialTempo);
56+
engine.SetTempo(kInitialTempo);
5757

5858
auto instrument = engine.CreateInstrument({{
5959
{InstrumentControlType::kGain, kGain},

include/barelymusician.h

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/// barely::Engine engine(/*sample_rate=*/48000, /*max_frame_count=*/512);
1919
///
2020
/// // Set the tempo.
21-
/// engine.SetControl(barely::EngineControlType::kTempo, 124.0f);
21+
/// engine.SetTempo(/*tempo=*/124.0);
2222
///
2323
/// // Update the timestamp.
2424
/// //
@@ -97,7 +97,7 @@
9797
/// BARELY_DEFAULT_REFERENCE_FREQUENCY, &engine);
9898
///
9999
/// // Set the tempo.
100-
/// BarelyEngine_SetControl(engine, BarelyEngineControlType_kTempo, /*tempo=*/124.0f);
100+
/// BarelyEngine_SetTempo(engine, /*tempo=*/124.0);
101101
///
102102
/// // Update the timestamp.
103103
/// //
@@ -237,10 +237,8 @@ typedef enum BarelyArpMode {
237237

238238
/// Engine control types.
239239
typedef enum BarelyEngineControlType {
240-
/// Tempo in beats per minute.
241-
BarelyEngineControlType_kTempo = 0,
242240
/// Compressor mix.
243-
BarelyEngineControlType_kCompressorMix,
241+
BarelyEngineControlType_kCompressorMix = 0,
244242
/// Compressor attack in seconds.
245243
BarelyEngineControlType_kCompressorAttack,
246244
/// Compressor release in seconds.
@@ -528,6 +526,13 @@ BARELY_API bool BarelyEngine_GetControl(BarelyEngineHandle engine, BarelyEngineC
528526
/// @return True if successful, false otherwise.
529527
BARELY_API bool BarelyEngine_GetSeed(BarelyEngineHandle engine, int32_t* out_seed);
530528

529+
/// Gets the tempo of an engine.
530+
///
531+
/// @param engine Engine handle.
532+
/// @param out_tempo Output tempo in beats per minute.
533+
/// @return True if successful, false otherwise.
534+
BARELY_API bool BarelyEngine_GetTempo(BarelyEngineHandle engine, double* out_tempo);
535+
531536
/// Gets the timestamp of an engine.
532537
///
533538
/// @param engine Engine handle.
@@ -563,6 +568,13 @@ BARELY_API bool BarelyEngine_SetControl(BarelyEngineHandle engine, BarelyEngineC
563568
/// @return True if successful, false otherwise.
564569
BARELY_API bool BarelyEngine_SetSeed(BarelyEngineHandle engine, int32_t seed);
565570

571+
/// Sets the tempo of an engine.
572+
///
573+
/// @param engine Engine handle.
574+
/// @param tempo Tempo in beats per minute.
575+
/// @return True if successful, false otherwise.
576+
BARELY_API bool BarelyEngine_SetTempo(BarelyEngineHandle engine, double tempo);
577+
566578
/// Updates an engine at timestamp.
567579
///
568580
/// @param engine Engine handle.
@@ -962,8 +974,6 @@ enum class InstrumentControlType {
962974

963975
/// Engine control types.
964976
enum class EngineControlType {
965-
/// Tempo in beats per minute.
966-
kTempo = BarelyEngineControlType_kTempo,
967977
/// Compressor mix.
968978
kCompressorMix = BarelyEngineControlType_kCompressorMix,
969979
/// Compressor attack in seconds.
@@ -1806,6 +1816,16 @@ class Engine : public HandleWrapper<BarelyEngineHandle> {
18061816
return static_cast<int>(seed);
18071817
}
18081818

1819+
/// Returns the tempo.
1820+
///
1821+
/// @return Tempo in beats per minute.
1822+
[[nodiscard]] double GetTempo() const noexcept {
1823+
double tempo = 0.0;
1824+
[[maybe_unused]] const bool success = BarelyEngine_GetTempo(*this, &tempo);
1825+
assert(success);
1826+
return tempo;
1827+
}
1828+
18091829
/// Returns the timestamp.
18101830
///
18111831
/// @return Timestamp in seconds.
@@ -1849,6 +1869,14 @@ class Engine : public HandleWrapper<BarelyEngineHandle> {
18491869
assert(success);
18501870
}
18511871

1872+
/// Sets the tempo.
1873+
///
1874+
/// @param tempo Tempo in beats per minute.
1875+
void SetTempo(double tempo) noexcept {
1876+
[[maybe_unused]] const bool success = BarelyEngine_SetTempo(*this, tempo);
1877+
assert(success);
1878+
}
1879+
18521880
/// Updates the engine at timestamp.
18531881
///
18541882
/// @param timestamp Timestamp in seconds.

0 commit comments

Comments
 (0)