Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alnitak committed Oct 23, 2024
1 parent d53e951 commit 0636011
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 43 deletions.
12 changes: 1 addition & 11 deletions example/lib/output_device/output_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,7 @@ class _HelloFlutterSoLoudState extends State<HelloFlutterSoLoud> {
body: Center(
child: DropdownMenu(
controller: textEditingController,
onSelected: (value) async {
/// When changing the output device, we need to reinitialize
/// the player. All existing audio sources will be stopped and
/// disposed as well.
// currentDevice = devices[value!];
// SoLoud.instance.deinit();
// await SoLoud.instance.init(deviceId: currentDevice.id);

// currentSound = await SoLoud.instance
// .loadAsset('assets/audio/8_bit_mentality.mp3');
// await SoLoud.instance.play(currentSound!, looping: true);
onSelected: (value) {
SoLoud.instance.changeDevice(newDevice: devices[value!]);
},
dropdownMenuEntries: [
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.1.5"
version: "2.1.7"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down
1 change: 1 addition & 0 deletions lib/flutter_soloud.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export 'src/enums.dart' hide PlayerErrors, PlayerStateNotification;
export 'src/exceptions/exceptions.dart';
export 'src/filter_params.dart';
export 'src/filters/filters.dart' show FilterType;
export 'src/helpers/playback_device.dart';
export 'src/soloud.dart';
export 'src/sound_handle.dart';
export 'src/sound_hash.dart';
Expand Down
1 change: 1 addition & 0 deletions lib/src/bindings/bindings_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:typed_data';
import 'package:flutter_soloud/src/bindings/audio_data.dart';
import 'package:flutter_soloud/src/enums.dart';
import 'package:flutter_soloud/src/filters/filters.dart';
import 'package:flutter_soloud/src/helpers/playback_device.dart';
import 'package:flutter_soloud/src/sound_handle.dart';
import 'package:flutter_soloud/src/sound_hash.dart';
import 'package:meta/meta.dart';
Expand Down
1 change: 1 addition & 0 deletions lib/src/bindings/bindings_player_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:flutter_soloud/src/bindings/bindings_player.dart';
import 'package:flutter_soloud/src/enums.dart';
import 'package:flutter_soloud/src/exceptions/exceptions.dart';
import 'package:flutter_soloud/src/filters/filters.dart';
import 'package:flutter_soloud/src/helpers/playback_device.dart';
import 'package:flutter_soloud/src/sound_handle.dart';
import 'package:flutter_soloud/src/sound_hash.dart';
import 'package:logging/logging.dart';
Expand Down
1 change: 1 addition & 0 deletions lib/src/bindings/bindings_player_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter_soloud/src/bindings/js_extension.dart';
import 'package:flutter_soloud/src/enums.dart';
import 'package:flutter_soloud/src/exceptions/exceptions.dart';
import 'package:flutter_soloud/src/filters/filters.dart';
import 'package:flutter_soloud/src/helpers/playback_device.dart';
import 'package:flutter_soloud/src/sound_handle.dart';
import 'package:flutter_soloud/src/sound_hash.dart';
import 'package:flutter_soloud/src/worker/worker.dart';
Expand Down
28 changes: 7 additions & 21 deletions lib/src/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ enum PlayerErrors {
soundHandleNotFound(17),

/// Error getting filter parameter.
filterParameterGetError(18);
filterParameterGetError(18),

/// No playback devices were found.
noPlaybackDevicesFound(19);

const PlayerErrors(this.value);

Expand Down Expand Up @@ -117,6 +120,9 @@ enum PlayerErrors {
case PlayerErrors.filterParameterGetError:
return 'An error (nan or inf value) occurred while getting a '
'filter parameter!';
case PlayerErrors.noPlaybackDevicesFound:
return 'No playback devices were found while initializing engine or '
'when changing the output device.';
}
}

Expand Down Expand Up @@ -265,23 +271,3 @@ enum Channels {
/// The channels count.
final int count;
}

/// CaptureDevice exposed to Dart
final class PlaybackDevice {
/// Constructs a new [PlaybackDevice].
// ignore: avoid_positional_boolean_parameters
const PlaybackDevice(this.id, this.isDefault, this.name);

/// The ID of the device.
final int id;

/// Whether this is the default playback device.
final bool isDefault;

/// The name of the device.
final String name;

@override
String toString() =>
'\nPlaybackDevice(id: $id, isDefault: $isDefault, name: $name)';
}
2 changes: 2 additions & 0 deletions lib/src/exceptions/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ abstract class SoLoudCppException extends SoLoudException {
return const SoLoudSoundHandleNotFoundCppException();
case PlayerErrors.filterParameterGetError:
return const SoLoudFilterParameterGetErrorCppException();
case PlayerErrors.noPlaybackDevicesFound:
return const SoLoudNoPlaybackDevicesFoundCppException();
}
}

Expand Down
11 changes: 11 additions & 0 deletions lib/src/exceptions/exceptions_from_cpp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,14 @@ class SoLoudReadSamplesFailedToReadPcmFramesCppException
String get description => 'An error occurred while reading PCM frames. '
'(on the C++ side).';
}

/// An error occurred when reading PCM frames.
class SoLoudNoPlaybackDevicesFoundCppException extends SoLoudCppException {
/// Creates a new [SoLoudNoPlaybackDevicesFoundCppException].
const SoLoudNoPlaybackDevicesFoundCppException([super.message]);

@override
String get description => 'No playback devices were found while '
'initializing engine or when changing the output device. '
'(on the C++ side).';
}
28 changes: 28 additions & 0 deletions lib/src/helpers/playback_device.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:flutter_soloud/src/soloud.dart';

/// CaptureDevice exposed to Dart.
///
/// Used to get a list available playback devices by calling
/// [SoLoud.listPlaybackDevices].
/// Once you have a list of playback devices you can use
/// [SoLoud.changeDevice] to change the output device while the engine
/// is running or use [SoLoud.init] to set the output device while
/// initializing the engine.
final class PlaybackDevice {
/// Constructs a new [PlaybackDevice].
// ignore: avoid_positional_boolean_parameters
const PlaybackDevice(this.id, this.isDefault, this.name);

/// The ID of the device.
final int id;

/// Whether this is the default playback device.
final bool isDefault;

/// The name of the device.
final String name;

@override
String toString() =>
'\nPlaybackDevice(id: $id, isDefault: $isDefault, name: $name)';
}
10 changes: 8 additions & 2 deletions lib/src/soloud.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:flutter_soloud/src/bindings/soloud_controller.dart';
import 'package:flutter_soloud/src/enums.dart';
import 'package:flutter_soloud/src/exceptions/exceptions.dart';
import 'package:flutter_soloud/src/filters/filters.dart';
import 'package:flutter_soloud/src/helpers/playback_device.dart';
import 'package:flutter_soloud/src/sound_handle.dart';
import 'package:flutter_soloud/src/sound_hash.dart';
import 'package:flutter_soloud/src/utils/loader.dart';
Expand Down Expand Up @@ -281,6 +282,8 @@ interface class SoLoud {
/// If you call any other methods (such as [play]) before initialization
/// completes, those calls will be ignored and you will get
/// a [SoLoudNotInitializedException] exception.
/// Could throw [SoLoudNoPlaybackDevicesFoundCppException] if there is not a
/// playback device available or if the given [device] is not found.
///
/// NOTE: Calling this method while the engine is already initialized will
/// first deinitialize the engine and then reinitialize it. This means
Expand Down Expand Up @@ -361,6 +364,7 @@ interface class SoLoud {
await _loader.initialize();
} else {
_log.severe('initialize() failed with error: $error');
throw SoLoudCppException.fromPlayerError(error);
}
}

Expand All @@ -371,17 +375,19 @@ interface class SoLoud {
/// default device (iOS and MacOS?).
///
/// Throws [SoLoudNotInitializedException] if the engine is not initialized.
PlayerErrors changeDevice({PlaybackDevice? newDevice}) {
/// Throws [SoLoudNoPlaybackDevicesFoundCppException] if the given [newDevice]
/// is not found.
void changeDevice({PlaybackDevice? newDevice}) {
if (!isInitialized) {
throw const SoLoudNotInitializedException();
}

final deviceId = newDevice?.id ?? -1;
final error = _controller.soLoudFFI.changeDevice(deviceId);
_logPlayerError(error, from: 'changeDevice() result');
if (error != PlayerErrors.noError) {
throw SoLoudCppException.fromPlayerError(error);
}
return error;
}

/// Lists all OS available playback devices.
Expand Down
4 changes: 4 additions & 0 deletions src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ extern "C"
{
bool hasSpecialChar = false;
/// check if the device name has some strange chars (happens on Linux)
/// It happens that some results had the name composed of non-text
/// ASCII characters with values ​​<0x20 (blank space). Doesn't happen on
/// my Linux anymore (maybe was a bug on audio drivers?), but worth
/// checking to be sure.
for (int n = 0; n < 5; n++)
{
if (d[i].name[n] < 0x20)
Expand Down
4 changes: 3 additions & 1 deletion src/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ typedef enum PlayerErrors
/// Audio handle is not found.
soundHandleNotFound = 17,
/// Error getting filter parameter.
filterParameterGetError = 18
filterParameterGetError = 18,
/// No playback devices were found.
noPlaybackDevicesFound = 19,
} PlayerErrors_t;

/// Possible read sample errors
Expand Down
8 changes: 5 additions & 3 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ PlayerErrors Player::init(unsigned int sampleRate, unsigned int bufferSize, unsi
// Calling this will init [pPlaybackInfos]
auto const devices = listPlaybackDevices();
if (devices.size() == 0 || deviceID >= devices.size())
return backendNotInited;
return noPlaybackDevicesFound;
playbackInfos_id = &pPlaybackInfos[deviceID].id;
}

Expand Down Expand Up @@ -73,7 +73,7 @@ PlayerErrors Player::changeDevice(int deviceID)
// Calling this will init [pPlaybackInfos]
auto const devices = listPlaybackDevices();
if (devices.size() == 0 || deviceID >= devices.size())
return backendNotInited;
return noPlaybackDevicesFound;
playbackInfos_id = &pPlaybackInfos[deviceID].id;

SoLoud::result result = soloud.miniaudio_changeDevice(playbackInfos_id);
Expand All @@ -95,7 +95,7 @@ std::vector<PlaybackDevice> Player::listPlaybackDevices()
ma_result result;
if ((result = ma_context_init(NULL, 0, NULL, &context)) != MA_SUCCESS)
{
printf("Failed to initialize context %d\n", result);
// Failed to initialize audio context.
return ret;
}

Expand Down Expand Up @@ -191,6 +191,8 @@ const std::string Player::getErrorString(PlayerErrors errorCode) const
return "error: audio handle is not found!";
case filterParameterGetError:
return "error: getting filter parameter error!";
case noPlaybackDevicesFound:
return "error: no playback devices found!";
}
return "Other error";
}
Expand Down
3 changes: 0 additions & 3 deletions src/soloud/src/core/soloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,11 @@ namespace SoLoud
result Soloud::miniaudio_changeDevice(void *pPlaybackInfos_id)
{
#if defined(WITH_MINIAUDIO)
// lockAudioMutex_internal();

int ret = 0;
if (mAudioThreadMutex != NULL)
{
ret = miniaudio_changeDevice_impl(pPlaybackInfos_id);
}
// unlockAudioMutex_internal();
return ret;
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion web/libflutter_soloud_plugin.js

Large diffs are not rendered by default.

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

0 comments on commit 0636011

Please sign in to comment.