nnnoiseless

repository·main·Indexed 18 days ago

https://github.com/jneem/nnnoiseless

A Rust port of the RNNoise C library for audio noise suppression based on a recurrent neural network. It provides a library interface, a command-line tool for processing WAV and RAW PCM files, and a C API. The crate includes functionality for loading custom neural network models, extracting audio features via DenoiseFeatures, and tools for training new models using Keras.

Tokens
6.7K
Snippets
31
Records
38
Agent score
62%

What's inside nnnoiseless

  1. Convert RNNoise weights to nnnoiseless format

    main

    Weights originally created for the RNNoise library (often in a text-based format) must be converted to the binary nnnoiseless format before use. You can use the provided Python conversion script located in the train/ directory.

    python train/convert_rnnoise.py input_file.txt output_file.rnn
  2. Install nnnoiseless as a C library

    main

    You can install nnnoiseless as a C library using an RNNoise-compatible header. This requires cargo-c to build and install the library into a staging directory or your system.

    $ cargo install cargo-c
    $ mkdir staging-nnnoiseless
    $ cargo cinstall --destdir staging-nnnoiseless
    $ sudo cp -a staging-nnnoiseless/* /
  3. Install the nnnoiseless CLI

    main

    You can install the nnnoiseless command-line tool using cargo. This tool allows you to process WAV or RAW PCM files for audio noise suppression.

    Ensure you have Rust installed before running the command.

    cargo install nnnoiseless
  4. Train a new model using Keras

    main

    Once training.h5 is generated, use the provided Python script to perform the actual training. This process requires python and the keras module installed.

    Execution: Run the rnn_train.py script from the root directory.

    Outputs:

    • weights.hdf5: A Keras-compatible model description.
    • weights.rnn: The model description specifically formatted for use with nnnoiseless.
    python train/rnn_train.py
  5. Generate training data features

    main

    Use the nnnoiseless training binary to generate training features from your audio samples. This step produces an training.h5 file required by the Python training script.

    Command: Run the train binary with the train feature enabled.

    Arguments:

    • --count=<COUNT>: The number of training frames to generate (e.g., 10000000 for 10 million).
    • --signal-glob=<PATH>: A glob pattern for speech/signal WAV files. You can use this flag multiple times for multiple sources.
    • --noise-glob=<PATH>: A glob pattern for noise WAV files. You can use this flag multiple times for multiple sources.
    • -o training.h5: The output filename (must be training.h5).
    cargo run --features=train --bin=train --release -- --count=<COUNT> --signal-glob=</PATH/TO/SPEECH/*.wav> --noise-glob=<PATH/TO/NOISE/*.wav> -o training.h5
  6. Prepare speech and noise audio samples

    main

    To train a new model, you must prepare two collections of audio files: one for speech (the target signal) and one for noise.

    Requirements:

    • Files must be 16-bit, little-endian, 48kHz, 1-channel WAV files.
    • If your files are in a different format, use ffmpeg to convert them.

    Data Sources:

    • rnnoise_contributions.tar.gz (available at https://media.xiph.org/rnnoise/rnnoise_contributions.tar.gz)
    • DNS Challenge
    ffmpeg -i $file -f s16le -ac 1 -ar 48000 output.wav
  7. Use the nnnoiseless CLI to suppress noise

    main

    Once installed, use the nnnoiseless command to process audio files. The basic syntax is:

    nnnoiseless <input_file> <output_file>

    Supported input formats include WAV and RAW PCM files.

    nnnoiseless input.wav output.wav
  8. Run nnnoiseless with a custom model

    main

    After training, you can use your newly learned weights.rnn file to process audio files using the nnnoiseless CLI.

    Command:

    cargo run --release -- --model weights.rnn <INPUT> <OUTPUT>

    Arguments:

    • --model weights.rnn: Path to your trained .rnn weights file.
    • <INPUT>: Path to the input audio file.
    • <OUTPUT>: Path where the processed audio will be saved.
  9. Load custom neural network models in Rust

    main

    If you are using nnnoiseless as a library in a Rust project, you can load custom neural network weights using the following methods on the RnnModel struct:

    • RnnModel::from_bytes: Load weights from a byte slice.
    • RnnModel::from_static_bytes: Load weights from a static byte slice.
  10. Use custom neural network models with the CLI

    main

    By default, nnnoiseless uses a built-in neural network. You can replace this with your own trained weights (e.g., a .rnn file) by using the --model option.

    nnnoiseless --model=weights.rnn input.wav output.wav
  11. Use DenoiseFeatures for audio feature extraction

    main

    The DenoiseFeatures struct is the primary interface for computing audio features used for noise removal, speech detection, or training new neural networks. It maintains the necessary state (buffers, Fourier transforms, and pitch detection) to process audio frames.

    To use it, initialize a new instance with DenoiseFeatures::new(), feed it audio input using shift_input or shift_and_filter_input, and then call compute_frame_features to extract the feature vector.

    use nnnoiseless::features::DenoiseFeatures;
    
    let mut df = DenoiseFeatures::new();
    
    // Assuming 'input_frame' is a &[f32] of length FRAME_SIZE
    df.shift_and_filter_input(input_frame);
    
    // compute_frame_features returns true if the input was essentially silent
    let is_silent = df.compute_frame_features();
    
    if !is_silent {
        let features = df.features();
        // Use the extracted features (e.g., for a neural network)
    }