torchcrepe Documentation

repository·master·Indexed 19 days ago

https://github.com/maxrmorrison/torchcrepe

A PyTorch implementation of the CREPE pitch tracker for high-fidelity pitch and periodicity estimation from audio. It supports 'tiny' and 'full' model capacities, multiple decoding methods including Viterbi, weighted_argmax, and argmax, and provides tools for filtering, thresholding, and extracting model activations or embeddings.

Tokens
2.1K
Snippets
7
Records
7
Agent score
18%

What's inside torchcrepe

  1. Filter and threshold pitch and periodicity

    master

    To handle noisy periodicity or quantization artifacts, use the filter and threshold submodules.

    Common Workflow:

    1. Median Filter: Smooth noisy periodicity values using torchcrepe.filter.median.
    2. Thresholding: Remove inharmonic regions using torchcrepe.threshold.At(threshold_value).
    3. Smoothing: Remove quantization artifacts in pitch using torchcrepe.filter.mean.

    Specialized Thresholding:

    • torchcrepe.threshold.Hysteresis: Provides fine-grained control for removing spurious voiced regions caused by noise.
    • torchcrepe.threshold.Silence(threshold_db): Manually sets periodicity to zero in silent regions (useful because CREPE may assign high confidence to silence).
    # Example: 15ms window assuming 5ms hop length (win_length = 3)
    win_length = 3
    
    # 1. Median filter noisy confidence
    periodicity = torchcrepe.filter.median(periodicity, win_length)
    
    # 2. Remove inharmonic regions (e.g., threshold of 0.21)
    pitch = torchcrepe.threshold.At(.21)(pitch, periodicity)
    
    # 3. Smooth pitch to remove quantization artifacts
    pitch = torchcrepe.filter.mean(pitch, win_length)
    
    # Handling silence specifically
    periodicity = torchcrepe.threshold.Silence(-60.)(periodicity, audio, sr, hop_length)
  2. Predict pitch and embeddings from files

    master

    Convenience functions are provided to process audio files directly from disk without manual loading.

    # Pitch prediction
    torchcrepe.predict_from_file(audio_file, ...)
    torchcrepe.predict_from_file_to_file(audio_file, output_pitch_file, output_periodicity_file, ...)
    torchcrepe.predict_from_files_to_files(audio_files, output_pitch_files, output_periodicity_files, ...)
    
    # Embedding extraction
    torchcrepe.embed_from_file(audio_file, ...)
    torchcrepe.embed_from_file_to_file(audio_file, output_file, ...)
    torchcrepe.embed_from_files_to_files(audio_files, output_files, ...)
  3. Select a decoding method for pitch prediction

    master

    By default, torchcrepe uses Viterbi decoding on the softmax of the network output to penalize large pitch jumps and reduce double/half frequency errors. You can specify different decoders via the decoder argument in torchcrepe.predict:

    • torchcrepe.decode.viterbi: Default; uses Viterbi decoding.
    • torchcrepe.decode.weighted_argmax: Matches the original CREPE implementation (weighted average near the argmax of binary cross-entropy probabilities).
    • torchcrepe.decode.argmax: Standard argmax operation.
    # Decode using viterbi decoding (default)
    torchcrepe.predict(..., decoder=torchcrepe.decode.viterbi)
    
    # Decode using weighted argmax (as in the original implementation)
    torchcrepe.predict(..., decoder=torchcrepe.decode.weighted_argmax)
    
    # Decode using argmax
    torchcrepe.predict(..., decoder=torchcrepe.decode.argmax)
  4. Compute pitch and periodicity from audio

    master

    Use torchcrepe.predict to estimate pitch from an audio waveform. You can also extract a periodicity metric (similar to the CREPE confidence score) by setting return_periodicity=True.

    import torchcrepe
    
    # Load audio
    audio, sr = torchcrepe.load.audio( ... )
    
    # Configuration parameters
    hop_length = int(sr / 200.)
    fmin = 50
    fmax = 550
    model = 'tiny'  # Options: 'tiny' or 'full'
    device = 'cuda:0'
    batch_size = 2048
    
    # Compute pitch
    pitch = torchcrepe.predict(audio,
                               sr,
                               hop_length,
                               fmin,
                               fmax,
                               model,
                               batch_size=batch_size,
                               device=device)
    
    # To also get periodicity:
    pitch, periodicity = torchcrepe.predict(audio,
                                              sr,
                                              hop_length,
                                              fmin,
                                              fmax,
                                              model,
                                              batch_size=batch_size,
                                              device=device,
                                              return_periodicity=True)
  5. Compute model activations and embeddings

    master

    For advanced use cases like DDSP, you can extract raw activations or pretrained pitch embeddings.

    • Activations: Use torchcrepe.preprocess to prepare the audio and torchcrepe.infer to get probabilities.
    • Embeddings: Use torchcrepe.embed to get embeddings from the fifth max-pooling layer.
    # Compute activations
    batch = next(torchcrepe.preprocess(audio, sr, hop_length))
    probabilities = torchcrepe.infer(batch)
    
    # Compute embeddings
    embeddings = torchcrepe.embed(audio, sr, hop_length)
  6. Use the torchcrepe CLI

    master

    You can run torchcrepe from the command line to process audio files.

    Available Arguments:

    • --audio_files AUDIO_FILES: The audio file(s) to process.
    • --output_files OUTPUT_FILES: File to save pitch or embedding.
    • --hop_length HOP_LENGTH: Analysis window hop length.
    • --output_periodicity_files OUTPUT_PERIODICITY_FILES: File to save periodicity.
    • --embed: Perform embedding extraction instead of pitch prediction.
    • --fmin FMIN: Minimum frequency allowed.
    • --fmax FMAX: Maximum frequency allowed.
    • --model MODEL: Model capacity (tiny or full).
    • --decoder DECODER: Decoder type (argmax, viterbi, or weighted_argmax).
    • --gpu GPU: GPU device for inference.
    • --no_pad: Whether to disable audio padding.
    python -m torchcrepe --audio_files input.wav --output_files out.npy --model tiny --gpu cuda:0