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: setInaudibleBehavior #96 #160

Merged
merged 4 commits into from
Jan 3, 2025
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
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "Flutter debug",
"type": "dart",
"request": "launch",
"program": "lib/waveform/waveform.dart",
"program": "lib/main.dart",
"flutterMode": "debug",
"cwd": "${workspaceFolder}/example"
},
Expand All @@ -24,7 +24,7 @@
"name": "Flutter release",
"type": "dart",
"request": "launch",
"program": "lib/buffer_stream/websocket.dart",
"program": "lib/main.dart",
"flutterMode": "release",
"cwd": "${workspaceFolder}/example"
},
Expand Down
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"label": "compile linux debug",
"command": "cd ${workspaceFolder}/example; flutter build linux -t lib/waveform/waveform.dart --debug",
"command": "cd ${workspaceFolder}/example; flutter build linux -t lib/main.dart --debug",
"type": "shell"
},
{
Expand All @@ -29,7 +29,7 @@
},
{
"label": "compile web debug",
"command": "cd ${workspaceFolder}/example; flutter run -d chrome --web-renderer canvaskit --web-browser-flag '--disable-web-security' -t lib/waveform/waveform.dart --release",
"command": "cd ${workspaceFolder}/example; flutter run -d chrome --web-renderer canvaskit --web-browser-flag '--disable-web-security' -t lib/main.dart --release",
"type": "shell"
},
{
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- fix: clicks and pops when changing waveform frequency #156
- added BufferStream #148. Now it's possible to add audio data and listen to them. It provides a customizable buffering length which automatycally pause the playing handle for example when receiving audio data from the web.
- added Limiter and Compressor filters (see `example/lib/filters`)
- added `setInaudibleBehavior`. When a 3D sound is too far to be eard, you can tell `flutter_soloud` to kill it.

### 2.1.7 (29 Oct 2024)
- added `listPlaybackDevices` to get all the OS output devices available.
Expand Down
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:developer' as dev;

import 'package:flutter/foundation.dart';
Expand Down
13 changes: 13 additions & 0 deletions lib/src/bindings/bindings_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,19 @@ abstract class FlutterSoLoud {
@mustBeOverridden
bool getProtectVoice(SoundHandle handle);

/// Set the inaudible behavior of a live sound. By default,
/// if a sound is inaudible, it's paused, and will resume when it
/// becomes audible again. With this function you can tell SoLoud
/// to either kill the sound if it becomes inaudible, or to keep
/// ticking the sound even if it's inaudible.
///
/// [handle] handle to check.
/// [mustTick] whether to keep ticking or not when the sound becomes
/// inaudible.
/// [kill] whether to kill the sound or not when the sound becomes inaudible.
@mustBeOverridden
void setInaudibleBehavior(SoundHandle handle, bool mustTick, bool kill);

/// Set a sound's protection state.
///
/// Normally, if you try to play more sounds than there are voices,
Expand Down
20 changes: 20 additions & 0 deletions lib/src/bindings/bindings_player_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,26 @@ class FlutterSoLoudFfi extends FlutterSoLoud {
late final _setProtectVoice =
_setProtectVoicePtr.asFunction<void Function(int, int)>();

@override
void setInaudibleBehavior(
SoundHandle handle,
bool mustTick,
bool kill,
) {
return _setInaudibleBehavior(
handle.id,
mustTick,
kill,
);
}

late final _setInaudibleBehaviorPtr = _lookup<
ffi.NativeFunction<
ffi.Void Function(
ffi.UnsignedInt, ffi.Bool, ffi.Bool)>>('setInaudibleBehavior');
late final _setInaudibleBehavior =
_setInaudibleBehaviorPtr.asFunction<void Function(int, bool, bool)>();

@override
int getMaxActiveVoiceCount() {
return _getMaxActiveVoiceCount();
Expand Down
5 changes: 5 additions & 0 deletions lib/src/bindings/bindings_player_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,11 @@ class FlutterSoLoudWeb extends FlutterSoLoud {
return wasmSetProtectVoice(handle.id, protect ? 1 : 0);
}

@override
void setInaudibleBehavior(SoundHandle handle, bool mustTick, bool kill) {
return wasmSetInaudibleBehavior(handle.id, mustTick ? 1 : 0, kill ? 1 : 0);
}

@override
int getMaxActiveVoiceCount() {
return wasmGetMaxActiveVoiceCount();
Expand Down
3 changes: 3 additions & 0 deletions lib/src/bindings/js_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ external int wasmGetVoiceCount();
@JS('Module._getProtectVoice')
external int wasmGetProtectVoice(int handle);

@JS('Module._setInaudibleBehavior')
external void wasmSetInaudibleBehavior(int handle, int mustTick, int kill);

@JS('Module._setProtectVoice')
external void wasmSetProtectVoice(int handle, int protect);

Expand Down
47 changes: 47 additions & 0 deletions lib/src/soloud.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,51 @@ interface class SoLoud {
_controller.soLoudFFI.setProtectVoice(handle, protect);
}

/// Set the inaudible behavior of a live 3D sound. By default,
/// if a sound is inaudible, it's paused, and will resume when it
/// becomes audible again. With this function you can tell SoLoud
/// to either kill the sound if it becomes inaudible, or to keep
/// ticking the sound even if it's inaudible.
///
/// [handle] handle to check.
/// [mustTick] whether to keep ticking or not when the sound becomes
/// inaudible.
/// [kill] whether to kill the sound or not when the sound becomes inaudible.
///
/// **Example**:
/// ```dart
/// final sound = SoLoud.instance.load('path/to/sound.mp3');
/// final handle = SoLoud.instance.play3d(sound, 0, 0, 0);
/// double xPos = 0;
///
/// // set the sound to be inaudible if it's more than 10 units away
/// SoLoud.instance.set3dSourceMinMaxDistance(handle, 0, 10);
/// // set the attenuation to `LINEAR_DISTANCE` and when its position
/// // is 10 units away, the volume will be 0 (inaudible).
/// SoLoud.instance.set3dSourceAttenuation(handle, 2, 1);
///
/// // if the sound is inaudible, it will be killed and the [handle]
/// // becomes invalid.
/// SoLoud.instance.setInaudibleBehavior(handle, false, true);
///
/// // here we shift the sound position away (up to you to cancel the Timer!)
/// // When [xPos] reaches 10 units, the handle will stop.
/// Timer.periodic(
/// const Duration(milliseconds: 100),
/// (timer) {
/// SoLoud.instance
/// .set3dSourcePosition(handle, xPos += 0.1, 0, 0);
/// },
/// );
/// ```
@mustBeOverridden
void setInaudibleBehavior(SoundHandle handle, bool mustTick, bool kill) {
if (!isInitialized) {
throw const SoLoudNotInitializedException();
}
_controller.soLoudFFI.setInaudibleBehavior(handle, mustTick, kill);
}

/// Gets the current maximum active voice count.
int getMaxActiveVoiceCount() {
if (!isInitialized) {
Expand Down Expand Up @@ -2289,6 +2334,7 @@ interface class SoLoud {

/// Sets the minimum and maximum distance parameters
/// of a live 3D audio source.
/// Default values are 1 and 1000000.
void set3dSourceMinMaxDistance(
SoundHandle handle, double minDistance, double maxDistance) {
_controller.soLoudFFI
Expand All @@ -2304,6 +2350,7 @@ interface class SoLoud {
/// 2 LINEAR_DISTANCE Linear distance attenuation model
/// 3 EXPONENTIAL_DISTANCE Exponential distance attenuation model
/// ```
/// The default values are NO_ATTENUATION and 1.
///
/// See https://solhsa.com/soloud/concepts3d.html.
void set3dSourceAttenuation(
Expand Down
17 changes: 17 additions & 0 deletions src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,23 @@ extern "C"
player.get()->setProtectVoice(handle, protect);
}

/// Set the inaudible behavior of a live sound. By default,
/// if a sound is inaudible, it's paused, and will resume when it
/// becomes audible again. With this function you can tell SoLoud
/// to either kill the sound if it becomes inaudible, or to keep
/// ticking the sound even if it's inaudible.
///
/// [handle] handle to check.
/// [mustTick] whether to keep ticking or not when the sound becomes inaudible.
/// [kill] whether to kill the sound or not when the sound becomes inaudible.
FFI_PLUGIN_EXPORT void setInaudibleBehavior(unsigned int handle, bool mustTick, bool kill)
{
if (player.get() == nullptr || !player.get()->isInited() ||
!player.get()->isValidHandle(handle))
return;
player.get()->setInaudibleBehavior(handle, mustTick, kill);
}

/// Get the current maximum active voice count.
FFI_PLUGIN_EXPORT unsigned int getMaxActiveVoiceCount()
{
Expand Down
4 changes: 3 additions & 1 deletion src/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#ifndef ENUMS_H
#define ENUMS_H

#include <stdbool.h> // for ffigen to not complain about bool type

/// Possible player errors.
///
/// WARNING: Keep these in sync with `lib/src/enums.dart`.
Expand Down Expand Up @@ -127,7 +129,7 @@ typedef struct PCMformat
unsigned int sampleRate;
unsigned int channels;
unsigned int bytesPerSample;
BufferPcmType dataType;
enum BufferPcmType dataType;
} PCMformat;


Expand Down
13 changes: 10 additions & 3 deletions src/ffi_gen_tmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@

//--------------------- copy here the new functions to generate

// Get the current buffer size in bytes of this sound with hash [hash].
// [hash] the hash of the stream sound.
FFI_PLUGIN_EXPORT enum PlayerErrors getBufferSize(unsigned int hash, unsigned int *sizeInBytes);
/// Set the inaudible behavior of a live sound. By default,
/// if a sound is inaudible, it's paused, and will resume when it
/// becomes audible again. With this function you can tell SoLoud
/// to either kill the sound if it becomes inaudible, or to keep
/// ticking the sound even if it's inaudible.
///
/// [handle] handle to check.
/// [mustTick] whether to keep ticking or not when the sound becomes inaudible.
/// [kill] whether to kill the sound or not when the sound becomes inaudible.
FFI_PLUGIN_EXPORT void setInaudibleBehavior(unsigned int handle, bool mustTick, bool kill);
5 changes: 5 additions & 0 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,11 @@ void Player::setProtectVoice(SoLoud::handle handle, bool protect)
soloud.setProtectVoice(handle, protect);
}

void Player::setInaudibleBehavior(SoLoud::handle handle, bool mustTick, bool kill)
{
soloud.setInaudibleBehavior(handle, mustTick, kill);
}

unsigned int Player::getMaxActiveVoiceCount()
{
return soloud.getMaxActiveVoiceCount();
Expand Down
7 changes: 7 additions & 0 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,13 @@ class Player
/// https://github.com/jarikomppa/soloud/issues/298
void setProtectVoice(SoLoud::handle handle, bool protect);

/// @brief Set the inaudible behavior of a live sound. By default,
/// if a sound is inaudible, it's paused, and will resume when it
/// becomes audible again. With this function you can tell SoLoud
/// to either kill the sound if it becomes inaudible, or to keep
/// ticking the sound even if it's inaudible.
void setInaudibleBehavior(SoLoud::handle handle, bool mustTick, bool kill);

/// @brief Get the current maximum active voice count.
unsigned int getMaxActiveVoiceCount();

Expand Down
4 changes: 4 additions & 0 deletions web/libflutter_soloud_plugin.js

Large diffs are not rendered by default.

Binary file modified web/libflutter_soloud_plugin.wasm
Binary file not shown.
Loading