Skip to content

lowfc/go-audio-denoise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-audio-denoise

A Go library for noise reduction in audio recordings. It works with 16bit PCM input. Mono and stereo.

Features

Remove background noise from audio files using spectral subtraction algorithm.

This library provides three noise reduction algorithms:

  • Spectral Subtract Method
  • Adaptive Method
  • Noise Gate Method

Try all on your data and choose the most suitable one.

Demo

Here is voice recording with a lot of harmonic noise:


Let's apply all the algorithms and see the result.


Spectral Subtract:

Noise Gate:

Adaptive:

Installation

go get github.com/lowfc/go-audio-denoise

Usage

package main

import (
	"log"
	"os"

	"github.com/go-audio/audio"
	"github.com/go-audio/wav"
	rd "github.com/lowfc/go-audio-denoise/reducer"
)

func main() {

	// For example, let's take a wav file.
	inputFile, err := os.Open("example.wav")
	if err != nil {
		log.Fatal("Failed open file:", err)
	}
	defer inputFile.Close()

	decoder := wav.NewDecoder(inputFile)
	if !decoder.IsValidFile() {
		panic("invalid wav file")
	}
	inputAudioData, errRead := decoder.FullPCMBuffer()
	if errRead != nil {
		panic(errRead)
	}

	// Create reducer with default parameters.
	reducer := rd.NewReducer(2048, 512, rd.SpectralSubtractMethod)

	// Since the reducer works with normalized values, we normalize the resulting buffer.
	noisedAudio := rd.NormalizeIntoFloat(inputAudioData.Data)

	// Now we are ready to create a noise sample.
	noiseSample := noisedAudio[:10000] // Take the first 10k samples as noise sample.
	// You can adjust this based on your needs.
	// Don't forget to set the noise sample before reducing the audio.
	reducer.NewNoiseSample(noiseSample)

	// Let's make a denoised audio.
	denoisedAudio, errReduce := reducer.Reduce(noisedAudio) // here is denoised audio
	if errReduce != nil {
		panic(errReduce)
	}
	// Now we need to convert the denoised audio back to int16 format.
	outputData := rd.DenormalizeIntoInt(denoisedAudio)

	// Ok! Now we can save the denoised audio to a file.
	outputFile, errCreate := os.Create("denoised_example.wav")
	if errCreate != nil {
		panic(errCreate)
	}
	defer outputFile.Close()

	encoder := wav.NewEncoder(
		outputFile,
		inputAudioData.Format.SampleRate,
		inputAudioData.SourceBitDepth,
		inputAudioData.Format.NumChannels,
		1, // PCM
	)

	outBuf := &audio.IntBuffer{
		Format: inputAudioData.Format,
		Data:   outputData,
	}

	if err := encoder.Write(outBuf); err != nil {
		panic(err)
	}
	encoder.Close()

	// We are done! The denoised audio is saved to "denoised_example.wav".
	// Yoy can also experiment with different noise reduction methods and parameters in reducer:
	//
	// OverSubtractFactor: Power of noise subtraction in spectral subtraction method
	// LowFreqFacrtor:     Frequency factors in adaptive subtraction method
	// HighFreqFacrtor:
	// ThresholdDb:        Threshold decibels in noise gate subtraction method
}

License

MIT License.

About

A library for performing noise reduction in audio by spectral subtraction

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages