1+ import ctypes
2+ import os
3+ import platform
4+ from UnityPy .streams import EndianBinaryWriter , EndianBinaryReader
5+ # pyfmodex init
16try :
2- from fsb5 import FSB5
3- except ImportError :
4- print ("Couldn't import fsb5.\n fsb5 is required to process convert audio clips." )
7+ ROOT = os .path .dirname (os .path .dirname (os .path .realpath (__file__ )))
8+ LIB_PATH = os .path .join (ROOT , "lib" , "FMOD" , platform .system (), platform .architecture ()[0 ])
59
10+ if platform .system () == 'Windows' :
11+ _dll = ctypes .WinDLL (os .path .join (LIB_PATH , "fmod.dll" ))
12+ elif platform .system () == "Linux" :
13+ _dll = ctypes .CDLL (os .path .join (LIB_PATH , "libfmod.so" ))
14+ elif platform .system () == "Darwin" :
15+ _dll = ctypes .CDLL (os .path .join (LIB_PATH , "libfmod.dylib" ))
616
7- def extract_audioclip_samples (audio ) -> dict :
8- """extracts all the sample data from an AudioClip
9- Copied from unitypack
10- https://github.com/HearthSim/UnityPack/blob/d9ce99fac3c917fa44b0042c9114b7cd03aa9884/unitypack/utils.py#L14
17+ import pyfmodex
18+ pyfmodex .fmodex ._dll = _dll
19+ from pyfmodex .structures import CREATESOUNDEXINFO
20+ from pyfmodex .flags import MODE , TIMEUNIT , INIT_FLAGS
21+ except Exception as e :
22+ print ("Error during the pyfmodex initialisation - AudioClips.samples won't work" , e )
1123
1224
13- :param audio: AudioClip
14- :type audio: AudioClip
15- :return: {filename : sample(bytes)}
16- :rtype: dict
17- """
25+ def extract_audioclip_samples (audio ) -> dict :
26+ """extracts all the samples from an AudioClip
27+ :param audio: AudioClip
28+ :type audio: AudioClip
29+ :return: {filename : sample(bytes)}
30+ :rtype: dict
31+ """
1832
1933 if not audio .m_AudioData :
2034 # eg. StreamedResource not available
@@ -25,22 +39,66 @@ def extract_audioclip_samples(audio) -> dict:
2539 return {'%s.ogg' % audio .name : audio .m_AudioData }
2640 elif magic == b'RIFF' :
2741 return {'%s.wav' % audio .name : audio .m_AudioData }
28- return _extract_fsb (audio )
42+ return dump_samples (audio )
2943
3044
31- def _extract_fsb (audio ) -> dict :
32- ret = {}
33- af = FSB5 (audio .m_AudioData )
34- for i , sample in enumerate (af .samples ):
45+ def dump_samples (clip ):
46+ # init system
47+ system = pyfmodex .System ()
48+ system .init (1 , INIT_FLAGS .NORMAL , None )
49+ exinfo = CREATESOUNDEXINFO (length = clip .m_Size ,)
50+
51+ # get sound
52+ sound = system .create_sound (
53+ bytes (clip .m_AudioData ),
54+ mode = MODE .OPENMEMORY ,
55+ exinfo = exinfo
56+ )
57+ # iterate over subsounds
58+ samples = {}
59+ for i in range (sound .num_subsounds ):
3560 if i > 0 :
36- filename = "%s-%i.%s " % (audio .name , i , af . get_sample_extension () )
61+ filename = "%s-%i.wav " % (clip .name , i )
3762 else :
38- filename = "%s.%s" % (audio .name , af .get_sample_extension ())
39- try :
40- sample = af .rebuild_sample (sample )
41- except ValueError as e :
42- print ("WARNING: Could not extract %r (%s)" % (audio , e ))
43- continue
44- ret [filename ] = sample
45-
46- return ret
63+ filename = "%s.wav" % (clip .name )
64+ subsound = sound .get_subsound (i )
65+ samples [filename ] = subsound_to_wav (subsound )
66+ subsound .release ()
67+
68+ sound .release ()
69+ system .release ()
70+ return samples
71+
72+
73+ def subsound_to_wav (subsound ):
74+ # get sound settings
75+ length = subsound .get_length (TIMEUNIT .PCMBYTES )
76+ channels = subsound .format .channels
77+ bits = subsound .format .bits
78+ sample_rate = int (subsound .default_frequency )
79+
80+ # write to buffer
81+ w = EndianBinaryWriter (endian = "<" )
82+ # riff chucnk
83+ w .write (b"RIFF" )
84+ w .write_int (length + 36 ) # sizeof(FmtChunk) + sizeof(RiffChunk) + length
85+ w .write (b"WAVE" )
86+ # fmt chunck
87+ w .write (b"fmt " )
88+ w .write_int (16 ) # sizeof(FmtChunk) - sizeof(RiffChunk)
89+ w .write_short (1 )
90+ w .write_short (channels )
91+ w .write_int (sample_rate )
92+ w .write_int (sample_rate * channels * bits // 8 )
93+ w .write_short (channels * bits // 8 )
94+ w .write_short (bits )
95+ # data chunck
96+ w .write (b"data" )
97+ w .write_int (length )
98+ # data
99+ lock = subsound .lock (0 , length )
100+ for ptr , length in lock :
101+ ptr_data = ctypes .string_at (ptr , length .value )
102+ w .write (ptr_data )
103+ subsound .unlock (* lock )
104+ return w .bytes
0 commit comments