bastibe / python-soundfile

SoundFile is an audio library based on libsndfile, CFFI, and NumPy
BSD 3-Clause "New" or "Revised" License
718 stars 111 forks source link

Choose MP3 bitrate when writing #390

Open lminer opened 1 year ago

lminer commented 1 year ago

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?

bastibe commented 1 year ago

Does libsndfile have this feature? We can probably support it if there's an API for it.

jimmieliu commented 8 months ago

is bitrate now supported?

bastibe commented 8 months ago

Does libsndfile provide an API for it? If so, we can support it.

sammlapp commented 6 months ago

Couldn't find an API so I asked here: https://github.com/libsndfile/libsndfile/issues/1008

sammlapp commented 6 months ago

I don't really understand the syntax but over in that libsndfile thread 1008 evpobr linked to this documentation: https://github.com/libsndfile/libsndfile/blob/c81375f070f3c6764969a738eacded64f53a076e/docs/command.md#sfc_set_bitrate_mode

bastibe commented 6 months ago

I think that's just a flag on how the bitrate is to be interpreted. But how do we set the bitrate itself?

sammlapp commented 6 months ago

Hm, I don't know in that case

sammlapp commented 6 months ago

@bastibe more details provided by arthurt now https://github.com/libsndfile/libsndfile/issues/1008#issuecomment-2135784810

it seems like exposing the SFC_SET_COMPRESSION_LEVEL in the python-soundfile API would be useful. The argument takes values 0-1.

For VBR, it translates to lame_set_VBR_qaulity(). 0.0 -> 0.0 (highest quality), 1.0 -> 10.0 (lowest quality)

behavior is different for average and constant bitrate methods, see aurthurt's comment for details

bastibe commented 5 months ago

That sounds great! Would anyone want to prepare a pull request?

sammlapp commented 5 months ago

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:

SFC_SET_COMPRESSION_LEVEL       = 0x1301,

then running pythin setup.py install, which made this constant visible in the soundfile._snd.SFC_SET_COMPRESSION_LEVEL API.

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 pointer_compression_level.

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.

Documentation: https://github.com/libsndfile/libsndfile/blob/c81375f070f3c6764969a738eacded64f53a076e/docs/command.md#sfc_set_compression_level

bastibe commented 5 months ago

Thank you for looking into this. I think the pointer_compression_level should be a double, not float. What do you get as return value from _snd.sf_command? It should equal SF_TRUE or SF_FALSE.

Also, check if your version of libsndfile is new enough to support the SFC_SET_COMPRESSION_LEVEL API.

sammlapp commented 5 months ago

I tried 'double', no change. The returned value is 0 (not SF_TRUE or SF_FALSE). I also updated libsndfile to 1.2.2 with brew install libsndfile, no difference.

bastibe commented 5 months ago

If you installed the wheel, you'll need to remove _soundfile_data from your site-packages before it'll pick up the system libsndfile. Or uninstall the wheel and install the source distribution, which does not come with _soundfile_data.

rawrbw commented 2 months ago

What's the status on this? When reading a 256kbs mp3 file with pysoundfile and writing it again it ends up as 32kb/s.

sammlapp commented 2 months ago

I personally had no success, not familiar with the development environment here and don't have time to pursue it

ytya commented 2 weeks ago

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 soundfile: 0.12.1 libsndfile: 1.2.0

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
bastibe commented 2 weeks ago

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.