-
Notifications
You must be signed in to change notification settings - Fork 112
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
Choose MP3 bitrate when writing #390
Comments
Does libsndfile have this feature? We can probably support it if there's an API for it. |
is bitrate now supported? |
Does libsndfile provide an API for it? If so, we can support it. |
Couldn't find an API so I asked here: |
I don't really understand the syntax but over in that libsndfile thread 1008 evpobr linked to this documentation: |
I think that's just a flag on how the bitrate is to be interpreted. But how do we set the bitrate itself? |
Hm, I don't know in that case |
@bastibe more details provided by arthurt now libsndfile/libsndfile#1008 (comment) it seems like exposing the SFC_SET_COMPRESSION_LEVEL in the python-soundfile API would be useful. The argument takes values 0-1.
behavior is different for average and constant bitrate methods, see aurthurt's comment for details |
That sounds great! Would anyone want to prepare a pull request? |
I gave it a try, but did not see any effect. I first added the constant defining SFC_SET_COMPRESSION_LEVEL to soundfile_build.py:
then running However, with the following code, the value assigned to pointer_compression_level has no effect on the file size produced: pointer_compression_level = _ffi.new('float *')
pointer_compression_level[0] = 0.0
with soundfile.SoundFile('./compressed.mp3','w', 44100, 1, format='MP3') as sfc:
_snd.sf_command(sfc._file, _snd.SFC_SET_COMPRESSION_LEVEL, pointer_compression_level, _ffi.sizeof(pointer_compression_level))
sfc.write(data) # data is numpy array of audio samples I also tried a different syntax for setting the float value: pointer_compression_level = _ffi.cast("float *",0.5) but the file sizes do not change with I don't have any grasp on how ffi really works so I'm not sure how to proceed. I feel it would be easiest for someone more familiar with the python-soundfile library to take it from here. I also tried with flac format and saw no difference in file size. |
Thank you for looking into this. I think the Also, check if your version of libsndfile is new enough to support the |
I tried 'double', no change. The returned value is 0 (not |
If you installed the wheel, you'll need to remove |
What's the status on this? When reading a 256kbs mp3 file with pysoundfile and writing it again it ends up as 32kb/s. |
I personally had no success, not familiar with the development environment here and don't have time to pursue it |
I changed the bitrate by adding lines to _cdata_io in soundfile.py. This was sufficient for my case, but there must be a better way. OS: Windows 11 def _cdata_io(self, action, data, ctype, frames):
assert ctype in _ffi_types.values()
self._check_if_closed()
if self.seekable():
curr = self.tell()
+ if action == "write":
+ pointer_compression_level = _ffi.new("double *")
+ pointer_compression_level[0] = 0 # 0:low compression level 1:high compression level
+ _snd.sf_command(self._file, 0x1301, pointer_compression_level, _ffi.sizeof(pointer_compression_level))
func = getattr(_snd, "sf_" + action + "f_" + ctype)
frames = func(self._file, data, frames)
_error_check(self._errorcode)
if self.seekable():
self.seek(curr + frames, SEEK_SET) # Update read & write position
return frames |
Cool! You got it working! I'd be happy to merge a pull request that adds this to the rest of the command interface features. |
Now that mp3 support has been added, it would be great to be able to choose the bitrate when writing to mp3. What is the default btw? 128?
The text was updated successfully, but these errors were encountered: