Skip to content

Commit

Permalink
fixed test for getPan float conversion check
Browse files Browse the repository at this point in the history
  • Loading branch information
alnitak committed Jun 5, 2024
1 parent 300f9a5 commit 4eadf0f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
9 changes: 7 additions & 2 deletions example/tests/tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,13 @@ Future<void> testPan() async {

SoLoud.instance.setPan(handle, -0.8);
var pan = SoLoud.instance.getPan(handle);
assert(pan == -0.8, 'setPan() or getPan() failed!');
assert(closeTo(pan, -0.8, 0.00001), 'setPan() or getPan() failed!');

await delay(1000);

SoLoud.instance.setPan(handle, 0.8);
pan = SoLoud.instance.getPan(handle);
assert(pan == 0.8, 'setPan() or getPan() failed!');
assert(closeTo(pan, 0.8, 0.00001), 'setPan() or getPan() failed!');
await delay(1000);

deinit();
Expand Down Expand Up @@ -525,6 +526,10 @@ Future<void> loadAsset() async {
});
}

bool closeTo(num value, num expected, num epsilon) {
return (value - expected).abs() <= epsilon.abs();
}

void printError(Object error, StackTrace stack) {
stderr.writeln('TEST error: $error\nstack: $stack');
exitCode = 1;
Expand Down
6 changes: 2 additions & 4 deletions lib/src/bindings_player_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -746,10 +746,8 @@ class FlutterSoLoudFfi {
/// middle and and 1 is right.
double getPan(int handle) {
// Note that because of the float<=>double conversion precision error
// (SoLoud lib uses floats), the returned value is not precise. Here we set
// a rounding of 5 digits
final ret = (_getPan(handle) * 100000).toInt() / 100000;
return ret;
// (SoLoud lib uses floats), the returned value is not precise.
return _getPan(handle);
}

late final _getPanPtr =
Expand Down
32 changes: 32 additions & 0 deletions lib/src/soloud.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,12 @@ interface class SoLoud {
/// and `1.0` meaning full volume.
///
/// Throws [SoLoudNotInitializedException] if the engine is not initialized.
///
/// Note that if you `setGlobalVolume()` to `0.8` and then
/// `getGlobalVolume()`, you might get a slightly different number,
/// such as `0.800000042353`.
/// This is expected since the internal audio engine uses float
/// instead of double, and so there are rounding errors.
double getGlobalVolume() {
if (!isInitialized) {
throw const SoLoudNotInitializedException();
Expand All @@ -1192,6 +1198,12 @@ interface class SoLoud {
/// to `1.0` (meaning full volume).
///
/// Throws [SoLoudNotInitializedException] if the engine is not initialized.
///
/// Note that if you `setGlobalVolume()` to `0.8` and then
/// `getGlobalVolume()`, you might get a slightly different number,
/// such as `0.800000042353`.
/// This is expected since the internal audio engine uses float
/// instead of double, and so there are rounding errors.
void setGlobalVolume(double volume) {
if (!isInitialized) {
throw const SoLoudNotInitializedException();
Expand All @@ -1211,6 +1223,11 @@ interface class SoLoud {
/// and `1.0` means its playing at full volume.
///
/// Throws [SoLoudNotInitializedException] if the engine is not initialized.
///
/// Note that if you `setVolume()` to `0.8` and then `getVolume()`, you might
/// get a slightly different number, such as `0.800000042353`.
/// This is expected since the internal audio engine uses float
/// instead of double, and so there are rounding errors.
double getVolume(SoundHandle handle) {
if (!isInitialized) {
throw const SoLoudNotInitializedException();
Expand All @@ -1225,6 +1242,11 @@ interface class SoLoud {
/// to `1.0` (meaning it should play at full volume).
///
/// Throws [SoLoudNotInitializedException] if the engine is not initialized.
///
/// Note that if you `setVolume()` to `0.8` and then `getVolume()`, you might
/// get a slightly different number, such as `0.800000042353`.
/// This is expected since the internal audio engine uses float
/// instead of double, and so there are rounding errors.
void setVolume(SoundHandle handle, double volume) {
if (!isInitialized) {
throw const SoLoudNotInitializedException();
Expand All @@ -1239,6 +1261,11 @@ interface class SoLoud {
/// middle and and 1 is right.
///
/// Throws [SoLoudNotInitializedException] if the engine is not initialized.
///
/// Note that if you `setPan()` to `0.8` and then `getPan()`, you might
/// get a slightly different number, such as `0.800000042353`.
/// This is expected since the internal audio engine uses float
/// instead of double, and so there are rounding errors.
double getPan(SoundHandle handle) {
if (!isInitialized) {
throw const SoLoudNotInitializedException();
Expand All @@ -1253,6 +1280,11 @@ interface class SoLoud {
/// middle and and 1 is right.
///
/// Throws [SoLoudNotInitializedException] if the engine is not initialized.
///
/// Note that if you `setPan()` to `0.8` and then `getPan()`, you might
/// get a slightly different number, such as `0.800000042353`.
/// This is expected since the internal audio engine uses float
/// instead of double, and so there are rounding errors.
void setPan(SoundHandle handle, double pan) {
if (!isInitialized) {
throw const SoLoudNotInitializedException();
Expand Down

0 comments on commit 4eadf0f

Please sign in to comment.