Delegate all audio management to the new audio package, then manage that through a new wolf3d class
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
92
packages/wolf_3d_synth/lib/src/wolf_3d_audio.dart
Normal file
92
packages/wolf_3d_synth/lib/src/wolf_3d_audio.dart
Normal file
@@ -0,0 +1,92 @@
|
||||
import 'package:flutter_soloud/flutter_soloud.dart';
|
||||
import 'package:wolf_3d_data_types/wolf_3d_data_types.dart';
|
||||
import 'package:wolf_3d_synth/src/imf_renderer.dart';
|
||||
|
||||
class WolfAudio {
|
||||
bool _isInitialized = false;
|
||||
|
||||
// --- Music State ---
|
||||
AudioSource? _currentMusicSource;
|
||||
SoundHandle? _currentMusicHandle;
|
||||
|
||||
/// Initializes the SoLoud audio engine.
|
||||
Future<void> init() async {
|
||||
if (_isInitialized) return;
|
||||
|
||||
try {
|
||||
await SoLoud.instance.init(
|
||||
sampleRate: 44100,
|
||||
bufferSize: 2048,
|
||||
channels: Channels.stereo,
|
||||
);
|
||||
_isInitialized = true;
|
||||
print("WolfAudio: SoLoud initialized successfully.");
|
||||
} catch (e) {
|
||||
print("WolfAudio: Failed to initialize SoLoud - $e");
|
||||
}
|
||||
}
|
||||
|
||||
/// Disposes of the audio engine and frees resources.
|
||||
void dispose() {
|
||||
stopMusic();
|
||||
SoLoud.instance.deinit();
|
||||
_isInitialized = false;
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// MUSIC MANAGEMENT
|
||||
// ==========================================
|
||||
|
||||
/// Renders and plays a specific IMF music track.
|
||||
Future<void> playMusic(ImfMusic track, {bool looping = true}) async {
|
||||
if (!_isInitialized) return;
|
||||
|
||||
// Stop currently playing music to prevent overlap
|
||||
stopMusic();
|
||||
|
||||
try {
|
||||
// Render hardware instructions into PCM and wrap in WAV
|
||||
final pcmSamples = ImfRenderer.render(track);
|
||||
final wavBytes = ImfRenderer.createWavFile(pcmSamples);
|
||||
|
||||
_currentMusicSource = await SoLoud.instance.loadMem(
|
||||
'track.wav',
|
||||
wavBytes,
|
||||
);
|
||||
_currentMusicHandle = await SoLoud.instance.play(
|
||||
_currentMusicSource!,
|
||||
looping: looping,
|
||||
);
|
||||
} catch (e) {
|
||||
print("WolfAudio: Error playing music track - $e");
|
||||
}
|
||||
}
|
||||
|
||||
/// Halts playback and frees memory for the current track.
|
||||
void stopMusic() {
|
||||
if (!_isInitialized) return;
|
||||
|
||||
if (_currentMusicHandle != null) {
|
||||
SoLoud.instance.stop(_currentMusicHandle!);
|
||||
_currentMusicHandle = null;
|
||||
}
|
||||
if (_currentMusicSource != null) {
|
||||
SoLoud.instance.disposeSource(_currentMusicSource!);
|
||||
_currentMusicSource = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Pauses the current track.
|
||||
void pauseMusic() {
|
||||
if (_isInitialized && _currentMusicHandle != null) {
|
||||
SoLoud.instance.setPause(_currentMusicHandle!, true);
|
||||
}
|
||||
}
|
||||
|
||||
/// Resumes a paused track.
|
||||
void resumeMusic() {
|
||||
if (_isInitialized && _currentMusicHandle != null) {
|
||||
SoLoud.instance.setPause(_currentMusicHandle!, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,4 +3,4 @@
|
||||
/// More dartdocs go here.
|
||||
library;
|
||||
|
||||
export 'src/imf_renderer.dart' show ImfRenderer;
|
||||
export 'src/wolf_3d_audio.dart' show WolfAudio;
|
||||
|
||||
@@ -9,6 +9,7 @@ environment:
|
||||
resolution: workspace
|
||||
|
||||
dependencies:
|
||||
flutter_soloud: ^3.5.1
|
||||
wolf_3d_data_types:
|
||||
|
||||
dev_dependencies:
|
||||
|
||||
Reference in New Issue
Block a user