Skip to content

Commit b2ccf2b

Browse files
isHarryhK0lb3
authored andcommitted
feat: reuse instance of pyfmodex system
1 parent aec2552 commit b2ccf2b

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

UnityPy/export/AudioClipConverter.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,23 @@ def extract_audioclip_samples(
111111
return dump_samples(audio, audio_data, convert_pcm_float)
112112

113113

114+
system_param = ()
115+
system_instance = None
116+
117+
def get_pyfmodex_system_instance(maxchannels, flags):
118+
global pyfmodex, system_param, system_instance
119+
# cache strategy check
120+
new_system_param = (maxchannels, flags, None)
121+
if system_param != new_system_param or system_instance is None:
122+
system_param = new_system_param
123+
if system_instance is not None:
124+
system_instance.release()
125+
# create a new instance
126+
system_instance = pyfmodex.System()
127+
system_instance.init(*system_param)
128+
return system_instance
129+
130+
114131
def dump_samples(
115132
clip: AudioClip, audio_data: bytes, convert_pcm_float: bool = True
116133
) -> Dict[str, bytes]:
@@ -119,8 +136,7 @@ def dump_samples(
119136
if not pyfmodex:
120137
return {}
121138
# init system
122-
system = pyfmodex.System()
123-
system.init(clip.m_Channels, pyfmodex.flags.INIT_FLAGS.NORMAL, None)
139+
system = get_pyfmodex_system_instance(clip.m_Channels, pyfmodex.flags.INIT_FLAGS.NORMAL)
124140

125141
sound = system.create_sound(
126142
bytes(audio_data),
@@ -144,7 +160,6 @@ def dump_samples(
144160
subsound.release()
145161

146162
sound.release()
147-
system.release()
148163
return samples
149164

150165

0 commit comments

Comments
 (0)