Skip to content

Commit

Permalink
fixed opened WAV files not being released (by making tmp copy)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurHeitmann committed Dec 18, 2022
1 parent 98ebe27 commit df708a9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
18 changes: 17 additions & 1 deletion lib/stateManagement/openFileTypes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,15 @@ class WavFileData with ChangeNotifier, HasUuid, AudioFileData {

@override
Future<void> load() async {
resource = await audioResourcesManager.getAudioResource(path);
resource = await audioResourcesManager.getAudioResource(path, makeCopy: true);
notifyListeners();
}

@override
void dispose() {
resource?.dispose();
super.dispose();
}
}
class WemFileData extends OpenFileData with AudioFileData {
ValueNotifier<WavFileData?> overrideData = ValueNotifier(null);
Expand Down Expand Up @@ -616,6 +622,7 @@ class WemFileData extends OpenFileData with AudioFileData {
await backupFile(path);
var wav = overrideData.value!;
await wavToWem(wav.path, path, basename(path).contains("BGM"));
overrideData.value!.dispose();
overrideData.value = null;

// reload
Expand All @@ -629,6 +636,15 @@ class WemFileData extends OpenFileData with AudioFileData {
onOverrideApplied.notifyListeners();
}

Future<void> removeOverride() async {
if (overrideData.value == null)
return;

overrideData.value!.dispose();
overrideData.value = null;
notifyListeners();
}

@override
Undoable takeSnapshot() {
var snapshot = WemFileData(
Expand Down
17 changes: 16 additions & 1 deletion lib/stateManagement/otherFileTypes/audioResourceManager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'dart:math';

import 'package:mutex/mutex.dart';
import 'package:path/path.dart';
import 'package:tuple/tuple.dart';

import '../../fileTypeUtils/audio/riffParser.dart';
Expand All @@ -28,11 +29,12 @@ class AudioResource {
class AudioResourcesManager {
final Map<String, AudioResource> _resources = {};
final Map<String, Mutex> _loadingMutexes = {};
String? _tmpDir;

/// Returns a reference to the audio file at the given path.
/// If the file is already loaded, it will return the same reference.
/// If the file is not loaded, it will load it.
Future<AudioResource> getAudioResource(String path) async {
Future<AudioResource> getAudioResource(String path, { bool makeCopy = false }) async {
if (!_loadingMutexes.containsKey(path))
_loadingMutexes[path] = Mutex();
await _loadingMutexes[path]!.acquire();
Expand All @@ -48,6 +50,9 @@ class AudioResourcesManager {
if (path.endsWith(".wem")) {
wavPath = await wemToWavTmp(path);
deleteOnDispose = true;
} else if (makeCopy) {
wavPath = await _copyToTmp(path);
deleteOnDispose = true;
}

var riff = await RiffFile.fromFile(wavPath);
Expand All @@ -71,6 +76,13 @@ class AudioResourcesManager {
return resource;
}

Future<String> _copyToTmp(String path) async {
_tmpDir ??= (await Directory.systemTemp.createTemp("tmpWav")).path;
var tmpPath = join(_tmpDir!, basename(path));
await File(path).copy(tmpPath);
return tmpPath;
}

Future<void> reloadAudioResource(AudioResource resource) async {
if (resource.wemPath != null)
resource.wavPath = await wemToWavTmp(resource.wemPath!);
Expand All @@ -94,6 +106,9 @@ class AudioResourcesManager {
if (await File(path).exists())
await File(path).delete();
}));

if (_tmpDir != null && await Directory(_tmpDir!).exists())
await Directory(_tmpDir!).delete(recursive: true);
}

/// Disposes the reference to the audio file at the given path.
Expand Down
3 changes: 1 addition & 2 deletions lib/widgets/propEditors/otherFileTypes/wemFileEditor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class _WemFileEditorState extends ChangeNotifierState<WemFileEditor> {
return;
}
var wavFile = WavFileData(file.path!);
await wavFile.load();
widget.wem.overrideData.value = wavFile;
}

Expand All @@ -80,7 +79,7 @@ class _WemFileEditorState extends ChangeNotifierState<WemFileEditor> {
const SizedBox(width: 10),
if (widget.wem.overrideData.value != null)
ElevatedButton(
onPressed: () => widget.wem.overrideData.value = null,
onPressed: () => widget.wem.removeOverride(),
style: getTheme(context).dialogSecondaryButtonStyle,
child: const Text("Remove"),
),
Expand Down

0 comments on commit df708a9

Please sign in to comment.