Skip to content

Commit 130c746

Browse files
committed
refactor: naming, header includes
1 parent decc31d commit 130c746

14 files changed

Lines changed: 184 additions & 199 deletions

packages/react-native-audio-api/common/cpp/audioapi/HostObjects/AudioParamHostObject.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#include "audioapi/HostObjects/AudioParamHostObject.h"
1+
#include <audioapi/HostObjects/AudioParamHostObject.h>
2+
#include <audioapi/core/AudioParam.h>
3+
#include <audioapi/core/utils/param/ParamEvent.h>
4+
#include <audioapi/jsi/JsiHostObject.h>
5+
#include <audioapi/utils/AudioArray.hpp>
26
#include <memory>
37
#include <string>
48
#include <utility>
5-
#include "audioapi/core/AudioParam.h"
6-
#include "audioapi/core/utils/automation/AutomationEvent.h"
7-
#include "audioapi/jsi/JsiHostObject.h"
8-
#include "audioapi/utils/AudioArray.hpp"
99

1010
namespace audioapi {
1111

@@ -60,7 +60,7 @@ JSI_PROPERTY_SETTER_IMPL(AudioParamHostObject, value) {
6060
JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, setValueAtTime) {
6161
auto startTime = args[1].getNumber();
6262
controlQueue_.purge(param_->getCurrentTime());
63-
controlQueue_.push(AutomationEvent(AutomationEventType::SET_VALUE, startTime));
63+
controlQueue_.push(ParamEvent(ParamEventType::SET_VALUE, startTime));
6464

6565
auto event = [param = param_, value = static_cast<float>(args[0].getNumber()), startTime](
6666
BaseAudioContext &) {
@@ -74,7 +74,7 @@ JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, setValueAtTime) {
7474
JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, linearRampToValueAtTime) {
7575
auto endTime = args[1].getNumber();
7676
controlQueue_.purge(param_->getCurrentTime());
77-
controlQueue_.push(AutomationEvent(AutomationEventType::LINEAR_RAMP, endTime));
77+
controlQueue_.push(ParamEvent(ParamEventType::LINEAR_RAMP, endTime));
7878

7979
auto event = [param = param_, value = static_cast<float>(args[0].getNumber()), endTime](
8080
BaseAudioContext &) {
@@ -88,7 +88,7 @@ JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, linearRampToValueAtTime) {
8888
JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, exponentialRampToValueAtTime) {
8989
auto endTime = args[1].getNumber();
9090
controlQueue_.purge(param_->getCurrentTime());
91-
controlQueue_.push(AutomationEvent(AutomationEventType::EXPONENTIAL_RAMP, endTime));
91+
controlQueue_.push(ParamEvent(ParamEventType::EXPONENTIAL_RAMP, endTime));
9292

9393
auto event = [param = param_, value = static_cast<float>(args[0].getNumber()), endTime](
9494
BaseAudioContext &) {
@@ -102,7 +102,7 @@ JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, exponentialRampToValueAtTime) {
102102
JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, setTargetAtTime) {
103103
auto startTime = args[1].getNumber();
104104
controlQueue_.purge(param_->getCurrentTime());
105-
controlQueue_.push(AutomationEvent(AutomationEventType::SET_TARGET, startTime));
105+
controlQueue_.push(ParamEvent(ParamEventType::SET_TARGET, startTime));
106106

107107
auto event = [param = param_,
108108
target = static_cast<float>(args[0].getNumber()),
@@ -119,8 +119,7 @@ JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, setValueCurveAtTime) {
119119
auto startTime = args[1].getNumber();
120120
auto duration = args[2].getNumber();
121121
controlQueue_.purge(param_->getCurrentTime());
122-
controlQueue_.push(
123-
AutomationEvent(AutomationEventType::SET_VALUE_CURVE, startTime, startTime + duration));
122+
controlQueue_.push(ParamEvent(ParamEventType::SET_VALUE_CURVE, startTime, startTime + duration));
124123

125124
auto arrayBuffer =
126125
args[0].getObject(runtime).getPropertyAsObject(runtime, "buffer").getArrayBuffer(runtime);
@@ -181,15 +180,15 @@ Result<NoneType, std::string> AudioParamHostObject::checkCurveExclusionFromJSI(
181180
jsi::Runtime &runtime,
182181
const jsi::Value *args) {
183182
auto arg = args[0].getObject(runtime);
184-
auto type = static_cast<AutomationEventType>(arg.getProperty(runtime, "type").getNumber());
183+
auto type = static_cast<ParamEventType>(arg.getProperty(runtime, "type").getNumber());
185184
auto automationTime = arg.getProperty(runtime, "automationTime").getNumber();
186185

187-
AutomationEvent event;
188-
if (type == AutomationEventType::SET_VALUE_CURVE) {
186+
ParamEvent event;
187+
if (type == ParamEventType::SET_VALUE_CURVE) {
189188
auto duration = arg.getProperty(runtime, "duration").getNumber();
190-
event = AutomationEvent(type, automationTime, automationTime + duration);
189+
event = ParamEvent(type, automationTime, automationTime + duration);
191190
} else {
192-
event = AutomationEvent(type, automationTime);
191+
event = ParamEvent(type, automationTime);
193192
}
194193

195194
return controlQueue_.checkCurveExclusion(event);

packages/react-native-audio-api/common/cpp/audioapi/HostObjects/AudioParamHostObject.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#pragma once
22

3+
#include <audioapi/core/utils/param/ParamControlQueue.h>
4+
#include <audioapi/jsi/JsiHostObject.h>
5+
#include <audioapi/utils/Result.hpp>
36
#include <jsi/jsi.h>
47
#include <cstddef>
58
#include <memory>
69
#include <string>
7-
#include "audioapi/core/utils/automation/AutomationControlQueue.h"
8-
#include "audioapi/jsi/JsiHostObject.h"
9-
#include "audioapi/utils/Result.hpp"
1010

1111
namespace audioapi {
1212
using namespace facebook;
@@ -37,7 +37,7 @@ class AudioParamHostObject : public JsiHostObject {
3737
friend class AudioNodeHostObject;
3838

3939
std::shared_ptr<AudioParam> param_;
40-
AutomationControlQueue controlQueue_;
40+
ParamControlQueue controlQueue_;
4141
float defaultValue_;
4242
float minValue_;
4343
float maxValue_;

packages/react-native-audio-api/common/cpp/audioapi/core/AudioParam.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include <audioapi/core/AudioParam.h>
22
#include <audioapi/core/BaseAudioContext.h>
3+
#include <audioapi/core/utils/param/ParamRenderEventFactory.hpp>
34
#include <audioapi/dsp/AudioUtils.hpp>
45
#include <audioapi/dsp/VectorMath.h>
56
#include <audioapi/utils/AudioArray.hpp>
67
#include <memory>
78
#include <utility>
8-
#include "audioapi/core/utils/automation/AutomationRenderEventFactory.hpp"
99

1010
namespace audioapi {
1111

@@ -36,20 +36,19 @@ float AudioParam::getValueAtTime(double time) {
3636
}
3737

3838
void AudioParam::setValueAtTime(float value, double startTime) {
39-
this->updateQueue(AutomationRenderEventFactory::createSetValueEvent(value, startTime));
39+
this->updateQueue(ParamRenderEventFactory::createSetValueEvent(value, startTime));
4040
}
4141

4242
void AudioParam::linearRampToValueAtTime(float value, double endTime) {
43-
this->updateQueue(AutomationRenderEventFactory::createLinearRampEvent(value, endTime));
43+
this->updateQueue(ParamRenderEventFactory::createLinearRampEvent(value, endTime));
4444
}
4545

4646
void AudioParam::exponentialRampToValueAtTime(float value, double endTime) {
47-
this->updateQueue(AutomationRenderEventFactory::createExponentialRampEvent(value, endTime));
47+
this->updateQueue(ParamRenderEventFactory::createExponentialRampEvent(value, endTime));
4848
}
4949

5050
void AudioParam::setTargetAtTime(float target, double startTime, double timeConstant) {
51-
this->updateQueue(
52-
AutomationRenderEventFactory::createSetTargetEvent(target, startTime, timeConstant));
51+
this->updateQueue(ParamRenderEventFactory::createSetTargetEvent(target, startTime, timeConstant));
5352
}
5453

5554
void AudioParam::setValueCurveAtTime(
@@ -58,7 +57,7 @@ void AudioParam::setValueCurveAtTime(
5857
double startTime,
5958
double duration) {
6059
this->updateQueue(
61-
AutomationRenderEventFactory::createSetValueCurveEvent(values, length, startTime, duration));
60+
ParamRenderEventFactory::createSetValueCurveEvent(values, length, startTime, duration));
6261
}
6362

6463
void AudioParam::cancelScheduledValues(double cancelTime) {

packages/react-native-audio-api/common/cpp/audioapi/core/AudioParam.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
#include <audioapi/core/AudioNode.h>
44
#include <audioapi/core/BaseAudioContext.h>
5-
#include <audioapi/core/types/AutomationEventType.h>
6-
#include <audioapi/core/utils/automation/AutomationRenderQueue.h>
7-
#include <audioapi/core/utils/automation/RenderAutomationEvent.hpp>
5+
#include <audioapi/core/types/ParamEventType.h>
6+
#include <audioapi/core/utils/param/ParamRenderQueue.h>
7+
#include <audioapi/core/utils/param/RenderParamEvent.h>
88
#include <audioapi/utils/AudioBuffer.hpp>
99

1010
#include <audioapi/utils/CrossThreadEventScheduler.hpp>
@@ -110,7 +110,7 @@ class AudioParam {
110110
float minValue_;
111111
float maxValue_;
112112

113-
AutomationRenderQueue eventRenderQueue_;
113+
ParamRenderQueue eventRenderQueue_;
114114

115115
// Input modulation system
116116
std::vector<AudioNode *> inputNodes_;
@@ -121,7 +121,7 @@ class AudioParam {
121121
/// @param event The new event to add to the queue.
122122
/// @note Resolves the event's startValue and startTime based on neighboring events,
123123
// and adjusts neighboring events to maintain the invariant of non-overlapping events in the queue.
124-
void updateQueue(RenderAutomationEvent &&event) {
124+
void updateQueue(RenderParamEvent &&event) {
125125
eventRenderQueue_.push(std::move(event));
126126
}
127127

packages/react-native-audio-api/common/cpp/audioapi/core/types/AutomationEventType.h renamed to packages/react-native-audio-api/common/cpp/audioapi/core/types/ParamEventType.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44
#include <string_view>
55
namespace audioapi {
66

7-
enum class AutomationEventType : uint8_t {
7+
enum class ParamEventType : uint8_t {
88
LINEAR_RAMP,
99
EXPONENTIAL_RAMP,
1010
SET_VALUE,
1111
SET_TARGET,
1212
SET_VALUE_CURVE,
1313
};
1414

15-
inline std::string_view toString(AutomationEventType type) {
15+
inline std::string_view toString(ParamEventType type) {
1616
switch (type) {
17-
case AutomationEventType::LINEAR_RAMP:
17+
case ParamEventType::LINEAR_RAMP:
1818
return "LinearRampToValueAtTime";
19-
case AutomationEventType::EXPONENTIAL_RAMP:
19+
case ParamEventType::EXPONENTIAL_RAMP:
2020
return "ExponentialRampToValueAtTime";
21-
case AutomationEventType::SET_VALUE:
21+
case ParamEventType::SET_VALUE:
2222
return "SetValueAtTime";
23-
case AutomationEventType::SET_TARGET:
23+
case ParamEventType::SET_TARGET:
2424
return "SetTargetAtTime";
25-
case AutomationEventType::SET_VALUE_CURVE:
25+
case ParamEventType::SET_VALUE_CURVE:
2626
return "SetValueCurveAtTime";
2727
}
2828
return "Unknown";

packages/react-native-audio-api/common/cpp/audioapi/core/utils/automation/AutomationControlQueue.h

Lines changed: 0 additions & 40 deletions
This file was deleted.

packages/react-native-audio-api/common/cpp/audioapi/core/utils/automation/AutomationControlQueue.cpp renamed to packages/react-native-audio-api/common/cpp/audioapi/core/utils/param/ParamControlQueue.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
1-
#include "audioapi/core/utils/automation/AutomationControlQueue.h"
1+
#include <audioapi/core/types/ParamEventType.h>
2+
#include <audioapi/core/utils/param/ParamControlQueue.h>
3+
#include <audioapi/core/utils/param/ParamEvent.h>
4+
#include <audioapi/utils/Result.hpp>
25
#include <cstddef>
36
#include <sstream>
4-
#include "audioapi/core/types/AutomationEventType.h"
5-
#include "audioapi/core/utils/automation/AutomationEvent.h"
6-
#include "audioapi/utils/Result.hpp"
77

88
namespace audioapi {
99

10-
EventConflictResult AutomationControlQueue::checkCurveExclusion(const AutomationEvent &event) {
11-
if (event.getType() == AutomationEventType::SET_VALUE_CURVE) {
10+
EventConflictResult ParamControlQueue::checkCurveExclusion(const ParamEvent &event) {
11+
if (event.getType() == ParamEventType::SET_VALUE_CURVE) {
1212
// For curve events, check for any event that occurs at or within the curve's time interval
1313
return isConflictInInterval(event, event.getStartTime(), event.getEndTime());
1414
}
1515
// For non-curve events check for curve events that conflict at the event's automationTime
1616
return isConflictAtTime(event, event.getAutomationTime());
1717
}
1818

19-
void AutomationControlQueue::purge(double currentTime) {
19+
void ParamControlQueue::purge(double currentTime) {
2020
eventQueue_.erase(eventQueue_.begin(), eventQueue_.lowerBound(currentTime));
2121
}
2222

23-
EventConflictResult AutomationControlQueue::isConflictAtTime(
24-
const AutomationEvent &newEvent,
23+
EventConflictResult ParamControlQueue::isConflictAtTime(
24+
const ParamEvent &newEvent,
2525
double automationTime) {
2626
// Check if a SET_VALUE_CURVE that starts before automationTime extends into it
2727
auto it = eventQueue_.upperBound(automationTime);
2828
if (it != eventQueue_.begin()) {
2929
const auto &pred = *std::prev(it);
30-
if (pred.getType() == AutomationEventType::SET_VALUE_CURVE &&
31-
automationTime < pred.getEndTime()) {
30+
if (pred.getType() == ParamEventType::SET_VALUE_CURVE && automationTime < pred.getEndTime()) {
3231
std::stringstream ss;
3332
ss << "Cannot schedule event of type " << toString(newEvent.getType()) << " at time "
3433
<< newEvent.getAutomationTime()
@@ -40,12 +39,12 @@ EventConflictResult AutomationControlQueue::isConflictAtTime(
4039
return Ok(None);
4140
}
4241

43-
EventConflictResult AutomationControlQueue::isConflictInInterval(
44-
const AutomationEvent &newEvent,
42+
EventConflictResult ParamControlQueue::isConflictInInterval(
43+
const ParamEvent &newEvent,
4544
double startTime,
4645
double endTime) {
4746
// Non-ramp events have automationTime == startTime, so lowerBound/lowerBound brackets them.
48-
// Ramp events have startTime == 0 (unresolved on the control thread), so getStartTime() >= startTime
47+
// Ramp events have startTime == 0 (unresolved on the control thread), so getStartTime() > startTime
4948
// filters them out.
5049
for (auto it = eventQueue_.lowerBound(startTime), hi = eventQueue_.lowerBound(endTime); it != hi;
5150
++it) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include <audioapi/core/utils/param/ParamEvent.h>
4+
#include <audioapi/core/utils/param/ParamQueueBase.hpp>
5+
#include <audioapi/utils/Result.hpp>
6+
#include <string>
7+
8+
namespace audioapi {
9+
10+
using EventConflictResult = Result<NoneType, std::string>;
11+
12+
/// @brief A queue for managing audio parameter change events on the JS/control thread.
13+
/// @note The invariant of the queue is that its internal buffer always contains non-overlapping events.
14+
class ParamControlQueue : public ParamQueueBase<ParamEvent> {
15+
public:
16+
/// @brief Validate if a new event can be added to the queue without violating curve exclusion rules.
17+
/// See: https://webaudio.github.io/web-audio-api/#automation-event-time
18+
/// @param event The new event to validate.
19+
/// @return Ok if the event can be added, Err with a message if it cannot be added.
20+
[[nodiscard]] EventConflictResult checkCurveExclusion(const ParamEvent &event);
21+
22+
/// @brief Remove all events with automationTime strictly before currentTime.
23+
/// @note Should be called before push to prevent the queue from filling up with past events.
24+
void purge(double currentTime);
25+
26+
private:
27+
/// @brief Check if a non-curve event at the given time conflicts with an existing curve event.
28+
/// @param event The new event being scheduled.
29+
/// @param time The automationTime of the new event.
30+
[[nodiscard]] EventConflictResult isConflictAtTime(const ParamEvent &event, double time);
31+
32+
/// @brief Check if a curve event over [startTime, endTime) conflicts with any existing event.
33+
/// @param event The new curve event being scheduled.
34+
/// @param startTime Start of the curve interval.
35+
/// @param endTime End of the curve interval.
36+
[[nodiscard]] EventConflictResult
37+
isConflictInInterval(const ParamEvent &event, double startTime, double endTime);
38+
};
39+
40+
} // namespace audioapi

0 commit comments

Comments
 (0)