Skip to content

Added PCMFLOAT support. #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Python library and tool to extract FSB5 (FMOD Sample Bank) files.

- MPEG
- Vorbis (OGG)
- WAVE (PCM8, PCM16, PCM32)
- WAVE (PCM8, PCM16, PCM32, PCMFLOAT)

Other formats can be identified but will be extracted as `.dat` files and may not play as the headers may be missing.

Expand Down
7 changes: 5 additions & 2 deletions fsb5/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def file_extension(self):

@property
def is_pcm(self):
return self in (SoundFormat.PCM8, SoundFormat.PCM16, SoundFormat.PCM32)
return self in (SoundFormat.PCM8, SoundFormat.PCM16, SoundFormat.PCM32, SoundFormat.PCMFLOAT)


FSB5Header = namedtuple("FSB5Header", [
Expand Down Expand Up @@ -211,11 +211,14 @@ def rebuild_sample(self, sample):
from . import vorbis
return vorbis.rebuild(sample)
elif self.header.mode.is_pcm:
from .pcm import rebuild
from .pcm import rebuild, rebuild_float
if self.header.mode == SoundFormat.PCM8:
width = 1
elif self.header.mode == SoundFormat.PCM16:
width = 2
elif self.header.mode == SoundFormat.PCMFLOAT:
width = 4
return rebuild_float(sample, width)
else:
width = 4
return rebuild(sample, width)
Expand Down
34 changes: 34 additions & 0 deletions fsb5/pcm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import wave
import struct
from io import BytesIO


Expand All @@ -9,3 +10,36 @@ def rebuild(sample, width):
wav.setparams((sample.channels, width, sample.frequency, 0, "NONE", "NONE"))
wav.writeframes(data)
return ret.getvalue()

def rebuild_float(sample, width):
data = sample.data[:sample.samples * width]
ret = BytesIO()
with PCMFloatWave_write(ret) as wav:
wav.setparams((sample.channels, width, sample.frequency, 0, "NONE", "NONE"))
wav.writeframes(data)
return ret.getvalue()

WAVE_FORMAT_IEEE_FLOAT = 3

class PCMFloatWave_write(wave.Wave_write):
def _write_header(self, initlength):
assert not self._headerwritten
self._file.write(b'RIFF')
if not self._nframes:
self._nframes = initlength // (self._nchannels * self._sampwidth)
self._datalength = self._nframes * self._nchannels * self._sampwidth
try:
self._form_length_pos = self._file.tell()
except (AttributeError, OSError):
self._form_length_pos = None
self._file.write(struct.pack('<L4s4sLHHLLHH4s',
36 + self._datalength, b'WAVE', b'fmt ', 16,
WAVE_FORMAT_IEEE_FLOAT, self._nchannels, self._framerate,
self._nchannels * self._framerate * self._sampwidth,
self._nchannels * self._sampwidth,
self._sampwidth * 8, b'data'))
if self._form_length_pos is not None:
self._data_length_pos = self._file.tell()
self._file.write(struct.pack('<L', self._datalength))
self._headerwritten = True