### LameEnc Installation Source: https://context7.com/chrisstaite/lameenc/llms.txt Install lameenc from PyPI to get pre-built binaries for your platform. ```APIDOC ## Installation Install lameenc from PyPI to get pre-built binaries for your platform. ```bash pip install lameenc ``` ``` -------------------------------- ### Complete WAV to MP3 Conversion Example Source: https://context7.com/chrisstaite/lameenc/llms.txt A comprehensive example demonstrating how to read a WAV audio file and convert it into an MP3 file using the LAME encoder. ```APIDOC ## Complete WAV to MP3 Conversion Example ### Description A full example showing how to read a WAV file and convert it to MP3. ### Method `wav_to_mp3(wav_path: str, mp3_path: str, bitrate: int = 192)` ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Parameters - **wav_path** (str) - Required - The file path to the input WAV file. - **mp3_path** (str) - Required - The file path where the output MP3 file will be saved. - **bitrate** (int) - Optional - The target bitrate for the MP3 encoding in kbps. Defaults to 192. ### Request Example ```python import lameenc import wave def wav_to_mp3(wav_path, mp3_path, bitrate=192): """Convert a WAV file to MP3.""" with wave.open(wav_path, 'rb') as wav: channels = wav.getnchannels() sample_rate = wav.getframerate() sample_width = wav.getsampwidth() if sample_width != 2: raise ValueError("Only 16-bit WAV files are supported") encoder = lameenc.Encoder() encoder.silence() encoder.set_channels(channels) encoder.set_in_sample_rate(sample_rate) encoder.set_bit_rate(bitrate) encoder.set_quality(2) # Example quality setting mp3_data = bytearray() chunk_frames = 4096 while True: pcm_data = wav.readframes(chunk_frames) if not pcm_data: break mp3_data += encoder.encode(pcm_data) mp3_data += encoder.flush() with open(mp3_path, 'wb') as f: f.write(mp3_data) return len(mp3_data) # Usage example: # size = wav_to_mp3('input.wav', 'output.mp3', bitrate=192) # print(f"Created MP3 file: {size} bytes") ``` ### Response Example Returns the size of the created MP3 file in bytes. ``` -------------------------------- ### Install lameenc Source: https://context7.com/chrisstaite/lameenc/llms.txt Install the lameenc library using pip. This command fetches pre-built binaries for your platform. ```bash pip install lameenc ``` -------------------------------- ### External Project Setup for LAME Library Source: https://github.com/chrisstaite/lameenc/blob/main/CMakeLists.txt Uses CMake's ExternalProject module to download, configure, build, and install the LAME library. ```cmake include(ExternalProject) ExternalProject_Add(lame PREFIX "${CMAKE_CURRENT_BINARY_DIR}/lame" URL https://sourceforge.net/projects/lame/files/lame/3.100/lame-3.100.tar.gz/download URL_HASH SHA256=ddfe36cab873794038ae2c1210557ad34857a4b6bdc515785d1da9e175b1da1e DOWNLOAD_NO_PROGRESS 1 PATCH_COMMAND "${CMAKE_COMMAND}" -D "SOURCE_DIR=" -P "${CMAKE_CURRENT_SOURCE_DIR}/patch-lame.cmake" CONFIGURE_COMMAND ${LAME_CONFIGURE} BUILD_COMMAND ${LAME_MAKE} INSTALL_COMMAND ${LAME_INSTALL} BUILD_IN_SOURCE 1 ) ``` -------------------------------- ### Configure Output Sample Rate (Resampling) Source: https://context7.com/chrisstaite/lameenc/llms.txt Set the output MP3 sample rate in Hz. This allows for resampling audio during the encoding process, for example, downsampling from 48kHz input to 44.1kHz output. ```python import lameenc # Downsample from 48kHz input to 44.1kHz output encoder = lameenc.Encoder() encoder.set_in_sample_rate(48000) encoder.set_out_sample_rate(44100) encoder.set_channels(2) encoder.set_bit_rate(128) pcm_48k = b'\x00\x00' * 48000 * 2 # 1 second stereo at 48kHz mp3_data = encoder.encode(pcm_48k) mp3_data += encoder.flush() ``` -------------------------------- ### Get External Project Properties Source: https://github.com/chrisstaite/lameenc/blob/main/CMakeLists.txt Retrieves the binary and source directories of the externally added LAME project. ```cmake ExternalProject_Get_Property(lame BINARY_DIR) ExternalProject_Get_Property(lame SOURCE_DIR) ``` -------------------------------- ### Encode PCM to MP3 using lameenc Source: https://github.com/chrisstaite/lameenc/blob/main/README.md Example of using the lameenc library to encode raw PCM data into MP3 format. Configure encoder settings like bitrate, sample rate, channels, and quality before encoding. Remember to flush the encoder after processing all data. ```python import lameenc encoder = lameenc.Encoder() encoder.set_bit_rate(128) encoder.set_in_sample_rate(16000) encoder.set_channels(2) encoder.set_quality(2) # 2-highest, 7-fastest # Can call this in a loop mp3_data = encoder.encode(interleaved_pcm_data) # Flush when finished encoding the entire stream mp3_data += encoder.flush() ``` -------------------------------- ### flush() Source: https://context7.com/chrisstaite/lameenc/llms.txt Flushes any remaining buffered MP3 data after all PCM data has been encoded. Must be called once at the end of encoding to get the complete MP3 stream. ```APIDOC ## flush() Flushes any remaining buffered MP3 data after all PCM data has been encoded. Must be called once at the end of encoding to get the complete MP3 stream. ```python import lameenc encoder = lameenc.Encoder() encoder.set_bit_rate(128) encoder.set_in_sample_rate(44100) encoder.set_channels(2) pcm_data = b'\x00\x00' * 44100 * 2 mp3_data = encoder.encode(pcm_data) # The flush() method would typically be called here to get the final MP3 data. # mp3_data += encoder.flush() ``` ``` -------------------------------- ### Flush Encoder for Final MP3 Frames Source: https://context7.com/chrisstaite/lameenc/llms.txt Call flush() to get the final MP3 frames after encoding. This is necessary to complete the MP3 data. ```python final_data = encoder.flush() complete_mp3 = mp3_data + final_data print(f"Encoded size: {len(mp3_data)} bytes") print(f"Flushed size: {len(final_data)} bytes") print(f"Total size: {len(complete_mp3)} bytes") ``` -------------------------------- ### Initialize and Configure Encoder Source: https://context7.com/chrisstaite/lameenc/llms.txt Create an Encoder instance and configure settings like bit rate, sample rate, channels, and quality before encoding. Default settings are 128 kbps stereo at 44.1 kHz with quality level 2. ```python import lameenc # Create encoder with default settings (128kbps, 44.1kHz, stereo, quality 2) encoder = lameenc.Encoder() # Configure as needed before encoding encoder.set_bit_rate(192) encoder.set_in_sample_rate(48000) encoder.set_channels(2) encoder.set_quality(2) # Encode PCM data (16-bit little-endian interleaved) pcm_data = b'\x00\x00' * 48000 * 2 # 1 second of silence, stereo mp3_data = encoder.encode(pcm_data) # Flush remaining data when done mp3_data += encoder.flush() # Write to file with open('output.mp3', 'wb') as f: f.write(mp3_data) ``` -------------------------------- ### Build lameenc with CMake Source: https://github.com/chrisstaite/lameenc/blob/main/README.md Standard build process for the library using CMake. Ensure you are in the build directory before running these commands. ```bash mkdir build cd build cmake .. make ``` -------------------------------- ### LAME Build Configuration for Windows Source: https://github.com/chrisstaite/lameenc/blob/main/CMakeLists.txt Configures LAME build for Windows, including copying configuration files and setting up build commands for MSVC. ```cmake elseif (WIN32) set(BUILT_FILE "lameenc-1.0.0-cp34-cp34m-win32.whl") set(LAME_CONFIGURE "${CMAKE_COMMAND}" -E copy "/configMS.h" "/config.h") if ( CMAKE_SIZEOF_VOID_P EQUAL 8 ) string(REPLACE "win32" "win_amd64" BUILT_FILE ${BUILT_FILE}) set(MACHINE "MSVCVER=Win64 MACHINE=/machine:x64") endif () set(LAME_MAKE nmake -f "/Makefile.MSVC" comp=msvc asm=no ${MACHINE} libmp3lame-static.lib) set(LAME_INSTALL ${CMAKE_COMMAND} -E copy "/output/libmp3lame-static.lib" "/lib/libmp3lame.lib") else () ``` -------------------------------- ### LAME Build Configuration for Linux Source: https://github.com/chrisstaite/lameenc/blob/main/CMakeLists.txt Sets up build flags and configure commands for LAME on Linux, with conditional logic for the ARCH environment variable. ```cmake # If you update this, don't forget .github/workflows/build.sh set(BUILT_FILE "lameenc-1.0.0-cp34-cp34m-linux_x86_64.whl") set(LAME_FLAGS "-fPIC") if (DEFINED ENV{ARCH}) set(LAME_CONFIGURE "/configure" "CFLAGS=${LAME_FLAGS}" "LDFLAGS=${LAME_FLAGS}" "--prefix=" "--host=$ENV{ARCH}" "--build=x86_64" "--enable-expopt" "--enable-nasm" "--disable-frontend" "--disable-decoder" "--disable-analyzer-hooks" "--disable-debug" "--disable-dependency-tracking") else () set(LAME_CONFIGURE "/configure" "CFLAGS=${LAME_FLAGS}" "LDFLAGS=${LAME_FLAGS}" "--prefix=" "--enable-expopt" "--enable-nasm" "--disable-frontend" "--disable-decoder" "--disable-analyzer-hooks" "--disable-debug" "--disable-dependency-tracking") endif () set(LAME_INSTALL $(MAKE) install) endif () ``` -------------------------------- ### Set Average Bitrate (ABR) for VBR Source: https://context7.com/chrisstaite/lameenc/llms.txt Use set_vbr_mean_bitrate_kbps() to target a specific average bitrate in kbps for ABR mode. Ensure VBR is set to lameenc.VBR_ABR. ```python import lameenc encoder = lameenc.Encoder() encoder.set_vbr(lameenc.VBR_ABR) encoder.set_vbr_mean_bitrate_kbps(192) encoder.set_in_sample_rate(44100) encoder.set_channels(2) pcm_data = b'\x00\x00' * 44100 * 2 mp3_data = encoder.encode(pcm_data) mp3_data += encoder.flush() ``` -------------------------------- ### LAME Build Configuration for macOS Source: https://github.com/chrisstaite/lameenc/blob/main/CMakeLists.txt Sets up build flags and configure commands for LAME on macOS, handling different architectures (x86_64, arm64). ```cmake set(ARCH_FLAGS "") set(HOST_PLATFORM "") if (APPLE) set(BUILT_FILE "lameenc-1.0.0-cp34-cp34m-macosx_10_6_intel.whl") set(HOST "") if ("${CMAKE_OSX_ARCHITECTURES}" STREQUAL "x86_64" OR "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "") set(ARCH_FLAGS "-arch x86_64") set(HOST "--host=x86_64") endif () if ("${CMAKE_OSX_ARCHITECTURES}" STREQUAL "arm64") set(ARCH_FLAGS "${ARCH_FLAGS} -arch arm64") set(HOST_PLATFORM "_PYTHON_HOST_PLATFORM=macosx-11.0-arm64") set(HOST "--host=aarch64") endif () set(LAME_FLAGS "${ARCH_FLAGS} -mmacosx-version-min=10.6") set(LAME_CONFIGURE "/configure" "CFLAGS=${LAME_FLAGS}" "LDFLAGS=${LAME_FLAGS}" "--prefix=" "${HOST}" "--enable-expopt" "--enable-nasm" "--disable-frontend" "--disable-analyzer-hooks" "--disable-debug" "--disable-dependency-tracking") set(LAME_INSTALL $(MAKE) install) endif () ``` -------------------------------- ### Configure Input Sample Rate Source: https://context7.com/chrisstaite/lameenc/llms.txt Set the input PCM sample rate in Hz. This is useful for encoding audio sources with specific sample rates, such as 16kHz which is common for speech. ```python import lameenc # Encode 16kHz audio (common for speech) encoder = lameenc.Encoder() encoder.set_in_sample_rate(16000) encoder.set_channels(1) encoder.set_bit_rate(32) speech_pcm = b'\x00\x00' * 16000 # 1 second mono at 16kHz mp3_data = encoder.encode(speech_pcm) mp3_data += encoder.flush() ``` -------------------------------- ### Find and Build Python Bindings with CMake Source: https://github.com/chrisstaite/lameenc/blob/main/CMakeLists.txt This CMake script iterates through specified Python versions, finds the corresponding interpreter, and configures a custom command to build the Python package using 'python -m build'. It includes checks for version compatibility and sets up build options for library and include directories. ```cmake if (${PYTHON_VERSIONS} STREQUAL "OFF") set(PYTHON_VERSIONS "3.10;3.11;3.12;3.13;3.14") endif () foreach (Version IN LISTS PYTHON_VERSIONS) unset(PYTHON_EXECUTABLE) unset(PYTHONINTERP_FOUND) if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.18) find_package(PythonInterp "${Version}.0" QUIET) else () find_package(PythonInterp "${Version}." EXACT QUIET) endif () if (PYTHONINTERP_FOUND) set(PYTHON${Version}_EXECUTABLE ${PYTHON_EXECUTABLE}) else () unset(PYTHON_EXECUTABLE) find_program(PYTHON${Version}_EXECUTABLE NAMES "python${Version}") if (PYTHON_EXECUTABLE) set(PYTHONINTERP_FOUND "Found") endif () endif () if (NOT ${PYTHON${Version}_EXECUTABLE} STREQUAL "PYTHON${Version}_EXECUTABLE-NOTFOUND") # Double check the version with the executable itself execute_process(COMMAND "${PYTHON${Version}_EXECUTABLE}" "--version" OUTPUT_VARIABLE VERSION_STRING) if (NOT "${VERSION_STRING}" MATCHES "${Version}") continue() endif() message("Building Python ${Version} with executable ${PYTHON${Version}_EXECUTABLE}") string(REPLACE "." "" VersionShort ${Version}) string(REPLACE "34" ${VersionShort} THIS_BUILT_FILE ${BUILT_FILE}) add_custom_command(OUTPUT ${THIS_BUILT_FILE} COMMAND ${CMAKE_COMMAND} ARGS -E env "ARCHFLAGS=${ARCH_FLAGS}" "${HOST_PLATFORM}" ${PYTHON${Version}_EXECUTABLE} -m build -w -o "${CMAKE_CURRENT_BINARY_DIR}" "-C=--build-option=--libdir=${BINARY_DIR}/lib" "-C=--build-option=--incdir=${SOURCE_DIR}/include" WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" DEPENDS lame "${CMAKE_CURRENT_SOURCE_DIR}/pyproject.toml" "${CMAKE_CURRENT_SOURCE_DIR}/setup.py" "${CMAKE_CURRENT_SOURCE_DIR}/lameenc.c" ) add_custom_target(library${Version} ALL DEPENDS ${THIS_BUILT_FILE}) endif () endforeach () ``` -------------------------------- ### set_out_sample_rate(sample_rate) Source: https://context7.com/chrisstaite/lameenc/llms.txt Sets the output MP3 sample rate in Hz. Use this to resample audio during encoding. ```APIDOC ## set_out_sample_rate(sample_rate) Sets the output MP3 sample rate in Hz. Use this to resample audio during encoding. ```python import lameenc # Downsample from 48kHz input to 44.1kHz output encoder = lameenc.Encoder() encoder.set_in_sample_rate(48000) encoder.set_out_sample_rate(44100) encoder.set_channels(2) encoder.set_bit_rate(128) pcm_48k = b'\x00\x00' * 48000 * 2 # 1 second stereo at 48kHz mp3_data = encoder.encode(pcm_48k) mp3_data += encoder.flush() ``` ``` -------------------------------- ### Encoder Class Usage Source: https://context7.com/chrisstaite/lameenc/llms.txt The main class that wraps the LAME MP3 encoder. Create an instance, configure settings, then encode PCM data in chunks. ```APIDOC ## Encoder Class The main class that wraps the LAME MP3 encoder. Create an instance, configure settings, then encode PCM data in chunks. ```python import lameenc # Create encoder with default settings (128kbps, 44.1kHz, stereo, quality 2) encoder = lameenc.Encoder() # Configure as needed before encoding encoder.set_bit_rate(192) encoder.set_in_sample_rate(48000) encoder.set_channels(2) encoder.set_quality(2) # Encode PCM data (16-bit little-endian interleaved) pcm_data = b'\x00\x00' * 48000 * 2 # 1 second of silence, stereo mp3_data = encoder.encode(pcm_data) # Flush remaining data when done mp3_data += encoder.flush() # Write to file with open('output.mp3', 'wb') as f: f.write(mp3_data) ``` ``` -------------------------------- ### Complete WAV to MP3 Conversion Source: https://context7.com/chrisstaite/lameenc/llms.txt A comprehensive function to convert WAV files to MP3, handling reading WAV data, encoding with LameEnc, and writing the MP3 output. Supports only 16-bit WAV files. ```python import lameenc import wave def wav_to_mp3(wav_path, mp3_path, bitrate=192): """Convert a WAV file to MP3.""" with wave.open(wav_path, 'rb') as wav: channels = wav.getnchannels() sample_rate = wav.getframerate() sample_width = wav.getsampwidth() if sample_width != 2: raise ValueError("Only 16-bit WAV files are supported") encoder = lameenc.Encoder() encoder.silence() encoder.set_channels(channels) encoder.set_in_sample_rate(sample_rate) encoder.set_bit_rate(bitrate) encoder.set_quality(2) mp3_data = bytearray() # Read and encode in chunks chunk_frames = 4096 while True: pcm_data = wav.readframes(chunk_frames) if not pcm_data: break mp3_data += encoder.encode(pcm_data) mp3_data += encoder.flush() with open(mp3_path, 'wb') as f: f.write(mp3_data) return len(mp3_data) # Usage # size = wav_to_mp3('input.wav', 'output.mp3', bitrate=192) # print(f"Created MP3 file: {size} bytes") ``` -------------------------------- ### set_channels(channels) Source: https://context7.com/chrisstaite/lameenc/llms.txt Sets the number of audio channels. Use 1 for mono or 2 for stereo. Must be called before encoding begins. ```APIDOC ## set_channels(channels) Sets the number of audio channels. Use 1 for mono or 2 for stereo. Must be called before encoding begins. ```python import lameenc # Mono encoding encoder = lameenc.Encoder() encoder.set_channels(1) encoder.set_bit_rate(64) encoder.set_in_sample_rate(22050) mono_pcm = b'\x00\x00' * 22050 # 1 second mono silence mp3_data = encoder.encode(mono_pcm) mp3_data += encoder.flush() ``` ``` -------------------------------- ### Set VBR Quality Level Source: https://context7.com/chrisstaite/lameenc/llms.txt Set the VBR quality using set_vbr_quality(), with 0.0 being the highest quality and 9.0 the lowest. This setting is only effective when VBR mode is enabled. ```python import lameenc encoder = lameenc.Encoder() encoder.set_vbr(lameenc.VBR_MTRH) encoder.set_vbr_quality(0.0) # Highest quality VBR encoder.set_in_sample_rate(44100) encoder.set_channels(2) pcm_data = b'\x00\x00' * 44100 * 2 mp3_data = encoder.encode(pcm_data) mp3_data += encoder.flush() ``` -------------------------------- ### Configure Mono Encoding Source: https://context7.com/chrisstaite/lameenc/llms.txt Set the number of audio channels to 1 for mono encoding. This must be called before encoding begins. Ensure sample rate and bit rate are appropriate for mono audio. ```python import lameenc # Mono encoding encoder = lameenc.Encoder() encoder.set_channels(1) encoder.set_bit_rate(64) encoder.set_in_sample_rate(22050) mono_pcm = b'\x00\x00' * 22050 # 1 second mono silence mp3_data = encoder.encode(mono_pcm) mp3_data += encoder.flush() ``` -------------------------------- ### Set VBR Min/Max Bitrate Source: https://context7.com/chrisstaite/lameenc/llms.txt The set_vbr_min_bitrate_kbps(bitrate) and set_vbr_max_bitrate_kbps(bitrate) methods allow you to define the minimum and maximum bitrate constraints for VBR encoding, ensuring the encoded audio stays within a specified range. ```APIDOC ## set_vbr_min_bitrate_kbps(bitrate) / set_vbr_max_bitrate_kbps(bitrate) ### Description Sets minimum and maximum bitrate constraints for VBR encoding. ### Method `set_vbr_min_bitrate_kbps(bitrate: int)` `set_vbr_max_bitrate_kbps(bitrate: int)` ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Parameters - **bitrate** (int) - Required - The bitrate value in kilobits per second (kbps) for the minimum or maximum constraint. ### Request Example ```python import lameenc encoder = lameenc.Encoder() encoder.set_vbr(lameenc.VBR_MTRH) encoder.set_vbr_min_bitrate_kbps(64) # Never go below 64kbps encoder.set_vbr_max_bitrate_kbps(256) # Never exceed 256kbps ``` ### Response Example These methods do not return a value. They set the minimum and maximum bitrate constraints for VBR encoding. ``` -------------------------------- ### Configure High Bit Rate Encoding Source: https://context7.com/chrisstaite/lameenc/llms.txt Set a high constant bit rate, such as 320 kbps, for higher quality MP3 encoding. Adjust sample rate and channels as needed for your audio source. ```python import lameenc # High quality 320kbps encoding encoder = lameenc.Encoder() encoder.set_bit_rate(320) encoder.set_in_sample_rate(44100) encoder.set_channels(2) pcm_data = b'\x00\x00' * 44100 * 2 # 1 second stereo mp3_data = encoder.encode(pcm_data) mp3_data += encoder.flush() ``` -------------------------------- ### Set VBR Bitrate Constraints Source: https://context7.com/chrisstaite/lameenc/llms.txt Constrain VBR encoding using set_vbr_min_bitrate_kbps() and set_vbr_max_bitrate_kbps(). These set the lower and upper bounds for the bitrate. ```python import lameenc encoder = lameenc.Encoder() encoder.set_vbr(lameenc.VBR_MTRH) encoder.set_vbr_quality(4.0) encoder.set_vbr_min_bitrate_kbps(64) # Never go below 64kbps encoder.set_vbr_max_bitrate_kbps(256) # Never exceed 256kbps encoder.set_in_sample_rate(44100) encoder.set_channels(2) pcm_data = b'\x00\x00' * 44100 * 2 mp3_data = encoder.encode(pcm_data) mp3_data += encoder.flush() ``` -------------------------------- ### Configure Fastest Encoding Quality Source: https://context7.com/chrisstaite/lameenc/llms.txt Set the encoding quality to a higher number (e.g., 7) for the fastest encoding speed, which results in lower quality and potentially smaller file sizes. The range is 0-9. ```python import lameenc # Fastest encoding (lower quality) encoder = lameenc.Encoder() encoder.set_quality(7) encoder.set_bit_rate(128) encoder.set_in_sample_rate(44100) encoder.set_channels(2) pcm_data = b'\x00\x00' * 44100 * 2 mp3_data = encoder.encode(pcm_data) mp3_data += encoder.flush() ``` -------------------------------- ### Python Versions Option Source: https://github.com/chrisstaite/lameenc/blob/main/CMakeLists.txt Defines a CMake option to specify the Python versions for which the project should be built. ```cmake option(PYTHON_VERSIONS "Python versions to build" "3.10;3.11;3.12;3.13;3.14") ``` -------------------------------- ### set_bit_rate(bitrate) Source: https://context7.com/chrisstaite/lameenc/llms.txt Sets the constant bit rate in kbps. Common values are 64, 128, 192, 256, 320. Higher values produce better quality but larger files. ```APIDOC ## set_bit_rate(bitrate) Sets the constant bit rate in kbps. Common values are 64, 128, 192, 256, 320. Higher values produce better quality but larger files. ```python import lameenc # High quality 320kbps encoding encoder = lameenc.Encoder() encoder.set_bit_rate(320) encoder.set_in_sample_rate(44100) encoder.set_channels(2) pcm_data = b'\x00\x00' * 44100 * 2 # 1 second stereo mp3_data = encoder.encode(pcm_data) mp3_data += encoder.flush() ``` ``` -------------------------------- ### LameEnc VBR Mode Constants Source: https://context7.com/chrisstaite/lameenc/llms.txt Lists the available module-level constants for selecting VBR modes in the LameEnc library. These constants are used with the set_vbr() method. ```python import lameenc # Available VBR mode constants print(lameenc.VBR_OFF) # 0 - Constant bit rate (CBR) print(lameenc.VBR_RH) # 2 - VBR old/RH algorithm print(lameenc.VBR_ABR) # 3 - Average bit rate print(lameenc.VBR_MTRH) # 4 - VBR MTRH (recommended VBR mode) ``` -------------------------------- ### Strictly Enforce Minimum VBR Bitrate Source: https://context7.com/chrisstaite/lameenc/llms.txt Enable strict enforcement of the minimum bitrate using set_vbr_hard_min(True). This ensures the bitrate never drops below the value set by set_vbr_min_bitrate_kbps(). ```python import lameenc encoder = lameenc.Encoder() encoder.set_vbr(lameenc.VBR_MTRH) encoder.set_vbr_quality(4.0) encoder.set_vbr_min_bitrate_kbps(128) encoder.set_vbr_hard_min(True) # Strictly enforce minimum encoder.set_in_sample_rate(44100) encoder.set_channels(2) pcm_data = b'\x00\x00' * 44100 * 2 mp3_data = encoder.encode(pcm_data) mp3_data += encoder.flush() ``` -------------------------------- ### set_in_sample_rate(sample_rate) Source: https://context7.com/chrisstaite/lameenc/llms.txt Sets the input PCM sample rate in Hz. Common values are 8000, 16000, 22050, 44100, 48000. ```APIDOC ## set_in_sample_rate(sample_rate) Sets the input PCM sample rate in Hz. Common values are 8000, 16000, 22050, 44100, 48000. ```python import lameenc # Encode 16kHz audio (common for speech) encoder = lameenc.Encoder() encoder.set_in_sample_rate(16000) encoder.set_channels(1) encoder.set_bit_rate(32) speech_pcm = b'\x00\x00' * 16000 # 1 second mono at 16kHz mp3_data = encoder.encode(speech_pcm) mp3_data += encoder.flush() ``` ``` -------------------------------- ### Set Variable Bit Rate (VBR) Mode Source: https://context7.com/chrisstaite/lameenc/llms.txt Configure VBR encoding using constants like VBR_MTRH. This must be called before encoding begins. Use set_vbr_quality to specify the desired quality level. ```python import lameenc encoder = lameenc.Encoder() encoder.set_vbr(lameenc.VBR_MTRH) # Best VBR algorithm encoder.set_vbr_quality(2.0) # 0.0 = best, 9.0 = worst encoder.set_in_sample_rate(44100) encoder.set_channels(2) pcm_data = b'\x00\x00' * 44100 * 2 mp3_data = encoder.encode(pcm_data) mp3_data += encoder.flush() ``` -------------------------------- ### Set VBR Mean Bitrate (ABR) Source: https://context7.com/chrisstaite/lameenc/llms.txt The set_vbr_mean_bitrate_kbps(bitrate) method specifies the target average bitrate in kilobits per second (kbps) for Average Bit Rate (ABR) encoding mode. ```APIDOC ## set_vbr_mean_bitrate_kbps(bitrate) ### Description Sets the target average bitrate in kbps for ABR (Average Bit Rate) mode. ### Method `set_vbr_mean_bitrate_kbps(bitrate: int)` ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Parameters - **bitrate** (int) - Required - The target average bitrate in kilobits per second (kbps). ### Request Example ```python import lameenc encoder = lameenc.Encoder() encoder.set_vbr(lameenc.VBR_ABR) # Enable ABR mode encoder.set_vbr_mean_bitrate_kbps(192) # Target 192kbps average bitrate ``` ### Response Example This method does not return a value. It sets the target average bitrate for ABR encoding. ``` -------------------------------- ### set_quality(quality) Source: https://context7.com/chrisstaite/lameenc/llms.txt Sets the encoding quality/speed trade-off. Value range is 0-9 where 2 is highest quality (slowest) and 7 is fastest (lower quality). ```APIDOC ## set_quality(quality) Sets the encoding quality/speed trade-off. Value range is 0-9 where 2 is highest quality (slowest) and 7 is fastest (lower quality). ```python import lameenc # Fastest encoding (lower quality) encoder = lameenc.Encoder() encoder.set_quality(7) encoder.set_bit_rate(128) encoder.set_in_sample_rate(44100) encoder.set_channels(2) pcm_data = b'\x00\x00' * 44100 * 2 mp3_data = encoder.encode(pcm_data) mp3_data += encoder.flush() ``` ``` -------------------------------- ### Flush Remaining MP3 Data Source: https://context7.com/chrisstaite/lameenc/llms.txt Call the `flush()` method after all PCM data has been encoded to retrieve any remaining buffered MP3 data. This ensures the complete MP3 stream is obtained. ```python import lameenc encoder = lameenc.Encoder() encoder.set_bit_rate(128) encoder.set_in_sample_rate(44100) encoder.set_channels(2) pcm_data = b'\x00\x00' * 44100 * 2 mp3_data = encoder.encode(pcm_data) # The flush() call would typically follow here to get the final MP3 data. ``` -------------------------------- ### Set VBR Hard Minimum Source: https://context7.com/chrisstaite/lameenc/llms.txt The set_vbr_hard_min(enabled) method, when set to True, strictly enforces the minimum bitrate previously defined by set_vbr_min_bitrate_kbps(). ```APIDOC ## set_vbr_hard_min(enabled) ### Description When enabled (True), strictly enforces the minimum bitrate set by `set_vbr_min_bitrate_kbps`. ### Method `set_vbr_hard_min(enabled: bool)` ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Parameters - **enabled** (bool) - Required - Set to `True` to strictly enforce the minimum bitrate, or `False` otherwise. ### Request Example ```python import lameenc encoder = lameenc.Encoder() encoder.set_vbr(lameenc.VBR_MTRH) encoder.set_vbr_min_bitrate_kbps(128) encoder.set_vbr_hard_min(True) # Strictly enforce minimum bitrate ``` ### Response Example This method does not return a value. It controls the strictness of the minimum bitrate enforcement. ``` -------------------------------- ### Set VBR Mode Source: https://context7.com/chrisstaite/lameenc/llms.txt The set_vbr(mode) method configures the Variable Bit Rate (VBR) encoding mode. It accepts constants like VBR_OFF, VBR_RH, VBR_ABR, and VBR_MTRH. This must be called before encoding begins. ```APIDOC ## set_vbr(mode) ### Description Sets the Variable Bit Rate mode. Use VBR constants: `VBR_OFF` (0), `VBR_RH` (2), `VBR_ABR` (3), `VBR_MTRH` (4). Must be called before encoding begins. ### Method `set_vbr(mode: int)` ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Parameters - **mode** (int) - Required - The VBR mode constant to set. Use constants from the `lameenc` module (e.g., `lameenc.VBR_MTRH`). ### Request Example ```python import lameenc encoder = lameenc.Encoder() encoder.set_vbr(lameenc.VBR_MTRH) # Set to recommended VBR mode ``` ### Response Example This method does not return a value. It configures the encoder's VBR mode. ``` -------------------------------- ### VBR Constants Source: https://context7.com/chrisstaite/lameenc/llms.txt Provides an overview of the module-level constants available for selecting the Variable Bit Rate (VBR) mode in the LAME encoder. ```APIDOC ## VBR Constants ### Description Module-level constants for VBR mode selection. ### Available Constants - **`lameenc.VBR_OFF`** (int): 0 - Constant bit rate (CBR) mode. - **`lameenc.VBR_RH`** (int): 2 - VBR old/RH algorithm. - **`lameenc.VBR_ABR`** (int): 3 - Average bit rate mode. - **`lameenc.VBR_MTRH`** (int): 4 - VBR MTRH algorithm (recommended VBR mode). ### Request Example ```python import lameenc print(f"VBR Off: {lameenc.VBR_OFF}") print(f"VBR RH: {lameenc.VBR_RH}") print(f"VBR ABR: {lameenc.VBR_ABR}") print(f"VBR MTRH: {lameenc.VBR_MTRH}") # Example of setting VBR mode: encoder = lameenc.Encoder() encoder.set_vbr(lameenc.VBR_MTRH) ``` ### Response Example Prints the integer values associated with each VBR constant. The `set_vbr` method configures the encoder. ``` -------------------------------- ### Encoder Flush Source: https://context7.com/chrisstaite/lameenc/llms.txt The flush() method is required to retrieve any remaining MP3 frames after all audio data has been encoded. This ensures that the complete MP3 file is generated. ```APIDOC ## Encoder Flush ### Description Retrieves the final MP3 frames from the encoder buffer. This method must be called after all audio data has been processed to complete the MP3 file. ### Method `flush()` ### Endpoint N/A (This is a method of the Encoder object) ### Request Example ```python import lameenc encoder = lameenc.Encoder() # ... configure encoder settings ... mp3_data = encoder.encode(pcm_data) final_frames = encoder.flush() complete_mp3 = mp3_data + final_frames ``` ### Response Example ```python # Returns bytes containing the final MP3 frames. final_frames: bytes ``` ``` -------------------------------- ### Set VBR Quality Source: https://context7.com/chrisstaite/lameenc/llms.txt The set_vbr_quality(quality) method sets the desired quality level for VBR encoding. The quality is specified as a float between 0.0 (best) and 9.0 (worst) and only applies when VBR mode is active. ```APIDOC ## set_vbr_quality(quality) ### Description Sets the VBR quality level as a float from 0.0 (best) to 9.0 (worst). Only applies when VBR mode is enabled. ### Method `set_vbr_quality(quality: float)` ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Parameters - **quality** (float) - Required - The VBR quality level, ranging from 0.0 (highest quality) to 9.0 (lowest quality). ### Request Example ```python import lameenc encoder = lameenc.Encoder() encoder.set_vbr(lameenc.VBR_MTRH) encoder.set_vbr_quality(2.0) # Set a high quality level ``` ### Response Example This method does not return a value. It configures the VBR quality setting. ``` -------------------------------- ### Encode PCM Data in Chunks Source: https://context7.com/chrisstaite/lameenc/llms.txt Encode blocks of 16-bit little-endian interleaved PCM data into MP3. The `encode` method can be called multiple times for streaming. Returns a bytearray of MP3 data. ```python import lameenc encoder = lameenc.Encoder() encoder.set_bit_rate(128) encoder.set_in_sample_rate(44100) encoder.set_channels(2) # Streaming encoding in chunks mp3_output = bytearray() chunk_size = 4096 # Simulate reading PCM data in chunks for i in range(10): pcm_chunk = b'\x00\x00' * chunk_size # 16-bit samples mp3_output += encoder.encode(pcm_chunk) mp3_output += encoder.flush() with open('streamed.mp3', 'wb') as f: f.write(mp3_output) ``` -------------------------------- ### encode(pcm_data) Source: https://context7.com/chrisstaite/lameenc/llms.txt Encodes a block of 16-bit little-endian interleaved PCM data into MP3. Can be called multiple times for streaming encoding. Returns a bytearray of MP3 data. ```APIDOC ## encode(pcm_data) Encodes a block of 16-bit little-endian interleaved PCM data into MP3. Can be called multiple times for streaming encoding. Returns a bytearray of MP3 data. ```python import lameenc encoder = lameenc.Encoder() encoder.set_bit_rate(128) encoder.set_in_sample_rate(44100) encoder.set_channels(2) # Streaming encoding in chunks mp3_output = bytearray() chunk_size = 4096 # Simulate reading PCM data in chunks for i in range(10): pcm_chunk = b'\x00\x00' * chunk_size # 16-bit samples mp3_output += encoder.encode(pcm_chunk) mp3_output += encoder.flush() with open('streamed.mp3', 'wb') as f: f.write(mp3_output) ``` ``` -------------------------------- ### Silence Output Source: https://context7.com/chrisstaite/lameenc/llms.txt The silence() method suppresses any output messages that LAME might generate to stdout or stderr. This is useful for achieving quiet operation during encoding. ```APIDOC ## silence() ### Description Suppresses LAME's stdout/stderr output messages. Call this if LAME is being verbose and you want quiet operation. ### Method `silence()` ### Endpoint N/A (This is a method of the Encoder object) ### Request Example ```python import lameenc encoder = lameenc.Encoder() encoder.silence() # Suppress LAME output messages # ... proceed with encoding ... ``` ### Response Example This method does not return any value. Its effect is to prevent LAME from printing messages to the console. ``` -------------------------------- ### Suppress LAME Output Messages Source: https://context7.com/chrisstaite/lameenc/llms.txt Use encoder.silence() to suppress LAME's stdout/stderr output messages for quiet operation. This should be called before setting encoding parameters. ```python import lameenc encoder = lameenc.Encoder() encoder.silence() # Suppress LAME output messages encoder.set_bit_rate(128) encoder.set_in_sample_rate(44100) encoder.set_channels(2) pcm_data = b'\x00\x00' * 44100 * 2 mp3_data = encoder.encode(pcm_data) mp3_data += encoder.flush() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.