Java声音背景噪声消除

时间:2015-06-29 20:39:54

标签: java sockets audio javasound noise

每当我在Java中使用SourceDataLine类读取包含音频的字节时,我在播放时会产生恼人的嗡嗡声背景噪音。谁能告诉我如何完全摆脱它?提前谢谢。

更新:我已经发布了代码,但我的实际目标是从麦克风获取字节数据并将其传输到另一个客户端。我正在为客户提供代码。服务器在这里并不重要,因为它的唯一作用是将字节数组转发到另一个客户端。

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

import java.net.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.*;

public class Client001 extends JFrame implements ActionListener {

    JButton startButton = new JButton("Open Microphone");
    DataInputStream fromServer;
    DataOutputStream toServer;
    AudioFormat format = new AudioFormat(44100, 16, 1, true, true);
    DataLine.Info targetInfo = new DataLine.Info(TargetDataLine.class, format);
    DataLine.Info sourceInfo = new DataLine.Info(SourceDataLine.class, format);
    TargetDataLine targetLine; 
    SourceDataLine sourceLine;

    public static void main(String[] args) {


        final Client001 c = new Client001();
        c.connect2Server();
        c.initializeSoundParams();
        c.initGUI();

        Thread receiver = new Thread(){
            public void run(){

                c.startSoundReceiver();
            }

        };
        receiver.start();







    }

    public void connect2Server(){

        try{


            Socket socket = new Socket("108.61.181.112",8000);
            fromServer = new DataInputStream(socket.getInputStream());
            toServer = new DataOutputStream(socket.getOutputStream());



        }

        catch(Exception e){

            e.printStackTrace();
        }
    }


    public void initializeSoundParams(){

        try{
            targetLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
            sourceLine = (SourceDataLine) AudioSystem.getLine(sourceInfo);
        }
        catch(Exception e){

        }

    }


    public byte[] filterNoise(byte[] data,int range){

        byte value = data[0];
        for (int i=1; i<data.length; i++){
                byte currentValue = data[i];
                value += (currentValue - value) / range;
                data[i] = value;
        }

        return data;
    }



    public void startSoundReceiver(){

        try{

            toServer.writeUTF("1");
            sourceLine.open(format);
            sourceLine.start();

            while(true){

                int dataSize = fromServer.readInt();                        
                System.out.println("Length = " + dataSize);                 
                byte[] targetData = new byte[dataSize];
                fromServer.readFully(targetData,0,dataSize);
                //targetData = filterNoise(targetData,60);
                sourceLine.write(targetData, 0, targetData.length);
            }


        }
        catch(Exception e){

            e.printStackTrace();
        }



    }

    public void startSoundSender(){


        try{

            targetLine.open(format);
            targetLine.start();

            int numBytesRead;
            byte[] targetData = new byte[targetLine.getBufferSize() / 5];

            //toServer.writeUTF("1");

            while (true) {
                numBytesRead = targetLine.read(targetData, 0, targetData.length);

                System.out.println("Length = " + numBytesRead);

                if (numBytesRead == -1) break;

                toServer.writeInt(numBytesRead);
                toServer.write(targetData,0,numBytesRead);

                //sourceLine.write(targetData, 0, numBytesRead);
            }
        }
        catch(Exception e){

            e.printStackTrace();
        }

    }





    public void initGUI(){


        setLayout(null);
        startButton.setBounds(30, 30, 180, 70);
        add(startButton);
        setTitle("Sound GUI");
        setSize(300,300);
        startButton.addActionListener(this);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == startButton){
            Thread sender = new Thread(){
                public void run(){

                    startSoundSender();
                }

            };
            sender.start();


        }



    }




}


import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

import java.net.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.*;

public class Client002 extends JFrame implements ActionListener {

    JButton startButton = new JButton("Open Microphone");
    DataInputStream fromServer;
    DataOutputStream toServer;
    AudioFormat format = new AudioFormat(44100, 8, 2, true, true);
    DataLine.Info targetInfo = new DataLine.Info(TargetDataLine.class, format);
    DataLine.Info sourceInfo = new DataLine.Info(SourceDataLine.class, format);
    TargetDataLine targetLine; 
    SourceDataLine sourceLine;

    public static void main(String[] args) {


        final Client002 c = new Client002();
        c.connect2Server();
        c.initializeSoundParams();
        c.initGUI();

        Thread receiver = new Thread(){
            public void run(){

                c.startSoundReceiver();
            }

        };
        receiver.start();







    }

    public void connect2Server(){

        try{


            Socket socket = new Socket("192.168.0.102",8000);
            fromServer = new DataInputStream(socket.getInputStream());
            toServer = new DataOutputStream(socket.getOutputStream());



        }

        catch(Exception e){

            e.printStackTrace();
        }
    }


    public void initializeSoundParams(){

        try{
            targetLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
            sourceLine = (SourceDataLine) AudioSystem.getLine(sourceInfo);
        }
        catch(Exception e){

        }

    }

    public byte[] filterNoise(byte[] data,int range){

        byte value = data[0];
        for (int i=1; i<data.length; i++){
                byte currentValue = data[i];
                value += (currentValue - value) / range;
                data[i] = value;
        }

        return data;
    }

    public void startSoundReceiver(){

        try{

            toServer.writeUTF("2");
            sourceLine.open(format);
            sourceLine.start();

            while(true){

                int dataSize = fromServer.readInt();                        
                System.out.println("Length = " + dataSize);                 
                byte[] targetData = new byte[dataSize];
                fromServer.readFully(targetData,0,dataSize);
                targetData = filterNoise(targetData,20);
                sourceLine.write(targetData, 0, targetData.length);
            }


        }
        catch(Exception e){

            e.printStackTrace();
        }



    }

    public void startSoundSender(){


        try{

            targetLine.open(format);
            targetLine.start();

            int numBytesRead;
            byte[] targetData = new byte[targetLine.getBufferSize() / 5];

            //toServer.writeUTF("1");

            while (true) {
                numBytesRead = targetLine.read(targetData, 0, targetData.length);

                System.out.println("Length = " + numBytesRead);

                if (numBytesRead == -1) break;

                toServer.writeInt(numBytesRead);
                toServer.write(targetData,0,numBytesRead);

                //sourceLine.write(targetData, 0, numBytesRead);
            }
        }
        catch(Exception e){

            e.printStackTrace();
        }

    }





    public void initGUI(){


        setLayout(null);
        startButton.setBounds(30, 30, 180, 70);
        add(startButton);
        setTitle("Sound GUI");
        setSize(300,300);
        startButton.addActionListener(this);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == startButton){
            Thread sender = new Thread(){
                public void run(){

                    startSoundSender();
                }

            };
            sender.start();


        }



    }




}

1 个答案:

答案 0 :(得分:1)

如果样本大小为 16位“ AudioFormat(44100,16,1,true,true);“

然后,您将无法使用单个字节创建过滤器

您应该尝试使用8位样本大小,或者尝试合并两个字节来进行过滤操作。