### Install and Use STFT Pitch Shift Python Package Source: https://github.com/jurihock/stftpitchshift/blob/main/README.md Install the Python package using pip. This example shows how to initialize the pitch shifter and perform a pitch shift. ```python from stftpitchshift import StftPitchShift pitchshifter = StftPitchShift(1024, 256, 44100) x = [0] * 44100 y = pitchshifter.shiftpitch(x, 1) ``` -------------------------------- ### Vcpkg Installation and Integration Source: https://github.com/jurihock/stftpitchshift/wiki/Publishing Commands to clone, bootstrap, and integrate Vcpkg into the system. ```bash git clone https://github.com/microsoft/vcpkg.git /opt/vcpkg cd /opt/vcpkg ./bootstrap-vcpkg.sh ./vcpkg integrate install ``` -------------------------------- ### Ubuntu Package Installation Source: https://github.com/jurihock/stftpitchshift/wiki/Publishing Commands to update the system and install necessary build tools and Python dependencies. ```bash apt update && apt upgrade apt install mc nano apt install curl tar unzip zip apt install build-essential cmake debhelper devscripts git ninja-build apt install python3 python3-pip python3-venv pip3 install click matplotlib numpy ``` -------------------------------- ### Launchpad Setup and Configuration Source: https://github.com/jurihock/stftpitchshift/wiki/Publishing Commands and configuration files for setting up Launchpad repository access. ```bash apt install software-properties-common ``` ```ini [stftpitchshift] fqdn = ppa.launchpad.net method = ftp incoming = ~jurihock/stftpitchshift login = jurihock allow_unsigned_uploads = 0 ``` ```bash gpg --import ... ``` -------------------------------- ### Docker Container Setup Source: https://github.com/jurihock/stftpitchshift/wiki/Publishing Commands to pull, run, and enter an Ubuntu Docker container. ```bash docker pull ubuntu:latest docker run -v : -dit --name=ubuntu ubuntu:latest docker exec -it ubuntu bash ``` -------------------------------- ### Homebrew Package Management Source: https://github.com/jurihock/stftpitchshift/wiki/Publishing Commands to install, test, and remove packages using Homebrew. ```bash brew install --build-from-source --verbose --debug stftpitchshift brew test --verbose --debug stftpitchshift brew remove stftpitchshift ``` ```bash brew audit --new-formula stftpitchshift ``` -------------------------------- ### Build STFT Pitch Shift Library with CMake Source: https://github.com/jurihock/stftpitchshift/blob/main/README.md Use CMake to build the C++ library, main, and example programs. Ensure you are in the project's root directory. ```cmd cmake -S . -B build cmake --build build ``` -------------------------------- ### Vcpkg Package Management Source: https://github.com/jurihock/stftpitchshift/wiki/Publishing Commands to install, remove, and version packages within Vcpkg. ```bash ./vcpkg remove stftpitchshift ./vcpkg install stftpitchshift ./vcpkg install stftpitchshift --head ``` ```bash ./vcpkg x-add-version --all ./vcpkg x-add-version stftpitchshift --overwrite-version ``` -------------------------------- ### CMake Configuration for StftPitchShift Examples Source: https://github.com/jurihock/stftpitchshift/blob/main/examples/CMakeLists.txt Defines executable targets and build requirements for the StftPitchShift project. Requires C++20 and linking against the stftpitchshift library. ```cmake cmake_minimum_required(VERSION 3.1...3.18) project(StftPitchShiftExamples) # basic example of single file processing add_executable(example "${CMAKE_CURRENT_LIST_DIR}/example.cpp") # advanced example of continuous stream processing add_executable(realtime "${CMAKE_CURRENT_LIST_DIR}/realtime.cpp") foreach(name example realtime) # at least C++ 20 is mandatory target_compile_features(${name} PRIVATE cxx_std_20) # just the case of default build e.g. if LibStftPitchShift.cmake is already included target_link_libraries(${name} stftpitchshift) # if installed via ppa:jurihock/stftpitchshift on Ubuntu # target_link_libraries(${name} stftpitchshift) # if installed via vcpkg # find_package(stftpitchshift CONFIG REQUIRED) # target_link_libraries(${name} stftpitchshift::stftpitchshift) endforeach() ``` -------------------------------- ### C++ Real-time Pitch Shifting with StftPitchShiftCore Source: https://context7.com/jurihock/stftpitchshift/llms.txt Example of real-time pitch shifting using the StftPitchShiftCore class in C++. Requires STFT and StftPitchShiftCore modules. Ensure buffers are correctly sized and initialized. ```cpp // C++ Example: Real-time pitch shifting with StftPitchShiftCore #include #include #include #include using namespace stftpitchshift; // Configuration const double samplerate = 44100; const size_t overlap = 4; const std::tuple framesize = {1024, 1024}; const size_t hopsize = std::get<1>(framesize) / overlap; // Processing modules std::shared_ptr> stft; std::shared_ptr> core; // Delay buffers std::vector input_buffer, output_buffer; void init() { const size_t total_buffer_size = std::get<0>(framesize) + std::get<1>(framesize); input_buffer.resize(total_buffer_size); output_buffer.resize(total_buffer_size); stft = std::make_shared>(framesize, hopsize); core = std::make_shared>(framesize, hopsize, samplerate); // Configure pitch shifting parameters core->factors({1.5}); // Shift up by 50% core->quefrency(1e-3); // 1ms quefrency for formant preservation core->distortion(1.0); // No timbre distortion core->normalization(false); // Disable RMS normalization } void audio_callback(const float* input, float* output, size_t frame_size) { // Copy input to buffer (with type conversion) std::copy(input, input + frame_size, input_buffer.begin() + std::get<0>(framesize)); // Process through STFT with pitch shifting callback (*stft)(input_buffer, output_buffer, [&](std::span> dft) { core->shiftpitch(dft); }); // Copy output from buffer (with type conversion) std::transform( output_buffer.begin(), output_buffer.begin() + frame_size, output, [](double v) { return static_cast(v); } ); } ``` -------------------------------- ### Python Pitch Shifting Examples Source: https://context7.com/jurihock/stftpitchshift/llms.txt Demonstrates various pitch shifting techniques in Python, including formant preservation and timbre distortion. Ensure the pitchshifter object is initialized before use. ```python output_preserved = pitchshifter.shiftpitch( input_audio, factors=1.5, quefrency=0.001 # 1 millisecond quefrency for formant extraction ) ``` ```python # Shift pitch up by 50% WITHOUT formant preservation (for comparison) output_shifted = pitchshifter.shiftpitch(input_audio, factors=1.5, quefrency=0) ``` ```python # Apply timbre distortion along with formant preservation output_distorted = pitchshifter.shiftpitch( input_audio, factors=1.5, quefrency=0.001, distortion=1.2 # Shift formants by 20% ) ``` -------------------------------- ### Command Line Interface for STFT Pitch Shifting Source: https://context7.com/jurihock/stftpitchshift/llms.txt Examples of using the stftpitchshift command-line tool for various audio processing tasks. Options include input/output files, pitch factors, formant preservation, and normalization. ```bash # Basic pitch shift up by one octave (factor = 2) stftpitchshift -i input.wav -o output.wav -p 2 ``` ```bash # Pitch shift down by one octave (factor = 0.5) stftpitchshift -i input.wav -o output.wav -p 0.5 ``` ```bash # Poly pitch shifting - create a chord (multiple factors) stftpitchshift -i input.wav -o output.wav -p 0.5,1,2 ``` ```bash # Pitch shift using semitone notation (+12 = one octave up) stftpitchshift -i input.wav -o output.wav -p +12 ``` ```bash # Pitch shift with cents precision (-11 semitones minus 100 cents) stftpitchshift -i input.wav -o output.wav -p -11-100 ``` ```bash # Formant-preserving pitch shift (quefrency = 1ms) stftpitchshift -i input.wav -o output.wav -p 1.5 -q 1 ``` ```bash # Formant preservation with timbre distortion stftpitchshift -i input.wav -o output.wav -p 1.5 -q 1 -t 1.2 ``` ```bash # Enable RMS normalization to maintain loudness stftpitchshift -i input.wav -o output.wav -p 2 -r ``` ```bash # Custom STFT parameters (window size 2048, overlap 64) stftpitchshift -i input.wav -o output.wav -p 1.5 -w 2048 -v 64 ``` ```bash # Enable runtime measurements (C++ only) stftpitchshift -i input.wav -o output.wav -p 1.5 -c ``` ```bash # Enable debug spectrograms (Python only) python -m stftpitchshift -i input.wav -o output.wav -p 1.5 -d ``` -------------------------------- ### Include STFT Pitch Shift Library in C++ Project Source: https://github.com/jurihock/stftpitchshift/blob/main/README.md Include the STFT Pitch Shift library in your C++ audio project. This example demonstrates basic initialization and pitch shifting. ```cpp #include using namespace stftpitchshift; StftPitchShift pitchshifter(1024, 256, 44100); std::vector x(44100); std::vector y(x.size()); pitchshifter.shiftpitch(x, y, 1); ``` -------------------------------- ### Launchpad Publishing Source: https://github.com/jurihock/stftpitchshift/wiki/Publishing Commands to execute the build script and sign/upload packages to Launchpad. ```bash ./ubuntu.sh ``` ```bash debsign build-debian/stftpitchshift__source.changes dput stftpitchshift build-debian/stftpitchshift__source.changes ``` -------------------------------- ### Configure Asymmetric Windows for Low Latency Source: https://context7.com/jurihock/stftpitchshift/llms.txt Use asymmetric analysis and synthesis windows to optimize latency in real-time audio processing. ```cpp // C++ Example: Low-latency configuration with asymmetric windows #include #include #include using namespace stftpitchshift; int main() { // Asymmetric window configuration: // - Analysis window: 1024 samples (for quality) // - Synthesis window: 256 samples (for lower latency) std::tuple framesize = {1024, 256}; size_t hopsize = 256 / 4; // Derived from synthesis window double samplerate = 44100; StftPitchShift pitchshifter(framesize, hopsize, samplerate); std::vector input(44100); std::vector output(input.size()); pitchshifter.shiftpitch(input, output, 1.5); return 0; } ``` ```python # Python Example: Asymmetric windows (using tuple for framesize) from stftpitchshift.stft import stft, istft import numpy as np # Asymmetric window configuration analysis_window = 1024 synthesis_window = 256 framesize = (analysis_window, synthesis_window) # Tuple for asymmetric hopsize = synthesis_window // 4 input_signal = np.random.randn(44100).astype(np.float32) # STFT with asymmetric windows frames = stft(input_signal, framesize, hopsize) output_signal = istft(frames, framesize, hopsize) ``` -------------------------------- ### CMake Build with Vcpkg Source: https://github.com/jurihock/stftpitchshift/wiki/Publishing Commands to configure and build a project using the Vcpkg toolchain file. ```bash cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=/opt/vcpkg/scripts/buildsystems/vcpkg.cmake cmake --build build ``` -------------------------------- ### Basic Pitch Shifting in C++ Source: https://context7.com/jurihock/stftpitchshift/llms.txt Demonstrates basic pitch shifting using the StftPitchShift class in C++. Requires initialization with frame size, hop size, and sample rate. Output buffer must be pre-allocated. ```cpp #include #include using namespace stftpitchshift; int main() { // Create pitch shifter with 1024 sample frame, 256 hop size, 44100Hz sample rate StftPitchShift pitchshifter(1024, 256, 44100); // Input and output buffers (1 second of audio) std::vector input(44100); std::vector output(input.size()); // Shift pitch up by one octave (factor = 2.0) double factor = 2.0; double quefrency = 0; // No formant preservation double distortion = 1.0; // No timbre shift pitchshifter.shiftpitch(input, output, factor, quefrency, distortion); return 0; } ``` -------------------------------- ### Read and Write Audio Files in Python Source: https://context7.com/jurihock/stftpitchshift/llms.txt Utilize the IO module to load WAV files, apply pitch shifting, and save results with specific bit depths. ```python # Python Example: Audio file I/O from stftpitchshift.io import read, write, play from stftpitchshift import StftPitchShift import numpy as np # Read a WAV file audio_data, sample_rate = read("input.wav") print(f"Loaded {len(audio_data)} samples at {sample_rate}Hz") # Process with pitch shifter pitchshifter = StftPitchShift(1024, 256, sample_rate) output_data = pitchshifter.shiftpitch(audio_data, factors=1.5) # Write output WAV file (default 32-bit) write("output.wav", output_data, sample_rate) # Write with specific bit depth write("output_16bit.wav", output_data, sample_rate, bits=16) # Play audio using external command (requires sox 'play' command) # play("output.wav") # Blocking playback # play("output.wav", wait=False) # Non-blocking playback ``` -------------------------------- ### Poly Pitch Shifting for Harmonization in Python Source: https://context7.com/jurihock/stftpitchshift/llms.txt Demonstrates poly pitch shifting in Python to create harmonies. Pass a list of factors to the shiftpitch method to apply multiple shifts simultaneously. ```python # Python Example: Poly pitch shifting for harmonization from stftpitchshift import StftPitchShift import numpy as np pitchshifter = StftPitchShift(1024, 256, 44100) # Load or generate input audio input_audio = np.random.randn(44100).astype(np.float32) * 0.5 # Create a major chord by shifting to root, major third, and fifth # Factor 1.0 = original, 1.26 = major third (~4 semitones), 1.5 = perfect fifth (~7 semitones) factors = [1.0, 1.26, 1.5] output_chord = pitchshifter.shiftpitch(input_audio, factors=factors) # Create an octave doubling effect factors_octave = [0.5, 1.0, 2.0] # One octave down, original, one octave up output_octaves = pitchshifter.shiftpitch(input_audio, factors=factors_octave) ``` -------------------------------- ### Poly Pitch Shifting in C++ Source: https://context7.com/jurihock/stftpitchshift/llms.txt Illustrates poly pitch shifting in C++ by passing a vector of double factors to the shiftpitch method for simultaneous pitch modifications. ```cpp // C++ Example: Poly pitch shifting #include #include using namespace stftpitchshift; int main() { StftPitchShift pitchshifter(1024, 256, 44100); std::vector input(44100); std::vector output(input.size()); // Apply multiple pitch shifts simultaneously (harmonization) std::vector factors = {0.5, 1.0, 2.0}; // Octave down, original, octave up pitchshifter.shiftpitch(input, output, factors); return 0; } ``` -------------------------------- ### Python STFT Module for Custom Pipelines Source: https://context7.com/jurihock/stftpitchshift/llms.txt Demonstrates manual STFT analysis and pitch shifting using Python's stftpitchshift module. This allows for custom processing pipelines. Ensure necessary imports are present. ```python # Python Example: Manual STFT processing from stftpitchshift.stft import stft, istft from stftpitchshift.vocoder import encode, decode from stftpitchshift.pitcher import shiftpitch import numpy as np # Configuration framesize = 1024 hopsize = 256 samplerate = 44100 # Generate test signal input_audio = np.sin(2 * np.pi * 440 * np.linspace(0, 1, samplerate)) # STFT analysis - returns complex DFT frames frames = stft(input_audio, framesize, hopsize) print(f"STFT frames shape: {frames.shape}") # (num_frames, framesize/2 + 1) # Encode DFT to magnitude + frequency representation encoded = encode(frames, framesize, hopsize, samplerate) # Apply pitch shifting to encoded frames shifted = shiftpitch(encoded.copy(), factors=[1.5], samplerate=samplerate) ``` -------------------------------- ### Vcpkg Environment Configuration Source: https://github.com/jurihock/stftpitchshift/wiki/Publishing Commands to manage CMake versions and set environment variables for Vcpkg. ```bash apt purge --auto-remove cmake pip3 install --upgrade cmake ``` ```bash export VCPKG_FORCE_SYSTEM_BINARIES=arm ``` -------------------------------- ### Basic Pitch Shifting in Python Source: https://context7.com/jurihock/stftpitchshift/llms.txt Shows basic pitch shifting using the StftPitchShift class in Python. Input audio is expected as a NumPy array. The function returns the pitch-shifted audio. ```python from stftpitchshift import StftPitchShift import numpy as np # Create pitch shifter with 1024 sample frame, 256 hop size, 44100Hz sample rate pitchshifter = StftPitchShift(1024, 256, 44100) # Generate 1 second of test audio (sine wave at 440Hz) sr = 44100 t = np.linspace(0, 1, sr) input_audio = np.sin(2 * np.pi * 440 * t).astype(np.float32) # Shift pitch up by one octave (factor = 2.0) output_audio = pitchshifter.shiftpitch(input_audio, factors=2.0) # Shift pitch down by one octave (factor = 0.5) output_audio_down = pitchshifter.shiftpitch(input_audio, factors=0.5) print(f"Input shape: {input_audio.shape}, Output shape: {output_audio.shape}") # Output: Input shape: (44100,), Output shape: (44100,) ``` -------------------------------- ### Formant-Preserving Pitch Shift in Python Source: https://context7.com/jurihock/stftpitchshift/llms.txt Shows how to perform pitch shifting while preserving formants in Python. Specify a 'quefrency' value to extract and maintain the spectral envelope, reducing the 'Mickey Mouse' effect. ```python # Python Example: Formant-preserving pitch shift from stftpitchshift import StftPitchShift import numpy as np pitchshifter = StftPitchShift(1024, 256, 44100) # Generate vocal-like audio (1 second) input_audio = np.random.randn(44100).astype(np.float32) # Shift pitch up by 50% WITH formant preservation ``` -------------------------------- ### Add cxxopts Library Source: https://github.com/jurihock/stftpitchshift/blob/main/cpp/StftPitchShift/cxxopts/CMakeLists.txt Adds the cxxopts library as an interface library. This is typically used for command-line argument parsing. ```cmake add_library(cxxopts INTERFACE) target_sources(cxxopts INTERFACE "${CMAKE_CURRENT_LIST_DIR}/cxxopts.hpp" ) target_include_directories(cxxopts INTERFACE "${CMAKE_CURRENT_LIST_DIR}/.." ) ``` -------------------------------- ### Configure dr_libs as a CMake Interface Library Source: https://github.com/jurihock/stftpitchshift/blob/main/cpp/StftPitchShift/dr_libs/CMakeLists.txt Defines an interface library target for dr_wav, setting up the necessary include paths and implementation definitions. ```cmake add_library(dr_libs INTERFACE) target_sources(dr_libs INTERFACE "${CMAKE_CURRENT_LIST_DIR}/dr_wav.h" ) target_include_directories(dr_libs INTERFACE "${CMAKE_CURRENT_LIST_DIR}/.." ) target_compile_definitions(dr_libs INTERFACE -DDR_WAV_IMPLEMENTATION ) ``` -------------------------------- ### Vocoder Module Usage Source: https://context7.com/jurihock/stftpitchshift/llms.txt Converts complex DFT data to magnitude and instantaneous frequency for pitch manipulation. ```python # Python Example: Direct vocoder usage from stftpitchshift.vocoder import encode, decode from stftpitchshift.stft import stft, istft import numpy as np framesize = 1024 hopsize = 256 samplerate = 44100 # Get DFT frames from STFT analysis input_signal = np.random.randn(samplerate).astype(np.float32) dft_frames = stft(input_signal, framesize, hopsize) # Encode: converts complex DFT (real + j*imag) to (magnitude + j*frequency) # The frequency values are instantaneous frequency estimates in Hz encoded_frames = encode(dft_frames, framesize, hopsize, samplerate) # Access magnitude and frequency components magnitudes = np.real(encoded_frames) # Spectral magnitude frequencies = np.imag(encoded_frames) # Instantaneous frequency in Hz print(f"Magnitude range: {magnitudes.min():.4f} to {magnitudes.max():.4f}") print(f"Frequency range: {frequencies.min():.1f} to {frequencies.max():.1f} Hz") # Modify the encoded data (e.g., shift frequencies) pitch_factor = 1.5 encoded_frames = magnitudes + 1j * (frequencies * pitch_factor) # Decode: converts back to complex DFT representation with reconstructed phase decoded_frames = decode(encoded_frames, framesize, hopsize, samplerate) # Synthesize output output_signal = istft(decoded_frames, framesize, hopsize) ``` -------------------------------- ### Decode and Synthesize STFT Source: https://context7.com/jurihock/stftpitchshift/llms.txt Reconstructs a time-domain signal from complex DFT representation. ```python # Decode back to complex DFT representation decoded = decode(shifted, framesize, hopsize, samplerate) # STFT synthesis - reconstruct time-domain signal output_audio = istft(decoded, framesize, hopsize) ``` -------------------------------- ### Cepster Formant Extraction Source: https://context7.com/jurihock/stftpitchshift/llms.txt Extracts spectral envelopes using cepstral liftering to preserve formants during processing. ```python # Python Example: Spectral envelope extraction from stftpitchshift.cepster import lifter from stftpitchshift.stft import stft from stftpitchshift.vocoder import encode import numpy as np framesize = 1024 hopsize = 256 samplerate = 44100 # Prepare encoded DFT frames (magnitude + j*frequency format) input_signal = np.random.randn(samplerate).astype(np.float32) frames = stft(input_signal, framesize, hopsize) encoded = encode(frames, framesize, hopsize, samplerate) # Extract spectral envelope using cepstral liftering # quefrency is specified in samples (convert from seconds: quefrency_sec * samplerate) quefrency_sec = 0.001 # 1 millisecond quefrency_samples = int(quefrency_sec * samplerate) envelopes = lifter(encoded, quefrency_samples) # The envelope represents vocal tract resonances (formants) # Divide magnitude by envelope to get residual/excitation signal residual = np.real(encoded) / envelopes residual[~np.isfinite(residual)] = 0 # Handle division by zero # After pitch shifting residual, multiply back by envelope # to preserve original formant structure ``` -------------------------------- ### Spectral Resampling Source: https://context7.com/jurihock/stftpitchshift/llms.txt Performs linear or bilinear interpolation on spectral data for pitch shifting. ```python # Python Example: Spectral resampling from stftpitchshift.resampler import linear, bilinear import numpy as np # Create a spectral magnitude vector spectrum = np.abs(np.fft.rfft(np.random.randn(1024))) # Resample for pitch shift up (stretch spectrum) factor_up = 2.0 # One octave up resampled_up = linear(spectrum, factor_up) # Resample for pitch shift down (compress spectrum) factor_down = 0.5 # One octave down resampled_down = linear(spectrum, factor_down) # Bilinear interpolation for smoother transitions between STFT hops spectrum_prev = np.abs(np.fft.rfft(np.random.randn(1024))) spectrum_curr = np.abs(np.fft.rfft(np.random.randn(1024))) # Average of two linearly resampled spectra resampled_smooth = bilinear(spectrum_prev, spectrum_curr, factor=1.5) print(f"Original length: {len(spectrum)}") print(f"Resampled length: {len(resampled_up)} (same, but content shifted)") ``` -------------------------------- ### RMS Normalization Source: https://context7.com/jurihock/stftpitchshift/llms.txt Maintains consistent loudness levels by normalizing shifted signals against original references. ```python # Python Example: RMS normalization from stftpitchshift.normalizer import normalize from stftpitchshift.stft import stft from stftpitchshift.vocoder import encode, decode from stftpitchshift.pitcher import shiftpitch import numpy as np framesize = 1024 hopsize = 256 samplerate = 44100 input_signal = np.random.randn(samplerate).astype(np.float32) # Process with normalization frames = stft(input_signal, framesize, hopsize) encoded = encode(frames, framesize, hopsize, samplerate) # Save original for normalization reference original_encoded = encoded.copy() # Apply pitch shift shifted = shiftpitch(encoded, factors=[2.0], samplerate=samplerate) # Normalize to match original RMS level per frame normalized = normalize(shifted, original_encoded) # Continue with decode and synthesis decoded = decode(normalized, framesize, hopsize, samplerate) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.