Skip to content

Commit 088179e

Browse files
committed
fix: checkCurveExclusion signature
1 parent 5310df6 commit 088179e

5 files changed

Lines changed: 73 additions & 52 deletions

File tree

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

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ JSI_PROPERTY_SETTER_IMPL(AudioParamHostObject, value) {
6060
}
6161

6262
JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, setValueAtTime) {
63-
auto event = [param = param_,
64-
value = static_cast<float>(args[0].getNumber()),
65-
startTime = args[1].getNumber()](BaseAudioContext &) {
63+
auto startTime = args[1].getNumber();
64+
controlQueue_.push(AutomationEvent(AutomationEventType::SET_VALUE, startTime));
65+
66+
auto event = [param = param_, value = static_cast<float>(args[0].getNumber()), startTime](
67+
BaseAudioContext &) {
6668
param->setValueAtTime(value, startTime);
6769
};
6870

@@ -71,9 +73,11 @@ JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, setValueAtTime) {
7173
}
7274

7375
JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, linearRampToValueAtTime) {
74-
auto event = [param = param_,
75-
value = static_cast<float>(args[0].getNumber()),
76-
endTime = args[1].getNumber()](BaseAudioContext &) {
76+
auto endTime = args[1].getNumber();
77+
controlQueue_.push(AutomationEvent(AutomationEventType::LINEAR_RAMP, endTime));
78+
79+
auto event = [param = param_, value = static_cast<float>(args[0].getNumber()), endTime](
80+
BaseAudioContext &) {
7781
param->linearRampToValueAtTime(value, endTime);
7882
};
7983

@@ -82,9 +86,11 @@ JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, linearRampToValueAtTime) {
8286
}
8387

8488
JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, exponentialRampToValueAtTime) {
85-
auto event = [param = param_,
86-
value = static_cast<float>(args[0].getNumber()),
87-
endTime = args[1].getNumber()](BaseAudioContext &) {
89+
auto endTime = args[1].getNumber();
90+
controlQueue_.push(AutomationEvent(AutomationEventType::EXPONENTIAL_RAMP, endTime));
91+
92+
auto event = [param = param_, value = static_cast<float>(args[0].getNumber()), endTime](
93+
BaseAudioContext &) {
8894
param->exponentialRampToValueAtTime(value, endTime);
8995
};
9096

@@ -93,9 +99,12 @@ JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, exponentialRampToValueAtTime) {
9399
}
94100

95101
JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, setTargetAtTime) {
102+
auto startTime = args[1].getNumber();
103+
controlQueue_.push(AutomationEvent(AutomationEventType::SET_TARGET, startTime));
104+
96105
auto event = [param = param_,
97106
target = static_cast<float>(args[0].getNumber()),
98-
startTime = args[1].getNumber(),
107+
startTime,
99108
timeConstant = args[2].getNumber()](BaseAudioContext &) {
100109
param->setTargetAtTime(target, startTime, timeConstant);
101110
};
@@ -105,17 +114,18 @@ JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, setTargetAtTime) {
105114
}
106115

107116
JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, setValueCurveAtTime) {
117+
auto startTime = args[1].getNumber();
118+
auto duration = args[2].getNumber();
119+
controlQueue_.push(
120+
AutomationEvent(AutomationEventType::SET_VALUE_CURVE, startTime, startTime + duration));
121+
108122
auto arrayBuffer =
109123
args[0].getObject(runtime).getPropertyAsObject(runtime, "buffer").getArrayBuffer(runtime);
110124
auto *rawValues = reinterpret_cast<float *>(arrayBuffer.data(runtime));
111125
auto length = static_cast<int>(arrayBuffer.size(runtime) / sizeof(float));
112126
auto values = std::make_shared<AudioArray>(rawValues, length);
113127

114-
auto event = [param = param_,
115-
values,
116-
length,
117-
startTime = args[1].getNumber(),
118-
duration = args[2].getNumber()](BaseAudioContext &) {
128+
auto event = [param = param_, values, length, startTime, duration](BaseAudioContext &) {
119129
param->setValueCurveAtTime(values, length, startTime, duration);
120130
};
121131

@@ -124,7 +134,10 @@ JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, setValueCurveAtTime) {
124134
}
125135

126136
JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, cancelScheduledValues) {
127-
auto event = [param = param_, cancelTime = args[0].getNumber()](BaseAudioContext &) {
137+
auto cancelTime = args[0].getNumber();
138+
controlQueue_.cancelScheduledValues(cancelTime);
139+
140+
auto event = [param = param_, cancelTime](BaseAudioContext &) {
128141
param->cancelScheduledValues(cancelTime);
129142
};
130143

@@ -133,7 +146,10 @@ JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, cancelScheduledValues) {
133146
}
134147

135148
JSI_HOST_FUNCTION_IMPL(AudioParamHostObject, cancelAndHoldAtTime) {
136-
auto event = [param = param_, cancelTime = args[0].getNumber()](BaseAudioContext &) {
149+
auto cancelTime = args[0].getNumber();
150+
controlQueue_.cancelScheduledValues(cancelTime);
151+
152+
auto event = [param = param_, cancelTime](BaseAudioContext &) {
137153
param->cancelAndHoldAtTime(cancelTime);
138154
};
139155

@@ -164,24 +180,17 @@ Result<NoneType, std::string> AudioParamHostObject::checkCurveExclusionFromJSI(
164180
const jsi::Value *args) {
165181
auto arg = args[0].getObject(runtime);
166182
auto type = static_cast<AutomationEventType>(arg.getProperty(runtime, "type").getNumber());
167-
168-
switch (type) {
169-
case AutomationEventType::SET_VALUE:
170-
case AutomationEventType::LINEAR_RAMP:
171-
case AutomationEventType::EXPONENTIAL_RAMP:
172-
case AutomationEventType::SET_TARGET: {
173-
auto startTime = arg.getProperty(runtime, "startTime").getNumber();
174-
return controlQueue_.checkCurveExclusion(AutomationEvent(type, startTime));
175-
}
176-
case AutomationEventType::SET_VALUE_CURVE: {
177-
auto startTime = arg.getProperty(runtime, "startTime").getNumber();
178-
auto duration = arg.getProperty(runtime, "duration").getNumber();
179-
return controlQueue_.checkCurveExclusion(
180-
AutomationEvent(type, startTime, startTime + duration));
181-
}
182-
default:
183-
return Ok(None);
183+
auto automationTime = arg.getProperty(runtime, "automationTime").getNumber();
184+
185+
AutomationEvent event;
186+
if (type == AutomationEventType::SET_VALUE_CURVE) {
187+
auto duration = arg.getProperty(runtime, "duration").getNumber();
188+
event = AutomationEvent(type, automationTime, automationTime + duration);
189+
} else {
190+
event = AutomationEvent(type, automationTime);
184191
}
192+
193+
return controlQueue_.checkCurveExclusion(event);
185194
}
186195

187196
} // namespace audioapi

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ void AutomationControlQueue::cancelScheduledValues(double cancelTime) {
4242
}
4343
}
4444

45-
const AutomationEvent *AutomationControlQueue::findEventAtTime(double time) const {
45+
const AutomationEvent *AutomationControlQueue::findEventAtTime(double automationTime) const {
4646
for (const auto &event : eventQueue_) {
47-
if ((event.getType() == AutomationEventType::SET_VALUE_CURVE && event.getStartTime() <= time &&
48-
time <= event.getEndTime()) ||
49-
event.getStartTime() == time) {
47+
if ((event.getType() == AutomationEventType::SET_VALUE_CURVE &&
48+
event.getStartTime() <= automationTime && automationTime <= event.getEndTime()) ||
49+
event.getAutomationEventTime() == automationTime) {
5050
return &event;
5151
}
5252
}

packages/react-native-audio-api/common/cpp/audioapi/core/utils/automation/AutomationEvent.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@ class AutomationEvent {
99
AutomationEvent() = default;
1010
~AutomationEvent() = default;
1111

12-
explicit AutomationEvent(AutomationEventType type, double startTime, double endTime = 0.0)
12+
explicit AutomationEvent(AutomationEventType type, double startTime, double endTime)
1313
: type_(type), startTime_(startTime), endTime_(endTime) {}
1414

15+
/// @brief Construct from a single automationTime value, setting startTime or endTime based on type.
16+
/// Ramp events (LINEAR_RAMP, EXPONENTIAL_RAMP) store automationTime as endTime.
17+
/// All other types store it as startTime.
18+
explicit AutomationEvent(AutomationEventType type, double automationTime)
19+
: type_(type),
20+
startTime_(isRamp(type) ? 0.0 : automationTime),
21+
endTime_(isRamp(type) ? automationTime : 0.0) {}
22+
1523
AutomationEvent(const AutomationEvent &) = delete;
1624
AutomationEvent &operator=(const AutomationEvent &) = delete;
1725

@@ -28,9 +36,7 @@ class AutomationEvent {
2836
}
2937

3038
[[nodiscard]] double getAutomationEventTime() const noexcept {
31-
bool isRamp =
32-
type_ == AutomationEventType::LINEAR_RAMP || type_ == AutomationEventType::EXPONENTIAL_RAMP;
33-
return isRamp ? endTime_ : startTime_;
39+
return isRamp(type_) ? endTime_ : startTime_;
3440
}
3541

3642
[[nodiscard]] double getStartTime() const noexcept {
@@ -53,6 +59,12 @@ class AutomationEvent {
5359
double startTime_ = 0.0;
5460
double endTime_ = 0.0;
5561
AutomationEventType type_ = AutomationEventType::SET_VALUE;
62+
63+
private:
64+
static bool isRamp(AutomationEventType type) noexcept {
65+
return type == AutomationEventType::LINEAR_RAMP ||
66+
type == AutomationEventType::EXPONENTIAL_RAMP;
67+
}
5668
};
5769

5870
} // namespace audioapi

packages/react-native-audio-api/src/core/AudioParam.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default class AudioParam {
3535

3636
const checkExclusionResult = this.audioParam.checkCurveExclusion({
3737
type: AutomationEventType.SET_VALUE,
38-
startTime,
38+
automationTime: startTime,
3939
});
4040

4141
if (checkExclusionResult.status === 'error') {
@@ -57,7 +57,7 @@ export default class AudioParam {
5757

5858
const checkExclusionResult = this.audioParam.checkCurveExclusion({
5959
type: AutomationEventType.LINEAR_RAMP,
60-
endTime,
60+
automationTime: endTime,
6161
});
6262

6363
if (checkExclusionResult.status === 'error') {
@@ -86,7 +86,7 @@ export default class AudioParam {
8686

8787
const checkExclusionResult = this.audioParam.checkCurveExclusion({
8888
type: AutomationEventType.EXPONENTIAL_RAMP,
89-
endTime,
89+
automationTime: endTime,
9090
});
9191

9292
if (checkExclusionResult.status === 'error') {
@@ -118,7 +118,7 @@ export default class AudioParam {
118118

119119
const checkExclusionResult = this.audioParam.checkCurveExclusion({
120120
type: AutomationEventType.SET_TARGET,
121-
startTime,
121+
automationTime: startTime,
122122
});
123123

124124
if (checkExclusionResult.status === 'error') {
@@ -154,7 +154,7 @@ export default class AudioParam {
154154

155155
const checkExclusionResult = this.audioParam.checkCurveExclusion({
156156
type: AutomationEventType.SET_VALUE_CURVE,
157-
startTime,
157+
automationTime: startTime,
158158
duration,
159159
});
160160

packages/react-native-audio-api/src/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,18 @@ export enum AutomationEventType {
259259
}
260260

261261
export type AutomationEventData =
262-
| { type: AutomationEventType.SET_VALUE; startTime: number }
263-
| { type: AutomationEventType.LINEAR_RAMP; endTime: number }
262+
| { type: AutomationEventType.SET_VALUE; automationTime: number }
263+
| { type: AutomationEventType.LINEAR_RAMP; automationTime: number }
264264
| {
265265
type: AutomationEventType.EXPONENTIAL_RAMP;
266-
endTime: number;
266+
automationTime: number;
267267
}
268268
| {
269269
type: AutomationEventType.SET_TARGET;
270-
startTime: number;
270+
automationTime: number;
271271
}
272272
| {
273273
type: AutomationEventType.SET_VALUE_CURVE;
274-
startTime: number;
274+
automationTime: number;
275275
duration: number;
276276
};

0 commit comments

Comments
 (0)