Fix errors when parsing audio

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-15 12:26:35 +01:00
parent b0f75d9f10
commit 431126f893
19 changed files with 385 additions and 62 deletions

View File

@@ -1,9 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter_soloud/flutter_soloud.dart';
import 'package:wolf_3d_data_types/wolf_3d_data_types.dart';
import 'package:wolf_3d_synth/wolf_3d_synth.dart';
import 'package:wolf_dart/features/difficulty/difficulty.dart';
import 'package:wolf_dart/features/renderer/renderer.dart';
class DifficultyScreen extends StatelessWidget {
class DifficultyScreen extends StatefulWidget {
const DifficultyScreen(
this.data, {
super.key,
@@ -11,7 +13,86 @@ class DifficultyScreen extends StatelessWidget {
final WolfensteinData data;
bool get isShareware => data.version == GameVersion.shareware;
@override
State<DifficultyScreen> createState() => _DifficultyScreenState();
}
class _DifficultyScreenState extends State<DifficultyScreen> {
AudioSource? _menuMusicSource;
SoundHandle? _menuMusicHandle;
bool get isShareware => widget.data.version == GameVersion.shareware;
@override
void initState() {
super.initState();
_playMenuMusic();
}
Future<void> _playMenuMusic() async {
final soloud = SoLoud.instance;
if (!soloud.isInitialized) {
return;
}
// 2. We only want to play music if the IMF data actually exists
if (widget.data.music.isNotEmpty) {
// Get the first track (usually the menu theme "Wondering About My Loved Ones")
final music = widget.data.music.first;
// Render the hardware instructions into PCM and wrap in a WAV header
final pcmSamples = ImfRenderer.render(music);
final wavBytes = ImfRenderer.createWavFile(pcmSamples);
// 3. Load the bytes into SoLoud's memory
// The 'menu_theme.wav' string is just a dummy name to tell SoLoud it's dealing with a WAV format
_menuMusicSource = await soloud.loadMem('menu_theme.wav', wavBytes);
// 4. Play the source and tell it to loop continuously!
_menuMusicHandle = await soloud.play(
_menuMusicSource!,
looping: true,
);
}
}
@override
void dispose() {
_cleanupAudio();
super.dispose();
}
void _cleanupAudio() {
final soloud = SoLoud.instance;
// Stop the playback
if (_menuMusicHandle != null) {
soloud.stop(_menuMusicHandle!);
}
// Free the raw WAV data from C++ memory
if (_menuMusicSource != null) {
soloud.disposeSource(_menuMusicSource!);
}
}
void _startGame(Difficulty difficulty, {bool showGallery = false}) {
// Stop the music and clear memory right before we push the new route
_cleanupAudio();
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (_) => WolfRenderer(
widget.data,
difficulty: difficulty,
isShareware: isShareware,
showSpriteGallery: showGallery,
),
),
);
}
@override
Widget build(BuildContext context) {
@@ -19,18 +100,7 @@ class DifficultyScreen extends StatelessWidget {
backgroundColor: Colors.black,
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.red[900],
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => WolfRenderer(
data,
difficulty: Difficulty.bringEmOn,
isShareware: isShareware,
showSpriteGallery: true,
),
),
);
},
onPressed: () => _startGame(Difficulty.bringEmOn, showGallery: true),
child: const Icon(Icons.bug_report, color: Colors.white),
),
body: Center(
@@ -46,6 +116,7 @@ class DifficultyScreen extends StatelessWidget {
fontFamily: 'Courier',
),
),
const SizedBox(height: 40),
// --- Difficulty Buttons ---
ListView.builder(
@@ -64,17 +135,7 @@ class DifficultyScreen extends StatelessWidget {
borderRadius: BorderRadius.circular(4),
),
),
onPressed: () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (_) => WolfRenderer(
data,
difficulty: difficulty,
isShareware: isShareware,
),
),
);
},
onPressed: () => _startGame(difficulty),
child: Text(
difficulty.title,
style: const TextStyle(fontSize: 18),

View File

@@ -1,7 +1,14 @@
import 'package:flutter/material.dart';
import 'package:flutter_soloud/flutter_soloud.dart';
import 'package:wolf_dart/game_select_screen.dart';
void main() {
void main() async {
await SoLoud.instance.init(
sampleRate: 44100, // Audio quality
bufferSize: 2048, // Buffer size affects latency
channels: Channels.stereo,
);
runApp(
const MaterialApp(
home: GameSelectScreen(),