Skip to content

Commit a4b0ead

Browse files
authored
Merge pull request #195 from alnitak/opusOgg
Embedding of opus and ogg libs on all platforms with a choice of doing this
2 parents 1860449 + ce5878e commit a4b0ead

File tree

219 files changed

+9042
-17025
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

219 files changed

+9042
-17025
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ web/worker.dart.js.map
4545
**/android/daemon
4646
**/android/android
4747

48-
# Ignore ogg and opus directories in xiph
49-
xiph/ogg
50-
xiph/opus
48+
# Ignore all the content of xiph folder, but not the scripts
49+
xiph/*
50+
!xiph/build_*

.metadata

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# This file should be version controlled and should not be manually edited.
55

66
version:
7-
revision: "8495dee1fd4aacbe9de707e7581203232f591b2f"
7+
revision: "35c388afb57ef061d06a39b537336c87e0e3d1b1"
88
channel: "stable"
99

1010
project_type: plugin_ffi
@@ -13,26 +13,26 @@ project_type: plugin_ffi
1313
migration:
1414
platforms:
1515
- platform: root
16-
create_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
17-
base_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
16+
create_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
17+
base_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
1818
- platform: android
19-
create_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
20-
base_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
19+
create_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
20+
base_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
2121
- platform: ios
22-
create_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
23-
base_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
22+
create_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
23+
base_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
2424
- platform: linux
25-
create_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
26-
base_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
25+
create_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
26+
base_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
2727
- platform: macos
28-
create_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
29-
base_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
28+
create_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
29+
base_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
3030
- platform: web
31-
create_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
32-
base_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
31+
create_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
32+
base_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
3333
- platform: windows
34-
create_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
35-
base_revision: 8495dee1fd4aacbe9de707e7581203232f591b2f
34+
create_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
35+
base_revision: 35c388afb57ef061d06a39b537336c87e0e3d1b1
3636

3737
# User provided section
3838

.vscode/c_cpp_properties.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,36 @@
4848
"cppStandard": "c++17",
4949
"intelliSenseMode": "windows-msvc-x64",
5050
"configurationProvider": "ms-vscode.cmake-tools"
51+
},
52+
{
53+
"name": "MacOS",
54+
"includePath": [
55+
"${workspaceFolder}/**",
56+
"${workspaceFolder}/flutter_soloud_ios/ios/Classes/src",
57+
"${workspaceFolder}/flutter_soloud_ios/ios/Classes/src/soloud/include",
58+
"${workspaceFolder}/src"
59+
],
60+
"defines": ["WITH_MINIAUDIO", "DR_MP3_IMPLEMENTATION"],
61+
"compilerPath": "/usr/bin/clang",
62+
"cStandard": "c17",
63+
"cppStandard": "c++17",
64+
"intelliSenseMode": "macos-clang-x64",
65+
"configurationProvider": "ms-vscode.cmake-tools"
66+
},
67+
{
68+
"name": "iOS",
69+
"includePath": [
70+
"${workspaceFolder}/**",
71+
"${workspaceFolder}/flutter_soloud_ios/ios/Classes/src",
72+
"${workspaceFolder}/flutter_soloud_ios/ios/Classes/src/soloud/include",
73+
"${workspaceFolder}/src"
74+
],
75+
"defines": ["WITH_MINIAUDIO", "DR_MP3_IMPLEMENTATION"],
76+
"compilerPath": "/usr/bin/clang",
77+
"cStandard": "c17",
78+
"cppStandard": "c++17",
79+
"intelliSenseMode": "ios-clang-arm64",
80+
"configurationProvider": "ms-vscode.cmake-tools"
5181
}
5282
],
5383
"version": 4

.vscode/launch.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"request": "launch",
1111
"program": "lib/main.dart",
1212
"flutterMode": "debug",
13+
// "env": {
14+
// "NO_OPUS_OGG_LIBS": "1"
15+
// },
1316
"cwd": "${workspaceFolder}/example"
1417
},
1518
{
@@ -18,14 +21,20 @@
1821
"request": "launch",
1922
"program": "lib/main.dart",
2023
"flutterMode": "profile",
24+
// "env": {
25+
// "NO_OPUS_OGG_LIBS": "1"
26+
// },
2127
"cwd": "${workspaceFolder}/example"
2228
},
2329
{
2430
"name": "Flutter release",
2531
"type": "dart",
2632
"request": "launch",
27-
"program": "lib/buffer_stream/websocket.dart",
33+
"program": "lib/main.dart",
2834
"flutterMode": "release",
35+
// "env": {
36+
// "NO_OPUS_OGG_LIBS": "1"
37+
// },
2938
"cwd": "${workspaceFolder}/example"
3039
},
3140
{

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,5 @@
109109
"text_encoding": "cpp",
110110
"coroutine": "cpp"
111111
},
112-
"java.configuration.updateBuildConfiguration": "interactive"
112+
"java.configuration.updateBuildConfiguration": "automatic"
113113
}

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### 3.0.3 (7 Mar 2025)
2+
- it's now possible to choose to not link opus and ogg libraries (see `NO_OPUS_OGG_LIBS.md`). Fix for #191 and #192.
3+
14
### 3.0.2 (25 Feb 2025)
25
- fixed crash when trying to play a sound after deactivating its active filter #189
36

NO_OPUS_OGG_LIBS.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
Opus and Ogg libraries are currently only used by the BufferStream to receive and fill audio data with Opus format and they are enabled by default.
2+
3+
These libs are prebuilt for all platforms and stored in the plugin folder. This means that even if they are not needed, the plugin still requires them to work, and the size of the app will grow slightly. To address this, a new environment variable has been introduced that can be used to prevent the prebuilt libs from being linked to the final app.
4+
5+
If trying to use, for example the `SoLoud.setBufferStream()` function, an exception will be thrown if the environment variable has been set.
6+
7+
---
8+
9+
### How to set the environment variable:
10+
11+
**Linux - Android - Windows**
12+
13+
If using **VS Code**, add `NO_OPUS_OGG_LIBS` set to an empty string in the `env` key to the *.vscode/launch.json* configuration, for example:
14+
15+
```
16+
{
17+
"name": "Flutter debug",
18+
"type": "dart",
19+
"request": "launch",
20+
"program": "lib/buffer_stream/websocket.dart",
21+
"flutterMode": "debug",
22+
"env": {
23+
"NO_OPUS_OGG_LIBS": "1"
24+
},
25+
"cwd": "${workspaceFolder}/example"
26+
}
27+
```
28+
29+
If using **Android Studio**, add `NO_OPUS_OGG_LIBS="1"` in the *Run/Debug Configurations* configuration under **Environment Variables** text field.
30+
31+
Alternatively, you can set the variable in the terminal and build the app, for example:
32+
33+
```
34+
flutter clean
35+
flutter pub get
36+
NO_OPUS_OGG_LIBS="1" && flutter run
37+
```
38+
39+
**MacOS - iOS**
40+
41+
To set the environment variable on MacOS and iOS, you can add the following line to the `app/ios/Podfile` or `app/macos/Podfile` at the top of the file:
42+
43+
```
44+
ENV['NO_OPUS_OGG_LIBS'] = '1'
45+
```
46+
47+
Alternatively, you can set the variable in the terminal and build the app, for example:
48+
49+
```
50+
flutter clean
51+
flutter pub get
52+
NO_OPUS_OGG_LIBS="1" && flutter run
53+
```
54+
if the environment variable was already been used before for testing purposes, you can use the following command:
55+
```
56+
flutter clean
57+
flutter pub get
58+
NO_OPUS_OGG_LIBS= && flutter run <-- this will unset the environment variable
59+
```
60+
61+
> [!NOTE]
62+
> The environment variable set in VS Code or in Android Studio will be ignored.
63+
64+
**Web**
65+
66+
To set the environment variable on Web, open `web/compile_wasm.sh` and change the line `NO_OPUS_OGG_LIBS="0"` to `NO_OPUS_OGG_LIBS="1"`.
67+
You should then run the script to build the WASM and JS files. You must have `emscripten` installed.
68+
69+
The script works on Linux and probably on MacOS. Windows users should run the script in WSL or wait until a *.bat* script is available.
70+
71+
> [!NOTE]
72+
> The environment variable set in VS Code or in Android Studio will be ignored.

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,7 @@ In other words, it is calling the C/C++ methods of the underlying audio engine d
4747
#### Opus format for streaming
4848
When using an `AudioSource` as an audio stream to play custom audio data (ie using setBufferStream/addAudioDataStream/setDataIsEnded), it is possible to add PCM RAW audio data in *s8*, *s16le*, *s32le*, *f32le* and since it supports also the *opus* format with the Ogg codec (ie to work with OpenAI APIs), the [Opus](https://www.opus-codec.org/) and [Ogg](https://xiph.org/ogg/) libraries from [Xiph.org](https://www.xiph.org/) are needed.
4949

50-
On Linux and MacOS theese libraries must be installed if you want to use this kind of `AudioSource`:
51-
- **Linux**
52-
- install them depending on the package manager used by you distribution
53-
- **MacOS**
54-
- `brew install opus libogg`
55-
56-
if the libraries are not found the plugin will throw an exception when calling `setBufferStream` using Opus format.
50+
These libraries are embedded and enabled by default for all platforms. This leads to an increased binary size by 600~1500 kb depending on platform. If you don't need to use audio streaming, hence you don't need the Opus and Ogg libraries, you can read the [NO_OPUS_OGG_LIBS.md](https://github.com/alnitak/flutter_soloud/blob/main/NO_OPUS_OGG_LIBS.md) for details.
5751

5852
The `SoLoud.setBufferStream` supports also `BufferingType.preserved` which behaves the same as a normal `AudioSource`, and `BufferingType.released` which will free the memory of the already played audio for longer playback. The latter will accept to play only one instance of the audio stream at the same time.
5953
|BufferingType.preserved|BufferingType.released|

android/CMakeLists.txt

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../src")
1111
## Add SoLoud custom cmake files
1212
message("**************** SOLOUD CONFIGURE.CMAKE")
1313
include (Configure.cmake)
14-
message("**************** SOLOUD SRC.CMAKE 1")
14+
message("**************** SOLOUD SRC.CMAKE START")
1515
if (UNIX AND NOT WIN32 AND NOT APPLE)
1616
if (CMAKE_SIZEOF_VOID_P MATCHES "8")
1717
set (LIB_POSTFIX "64" CACHE STRING "suffix for 32/64 dir placement")
@@ -29,7 +29,7 @@ include_directories(${SRC_DIR}/soloud/include)
2929
include_directories(${SRC_DIR}/soloud/src)
3030
include_directories(${SRC_DIR})
3131
include (src.cmake)
32-
message("**************** SOLOUD SRC.CMAKE 2 ${TARGET_NAME}")
32+
message("**************** SOLOUD SRC.CMAKE END ${TARGET_NAME}")
3333

3434

3535

@@ -70,22 +70,23 @@ target_include_directories(${PLUGIN_NAME} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}
7070
target_link_libraries(${PLUGIN_NAME} PRIVATE ${log-lib} android)
7171

7272

73-
# Add opus library
74-
set(OPUS_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libopus.so)
75-
target_link_libraries(${PLUGIN_NAME} PRIVATE ${OPUS_LIBRARY})
76-
set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib")
77-
78-
# Add ogg library
79-
set(OGG_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libogg.so)
80-
target_link_libraries(${PLUGIN_NAME} PRIVATE ${OGG_LIBRARY})
81-
set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib")
82-
83-
# include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/ogg)
84-
# include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/opus)
85-
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
86-
add_compile_definitions(LIBOPUS_OGG_AVAILABLE)
73+
if(NOT DEFINED ENV{NO_OPUS_OGG_LIBS})
74+
message("NO_OPUS_OGG_LIBS has not been set. Linking Opus and Ogg libraries!")
75+
# Add opus library
76+
set(OPUS_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libopus.so)
77+
target_link_libraries(${PLUGIN_NAME} PRIVATE ${OPUS_LIBRARY})
78+
set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib")
8779

80+
# Add ogg library
81+
set(OGG_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libogg.so)
82+
target_link_libraries(${PLUGIN_NAME} PRIVATE ${OGG_LIBRARY})
83+
set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib")
8884

85+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
86+
else()
87+
message("NO_OPUS_OGG_LIBS has been set. Not linking Opus and Ogg libraries!")
88+
add_definitions(-DNO_OPUS_OGG_LIBS)
89+
endif()
8990

9091
# target_compile_definitions(flutter_soloud PUBLIC DART_SHARED_LIB)
9192

android/build.gradle

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ group = "flutter.soloud.flutter_soloud"
44
version = "1.0"
55

66
buildscript {
7-
// ext.kotlin_version = '1.7.10'
87
repositories {
98
google()
109
mavenCentral()
1110
}
1211

1312
dependencies {
1413
// The Android Gradle Plugin knows how to build native code with the NDK.
15-
classpath 'com.android.tools.build:gradle:7.3.1'
16-
// classpath("com.android.tools.build:gradle:8.1.0")
17-
// classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
14+
classpath("com.android.tools.build:gradle:8.7.0")
1815
}
1916
}
2017

@@ -32,7 +29,7 @@ android {
3229

3330
// Bumping the plugin compileSdk version requires all clients of this plugin
3431
// to bump the version in their app.
35-
compileSdk = 33
32+
compileSdk = 35
3633

3734
// Use the NDK version
3835
// declared in /android/app/build.gradle file of the Flutter project.
@@ -55,12 +52,34 @@ android {
5552
}
5653
}
5754

55+
defaultConfig {
56+
minSdk = 21
57+
58+
// Add this inside defaultConfig to affect all variants
59+
externalNativeBuild {
60+
cmake {
61+
// Pass environment variable to CMake
62+
arguments "-DNO_OPUS_OGG_LIBS=${System.getenv('NO_OPUS_OGG_LIBS') ?: 'false'}"
63+
64+
// Specify the correct target name
65+
targets "flutter_soloud_plugin"
66+
}
67+
}
68+
}
69+
70+
// Task to check if NO_OPUS_OGG_LIBS has changed. If yes the cmake task will be re-run
71+
tasks.whenTaskAdded { task ->
72+
if (task.name.contains("externalNativeBuild")) {
73+
// println "=== Flutter SoLoud Plugin ==="
74+
// println "NO_OPUS_OGG_LIBS=${System.getenv('NO_OPUS_OGG_LIBS') ?: 'false'}"
75+
76+
// Always run CMake configuration
77+
task.outputs.upToDateWhen { false }
78+
}
79+
}
80+
5881
compileOptions {
5982
sourceCompatibility = JavaVersion.VERSION_11
6083
targetCompatibility = JavaVersion.VERSION_11
6184
}
62-
63-
defaultConfig {
64-
minSdk = 21
65-
}
6685
}

0 commit comments

Comments
 (0)