0

I am trying to write a code in Java that’s takes a simple wav file and adds to it a comfort noise. the end result should be one wav file that is my a mixed between the two sound together. (not to concatenate). I found a python script that does something like that but I would like to do this in native Java. I know that using a codec g729b will have some similar affect but sadly this is also out of my scope using java.

I will appreciate any suggestion on how to add comfort noise with java to my source wav file.

This is the python script that I found but if there is a better way I have no problem with that:

import sys
import scipy.io.wavfile as wav
from scipy.signal import lfilter
from numpy.random import randn

source_wav_file = sys.argv[1]
print("source_wav_file is:", source_wav_file)



def add_noise(inputFileName):
    #Audio augumentation using generated comfort noise

    #Parameters:
    #inputFileName : Audio wave file name, with no '.wav' extension
    #   accepts only PCM, 32bit Float, 8kHz, mono (as in enrollment channel)

    #Writes processed wave file.
    #This is a prototype.
    #No exceptions are handled.



    # input only PCM, 32bit Float, 8kHz, mono (as in enrollment channel)
    (rate, audio) = wav.read(inputFileName)
    audio = audio / 32767.0  # convert audio back to float
    noise = 7 / 32768.0 * randn(len(audio))  # geneate comfort noise
    b = [0.98]  # design lowpasIIR filter numeator
    a = [1, -b[0]]  # design lowpasIIR filter denominator
    noise = lfilter(b, a, noise)  # apply lowpass filter to white noise
    audio = audio + noise  # mix audio with noise
    out_wav_file = inputFileName + "_out.wav"
    wav.write(out_wav_file, 8000, audio)  # save the output wave file
    print("out wav file is: ", out_wav_file)
    return

add_noise(source_wav_file)  # add comfort noise to the file "audio.wav", and write "audio_out.wav"


Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Asaf Magen
  • 862
  • 10
  • 22
  • Not sure if this is what you want but Google might get you closer. These are 2 different answers but see which actually works. https://stackoverflow.com/questions/18453564/java-record-mix-two-audio-streams. https://stackoverflow.com/questions/9079900/mixing-audio-files-in-java – jiveturkey Apr 28 '20 at 13:03
  • i tried that and the end result is different with a lot of background noise. i think it missing the low pass filter as in the python script. – Asaf Magen Apr 28 '20 at 14:09

0 Answers0