Skip to content

Added two commands: set_compression_level and set_vbr_encoding_quality #341

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 2 commits 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
56 changes: 56 additions & 0 deletions soundfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,62 @@ def truncate(self, frames=None):
raise LibsndfileError(err, "Error truncating the file")
self._info.frames = frames

def set_compression_level(self, level):
"""Set the compression level

The compression level should be between 0.0 (minimum compression
level) and 1.0 (highest compression level). Currenly this command
is only implemented for FLAC and Ogg/Vorbis files. It has no
effect on uncompressed file formats.

Setting the compression level is only valid when open in
write mode. This method must be called before any data is actually
written

Parameters
----------
level : float
The compression level, between 0. (minimum compression) and
1.0 (highest compression)
"""
if self._mode == 'r':
raise SoundFileRuntimeError("Cannot set compression level "
"for a file in 'read' mode")

err = _snd.sf_command(self._file, _snd.SFC_SET_COMPRESSION_LEVEL,
_ffi.new("double*", level),
_ffi.sizeof("double"))
if err == SF_FALSE:
err = _snd.sf_error(self._file)
raise LibsndfileError(err, "Error setting the compression level")

def set_vbr_encoding_quality(self, quality):
"""Set the Variable Bit Rate encoding quality.

The encoding quality value should be between 0.0 (lowest quality)
and 1.0 (highest quality). Currenly this command is only implemented
for FLAC and Ogg/Vorbis files. It has no effect on un-compressed file
formats.

Parameters
----------
quality : float
The encoding quality as a value between 0.0 (lowest quality) and
1.0 (highest quality)
"""
if self._mode == 'r':
raise SoundFileRuntimeError("Cannot set VBR encoding quality "
"for a file in 'read' mode")

err = _snd.sf_command(self._file, _snd.SFC_SET_VBR_ENCODING_QUALITY,
_ffi.new("double*", quality),
_ffi.sizeof("double"))

if err == SF_FALSE:
err = _snd.sf_error(self._file)
raise LibsndfileError(err, "Error setting the encoding quality")


def flush(self):
"""Write unwritten data to the file system.

Expand Down
3 changes: 3 additions & 0 deletions soundfile_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

SFC_SET_SCALE_FLOAT_INT_READ = 0x1014,
SFC_SET_SCALE_INT_FLOAT_WRITE = 0x1015,

SFC_SET_VBR_ENCODING_QUALITY = 0x1300,
SFC_SET_COMPRESSION_LEVEL = 0x1301,
} ;

enum
Expand Down