Skip to content

Conversation

@sushmullur
Copy link

Implements CBR (Constant Bit Rate) support for MP3Compressor to complement the existing VBR (Variable Bit Rate) functionality, addressing issue #430.

Summary

Adds CBR mode support while maintaining full backward compatibility with existing VBR functionality. Users can now set constant bitrates from 32-320 kbps and
switch between VBR and CBR modes seamlessly.

API Changes

from pedalboard import MP3Compressor, MP3CompressorMode

# Existing VBR usage (unchanged)
vbr_compressor = MP3Compressor(vbr_quality=2.0)

# New CBR usage
cbr_compressor = MP3Compressor()
cbr_compressor.bitrate = 192  # Switches to CBR mode

# Mode switching
compressor = MP3Compressor(vbr_quality=3.0)  # VBR
compressor.bitrate = 128                      # Switch to CBR
compressor.vbr_quality = 1.0                  # Switch back to VBR

# Check current mode
print(compressor.mode)     # MP3CompressorMode.VBR or MP3CompressorMode.CBR
print(compressor.bitrate)  # Current CBR bitrate (when in CBR mode)

Implementation Details

C++ Changes (pedalboard/plugins/MP3Compressor.h):
- Added enum class Mode { VBR, CBR } for mode tracking
- Added setBitrate(), getBitrate(), getMode() methods with validation
- Updated prepare() to use appropriate LAME API calls:
  - VBR: lame_set_VBR(vbr_default) + lame_set_VBR_quality()
  - CBR: lame_set_VBR(vbr_off) + lame_set_brate()
- Added Python bindings for new properties and MP3CompressorMode enum

Type Hints (pedalboard_native/__init__.pyi):
- Added MP3CompressorMode enum class
- Added type hints for bitrate and mode properties
- Updated docstring to document CBR support

Test Coverage (tests/test_mp3_compressor.py):
- 9 new test functions covering all CBR functionality
- Tests all valid bitrates (32-320 kbps)
- Tests invalid bitrate rejection, mode switching, and edge cases

Supported CBR Bitrates

All MPEG-1 and MPEG-2 standard bitrates: 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 kbps

Invalid bitrates throw descriptive error messages listing valid options.

Validation

- Backward compatibility: All existing VBR tests pass unchanged
- Uses standard LAME API calls for CBR functionality
- Comprehensive error handling with clear validation messages
- Cross-platform compatibility with existing MP3 sample rate restrictions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant