From fd1d993b2a4b806d42919ea770a41dfbe8231d83 Mon Sep 17 00:00:00 2001 From: gesellkammer Date: Sun, 3 Jul 2022 20:15:29 +0200 Subject: [PATCH 1/2] added two commands: set_compression_level and set_vbr_encoding_quality, two configure the compression/quality when writing to a compressed format (ogg or flac) --- soundfile.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++ soundfile_build.py | 3 +++ 2 files changed, 59 insertions(+) diff --git a/soundfile.py b/soundfile.py index b15a706..023e1cd 100644 --- a/soundfile.py +++ b/soundfile.py @@ -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. diff --git a/soundfile_build.py b/soundfile_build.py index 774fd9b..9aed33d 100644 --- a/soundfile_build.py +++ b/soundfile_build.py @@ -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 From 1d8653c7b49c02692f95d6e73fad02c9c9aa3bbc Mon Sep 17 00:00:00 2001 From: gesellkammer Date: Mon, 4 Jul 2022 10:10:57 +0200 Subject: [PATCH 2/2] fix typo --- soundfile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/soundfile.py b/soundfile.py index 023e1cd..4bfac20 100644 --- a/soundfile.py +++ b/soundfile.py @@ -1169,7 +1169,7 @@ def set_compression_level(self, level): err = _snd.sf_command(self._file, _snd.SFC_SET_COMPRESSION_LEVEL, _ffi.new("double*", level), _ffi.sizeof("double")) - if err == SF_FALSE + if err == SF_FALSE: err = _snd.sf_error(self._file) raise LibsndfileError(err, "Error setting the compression level") @@ -1195,7 +1195,7 @@ def set_vbr_encoding_quality(self, quality): _ffi.new("double*", quality), _ffi.sizeof("double")) - if err == SF_FALSE + if err == SF_FALSE: err = _snd.sf_error(self._file) raise LibsndfileError(err, "Error setting the encoding quality")