Skip to content

Commit df2d9e3

Browse files
committed
1.5.3 - AudioClip extraction added
AudioClip to wav via pyfmodex
1 parent 0454144 commit df2d9e3

29 files changed

Lines changed: 203 additions & 108 deletions

‎.gitignore‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# Byte-compiled / optimized / DLL files
22
__pycache__/
3+
.dump
34
.idea/
45
*.py[cod]
56
*$py.class
67
test.py
78
AssetStudio/
8-
# C extensions
9-
*.so
109

1110
# Distribution / packaging
1211
.Python
@@ -16,8 +15,6 @@ dist/
1615
downloads/
1716
eggs/
1817
.eggs/
19-
lib/
20-
lib64/
2118
parts/
2219
sdist/
2320
var/

‎MANIFEST.in‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
include UnityPy/*
1+
include UnityPy/*
2+
3+
recursive-include UnityPy/lib *.dll *.lib *.so *.dylib

‎README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ but if it doesn't, then it is usually necessary to investigate the class that lo
155155

156156
* ``.samples`` - ``{sample-name : sample-data}`` dict
157157

158+
The samples are converted into the .wav format.
159+
The sample-data is a .wav file in bytes.
160+
161+
```python
162+
clip : AudioClip
163+
for name, data in clip.samples.items():
164+
with open(name, "wb") as f:
165+
f.write(data)
166+
```
167+
158168
## Goals
159169

160170
### WIP

‎UnityPy/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "1.5.2"
1+
__version__ = "1.5.3.1"
22

33
from .environment import Environment
44

‎UnityPy/classes/AudioClip.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ def __init__(self, reader):
1818

1919
if version >= (3, 2): # and version <= (5,): # 3.2.0 to 5
2020
self.m_Stream = reader.read_int()
21-
m_Size = reader.read_int()
22-
tsize = m_Size + 4 - m_Size % 4 if (m_Size % 4 != 0) else m_Size
21+
self.m_Size = reader.read_int()
22+
tsize = self.m_Size + 4 - self.m_Size % 4 if (self.m_Size % 4 != 0) else self.m_Size
2323
if reader.byte_size + reader.byte_start - reader.Position != tsize:
2424
m_Offset = reader.read_u_int()
2525
self.m_Source = self.assets_file.full_name + ".resS"
2626
else:
27-
m_Size = reader.read_int()
27+
self.m_Size = reader.read_int()
2828

2929
self.extension = AUDIO_TYPE_EXTEMSION.get(self.m_Type, ".audioclip")
3030
else:
@@ -41,8 +41,8 @@ def __init__(self, reader):
4141
self.m_Legacy3D = reader.read_boolean()
4242
reader.align_stream()
4343
self.m_Source = reader.read_aligned_string()
44-
m_Offset = reader.read_u_long()
45-
m_Size = reader.read_long()
44+
self.m_Offset = reader.read_u_long()
45+
self.m_Size = reader.read_long()
4646
self.m_CompressionFormat = AudioCompressionFormat(reader.read_int())
4747

4848
self.extension = AUDIO_TYPE_EXTEMSION.get(
@@ -51,10 +51,10 @@ def __init__(self, reader):
5151

5252
if self.m_Source:
5353
self.m_AudioData = get_resource_data(
54-
self.m_Source, self.assets_file, m_Offset, m_Size
54+
self.m_Source, self.assets_file, self.m_Offset, self.m_Size
5555
)
5656
else:
57-
self.m_AudioData = reader.read_bytes(m_Size)
57+
self.m_AudioData = reader.read_bytes(self.m_Size)
5858

5959
@property
6060
def samples(self) -> dict:

‎UnityPy/enums/GfxPrimitiveType.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from enum import Enum
2+
3+
4+
class GfxPrimitiveType(Enum):
5+
kPrimitiveTriangles = 0
6+
kPrimitiveTriangleStrip = 1
7+
kPrimitiveQuads = 2
8+
kPrimitiveLines = 3
9+
kPrimitiveLineStrip = 4
10+
kPrimitivePoints = 5

‎UnityPy/enums/TextureFormat.py‎

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,67 @@
1-
from enum import IntEnum
1+
from enum import Enum
22

33

4-
class TextureFormat(IntEnum):
5-
Alpha8 = (1,)
6-
ARGB4444 = (2,)
7-
RGB24 = (3,)
8-
RGBA32 = (4,)
9-
ARGB32 = (5,)
10-
RGB565 = (7,)
11-
R16 = (9,)
12-
DXT1 = (10,)
13-
DXT5 = (12,)
14-
RGBA4444 = (13,)
15-
BGRA32 = (14,)
16-
RHalf = (15,)
17-
RGHalf = (16,)
18-
RGBAHalf = (17,)
19-
RFloat = (18,)
20-
RGFloat = (19,)
21-
RGBAFloat = (20,)
22-
YUY2 = (21,)
23-
RGB9e5Float = (22,)
24-
BC4 = (26,)
25-
BC5 = (27,)
26-
BC6H = (24,)
27-
BC7 = (25,)
28-
DXT1Crunched = (28,)
29-
DXT5Crunched = (29,)
30-
PVRTC_RGB2 = (30,)
31-
PVRTC_RGBA2 = (31,)
32-
PVRTC_RGB4 = (32,)
33-
PVRTC_RGBA4 = (33,)
34-
ETC_RGB4 = (34,)
35-
ATC_RGB4 = (35,)
36-
ATC_RGBA8 = (36,)
37-
EAC_R = (41,)
38-
EAC_R_SIGNED = (42,)
39-
EAC_RG = (43,)
40-
EAC_RG_SIGNED = (44,)
41-
ETC2_RGB = (45,)
42-
ETC2_RGBA1 = (46,)
43-
ETC2_RGBA8 = (47,)
44-
ASTC_RGB_4x4 = (48,)
45-
ASTC_RGB_5x5 = (49,)
46-
ASTC_RGB_6x6 = (50,)
47-
ASTC_RGB_8x8 = (51,)
48-
ASTC_RGB_10x10 = (52,)
49-
ASTC_RGB_12x12 = (53,)
50-
ASTC_RGBA_4x4 = (54,)
51-
ASTC_RGBA_5x5 = (55,)
52-
ASTC_RGBA_6x6 = (56,)
53-
ASTC_RGBA_8x8 = (57,)
54-
ASTC_RGBA_10x10 = (58,)
55-
ASTC_RGBA_12x12 = (59,)
56-
ETC_RGB4_3DS = (60,)
57-
ETC_RGBA8_3DS = (61,)
58-
RG16 = (62,)
59-
R8 = (63,)
60-
ETC_RGB4Crunched = (64,)
61-
ETC2_RGBA8Crunched = (65,)
62-
ASTC_HDR_4x4 = (66,)
63-
ASTC_HDR_5x5 = (67,)
64-
ASTC_HDR_6x6 = (68,)
65-
ASTC_HDR_8x8 = (69,)
66-
ASTC_HDR_10x10 = (70,)
67-
ASTC_HDR_12x12 = (71,)
4+
class TextureFormat(Enum):
5+
Alpha8 = 1
6+
ARGB4444 = 2
7+
RGB24 = 3
8+
RGBA32 = 4
9+
ARGB32 = 5
10+
RGB565 = 7
11+
R16 = 9
12+
DXT1 = 10
13+
DXT5 = 12
14+
RGBA4444 = 13
15+
BGRA32 = 14
16+
RHalf = 15
17+
RGHalf = 16
18+
RGBAHalf = 17
19+
RFloat = 18
20+
RGFloat = 19
21+
RGBAFloat = 20
22+
YUY2 = 21
23+
RGB9e5Float = 22
24+
BC4 = 26
25+
BC5 = 27
26+
BC6H = 24
27+
BC7 = 25
28+
DXT1Crunched = 28
29+
DXT5Crunched = 29
30+
PVRTC_RGB2 = 30
31+
PVRTC_RGBA2 = 31
32+
PVRTC_RGB4 = 32
33+
PVRTC_RGBA4 = 33
34+
ETC_RGB4 = 34
35+
ATC_RGB4 = 35
36+
ATC_RGBA8 = 36
37+
EAC_R = 41
38+
EAC_R_SIGNED = 42
39+
EAC_RG = 43
40+
EAC_RG_SIGNED = 44
41+
ETC2_RGB = 45
42+
ETC2_RGBA1 = 46
43+
ETC2_RGBA8 = 47
44+
ASTC_RGB_4x4 = 48
45+
ASTC_RGB_5x5 = 49
46+
ASTC_RGB_6x6 = 50
47+
ASTC_RGB_8x8 = 51
48+
ASTC_RGB_10x10 = 52
49+
ASTC_RGB_12x12 = 53
50+
ASTC_RGBA_4x4 = 54
51+
ASTC_RGBA_5x5 = 55
52+
ASTC_RGBA_6x6 = 56
53+
ASTC_RGBA_8x8 = 57
54+
ASTC_RGBA_10x10 = 58
55+
ASTC_RGBA_12x12 = 59
56+
ETC_RGB4_3DS = 60
57+
ETC_RGBA8_3DS = 61
58+
RG16 = 62
59+
R8 = 63
60+
ETC_RGB4Crunched = 64
61+
ETC2_RGBA8Crunched = 65
62+
ASTC_HDR_4x4 = 66
63+
ASTC_HDR_5x5 = 67
64+
ASTC_HDR_6x6 = 68
65+
ASTC_HDR_8x8 = 69
66+
ASTC_HDR_10x10 = 70
67+
ASTC_HDR_12x12 = 71

‎UnityPy/enums/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
from .FileType import FileType
55
from .TextureFormat import TextureFormat
66
from .SpriteMeshType import SpriteMeshType
7+
from .GfxPrimitiveType import GfxPrimitiveType
Lines changed: 85 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
1+
import ctypes
2+
import os
3+
import platform
4+
from UnityPy.streams import EndianBinaryWriter, EndianBinaryReader
5+
# pyfmodex init
16
try:
2-
from fsb5 import FSB5
3-
except ImportError:
4-
print("Couldn't import fsb5.\nfsb5 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
2.57 MB
Binary file not shown.

0 commit comments

Comments
 (0)