File tree Expand file tree Collapse file tree 7 files changed +47
-21
lines changed
apps/common-app/src/examples/DrumMachine
packages/react-native-audio-api
android/src/main/cpp/AudioPlayer Expand file tree Collapse file tree 7 files changed +47
-21
lines changed Original file line number Diff line number Diff line change @@ -133,6 +133,10 @@ export default function usePlayer(options: PlayerOptions) {
133
133
playingInstruments . value = getPlayingInstruments ( ) ;
134
134
}
135
135
136
+ return ( ) => {
137
+ audioContext . close ( ) ;
138
+ } ;
139
+
136
140
// \/ Shared values are not necessary in deps array
137
141
// eslint-disable-next-line react-hooks/exhaustive-deps
138
142
} , [ isPlaying , setup ] ) ;
Original file line number Diff line number Diff line change @@ -21,8 +21,8 @@ AudioPlayer::AudioPlayer(
21
21
->setDataCallback (this )
22
22
->openStream (mStream_ );
23
23
24
- mBus_ = std::make_shared<AudioBus>(
25
- getSampleRate (), getBufferSizeInFrames (), CHANNEL_COUNT) ;
24
+ mBus_ = std::make_shared<AudioBus>(getSampleRate (), getBufferSizeInFrames (), CHANNEL_COUNT);
25
+ isInitialized_ = true ;
26
26
}
27
27
28
28
int AudioPlayer::getSampleRate () const {
@@ -40,6 +40,8 @@ void AudioPlayer::start() {
40
40
}
41
41
42
42
void AudioPlayer::stop () {
43
+ isInitialized_ = false ;
44
+
43
45
if (mStream_ ) {
44
46
mStream_ ->requestStop ();
45
47
mStream_ ->close ();
@@ -51,8 +53,11 @@ DataCallbackResult AudioPlayer::onAudioReady(
51
53
AudioStream *oboeStream,
52
54
void *audioData,
53
55
int32_t numFrames) {
54
- auto buffer = static_cast <float *>(audioData);
56
+ if (!isInitialized_) {
57
+ return DataCallbackResult::Continue;
58
+ }
55
59
60
+ auto buffer = static_cast <float *>(audioData);
56
61
renderAudio_ (mBus_ .get (), numFrames);
57
62
58
63
// TODO: optimize this with SIMD?
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ class AudioPlayer : public AudioStreamDataCallback {
28
28
std::function<void (AudioBus *, int )> renderAudio_;
29
29
std::shared_ptr<AudioStream> mStream_ ;
30
30
std::shared_ptr<AudioBus> mBus_ ;
31
+ bool isInitialized_ = false ;
31
32
};
32
33
33
34
} // namespace audioapi
Original file line number Diff line number Diff line change 8
8
9
9
namespace audioapi {
10
10
11
- AudioContext::AudioContext () : BaseAudioContext() {}
11
+ AudioContext::AudioContext () : BaseAudioContext() {
12
+ audioPlayer_->start ();
13
+ }
12
14
13
15
void AudioContext::close () {
14
16
state_ = ContextState::CLOSED;
15
-
16
- if (audioPlayer_) {
17
- audioPlayer_->stop ();
18
- }
19
-
20
- audioPlayer_.reset ();
21
- destination_.reset ();
17
+ audioPlayer_->stop ();
22
18
}
19
+
23
20
} // namespace audioapi
Original file line number Diff line number Diff line change @@ -23,17 +23,15 @@ double AudioDestinationNode::getCurrentTime() const {
23
23
return static_cast <double >(currentSampleFrame_) / context_->getSampleRate ();
24
24
}
25
25
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_) {
33
28
return ;
34
29
}
35
30
36
- AudioBus *processedBus = processAudio (destinationBus, numFrames);
31
+ context_->getNodeManager ()->preProcessGraph ();
32
+ destinationBus->zero ();
33
+
34
+ AudioBus* processedBus = processAudio (destinationBus, numFrames);
37
35
38
36
if (processedBus && processedBus != destinationBus) {
39
37
destinationBus->copy (processedBus);
Original file line number Diff line number Diff line change @@ -33,11 +33,19 @@ BaseAudioContext::BaseAudioContext() {
33
33
sampleRate_ = audioPlayer_->getSampleRate ();
34
34
bufferSizeInFrames_ = audioPlayer_->getBufferSizeInFrames ();
35
35
36
- audioPlayer_->start ();
37
36
nodeManager_ = std::make_shared<AudioNodeManager>();
38
37
destination_ = std::make_shared<AudioDestinationNode>(this );
39
38
}
40
39
40
+ BaseAudioContext::~BaseAudioContext () {
41
+ if (isRunning ()) {
42
+ return ;
43
+ }
44
+
45
+ state_ = ContextState::CLOSED;
46
+ audioPlayer_->stop ();
47
+ }
48
+
41
49
std::string BaseAudioContext::getState () {
42
50
return BaseAudioContext::toString (state_);
43
51
}
@@ -112,7 +120,7 @@ std::shared_ptr<AudioBuffer> BaseAudioContext::decodeAudioDataSource(
112
120
#endif
113
121
114
122
std::function<void (AudioBus *, int )> BaseAudioContext::renderAudio () {
115
- if (state_ == ContextState::CLOSED ) {
123
+ if (isClosed () ) {
116
124
return [](AudioBus *, int ) {};
117
125
}
118
126
@@ -125,6 +133,14 @@ AudioNodeManager *BaseAudioContext::getNodeManager() {
125
133
return nodeManager_.get ();
126
134
}
127
135
136
+ bool BaseAudioContext::isRunning () const {
137
+ return state_ == ContextState::RUNNING;
138
+ }
139
+
140
+ bool BaseAudioContext::isClosed () const {
141
+ return state_ == ContextState::CLOSED;
142
+ }
143
+
128
144
std::string BaseAudioContext::toString (ContextState state) {
129
145
switch (state) {
130
146
case ContextState::SUSPENDED:
Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ class IOSAudioDecoder;
32
32
class BaseAudioContext {
33
33
public:
34
34
BaseAudioContext ();
35
+ ~BaseAudioContext ();
35
36
std::string getState ();
36
37
[[nodiscard]] int getSampleRate () const ;
37
38
[[nodiscard]] double getCurrentTime () const ;
@@ -57,6 +58,10 @@ class BaseAudioContext {
57
58
AudioNodeManager *getNodeManager ();
58
59
std::function<void (AudioBus *, int )> renderAudio ();
59
60
61
+ AudioNodeManager* getNodeManager ();
62
+ bool isRunning () const ;
63
+ bool isClosed () const ;
64
+
60
65
protected:
61
66
static std::string toString (ContextState state);
62
67
std::shared_ptr<AudioDestinationNode> destination_;
You can’t perform that action at this time.
0 commit comments