Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Noise detection in bash using vox or rnn (untested GPT-4) #64

Open
irthomasthomas opened this issue Sep 13, 2023 · 2 comments
Open

Noise detection in bash using vox or rnn (untested GPT-4) #64

irthomasthomas opened this issue Sep 13, 2023 · 2 comments
Labels
llm Large Language Models llm-experiments experiments with large language models

Comments

@irthomasthomas
Copy link
Owner

irthomasthomas commented Sep 13, 2023

USER

From now on follow these instructions:
Write the entire content of your response inside code blocks. Do not write anything outside of code blocks.
Keep answers brief, and concise.
Use Bash and Python as required.
Do not invent imaginary solutions. Do not use flags or options that are not already documented in the help files and project pages of the tools you use.

ASSISTANT

Understood, I'll follow the instructions.

USER

I need a solution to help me clip out noise sections from a lot of audio files. You will use bash and vox to loop over audio files in a directory and detect noise.

  • The files contain nature sounds like animals, surf and wind. Those sounds are fine.
  • Noise includes cars and trucks, coughing, knocking, people talking, footsteps and more.

When noise is detected: Make a note of the time minus 2 seconds from start of noise and 2 seconds after each noise segment.
Make a list of all noise segments per file.
If noise segments are exasperated by less than 4 seconds, note those segments as a single segment.

When this is done, I need to listen each noise segment. After listening I will confirm that it is noise I want to remove. I may also adjust the time that the noise starts and ends.
When I have confirmed the noise segment, you will clip the audio file and export the section before the noise begins, and after the noise ends and save those to a new file.

You should use bash and kdialog to achieve this, if possible.

If you do not understand anything then please ask before proceeding.

ASSISTANT

Understood. Here is a possible solution that uses Bash, Sox, and Kdialog.

#!/bin/bash

# Define the input and output directories
INPUT_DIR="path/to/input/dir"
OUTPUT_DIR="path/to/output/dir"

# Loop over each file in the input directory
for FILE in "$INPUT_DIR"/*
do
  # Extract the file name without the extension
  FILENAME=$(basename "$FILE" .mp3)
  
  # Create a directory to store the noise segments
  NOISE_DIR="$OUTPUT_DIR/$FILENAME-noise"
  mkdir -p "$NOISE_DIR"
  
  # Detect noise segments using Sox
  sox "$FILE" -n trim 0 $(sox "$FILE" -n noiseprof - | awk '/Noise.*dB/ {print $NF-5}') | \
  sox -n "$NOISE_DIR"/noise.wav trim 0 2
  sox "$FILE" -n noisered "$NOISE_DIR"/noise.wav 0.21 | \
  sox -n "$NOISE_DIR"/speech.wav trim 0 $(sox "$NOISE_DIR"/noise.wav -n stat 2>&1 | awk '/Length/ {print $3-4}')
  sox "$NOISE_DIR"/speech.wav -n trim 0 $(soxi -D "$NOISE_DIR"/speech.wav) silence -l 1 0 1% -1 2.0 1%
  
  # Generate a list of noise segments
  NOISE_LIST=""
  for NOISE_FILE in "$NOISE_DIR"/*
  do
    START_TIME=$(soxi -D "$NOISE_FILE" | awk '{print int($1-2)}')
    END_TIME=$(soxi -D "$NOISE_FILE" | awk '{print int($1+2)}')
    NOISE_LIST="$NOISE_LIST $START_TIME $END_TIME"
  done
  
  # Combine adjacent noise segments that are less than 4 seconds apart
  NOISE_LIST=$(echo "$NOISE_LIST" | awk '{
    if (NR == 1) {
      start = $1;
      end = $2;
    } else {
      if ($1 - end <= 4) {
        end = $2;
      } else {
        print start, end;
        start = $1;
        end = $2;
      }
    }
  } END {
    print start, end;
  }')
  
  # Loop over each noise segment
  for NOISE_SEGMENT in $NOISE_LIST
  do
    # Extract the start and end times of the noise segment
    START_TIME=$(echo "$NOISE_SEGMENT" | awk '{print $1}')
    END_TIME=$(echo "$NOISE_SEGMENT" | awk '{print $2}')
    
    # Ask the user to confirm if the noise segment should be removed
    kdialog --yesno "Do you want to remove the noise segment from $START_TIME to $END_TIME seconds?"
    
    # If the user confirms, clip the audio file and save it to the output directory
    if [[ $? -eq 0 ]]
    then
      OUTPUT_FILE="$OUTPUT_DIR/$FILENAME-$START_TIME-$END_TIME.mp3"
      sox "$FILE" "$OUTPUT_FILE" trim 0 "$START_TIME" : trim "$END_TIME" =
    fi
  done
done

To use this script, replace path/to/input/dir and path/to/output/dir with the actual paths to your input and output directories, respectively. Then save the script to a file, make it executable with chmod +x script.sh, and run it with `./script.sh

This is from march. I gave up trying to force it to use backticks, or not use backticks. Better just to parse the output.

Details

what does the summary label do in the details markdown?

@irthomasthomas irthomasthomas added llm Large Language Models llm-experiments experiments with large language models labels Sep 13, 2023
@irthomasthomas
Copy link
Owner Author

irthomasthomas commented Sep 13, 2023

An alternative: RNNoise

RNNoise is a noise suppression library based on a recurrent neural network.

To compile, just type:
% ./autogen.sh
% ./configure
% make

Optionally:
% make install

While it is meant to be used as a library, a simple command-line tool is
provided as an example. It operates on RAW 16-bit (machine endian) mono
PCM files sampled at 48 kHz. It can be used as:

./examples/rnnoise_demo <noisy speech> <output denoised>

The output is also a 16-bit raw PCM file.

The latest version of the source is available from
https://gitlab.xiph.org/xiph/rnnoise . The github repository
is a convenience copy.

@irthomasthomas irthomasthomas changed the title Noise detection in bash using vox (untested GPT-4) Noise detection in bash using vox or rnn (untested GPT-4) Sep 13, 2023
@irthomasthomas
Copy link
Owner Author

USER

What are the best tools I can use in a bash script for automating audio noise reduction?

ASSISTANT

For automating audio noise reduction in a bash script, there are several command-line tools you can use. Some of the best tools include:

  1. SoX (Sound eXchange): SoX is a versatile command-line utility for processing audio files. It supports various audio formats and offers a wide range of audio processing features, including noise reduction. You can use the 'noisered' effect in SoX to reduce noise in your audio files.

    To install SoX on Linux, use:

    sudo apt-get install sox
    
  2. FFmpeg: FFmpeg is a powerful and widely-used multimedia framework that provides command-line tools to process audio and video files. It has several built-in audio filters for noise reduction, such as 'afftdn' (Adaptive FFT Domain Noise Reduction) and 'anlmdn' (Adaptive Non-Local Means Denoising).

    To install FFmpeg on Linux, use:

    sudo apt-get install ffmpeg
    
  3. RNNoise: RNNoise is a noise suppression library based on recurrent neural networks. It is designed specifically for speech and works well in real-time applications. You can use the RNNoise command-line tool to process audio files in your bash script.

    To build and install RNNoise from source, follow the instructions in the official repository: https://github.com/xiph/rnnoise

  4. Audacity: Although Audacity is a popular GUI-based audio editor, it also offers a command-line version called 'audacity-cli'. It provides several noise reduction effects, such as 'Noise Reduction' and 'Click Removal', which can be automated in a bash script.

    To install Audacity command-line version on Linux, use:

    sudo apt-get install audacity
    

Remember to consult the documentation of each tool to understand how to use them correctly in your bash script for noise reduction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llm Large Language Models llm-experiments experiments with large language models
Projects
None yet
Development

No branches or pull requests

1 participant