-
-
Notifications
You must be signed in to change notification settings - Fork 38
Refactor/communication between audio and js threads #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
maciejmakowski2003
wants to merge
12
commits into
main
Choose a base branch
from
refactor/communication-between-audio-and-js-threads
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
50885c5
feat: audio event scheduler
eed4ed5
refactor: audio buffer source node
718097b
fix: missing header
caca77b
fix: docstring
b27ea3c
refactor: template for scheduleEvent
fc907d8
fix: aligned AudioPlayer on Android with iOS
c88d9ff
refactor: audio param
11309e2
feat: implemented fat function for fully stacked allocated callable o…
poneciak57 8903d17
refactor: onLoopEndedCallback and comments
dcc84c0
fix: fixed use-after-free caused by naive memcpy in fat function move…
poneciak57 d96a108
chore: formatting
poneciak57 079e895
feat: improved fat function to only create deleter and mover when it …
poneciak57 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,7 +15,10 @@ AudioPlayer::AudioPlayer( | |||||||||
| const std::function<void(std::shared_ptr<AudioBus>, int)> &renderAudio, | ||||||||||
| float sampleRate, | ||||||||||
| int channelCount) | ||||||||||
| : renderAudio_(renderAudio), sampleRate_(sampleRate), channelCount_(channelCount) { | ||||||||||
| : renderAudio_(renderAudio), | ||||||||||
| sampleRate_(sampleRate), | ||||||||||
| channelCount_(channelCount), | ||||||||||
| isRunning_(false) { | ||||||||||
| isInitialized_ = openAudioStream(); | ||||||||||
|
|
||||||||||
| nativeAudioPlayer_ = jni::make_global(NativeAudioPlayer::create()); | ||||||||||
|
|
@@ -41,15 +44,16 @@ bool AudioPlayer::openAudioStream() { | |||||||||
| return false; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| mBus_ = std::make_shared<AudioBus>(RENDER_QUANTUM_SIZE, channelCount_, sampleRate_); | ||||||||||
| audioBus_ = std::make_shared<AudioBus>(RENDER_QUANTUM_SIZE, channelCount_, sampleRate_); | ||||||||||
| return true; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| bool AudioPlayer::start() { | ||||||||||
| if (mStream_) { | ||||||||||
| jni::ThreadScope::WithClassLoader([this]() { nativeAudioPlayer_->start(); }); | ||||||||||
| auto result = mStream_->requestStart(); | ||||||||||
| return result == oboe::Result::OK; | ||||||||||
| auto result = mStream_->requestStart() == oboe::Result::OK; | ||||||||||
| isRunning_.store(result, std::memory_order_release); | ||||||||||
| return result; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return false; | ||||||||||
|
|
@@ -58,21 +62,28 @@ bool AudioPlayer::start() { | |||||||||
| void AudioPlayer::stop() { | ||||||||||
| if (mStream_) { | ||||||||||
| jni::ThreadScope::WithClassLoader([this]() { nativeAudioPlayer_->stop(); }); | ||||||||||
| isRunning_.store(false, std::memory_order_release); | ||||||||||
| mStream_->requestStop(); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| bool AudioPlayer::resume() { | ||||||||||
| if (isRunning()) { | ||||||||||
| return true; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (mStream_) { | ||||||||||
| auto result = mStream_->requestStart(); | ||||||||||
| return result == oboe::Result::OK; | ||||||||||
| auto result = mStream_->requestStart() == oboe::Result::OK; | ||||||||||
| isRunning_.store(result, std::memory_order_release); | ||||||||||
| return result; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return false; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| void AudioPlayer::suspend() { | ||||||||||
| if (mStream_) { | ||||||||||
| isRunning_.store(false, std::memory_order_release); | ||||||||||
| mStream_->requestPause(); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
@@ -87,7 +98,8 @@ void AudioPlayer::cleanup() { | |||||||||
| } | ||||||||||
|
|
||||||||||
| bool AudioPlayer::isRunning() const { | ||||||||||
| return mStream_ && mStream_->getState() == oboe::StreamState::Started; | ||||||||||
| return mStream_ && mStream_->getState() == oboe::StreamState::Started && | ||||||||||
| isRunning_.load(std::memory_order_acquire); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| DataCallbackResult | ||||||||||
|
|
@@ -99,17 +111,19 @@ AudioPlayer::onAudioReady(AudioStream *oboeStream, void *audioData, int32_t numF | |||||||||
| auto buffer = static_cast<float *>(audioData); | ||||||||||
| int processedFrames = 0; | ||||||||||
|
|
||||||||||
| assert(buffer != nullptr); | ||||||||||
|
|
||||||||||
| while (processedFrames < numFrames) { | ||||||||||
| int framesToProcess = std::min(numFrames - processedFrames, RENDER_QUANTUM_SIZE); | ||||||||||
| renderAudio_(mBus_, framesToProcess); | ||||||||||
|
|
||||||||||
| // TODO: optimize this with SIMD? | ||||||||||
| if (isRunning_.load(std::memory_order_acquire)) { | ||||||||||
| renderAudio_(audioBus_, framesToProcess); | ||||||||||
| } else { | ||||||||||
| audioBus_->zero(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| for (int i = 0; i < framesToProcess; i++) { | ||||||||||
| for (int channel = 0; channel < channelCount_; channel += 1) { | ||||||||||
|
Comment on lines
123
to
124
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
consistency |
||||||||||
| buffer[(processedFrames + i) * channelCount_ + channel] = | ||||||||||
| mBus_->getChannel(channel)->getData()[i]; | ||||||||||
| audioBus_->getChannel(channel)->getData()[i]; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can get rid of all implicit stream to bool casts in this pr