Skip to content

Commit 87a6e5a

Browse files
isHarryhK0lb3
authored andcommitted
fix: concurrent issue to pyfmodex system
1 parent 7a21b70 commit 87a6e5a

1 file changed

Lines changed: 27 additions & 23 deletions

File tree

‎UnityPy/export/AudioClipConverter.py‎

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import ctypes
44
import os
55
import platform
6+
from threading import Lock
67
from typing import TYPE_CHECKING, Dict, Union
78

89
from UnityPy.streams import EndianBinaryWriter
@@ -111,6 +112,7 @@ def extract_audioclip_samples(
111112
return dump_samples(audio, audio_data, convert_pcm_float)
112113

113114

115+
system_lock = Lock()
114116
system_param = ()
115117
system_instance = None
116118

@@ -137,31 +139,33 @@ def dump_samples(
137139
if not pyfmodex:
138140
return {}
139141

140-
# init system
141-
system = get_pyfmodex_system_instance(clip.m_Channels, pyfmodex.flags.INIT_FLAGS.NORMAL)
142-
143-
sound = system.create_sound(
144-
bytes(audio_data),
145-
pyfmodex.flags.MODE.OPENMEMORY,
146-
exinfo=pyfmodex.structure_declarations.CREATESOUNDEXINFO(
147-
length=len(audio_data),
148-
numchannels=clip.m_Channels,
149-
defaultfrequency=clip.m_Frequency,
150-
),
151-
)
142+
# avoid asynchronous access to pyfmodex
143+
with system_lock:
144+
# init system
145+
system = get_pyfmodex_system_instance(clip.m_Channels, pyfmodex.flags.INIT_FLAGS.NORMAL)
146+
147+
sound = system.create_sound(
148+
bytes(audio_data),
149+
pyfmodex.flags.MODE.OPENMEMORY,
150+
exinfo=pyfmodex.structure_declarations.CREATESOUNDEXINFO(
151+
length=len(audio_data),
152+
numchannels=clip.m_Channels,
153+
defaultfrequency=clip.m_Frequency,
154+
),
155+
)
152156

153-
# iterate over subsounds
154-
samples = {}
155-
for i in range(sound.num_subsounds):
156-
if i > 0:
157-
filename = "%s-%i.wav" % (clip.m_Name, i)
158-
else:
159-
filename = "%s.wav" % clip.m_Name
160-
subsound = sound.get_subsound(i)
161-
samples[filename] = subsound_to_wav(subsound, convert_pcm_float)
162-
subsound.release()
157+
# iterate over subsounds
158+
samples = {}
159+
for i in range(sound.num_subsounds):
160+
if i > 0:
161+
filename = "%s-%i.wav" % (clip.m_Name, i)
162+
else:
163+
filename = "%s.wav" % clip.m_Name
164+
subsound = sound.get_subsound(i)
165+
samples[filename] = subsound_to_wav(subsound, convert_pcm_float)
166+
subsound.release()
163167

164-
sound.release()
168+
sound.release()
165169
return samples
166170

167171

0 commit comments

Comments
 (0)