Skip to content

Commit 1158c84

Browse files
maciejmakowski2003Maciej Makowski
andauthored
Fix/memory manage improvements (#378)
* chore: .clang-format refactor * refactor: examples refactoring * fix: memory management * fix: fixed small nitpicks --------- Co-authored-by: Maciej Makowski <maciej.makowski2608@gmail.com>
1 parent 306643b commit 1158c84

File tree

14 files changed

+147
-211
lines changed

14 files changed

+147
-211
lines changed

apps/common-app/src/examples/DrumMachine/DrumMachine.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ const DrumMachine: React.FC = () => {
136136
label="BPM"
137137
step={1}
138138
min={24}
139-
max={320}
139+
max={2000}
140140
value={bpm}
141141
onValueChange={setBpm}
142142
/>
@@ -148,7 +148,8 @@ const DrumMachine: React.FC = () => {
148148
// @ts-ignore ???
149149
width={size}
150150
height={size}
151-
onLayout={onCanvasLayout}>
151+
onLayout={onCanvasLayout}
152+
>
152153
<Grid />
153154
{patterns.map((pattern) => (
154155
<PatternShape key={pattern.instrumentName} pattern={pattern} />

apps/common-app/src/examples/DrumMachine/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const padding = 24;
99
export const maxSize =
1010
Math.min(screenSize.width, screenSize.height) - padding * 2;
1111

12-
export const initialBpm = 120;
12+
export const initialBpm = 240;
1313
export const numBeats = 16;
1414
export const noteDensity = 2;
1515

apps/common-app/src/utils/usePlayer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export default function usePlayer(options: PlayerOptions) {
137137
}
138138

139139
return () => {
140+
playingInstruments.value = getPlayingInstruments();
140141
audioContext.close();
141142
};
142143

packages/react-native-audio-api/.clang-format

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ AlignAfterOpenBracket: AlwaysBreak
44
AlignConsecutiveAssignments: false
55
AlignConsecutiveDeclarations: false
66
AlignEscapedNewlinesLeft: true
7-
AlignOperands: false
7+
AlignOperands: false
88
AlignTrailingComments: false
99
AllowAllParametersOfDeclarationOnNextLine: false
1010
AllowShortBlocksOnASingleLine: false
@@ -18,45 +18,45 @@ AlwaysBreakTemplateDeclarations: true
1818
BinPackArguments: false
1919
BinPackParameters: false
2020
BraceWrapping:
21-
AfterClass: false
21+
AfterClass: false
2222
AfterControlStatement: false
23-
AfterEnum: false
24-
AfterFunction: false
25-
AfterNamespace: false
23+
AfterEnum: false
24+
AfterFunction: false
25+
AfterNamespace: false
2626
AfterObjCDeclaration: false
27-
AfterStruct: false
28-
AfterUnion: false
29-
BeforeCatch: false
30-
BeforeElse: false
31-
IndentBraces: false
27+
AfterStruct: false
28+
AfterUnion: false
29+
BeforeCatch: false
30+
BeforeElse: false
31+
IndentBraces: false
3232
BreakBeforeBinaryOperators: None
3333
BreakBeforeBraces: Attach
3434
BreakBeforeTernaryOperators: true
3535
BreakConstructorInitializersBeforeComma: false
3636
BreakAfterJavaFieldAnnotations: false
3737
BreakStringLiterals: false
38-
ColumnLimit: 80
39-
CommentPragmas: '^ IWYU pragma:'
38+
ColumnLimit: 80
39+
CommentPragmas: '^ IWYU pragma:'
4040
ConstructorInitializerAllOnOneLineOrOnePerLine: true
4141
ConstructorInitializerIndentWidth: 4
4242
ContinuationIndentWidth: 4
4343
Cpp11BracedListStyle: true
4444
DerivePointerAlignment: false
45-
DisableFormat: false
46-
ForEachMacros: [ FOR_EACH_RANGE, FOR_EACH, ]
45+
DisableFormat: false
46+
ForEachMacros: [FOR_EACH_RANGE, FOR_EACH]
4747
IncludeCategories:
48-
- Regex: '^<.*\.h(pp)?>'
49-
Priority: 1
50-
- Regex: '^<.*'
51-
Priority: 2
52-
- Regex: '.*'
53-
Priority: 3
48+
- Regex: '^<.*\.h(pp)?>'
49+
Priority: 1
50+
- Regex: '^<.*'
51+
Priority: 2
52+
- Regex: '.*'
53+
Priority: 3
5454
IndentCaseLabels: true
55-
IndentWidth: 2
55+
IndentWidth: 2
5656
IndentWrappedFunctionNames: false
5757
KeepEmptyLinesAtTheStartOfBlocks: false
5858
MacroBlockBegin: ''
59-
MacroBlockEnd: ''
59+
MacroBlockEnd: ''
6060
MaxEmptyLinesToKeep: 1
6161
NamespaceIndentation: None
6262
ObjCBlockIndentWidth: 2
@@ -69,23 +69,22 @@ PenaltyBreakString: 1000
6969
PenaltyExcessCharacter: 1000000
7070
PenaltyReturnTypeOnItsOwnLine: 200
7171
PointerAlignment: Right
72-
ReflowComments: true
73-
SortIncludes: true
72+
ReflowComments: true
73+
SortIncludes: true
7474
SpaceAfterCStyleCast: false
7575
SpaceBeforeAssignmentOperators: true
7676
SpaceBeforeParens: ControlStatements
7777
SpaceInEmptyParentheses: false
7878
SpacesBeforeTrailingComments: 1
79-
SpacesInAngles: false
79+
SpacesInAngles: false
8080
SpacesInContainerLiterals: true
8181
SpacesInCStyleCastParentheses: false
8282
SpacesInParentheses: false
8383
SpacesInSquareBrackets: false
84-
Standard: Cpp11
85-
TabWidth: 4
86-
UseTab: Never
84+
Standard: Cpp11
85+
TabWidth: 2
86+
UseTab: Never
8787
---
8888
Language: ObjC
89-
ColumnLimit: 120
89+
ColumnLimit: 120
9090
BreakBeforeBraces: WebKit
91-
...

packages/react-native-audio-api/android/src/main/cpp/audioapi/android/core/AudioDecoder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ std::shared_ptr<AudioBus> AudioDecoder::decodeWithFilePath(
3636

3737
ma_uint64 framesDecoded;
3838
ma_decoder_read_pcm_frames(&decoder, buffer, totalFrameCount, &framesDecoded);
39+
3940
if (framesDecoded == 0) {
4041
__android_log_print(ANDROID_LOG_ERROR, "AudioDecoder", "Failed to decode");
4142

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

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ AudioNode::AudioNode(BaseAudioContext *context) : context_(context) {
1212
}
1313

1414
AudioNode::~AudioNode() {
15-
isInitialized_ = false;
15+
if (isInitialized_) {
16+
cleanup();
17+
}
1618
}
1719

1820
int AudioNode::getNumberOfInputs() const {
@@ -41,8 +43,9 @@ void AudioNode::connect(const std::shared_ptr<AudioNode> &node) {
4143
}
4244

4345
void AudioNode::disconnect() {
44-
for (auto &outputNode : outputNodes_) {
45-
disconnect(outputNode);
46+
for (auto it = outputNodes_.begin(), end = outputNodes_.end(); it != end;
47+
++it) {
48+
disconnect(*it);
4649
}
4750
}
4851

@@ -56,18 +59,28 @@ bool AudioNode::isEnabled() const {
5659
}
5760

5861
void AudioNode::enable() {
62+
if (isEnabled()) {
63+
return;
64+
}
65+
5966
isEnabled_ = true;
6067

61-
for (auto &outputNode : outputNodes_) {
62-
outputNode->onInputEnabled();
68+
for (auto it = outputNodes_.begin(), end = outputNodes_.end(); it != end;
69+
++it) {
70+
it->get()->onInputEnabled();
6371
}
6472
}
6573

6674
void AudioNode::disable() {
75+
if (!isEnabled()) {
76+
return;
77+
}
78+
6779
isEnabled_ = false;
6880

69-
for (auto &outputNode : outputNodes_) {
70-
outputNode->onInputDisabled();
81+
for (auto it = outputNodes_.begin(), end = outputNodes_.end(); it != end;
82+
++it) {
83+
it->get()->onInputDisabled();
7184
}
7285
}
7386

@@ -96,7 +109,7 @@ std::string AudioNode::toString(ChannelInterpretation interpretation) {
96109
}
97110

98111
std::shared_ptr<AudioBus> AudioNode::processAudio(
99-
std::shared_ptr<AudioBus> outputBus,
112+
const std::shared_ptr<AudioBus> &outputBus,
100113
int framesToProcess,
101114
bool checkIsAlreadyProcessed) {
102115
if (!isInitialized_) {
@@ -148,7 +161,9 @@ std::shared_ptr<AudioBus> AudioNode::processInputs(
148161
processingBus->zero();
149162

150163
int maxNumberOfChannels = 0;
151-
for (auto inputNode : inputNodes_) {
164+
for (auto it = inputNodes_.begin(), end = inputNodes_.end(); it != end;
165+
++it) {
166+
auto inputNode = *it;
152167
assert(inputNode != nullptr);
153168

154169
if (!inputNode->isEnabled()) {
@@ -169,7 +184,7 @@ std::shared_ptr<AudioBus> AudioNode::processInputs(
169184
}
170185

171186
std::shared_ptr<AudioBus> AudioNode::applyChannelCountMode(
172-
std::shared_ptr<AudioBus> processingBus) {
187+
const std::shared_ptr<AudioBus> &processingBus) {
173188
// If the channelCountMode is EXPLICIT, the node should output the number of
174189
// channels specified by the channelCount.
175190
if (channelCountMode_ == ChannelCountMode::EXPLICIT) {
@@ -189,21 +204,25 @@ std::shared_ptr<AudioBus> AudioNode::applyChannelCountMode(
189204
void AudioNode::mixInputsBuses(const std::shared_ptr<AudioBus> &processingBus) {
190205
assert(processingBus != nullptr);
191206

192-
for (const auto &inputBus : inputBuses_) {
193-
processingBus->sum(inputBus.get(), channelInterpretation_);
207+
for (auto it = inputBuses_.begin(), end = inputBuses_.end(); it != end;
208+
++it) {
209+
processingBus->sum(it->get(), channelInterpretation_);
194210
}
195211

196212
inputBuses_.clear();
197213
}
198214

199215
void AudioNode::connectNode(const std::shared_ptr<AudioNode> &node) {
200-
outputNodes_.insert(node);
201-
node->onInputConnected(this);
216+
auto position = outputNodes_.find(node);
217+
218+
if (position == outputNodes_.end()) {
219+
outputNodes_.insert(node);
220+
node->onInputConnected(this);
221+
}
202222
}
203223

204224
void AudioNode::disconnectNode(const std::shared_ptr<AudioNode> &node) {
205225
outputNodes_.erase(node);
206-
node->onInputDisconnected(this);
207226
}
208227

209228
void AudioNode::onInputEnabled() {
@@ -239,30 +258,26 @@ void AudioNode::onInputDisconnected(AudioNode *node) {
239258
return;
240259
}
241260

242-
inputNodes_.erase(node);
261+
if (node->isEnabled()) {
262+
onInputDisabled();
263+
}
243264

244-
if (isEnabled()) {
245-
node->onInputDisabled();
265+
auto position = inputNodes_.find(node);
266+
267+
if (position != inputNodes_.end()) {
268+
inputNodes_.erase(position);
246269
}
247270
}
248271

249272
void AudioNode::cleanup() {
250273
isInitialized_ = false;
251274

252-
for (const auto &inputNode : inputNodes_) {
253-
if (inputNode) {
254-
inputNode->disconnectNode(shared_from_this());
255-
}
256-
}
257-
258-
for (const auto &outputNode : outputNodes_) {
259-
if (outputNode) {
260-
outputNode->onInputDisconnected(this);
261-
}
275+
for (auto it = outputNodes_.begin(), end = outputNodes_.end(); it != end;
276+
++it) {
277+
it->get()->onInputDisconnected(this);
262278
}
263279

264280
outputNodes_.clear();
265-
inputNodes_.clear();
266281
}
267282

268283
} // namespace audioapi

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,17 @@ class AudioNode : public std::enable_shared_from_this<AudioNode> {
5858
std::size_t lastRenderedFrame_{SIZE_MAX};
5959

6060
private:
61-
friend class StretcherNode;
62-
6361
std::vector<std::shared_ptr<AudioBus>> inputBuses_ = {};
6462

6563
static std::string toString(ChannelCountMode mode);
6664
static std::string toString(ChannelInterpretation interpretation);
6765

68-
virtual std::shared_ptr<AudioBus> processAudio(std::shared_ptr<AudioBus> outputBus, int framesToProcess, bool checkIsAlreadyProcessed);
66+
virtual std::shared_ptr<AudioBus> processAudio(const std::shared_ptr<AudioBus> &outputBus, int framesToProcess, bool checkIsAlreadyProcessed);
6967
virtual void processNode(const std::shared_ptr<AudioBus>&, int) = 0;
7068

7169
bool isAlreadyProcessed();
7270
std::shared_ptr<AudioBus> processInputs(const std::shared_ptr<AudioBus>& outputBus, int framesToProcess, bool checkIsAlreadyProcessed);
73-
std::shared_ptr<AudioBus> applyChannelCountMode(std::shared_ptr<AudioBus> processingBus);
71+
std::shared_ptr<AudioBus> applyChannelCountMode(const std::shared_ptr<AudioBus> &processingBus);
7472
void mixInputsBuses(const std::shared_ptr<AudioBus>& processingBus);
7573

7674
void connectNode(const std::shared_ptr<AudioNode> &node);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ PeriodicWave::~PeriodicWave() {
7272
delete[] bandLimitedTables_[i];
7373
}
7474
delete[] bandLimitedTables_;
75+
bandLimitedTables_ = nullptr;
7576
}
7677

7778
int PeriodicWave::getPeriodicWaveSize() const {

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,23 @@ std::mutex &AudioBufferSourceNode::getBufferLock() {
144144
void AudioBufferSourceNode::processNode(
145145
const std::shared_ptr<AudioBus> &processingBus,
146146
int framesToProcess) {
147-
if (!Locker::tryLock(getBufferLock())) {
148-
processingBus->zero();
149-
return;
150-
}
147+
if (auto locker = Locker::tryLock(getBufferLock())) {
148+
// No audio data to fill, zero the output and return.
149+
if (!alignedBus_) {
150+
processingBus->zero();
151+
return;
152+
}
151153

152-
// No audio data to fill, zero the output and return.
153-
if (!alignedBus_) {
154-
processingBus->zero();
155-
return;
156-
}
154+
if (!pitchCorrection_) {
155+
processWithoutPitchCorrection(processingBus, framesToProcess);
156+
} else {
157+
processWithPitchCorrection(processingBus, framesToProcess);
158+
}
157159

158-
if (!pitchCorrection_) {
159-
processWithoutPitchCorrection(processingBus, framesToProcess);
160+
handleStopScheduled();
160161
} else {
161-
processWithPitchCorrection(processingBus, framesToProcess);
162+
processingBus->zero();
162163
}
163-
164-
handleStopScheduled();
165164
}
166165

167166
double AudioBufferSourceNode::getStopTime() const {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ OscillatorNode::OscillatorNode(BaseAudioContext *context)
1616
1200 * LOG2_MOST_POSITIVE_SINGLE_FLOAT);
1717
type_ = OscillatorType::SINE;
1818
periodicWave_ = context_->getBasicWaveForm(type_);
19+
1920
isInitialized_ = true;
2021
}
2122

0 commit comments

Comments
 (0)