Skip to content

Commit 6944aae

Browse files
author
Maciej Makowski
committed
Merge branch 'main' into feat/audio-from-url
2 parents a88616d + 261b8ac commit 6944aae

File tree

7 files changed

+47
-21
lines changed

7 files changed

+47
-21
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ export default function usePlayer(options: PlayerOptions) {
133133
playingInstruments.value = getPlayingInstruments();
134134
}
135135

136+
return () => {
137+
audioContext.close();
138+
};
139+
136140
// \/ Shared values are not necessary in deps array
137141
// eslint-disable-next-line react-hooks/exhaustive-deps
138142
}, [isPlaying, setup]);

packages/react-native-audio-api/android/src/main/cpp/AudioPlayer/AudioPlayer.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ AudioPlayer::AudioPlayer(
2121
->setDataCallback(this)
2222
->openStream(mStream_);
2323

24-
mBus_ = std::make_shared<AudioBus>(
25-
getSampleRate(), getBufferSizeInFrames(), CHANNEL_COUNT);
24+
mBus_ = std::make_shared<AudioBus>(getSampleRate(), getBufferSizeInFrames(), CHANNEL_COUNT);
25+
isInitialized_ = true;
2626
}
2727

2828
int AudioPlayer::getSampleRate() const {
@@ -40,6 +40,8 @@ void AudioPlayer::start() {
4040
}
4141

4242
void AudioPlayer::stop() {
43+
isInitialized_ = false;
44+
4345
if (mStream_) {
4446
mStream_->requestStop();
4547
mStream_->close();
@@ -51,8 +53,11 @@ DataCallbackResult AudioPlayer::onAudioReady(
5153
AudioStream *oboeStream,
5254
void *audioData,
5355
int32_t numFrames) {
54-
auto buffer = static_cast<float *>(audioData);
56+
if (!isInitialized_) {
57+
return DataCallbackResult::Continue;
58+
}
5559

60+
auto buffer = static_cast<float *>(audioData);
5661
renderAudio_(mBus_.get(), numFrames);
5762

5863
// TODO: optimize this with SIMD?

packages/react-native-audio-api/android/src/main/cpp/AudioPlayer/AudioPlayer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class AudioPlayer : public AudioStreamDataCallback {
2828
std::function<void(AudioBus *, int)> renderAudio_;
2929
std::shared_ptr<AudioStream> mStream_;
3030
std::shared_ptr<AudioBus> mBus_;
31+
bool isInitialized_ = false;
3132
};
3233

3334
} // namespace audioapi

packages/react-native-audio-api/common/cpp/core/AudioContext.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88

99
namespace audioapi {
1010

11-
AudioContext::AudioContext() : BaseAudioContext() {}
11+
AudioContext::AudioContext() : BaseAudioContext() {
12+
audioPlayer_->start();
13+
}
1214

1315
void AudioContext::close() {
1416
state_ = ContextState::CLOSED;
15-
16-
if (audioPlayer_) {
17-
audioPlayer_->stop();
18-
}
19-
20-
audioPlayer_.reset();
21-
destination_.reset();
17+
audioPlayer_->stop();
2218
}
19+
2320
} // namespace audioapi

packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@ double AudioDestinationNode::getCurrentTime() const {
2323
return static_cast<double>(currentSampleFrame_) / context_->getSampleRate();
2424
}
2525

26-
void AudioDestinationNode::renderAudio(
27-
AudioBus *destinationBus,
28-
int32_t numFrames) {
29-
context_->getNodeManager()->preProcessGraph();
30-
destinationBus->zero();
31-
32-
if (!numFrames) {
26+
void AudioDestinationNode::renderAudio(AudioBus *destinationBus, int32_t numFrames) {
27+
if (!numFrames || !destinationBus || !isInitialized_) {
3328
return;
3429
}
3530

36-
AudioBus *processedBus = processAudio(destinationBus, numFrames);
31+
context_->getNodeManager()->preProcessGraph();
32+
destinationBus->zero();
33+
34+
AudioBus* processedBus = processAudio(destinationBus, numFrames);
3735

3836
if (processedBus && processedBus != destinationBus) {
3937
destinationBus->copy(processedBus);

packages/react-native-audio-api/common/cpp/core/BaseAudioContext.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,19 @@ BaseAudioContext::BaseAudioContext() {
3333
sampleRate_ = audioPlayer_->getSampleRate();
3434
bufferSizeInFrames_ = audioPlayer_->getBufferSizeInFrames();
3535

36-
audioPlayer_->start();
3736
nodeManager_ = std::make_shared<AudioNodeManager>();
3837
destination_ = std::make_shared<AudioDestinationNode>(this);
3938
}
4039

40+
BaseAudioContext::~BaseAudioContext() {
41+
if (isRunning()) {
42+
return;
43+
}
44+
45+
state_ = ContextState::CLOSED;
46+
audioPlayer_->stop();
47+
}
48+
4149
std::string BaseAudioContext::getState() {
4250
return BaseAudioContext::toString(state_);
4351
}
@@ -112,7 +120,7 @@ std::shared_ptr<AudioBuffer> BaseAudioContext::decodeAudioDataSource(
112120
#endif
113121

114122
std::function<void(AudioBus *, int)> BaseAudioContext::renderAudio() {
115-
if (state_ == ContextState::CLOSED) {
123+
if (isClosed()) {
116124
return [](AudioBus *, int) {};
117125
}
118126

@@ -125,6 +133,14 @@ AudioNodeManager *BaseAudioContext::getNodeManager() {
125133
return nodeManager_.get();
126134
}
127135

136+
bool BaseAudioContext::isRunning() const {
137+
return state_ == ContextState::RUNNING;
138+
}
139+
140+
bool BaseAudioContext::isClosed() const {
141+
return state_ == ContextState::CLOSED;
142+
}
143+
128144
std::string BaseAudioContext::toString(ContextState state) {
129145
switch (state) {
130146
case ContextState::SUSPENDED:

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class IOSAudioDecoder;
3232
class BaseAudioContext {
3333
public:
3434
BaseAudioContext();
35+
~BaseAudioContext();
3536
std::string getState();
3637
[[nodiscard]] int getSampleRate() const;
3738
[[nodiscard]] double getCurrentTime() const;
@@ -57,6 +58,10 @@ class BaseAudioContext {
5758
AudioNodeManager *getNodeManager();
5859
std::function<void(AudioBus *, int)> renderAudio();
5960

61+
AudioNodeManager* getNodeManager();
62+
bool isRunning() const;
63+
bool isClosed() const;
64+
6065
protected:
6166
static std::string toString(ContextState state);
6267
std::shared_ptr<AudioDestinationNode> destination_;

0 commit comments

Comments
 (0)