因此,我用正弦波(或某种在某种意义上类似正弦波的东西)填充了麦克风,并且您的程序运行正常。
因此,我的具体更改是:
package audioclient;import java.io.*;import java.net.*;import java.nio.ByteBuffer;import javax.sound.sampled.*;public class Mic { public byte[] buffer; private int port; static AudioInputStream ais; public static void main(String[] args) { TargetDataLine line; DatagramPacket dgp; AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED; float rate = 44100.0f; int channels = 2; int sampleSize = 16; boolean bigEndian = true; InetAddress addr; AudioFormat format = new AudioFormat(encoding, rate, sampleSize, channels, (sampleSize / 8) * channels, rate, bigEndian); DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); if (!AudioSystem.isLineSupported(info)) { System.out.println("Line matching " + info + " not supported."); return; } try { line = (TargetDataLine) AudioSystem.getLine(info); //TOTALLY missed this. int buffsize = line.getBufferSize() / 5; buffsize += 512; line.open(format); line.start(); int numBytesRead; byte[] data = new byte[buffsize]; { final int λ = 16; ByteBuffer buffer = ByteBuffer.allocate(λ * 2 * 8); for(int j = 0; j < 2; j++) { for(double i = 0.0; i < λ; i++) { System.out.println(j + " " + i); //once for each sample buffer.putShort((short)(Math.sin(Math.PI * (λ/i)) * Short.MAX_VALUE)); buffer.putShort((short)(Math.sin(Math.PI * (λ/i)) * Short.MAX_VALUE)); } } data = buffer.array(); } addr = InetAddress.getByName("127.0.0.1"); try(DatagramSocket socket = new DatagramSocket()) { while (true) { for(byte b : data) System.out.print(b + " "); // Read the next chunk of data from the TargetDataLine.// numBytesRead = line.read(data, 0, data.length); for(int i = 0; i < 64; i++) { byte b = data[i]; System.out.print(b + " "); } System.out.println(); // Save this chunk of data. dgp = new DatagramPacket(data, data.length, addr, 50005); for(int i = 0; i < 64; i++) { byte b = dgp.getData()[i]; System.out.print(b + " "); } System.out.println(); socket.send(dgp); } } } catch (LineUnavailableException e) { e.printStackTrace(); } catch (UnknownHostException e) { // TODO: handle exception } catch (SocketException e) { // TODO: handle exception } catch (IOException e2) { // TODO: handle exception } }}显然,我将其误解为一个512字节长的片段,并破坏了正弦波,但事实是,它产生的声音恰恰是它的本意-在特定的音调下令人麻木的声音。
考虑到这一点,我不怀疑问题出在您的代码中。我要检查的第一件事是系统正在窃听音频的线路。您有连接多个麦克风吗?网络摄像头麦克风,也许吗?您可能需要使用PulseAudio音量控制之类的实用程序进行检查。如果您尚未检查麦克风的功能,也可以这样做。他们确实有寿命。
扰乱音频流中的位并不少见,也不难。但我看不到任何可以执行此操作的地方。
一种想法可能是修改程序,以尝试在本地播放声音,然后再将其发送到服务器。这样,您至少可以确定问题是麦克风前还是麦克风后。



