33import ctypes
44import os
55import platform
6+ from threading import Lock
67from typing import TYPE_CHECKING , Dict , Union
78
89from 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 ()
114116system_param = ()
115117system_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