【问题标题】:Java Sound Background Noise RemovalJava 声音背景噪声去除
【发布时间】:2015-09-16 11:25:19
【问题描述】:

每当我在 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();


        }



    }




}

【问题讨论】:

  • 嗡嗡声很可能来自您对数据处理不当。由于您没有发布任何代码,我们永远不会知道。
  • @jaket 发布了一些代码。很抱歉当时没有这样做
  • 有点过火了。你能给我们最小数量的代码来重现你的问题吗?此外,您是否测试/验证了过滤器等组件是否正常工作?

标签: java sockets audio javasound noise


【解决方案1】:

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

那么,你不能用单字节制作你的过滤器

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 2017-07-07
    • 2021-12-07
    • 1970-01-01
    • 2018-07-18
    • 2018-07-15
    相关资源
    最近更新 更多