Skip to content

Commit decc31d

Browse files
committed
fix: review comments
1 parent 6121afb commit decc31d

14 files changed

Lines changed: 80 additions & 115 deletions

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
#include <audioapi/HostObjects/AudioParamHostObject.h>
2-
3-
#include <audioapi/core/AudioParam.h>
4-
#include <audioapi/core/utils/automation/AutomationEvent.hpp>
5-
#include <audioapi/utils/AudioArray.hpp>
6-
1+
#include "audioapi/HostObjects/AudioParamHostObject.h"
72
#include <memory>
83
#include <string>
94
#include <utility>
5+
#include "audioapi/core/AudioParam.h"
6+
#include "audioapi/core/utils/automation/AutomationEvent.h"
107
#include "audioapi/jsi/JsiHostObject.h"
8+
#include "audioapi/utils/AudioArray.hpp"
119

1210
namespace audioapi {
1311

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

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

3-
#include <audioapi/jsi/JsiHostObject.h>
4-
53
#include <jsi/jsi.h>
64
#include <cstddef>
75
#include <memory>
86
#include <string>
97
#include "audioapi/core/utils/automation/AutomationControlQueue.h"
8+
#include "audioapi/jsi/JsiHostObject.h"
109
#include "audioapi/utils/Result.hpp"
1110

1211
namespace audioapi {
@@ -32,7 +31,6 @@ class AudioParamHostObject : public JsiHostObject {
3231
JSI_HOST_FUNCTION_DECL(setValueCurveAtTime);
3332
JSI_HOST_FUNCTION_DECL(cancelScheduledValues);
3433
JSI_HOST_FUNCTION_DECL(cancelAndHoldAtTime);
35-
3634
JSI_HOST_FUNCTION_DECL(checkCurveExclusion);
3735

3836
private:

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#pragma once
22

3+
#include <cstdint>
34
#include <string_view>
45
namespace audioapi {
56

6-
enum class AutomationEventType {
7+
enum class AutomationEventType : uint8_t {
78
LINEAR_RAMP,
89
EXPONENTIAL_RAMP,
910
SET_VALUE,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include <cmath>
4-
#include <cstddef>
54
#include <limits>
65
#include <new>
76
#include <numbers>
Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,63 @@
11
#include "audioapi/core/utils/automation/AutomationControlQueue.h"
22
#include <cstddef>
33
#include <sstream>
4-
#include <string>
54
#include "audioapi/core/types/AutomationEventType.h"
6-
#include "audioapi/core/utils/automation/AutomationEvent.hpp"
5+
#include "audioapi/core/utils/automation/AutomationEvent.h"
76
#include "audioapi/utils/Result.hpp"
87

98
namespace audioapi {
109

11-
Result<NoneType, std::string> AutomationControlQueue::checkCurveExclusion(
12-
const AutomationEvent &event) {
10+
EventConflictResult AutomationControlQueue::checkCurveExclusion(const AutomationEvent &event) {
1311
if (event.getType() == AutomationEventType::SET_VALUE_CURVE) {
1412
// For curve events, check for any event that occurs at or within the curve's time interval
15-
const auto *conflict = findEventInInterval(event.getStartTime(), event.getEndTime());
16-
if (conflict != nullptr) {
17-
std::stringstream ss;
18-
ss << "Cannot schedule curve event from time " << event.getStartTime() << " to "
19-
<< event.getEndTime() << " because it conflicts with an existing event of type "
20-
<< toString(conflict->getType()) << " at time " << conflict->getAutomationTime() << ".";
21-
return Err(ss.str());
22-
}
23-
} else {
24-
// For non-curve events check for curve events that conflict at the event's automationTime
25-
const auto *conflict = findEventAtTime(event.getAutomationTime());
26-
if ((conflict != nullptr) && conflict->getType() == AutomationEventType::SET_VALUE_CURVE) {
27-
std::stringstream ss;
28-
ss << "Cannot schedule event of type " << toString(event.getType()) << " at time "
29-
<< event.getAutomationTime()
30-
<< " because it conflicts with an existing curve event from time "
31-
<< conflict->getStartTime() << " to " << conflict->getEndTime() << ".";
32-
return Err(ss.str());
33-
}
13+
return isConflictInInterval(event, event.getStartTime(), event.getEndTime());
3414
}
35-
return Ok(None);
15+
// For non-curve events check for curve events that conflict at the event's automationTime
16+
return isConflictAtTime(event, event.getAutomationTime());
3617
}
3718

3819
void AutomationControlQueue::purge(double currentTime) {
3920
eventQueue_.erase(eventQueue_.begin(), eventQueue_.lowerBound(currentTime));
4021
}
4122

42-
const AutomationEvent *AutomationControlQueue::findEventAtTime(double automationTime) {
23+
EventConflictResult AutomationControlQueue::isConflictAtTime(
24+
const AutomationEvent &newEvent,
25+
double automationTime) {
4326
// Check if a SET_VALUE_CURVE that starts before automationTime extends into it
4427
auto it = eventQueue_.upperBound(automationTime);
4528
if (it != eventQueue_.begin()) {
4629
const auto &pred = *std::prev(it);
4730
if (pred.getType() == AutomationEventType::SET_VALUE_CURVE &&
48-
automationTime <= pred.getEndTime()) {
49-
return &pred;
31+
automationTime < pred.getEndTime()) {
32+
std::stringstream ss;
33+
ss << "Cannot schedule event of type " << toString(newEvent.getType()) << " at time "
34+
<< newEvent.getAutomationTime()
35+
<< " because it conflicts with an existing curve event from time " << pred.getStartTime()
36+
<< " to " << pred.getEndTime() << ".";
37+
return Err(ss.str());
5038
}
5139
}
52-
53-
// Check for an event with an exact automationTime match
54-
auto lo = eventQueue_.lowerBound(automationTime);
55-
if (lo != it) {
56-
return &(*lo);
57-
}
58-
59-
return nullptr;
40+
return Ok(None);
6041
}
6142

62-
const AutomationEvent *AutomationControlQueue::findEventInInterval(
43+
EventConflictResult AutomationControlQueue::isConflictInInterval(
44+
const AutomationEvent &newEvent,
6345
double startTime,
6446
double endTime) {
6547
// Non-ramp events have automationTime == startTime, so lowerBound/lowerBound brackets them.
6648
// Ramp events have startTime == 0 (unresolved on the control thread), so getStartTime() >= startTime
6749
// filters them out.
6850
for (auto it = eventQueue_.lowerBound(startTime), hi = eventQueue_.lowerBound(endTime); it != hi;
6951
++it) {
70-
if (it->getStartTime() >= startTime) {
71-
return &(*it);
52+
if (it->getStartTime() > startTime) {
53+
std::stringstream ss;
54+
ss << "Cannot schedule curve event from time " << newEvent.getStartTime() << " to "
55+
<< newEvent.getEndTime() << " because it conflicts with an existing event of type "
56+
<< toString(it->getType()) << " at time " << it->getAutomationTime() << ".";
57+
return Err(ss.str());
7258
}
7359
}
74-
return nullptr;
60+
return Ok(None);
7561
}
7662

7763
} // namespace audioapi
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
#pragma once
22

3-
#include <audioapi/core/types/AutomationEventType.h>
4-
#include <audioapi/core/utils/automation/AutomationEvent.hpp>
5-
#include <audioapi/core/utils/automation/AutomationQueueBase.hpp>
6-
#include <audioapi/utils/Result.hpp>
73
#include <string>
4+
#include "audioapi/core/utils/automation/AutomationEvent.h"
5+
#include "audioapi/core/utils/automation/AutomationQueueBase.hpp"
6+
#include "audioapi/utils/Result.hpp"
87

98
namespace audioapi {
109

10+
using EventConflictResult = Result<NoneType, std::string>;
11+
1112
/// @brief A queue for managing audio parameter change events on the JS/control thread.
1213
/// @note The invariant of the queue is that its internal buffer always contains non-overlapping events.
1314
class AutomationControlQueue : public AutomationQueueBase<AutomationEvent> {
1415
public:
15-
explicit AutomationControlQueue() = default;
16-
1716
/// @brief Validate if a new event can be added to the queue without violating curve exclusion rules.
1817
/// See: https://webaudio.github.io/web-audio-api/#automation-event-time
1918
/// @param event The new event to validate.
2019
/// @return Ok if the event can be added, Err with a message if it cannot be added.
21-
[[nodiscard]] Result<NoneType, std::string> checkCurveExclusion(const AutomationEvent &event);
20+
[[nodiscard]] EventConflictResult checkCurveExclusion(const AutomationEvent &event);
2221

2322
/// @brief Remove all events with automationTime strictly before currentTime.
2423
/// @note Should be called before push to prevent the queue from filling up with past events.
@@ -28,13 +27,14 @@ class AutomationControlQueue : public AutomationQueueBase<AutomationEvent> {
2827
/// @brief Find an event that occurs at the given time. For curve events, checks if time is within the event's interval.
2928
/// @param time The time to check for an event.
3029
/// @return A pointer to the conflicting event, or nullptr if no conflict.
31-
[[nodiscard]] const AutomationEvent *findEventAtTime(double time);
30+
[[nodiscard]] EventConflictResult isConflictAtTime(const AutomationEvent &event, double time);
3231

3332
/// @brief Find an event that starts within the given time interval.
3433
/// @param startTime The start time of the interval.
3534
/// @param endTime The end time of the interval.
3635
/// @return A pointer to the conflicting event, or nullptr if no conflict.
37-
[[nodiscard]] const AutomationEvent *findEventInInterval(double startTime, double endTime);
36+
[[nodiscard]] EventConflictResult
37+
isConflictInInterval(const AutomationEvent &event, double startTime, double endTime);
3838
};
3939

4040
} // namespace audioapi

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

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

3-
#include <audioapi/core/types/AutomationEventType.h>
3+
#include "audioapi/core/types/AutomationEventType.h"
44

55
namespace audioapi {
66

@@ -22,8 +22,8 @@ class AutomationEvent {
2222
startTime_(isRamp(type) ? 0.0 : automationTime),
2323
endTime_(isRamp(type) ? automationTime : 0.0) {}
2424

25-
AutomationEvent(const AutomationEvent &) = delete;
26-
AutomationEvent &operator=(const AutomationEvent &) = delete;
25+
AutomationEvent(const AutomationEvent &) = default;
26+
AutomationEvent &operator=(const AutomationEvent &) = default;
2727

2828
AutomationEvent(AutomationEvent &&other) noexcept
2929
: type_(other.type_), startTime_(other.startTime_), endTime_(other.endTime_) {}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <utility>
44
#include "audioapi/core/utils/Constants.h"
55
#include "audioapi/utils/BoundedPriorityQueue.hpp"
6+
#include "audioapi/utils/Macros.h"
67

78
namespace audioapi {
89

@@ -14,10 +15,7 @@ template <AutomationEventConcept TEvent>
1415
class AutomationQueueBase {
1516
public:
1617
AutomationQueueBase() = default;
17-
AutomationQueueBase(const AutomationQueueBase &) = delete;
18-
AutomationQueueBase &operator=(const AutomationQueueBase &) = delete;
19-
AutomationQueueBase(AutomationQueueBase &&) noexcept = delete;
20-
AutomationQueueBase &operator=(AutomationQueueBase &&) noexcept = delete;
18+
DELETE_COPY_AND_MOVE(AutomationQueueBase);
2119
virtual ~AutomationQueueBase() = default;
2220

2321
/// @brief Cancel scheduled parameter changes at or after the given time.

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#include <audioapi/core/utils/automation/AutomationRenderEventFactory.hpp>
2-
#include <audioapi/core/utils/automation/AutomationRenderQueue.h>
1+
#include "audioapi/core/utils/automation/AutomationRenderQueue.h"
32
#include <cstddef>
43
#include <optional>
54
#include <utility>
65
#include "audioapi/core/types/AutomationEventType.h"
6+
#include "audioapi/core/utils/automation/AutomationQueueBase.hpp"
7+
#include "audioapi/core/utils/automation/AutomationRenderEventFactory.hpp"
78

89
namespace audioapi {
910

@@ -31,7 +32,7 @@ std::optional<float> AutomationRenderQueue::computeValueAtTime(double time) {
3132

3233
bool AutomationRenderQueue::push(RenderAutomationEvent &&event) {
3334
resolveEventValues(event);
34-
return eventQueue_.push(std::move(event));
35+
return AutomationQueueBase::push(std::move(event));
3536
}
3637

3738
void AutomationRenderQueue::resolveEventValues(RenderAutomationEvent &event) {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#pragma once
22

3-
#include <audioapi/core/types/AutomationEventType.h>
4-
#include <audioapi/core/utils/automation/AutomationQueueBase.hpp>
5-
#include <audioapi/core/utils/automation/RenderAutomationEvent.hpp>
63
#include <optional>
4+
#include "audioapi/core/utils/automation/AutomationQueueBase.hpp"
5+
#include "audioapi/core/utils/automation/RenderAutomationEvent.hpp"
76

87
namespace audioapi {
98

0 commit comments

Comments
 (0)