Skip to content
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

feat: add api to set volume per sound #9

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion android/src/main/cpp/AudioEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ void AudioEngine::seekSoundsTo(const std::vector<std::pair<std::string, double>>
}
}

void AudioEngine::setSoundsVolume(const std::vector<std::pair<std::string, double>> & pairs) {
for (const auto& pair: pairs) {
auto it = mPlayers.find(pair.first);
if(it != mPlayers.end()) {
it->second->setVolume(static_cast<float >(pair.second));
}
}
}


LoadSoundResult AudioEngine::loadSound(int fd, int offset, int length) {
auto playersSize = mPlayers.size();

Expand Down Expand Up @@ -149,4 +159,3 @@ UnloadSoundResult AudioEngine::unloadSound(const std::string &playerId) {

return {.error = std::nullopt};
}

1 change: 1 addition & 0 deletions android/src/main/cpp/AudioEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class AudioEngine : public oboe::AudioStreamDataCallback{
void playSounds(const std::vector<std::pair<std::string, bool>>&);
void loopSounds(const std::vector<std::pair<std::string, bool>>&);
void seekSoundsTo(const std::vector<std::pair<std::string, double>>&);
void setSoundsVolume(const std::vector<std::pair<std::string, double>>&);
LoadSoundResult loadSound(int fd, int offset, int length);
UnloadSoundResult unloadSound(const std::string &playerId);

Expand Down
2 changes: 1 addition & 1 deletion android/src/main/cpp/audio/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void Player::renderAudio(float *targetData, int32_t numFrames){

for (int i = 0; i < framesToRenderFromData; ++i) {
for (int j = 0; j < properties.channelCount; ++j) {
targetData[(i*properties.channelCount)+j] += data[(mReadFrameIndex*properties.channelCount)+j];
targetData[(i*properties.channelCount)+j] += (mVolume * data[(mReadFrameIndex*properties.channelCount)+j]);
}

// Increment and handle wraparound
Expand Down
2 changes: 2 additions & 0 deletions android/src/main/cpp/audio/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ class Player : public IRenderableAudio{
void renderAudio(float *targetData, int32_t numFrames) override;
void setPlaying(bool isPlaying) { LOGE("Setting isPlaying to: %d", isPlaying); mIsPlaying = isPlaying; };
void setLooping(bool isLooping) { mIsLooping = isLooping; };
void setVolume(float volume) { mVolume = volume; };
void seekTo(int64_t timeInMs);

private:
int32_t mReadFrameIndex = 0;
float mVolume = 1;
std::atomic<bool> mIsPlaying { false };
std::atomic<bool> mIsLooping { false };
std::unique_ptr<DataSource> mSource;
Expand Down
7 changes: 7 additions & 0 deletions android/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,11 @@ Java_com_audioplayback_AudioPlaybackModule_seekSoundsToNative(JNIEnv *env, jobje
jdoubleArray values) {
audioEngine->seekSoundsTo(zipStringDoubleArrays(env, ids, values));
}

JNIEXPORT void JNICALL
Java_com_audioplayback_AudioPlaybackModule_setSoundsVolumeNative(JNIEnv *env, jobject thiz,
jobjectArray ids,
jdoubleArray values) {
audioEngine->setSoundsVolume(zipStringDoubleArrays(env, ids, values));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class AudioPlaybackModule internal constructor(context: ReactApplicationContext)
seekSoundsToNative(ids, doubles)
}

@ReactMethod
override fun setSoundsVolume(arg: ReadableArray) {
val (ids, doubles) = readableArrayToStringDoubleArray(arg)
setSoundsVolumeNative(ids, doubles)
}


@ReactMethod(isBlockingSynchronousMethod = true)
override fun unloadSound(id: String): WritableMap {
Expand Down Expand Up @@ -178,6 +184,7 @@ class AudioPlaybackModule internal constructor(context: ReactApplicationContext)
private external fun playSoundsNative(ids: Array<String>, values: BooleanArray)
private external fun loopSoundsNative(ids: Array<String>, values: BooleanArray)
private external fun seekSoundsToNative(ids: Array<String>, values: DoubleArray)
private external fun setSoundsVolumeNative(ids: Array<String>, values: DoubleArray)
private external fun loadSoundNative(fd: Int, fileLength: Int, fileOffset: Int): LoadSoundResult
private external fun unloadSoundNative(playerId: String): UnloadSoundResult

Expand Down
2 changes: 2 additions & 0 deletions android/src/oldarch/AudioPlaybackSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ abstract class AudioPlaybackSpec internal constructor(context: ReactApplicationC

abstract fun seekSoundsTo(arg: ReadableArray)

abstract fun setSoundsVolume(arg: ReadableArray)

abstract fun unloadSound(id: String): WritableMap

abstract fun loadSound(uri: String, promise: Promise)
Expand Down
119 changes: 119 additions & 0 deletions example/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
GEM
remote: https://rubygems.org/
specs:
CFPropertyList (3.0.7)
base64
nkf
rexml
activesupport (7.1.5)
base64
benchmark (>= 0.3)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1)
mutex_m
securerandom (>= 0.3)
tzinfo (~> 2.0)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
algoliasearch (1.27.5)
httpclient (~> 2.8, >= 2.8.3)
json (>= 1.5.1)
atomos (0.1.3)
base64 (0.2.0)
benchmark (0.4.0)
bigdecimal (3.1.8)
claide (1.1.0)
cocoapods (1.15.2)
addressable (~> 2.8)
claide (>= 1.0.2, < 2.0)
cocoapods-core (= 1.15.2)
cocoapods-deintegrate (>= 1.0.3, < 2.0)
cocoapods-downloader (>= 2.1, < 3.0)
cocoapods-plugins (>= 1.0.0, < 2.0)
cocoapods-search (>= 1.0.0, < 2.0)
cocoapods-trunk (>= 1.6.0, < 2.0)
cocoapods-try (>= 1.1.0, < 2.0)
colored2 (~> 3.1)
escape (~> 0.0.4)
fourflusher (>= 2.3.0, < 3.0)
gh_inspector (~> 1.0)
molinillo (~> 0.8.0)
nap (~> 1.0)
ruby-macho (>= 2.3.0, < 3.0)
xcodeproj (>= 1.23.0, < 2.0)
cocoapods-core (1.15.2)
activesupport (>= 5.0, < 8)
addressable (~> 2.8)
algoliasearch (~> 1.0)
concurrent-ruby (~> 1.1)
fuzzy_match (~> 2.0.4)
nap (~> 1.0)
netrc (~> 0.11)
public_suffix (~> 4.0)
typhoeus (~> 1.0)
cocoapods-deintegrate (1.0.5)
cocoapods-downloader (2.1)
cocoapods-plugins (1.0.0)
nap
cocoapods-search (1.0.1)
cocoapods-trunk (1.6.0)
nap (>= 0.8, < 2.0)
netrc (~> 0.11)
cocoapods-try (1.2.0)
colored2 (3.1.2)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
drb (2.2.1)
escape (0.0.4)
ethon (0.16.0)
ffi (>= 1.15.0)
ffi (1.17.0)
fourflusher (2.3.1)
fuzzy_match (2.0.4)
gh_inspector (1.1.3)
httpclient (2.8.3)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
json (2.8.2)
logger (1.6.1)
minitest (5.25.2)
molinillo (0.8.0)
mutex_m (0.3.0)
nanaimo (0.3.0)
nap (1.1.0)
netrc (0.11.0)
nkf (0.2.0)
public_suffix (4.0.7)
rexml (3.3.9)
ruby-macho (2.5.1)
securerandom (0.3.2)
typhoeus (1.4.1)
ethon (>= 0.9.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
xcodeproj (1.25.1)
CFPropertyList (>= 2.3.3, < 4.0)
atomos (~> 0.1.3)
claide (>= 1.0.2, < 2.0)
colored2 (~> 3.1)
nanaimo (~> 0.3.0)
rexml (>= 3.3.6, < 4.0)

PLATFORMS
ruby

DEPENDENCIES
activesupport (>= 6.1.7.5, != 7.1.0)
cocoapods (>= 1.13, != 1.15.1, != 1.15.0)
xcodeproj (< 1.26.0)

RUBY VERSION
ruby 3.0.0p0

BUNDLED WITH
2.5.17
135 changes: 91 additions & 44 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,49 @@ PODS:
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- react-native-slider (4.5.5):
- DoubleConversion
- glog
- hermes-engine
- RCT-Folly (= 2024.01.01.00)
- RCTRequired
- RCTTypeSafety
- React-Core
- React-debug
- React-Fabric
- React-featureflags
- React-graphics
- React-ImageManager
- react-native-slider/common (= 4.5.5)
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
- React-utils
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- react-native-slider/common (4.5.5):
- DoubleConversion
- glog
- hermes-engine
- RCT-Folly (= 2024.01.01.00)
- RCTRequired
- RCTTypeSafety
- React-Core
- React-debug
- React-Fabric
- React-featureflags
- React-graphics
- React-ImageManager
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
- React-utils
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- React-nativeconfig (0.76.2)
- React-NativeModulesApple (0.76.2):
- glog
Expand Down Expand Up @@ -1572,6 +1615,7 @@ DEPENDENCIES:
- React-Mapbuffer (from `../node_modules/react-native/ReactCommon`)
- React-microtasksnativemodule (from `../node_modules/react-native/ReactCommon/react/nativemodule/microtasks`)
- react-native-audio-playback (from `../..`)
- "react-native-slider (from `../node_modules/@react-native-community/slider`)"
- React-nativeconfig (from `../node_modules/react-native/ReactCommon`)
- React-NativeModulesApple (from `../node_modules/react-native/ReactCommon/react/nativemodule/core/platform/ios`)
- React-perflogger (from `../node_modules/react-native/ReactCommon/reactperflogger`)
Expand Down Expand Up @@ -1679,6 +1723,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/ReactCommon/react/nativemodule/microtasks"
react-native-audio-playback:
:path: "../.."
react-native-slider:
:path: "../node_modules/@react-native-community/slider"
React-nativeconfig:
:path: "../node_modules/react-native/ReactCommon"
React-NativeModulesApple:
Expand Down Expand Up @@ -1749,60 +1795,61 @@ SPEC CHECKSUMS:
RCTTypeSafety: 30e36ceafa26979860e13fb3f234fb61692924c2
React: 10ad41b51f981992714011b6a4e081234c28dc2e
React-callinvoker: 58b51494f8b2cca07a27fc6f69273239c30a1e70
React-Core: 54860c16fb5873a6f00dd44d8979bbb648b34c7c
React-CoreModules: 443101f113a7b5d51b93e061574dcadf7850f8cc
React-cxxreact: 5407ecb854a755de34c0e6b03965d3a51c28c933
React-Core: 7a5e9897daf0189c0233b25243d6704e5b9025d8
React-CoreModules: 09d4f4ddd85ce9301c4b06dfe68750a82ee4b4f5
React-cxxreact: 29bfe097a993c73a314f569998fe863eb6fb8a18
React-debug: 252c723eb205cc508aa9690a16dff46293c30ed8
React-defaultsnativemodule: 1fb982daba4ac5bb3052ae262a919a0d117f3b1b
React-domnativemodule: 914401fb13804b7828020d7b3a89afa72ff22564
React-Fabric: 58696d9eaee305bb5a5af26071966dcfb941f9eb
React-FabricComponents: a037b977430eceae4bac539934497bacc8de3971
React-FabricImage: 2658c3e383195f69e7c83e4f75519bee17c3169a
React-defaultsnativemodule: f32fc96c2787aa5548a2438f9c9d39df30457529
React-domnativemodule: 409bb66e48c19997551f7e5adc0e4402adf3e5e8
React-Fabric: 7214dbd96d80c2c5871e3940b2a922ae4ff7a66d
React-FabricComponents: 4b46303dad5862b2c132494c6933d70e8b2b5271
React-FabricImage: ac6e46b8d6e12deb20887684c8cd1b60d573c738
React-featureflags: 7dc483869b3a940dcd92c7942c5e3492ad6afe68
React-featureflagsnativemodule: 7a50dda8a6d3883139c47b4bda797156737240db
React-graphics: 066863eb87b142f0603fed08c71bac452238ac3e
React-hermes: 8f31f252aff98a4cb711ccf6644cccfe35d8edd1
React-idlecallbacksnativemodule: c3f68fe97e379c4c1b6c4ba25e5d450cbe931e25
React-ImageManager: 36240f8ab7181551574ca443da507272dbbf7014
React-jserrorhandler: 1aa045c492222751dc476bcb973f787e82f952b9
React-jsi: b96853ac12c1dab5fe3ea131f959fda0bbaf1151
React-jsiexecutor: e38748a0e9d899f63dec562f93ac06c7acbc813d
React-jsinspector: 91b3c73d2afb7627af7872cedb0b74a0f00f57d1
React-jsitracing: a340047c9fd31a36b222569c402e472e20557805
React-logger: 81d58ca6f1d93fca9a770bda6cc1c4fbfcc99c9c
React-Mapbuffer: 726951e68f4bb1c2513d322f2548798b2a3d628d
React-microtasksnativemodule: 7a69a9b8fded72ea3cf81923ecf75cad5558ed26
react-native-audio-playback: 90ecd527297c496404fbad379c672d59a53bb6cc
React-featureflagsnativemodule: 2e65d2c94448d588a605de0d2aadce9c39c7ce31
React-graphics: 52ca86af6628cb95cc472b2f0f7c42018906881d
React-hermes: ab8705477c497a5839966bd57471ee3611f864f8
React-idlecallbacksnativemodule: ad29fb45bc50ebbbbb6a08a93e25909f65e2ca12
React-ImageManager: 68a1bc3ba2c11eb01b01ebbd1e682fa265b6dc2e
React-jserrorhandler: 8bc8fc6c85c743af48143d693a8d661cb23bc96d
React-jsi: de2c6119671b281671fabf9e96eb11110207fe9d
React-jsiexecutor: 936132921f4d991af7b4faa7424fc54e67791dd0
React-jsinspector: adc8764a6d2a331c20f42db9192f348b3f8050fa
React-jsitracing: 3f04035f1a39efe206056c4a28b725673c2e696b
React-logger: addd140841248966c2547eb94836399cc1061f4d
React-Mapbuffer: 029b5332e78af8c67c4b5e65edfc717068b8eac1
React-microtasksnativemodule: f30949ee318ba90b9668de1e325b98838b9a4da2
react-native-audio-playback: 37e5bb870f3ce91609c94243d99351f9bc68914d
react-native-slider: d1a9121980fc81678c6d30b82f312c778fba563c
React-nativeconfig: 470fce6d871c02dc5eff250a362d56391b7f52d6
React-NativeModulesApple: 6297fc3136c1fd42884795c51d7207de6312b606
React-perflogger: f2c94413cfad44817c96cab33753831e73f0d0dd
React-performancetimeline: d6e493713e6aab3cc8b7c1c07e97160e22dd79cc
React-NativeModulesApple: 1586448c61a7c2bd4040cc03ccde66a72037e77e
React-perflogger: c8860eaab4fe60d628b27bf0086a372c429fc74f
React-performancetimeline: 94e04b2067bc774df42ced96c8c3582fba9d7f88
React-RCTActionSheet: 2eb26cbf384f3d3b2cb2e23be850a956d83f77ab
React-RCTAnimation: 59463699a92edc6705ce5306bb789d6a0ca4df0b
React-RCTAppDelegate: 4d9efca7caa477b106e3d55af339d0e071441536
React-RCTBlob: 0883f5363069ad30f628c970fcb413a619e42804
React-RCTFabric: 4c761af601a1fe0061b15df97af9fb77403362a2
React-RCTImage: 78884b7ea6ef4f7bb9655614bf09a40054f282ce
React-RCTLinking: b9beba7465fd9a1ed7a88a4e7fc403d26e17ab95
React-RCTNetwork: 701d9c050077596b15a11b6b573ed95c309d2315
React-RCTSettings: e700a82e3e923c10060b8f65297f9d321b93d8eb
React-RCTText: e782ce1c3f9d915daf50d97157f8c226e8f3d206
React-RCTVibration: 2a19c56be78cb7afce9f4f3471aacfb063f32a00
React-RCTAnimation: aa0a663829963ca72f4c722e71bd5debbecc1348
React-RCTAppDelegate: 12688b64e1e28e0eb1c628690678ae5d3ab356b4
React-RCTBlob: bef788ef3433170f9748d0e00d1afc7be64bc51d
React-RCTFabric: f1ce02d6fecbf5266bfafa228eed179ed2c182e7
React-RCTImage: a9de66d305fa02008759a2aa5a723b68d18907e5
React-RCTLinking: 15fe8ccad84a4a5274d55b9d43e223896718772d
React-RCTNetwork: 7635ab6b7617648e5b5e35cdb3a4edab6fa309a6
React-RCTSettings: 18e666705ea62aac59f2a8d50ced87b9b8902c7b
React-RCTText: 5cf76f649b4781362d23f9ee3d52e8d12a74dd18
React-RCTVibration: bd72dc267866c8cd524c9a61d15060949ff24cf9
React-rendererconsistency: b389e324712bf0869529823216e922836ed9b737
React-rendererdebug: 9f5629032c0937c62b21dcaf96b374a149bf8a44
React-rendererdebug: 6b5dcd3797ec96001304e6bfaae408fa4f3ce3f3
React-rncore: 2cf6b2348ee5d5431c4735180364b207ecf47123
React-RuntimeApple: 84d648b9a87c34041d6628dde50d1acf54e5bf89
React-RuntimeCore: 79290b2eb17a25c1b23c4d5dde13d43c20467eef
React-RuntimeApple: cb6e7f3e8911da8ebfdd3b0e7776c1db656c7f6b
React-RuntimeCore: b6213eb42011ecc9a97060f8714d8d57b97775c4
React-runtimeexecutor: 69e27948ee2127400297c7de50b809a7cd127a15
React-RuntimeHermes: 5fe2082f98187410c1815c532f72348fbe1d0517
React-runtimescheduler: 95b7087f459699756c1b7feb3f4637066c337b62
React-RuntimeHermes: 3a974aa24b83a7a0396edb35cb333c1d744563e0
React-runtimescheduler: 567b00b76261df4a791ea37eb076c1c496c08ac2
React-timing: 97673939f96f79031d2a5a0a92285618475149ec
React-utils: ed6cb7ba089ac0856aa104df12691e99abbf14e1
ReactCodegen: 93b271af49774429f34d7fd561197020d86436e2
ReactCommon: 208cb02e3c0bb8a727b3e1a1782202bcfa5d9631
React-utils: c8c0c746031419a29cfd8c72a394fdeac0684a84
ReactCodegen: a64e8f3a8bba0ecf88fce06c2874e021d55148f3
ReactCommon: 7b9403030ff3430ccffed63cd25c8aeab2a3ea7e
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
Yoga: 96872ee462cfc43866ad013c8160d4ff6b85709b

PODFILE CHECKSUM: db41a2e031518fb452cb8d57b6501f7d2b8a89b0

COCOAPODS: 1.14.3
COCOAPODS: 1.15.2
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"build:ios": "react-native build-ios --scheme AudioPlaybackExample --mode Debug --extra-params \"-sdk iphonesimulator CC=clang CPLUSPLUS=clang++ LD=clang LDPLUSPLUS=clang++ GCC_OPTIMIZATION_LEVEL=0 GCC_PRECOMPILE_PREFIX_HEADER=YES ASSETCATALOG_COMPILER_OPTIMIZATION=time DEBUG_INFORMATION_FORMAT=dwarf COMPILER_INDEX_STORE_ENABLE=NO\""
},
"dependencies": {
"@react-native-community/slider": "4.5.5",
"react": "18.3.1",
"react-native": "0.76.2"
},
Expand Down
Loading
Loading