Skip to content

Commit 311cba5

Browse files
committed
simplify api return values
1 parent 3661eff commit 311cba5

14 files changed

Lines changed: 441 additions & 848 deletions

File tree

examples/demo/c_demo.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,14 @@ int main(void) {
8181
// Initialize the engine.
8282
BarelyEngineConfig config = BARELY_ENGINE_CONFIG_DEFAULT(kSampleRate);
8383
config.max_frame_count = kFrameCount;
84-
int32_t allocation_size = 0;
85-
BarelyEngineConfig_GetRequiredAllocationSize(&config, &allocation_size);
84+
const int32_t allocation_size = BarelyEngineConfig_GetRequiredAllocationSize(&config);
8685
printf("Allocating %.2f KB...\n", (float)allocation_size / 1024.0f);
8786
void* allocation = malloc(allocation_size);
8887

89-
BarelyEngine* engine = NULL;
90-
BarelyEngine_Create(&config, allocation, allocation_size, &engine);
88+
BarelyEngine* engine = BarelyEngine_Create(&config, allocation, allocation_size);
9189
BarelyEngine_SetTempo(engine, kTempo);
9290

93-
BarelyEngine_CreateInstrument(engine, &g_instrument_id);
91+
g_instrument_id = BarelyEngine_CreateInstrument(engine);
9492
BarelyInstrument_SetControl(engine, g_instrument_id, BarelyInstrumentControlType_kGain, kGain);
9593
BarelyInstrument_SetControl(engine, g_instrument_id, BarelyInstrumentControlType_kOscMix, 1.0f);
9694
BarelyInstrument_SetControl(engine, g_instrument_id, BarelyInstrumentControlType_kOscShape,
@@ -100,13 +98,11 @@ int main(void) {
10098
BarelyInstrument_SetControl(engine, g_instrument_id, BarelyInstrumentControlType_kRelease,
10199
kRelease);
102100

103-
uint32_t performer_id = 0;
104-
BarelyEngine_CreatePerformer(engine, &performer_id);
105-
uint32_t task_ids[kMelodyNoteCount];
101+
const uint32_t performer_id = BarelyEngine_CreatePerformer(engine);
106102
for (int i = 0; i < kMelodyNoteCount; ++i) {
107103
BarelyPerformer_CreateTask(engine, performer_id, kMelodyPositions[i],
108104
kMelodyPositions[i + 1] - kMelodyPositions[i], 0, TaskCallback,
109-
engine, &task_ids[i]);
105+
engine);
110106
}
111107

112108
// Initialize the audio device.

include/barelymusician.h

Lines changed: 132 additions & 322 deletions
Large diffs are not rendered by default.

platforms/godot/engine.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,7 @@ BarelyEngine::~BarelyEngine() {
9191
engine_ = nullptr;
9292
}
9393

94-
double BarelyEngine::get_timestamp() {
95-
double timestamp = 0.0;
96-
BarelyEngine_GetTimestamp(get(), &timestamp);
97-
return timestamp;
98-
}
94+
double BarelyEngine::get_timestamp() { return BarelyEngine_GetTimestamp(get()); }
9995

10096
void BarelyEngine::set_tempo(double tempo) {
10197
if (tempo_ != tempo) {
@@ -108,11 +104,10 @@ ::BarelyEngine* BarelyEngine::get() {
108104
if (engine_ == nullptr) {
109105
sample_rate_ = static_cast<int32_t>(AudioServer::get_singleton()->get_mix_rate());
110106
const BarelyEngineConfig config = BARELY_ENGINE_CONFIG_DEFAULT(sample_rate_);
111-
int32_t allocation_size = 0;
112-
BarelyEngineConfig_GetRequiredAllocationSize(&config, &allocation_size);
107+
const int32_t allocation_size = BarelyEngineConfig_GetRequiredAllocationSize(&config);
113108
engine_allocation_.resize(allocation_size);
114109
temp_samples_.resize(config.max_frame_count);
115-
BarelyEngine_Create(&config, engine_allocation_.data(), allocation_size, &engine_);
110+
engine_ = BarelyEngine_Create(&config, engine_allocation_.data(), allocation_size);
116111
BARELY_GODOT_ENGINE_CONTROLS(BARELY_SET_DEFAULT_GODOT_ENGINE_CONTROL);
117112
if (SceneTree* tree = Object::cast_to<SceneTree>(Engine::get_singleton()->get_main_loop())) {
118113
if (audio_player_ == nullptr) { // start audio processing

platforms/godot/instrument.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void BarelySliceResource::_bind_methods() {
7171

7272
BarelyInstrument::BarelyInstrument() {
7373
::BarelyEngine* engine = BarelyEngine::get_singleton()->get();
74-
BarelyEngine_CreateInstrument(engine, &instrument_id_);
74+
instrument_id_ = BarelyEngine_CreateInstrument(engine);
7575
BARELY_GODOT_INSTRUMENT_CONTROLS(BARELY_SET_DEFAULT_GODOT_INSTRUMENT_CONTROL);
7676
}
7777

platforms/godot/performer.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void BarelyTaskResource::_bind_methods() {
5353
}
5454

5555
BarelyPerformer::BarelyPerformer() {
56-
BarelyEngine_CreatePerformer(BarelyEngine::get_singleton()->get(), &performer_id_);
56+
performer_id_ = BarelyEngine_CreatePerformer(BarelyEngine::get_singleton()->get());
5757
tasks_ = TypedArray<Ref<BarelyTaskResource>>();
5858
}
5959

@@ -113,9 +113,7 @@ void BarelyPerformer::set_tasks(const TypedArray<Ref<BarelyTaskResource>>& tasks
113113
}
114114

115115
double BarelyPerformer::get_position() const {
116-
double position = 0.0;
117-
BarelyPerformer_GetPosition(BarelyEngine::get_singleton()->get(), performer_id_, &position);
118-
return position;
116+
return BarelyPerformer_GetPosition(BarelyEngine::get_singleton()->get(), performer_id_);
119117
}
120118

121119
void BarelyPerformer::_bind_methods() {
@@ -166,24 +164,19 @@ void BarelyPerformer::_on_task_changed() {
166164
if (task.is_null()) {
167165
continue;
168166
}
169-
170-
uint32_t task_id = 0;
171-
BarelyPerformer_CreateTask(
167+
task_ids_.push_back(BarelyPerformer_CreateTask(
172168
BarelyEngine::get_singleton()->get(), performer_id_, task->get_position(),
173169
task->get_duration(), task->get_priority(),
174170
[](BarelyTaskEventType type, void* user_data) {
175171
BarelyTaskResource* task = static_cast<BarelyTaskResource*>(user_data);
176-
177172
if (!task) return;
178-
179173
if (type == BarelyTaskEventType_kBegin) {
180174
task->emit_signal("task_begin");
181175
} else if (type == BarelyTaskEventType_kEnd) {
182176
task->emit_signal("task_end");
183177
}
184178
},
185-
task.ptr(), &task_id);
186-
task_ids_.push_back(task_id);
179+
task.ptr()));
187180
}
188181
}
189182

platforms/godot/resources.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ void BarelyQuantizationResource::set_amount(float amount) {
2525
}
2626

2727
double BarelyQuantizationResource::get_position(double position) const {
28-
double quantized_position = 0.0;
29-
if (!BarelyQuantization_GetPosition(&quantization_, position, &quantized_position)) {
30-
return position;
31-
}
32-
return quantized_position;
28+
return BarelyQuantization_GetPosition(&quantization_, position);
3329
}
3430

3531
void BarelyQuantizationResource::_bind_methods() {
@@ -68,11 +64,7 @@ void BarelyScaleResource::set_mode(int32_t mode) {
6864
}
6965

7066
float BarelyScaleResource::get_pitch(int32_t degree) const {
71-
float pitch = 0.0f;
72-
if (!BarelyScale_GetPitch(&scale_, degree, &pitch)) {
73-
return 0.0f;
74-
}
75-
return pitch;
67+
return BarelyScale_GetPitch(&scale_, degree);
7668
}
7769

7870
void BarelyScaleResource::_bind_methods() {

0 commit comments

Comments
 (0)