gpt4 book ai didi

java - 播放解码的 Speex 数据时,我听到的都是静态的

转载 作者:行者123 更新时间:2023-11-30 09:26:07 26 4
gpt4 key购买 nike

我正在学习如何将 JSpeex 用于 VoIP 应用程序,稍后我将出于教育目的编写该应用程序。为了尝试了解如何使用 JSpeex,我决定编写一个简单的 echo 应用程序。基本上,它从音频输入线读取输入,对数据进行编码(使用 Speex),然后解码数据,并将其写入音频输出线。但是,当我运行该应用程序时,我听到的都是静态的,没有任何声音。我试图修补音频格式,如何初始化解码器和编码器,所有这些我都没有运气。任何人都可以查看代码并尝试指出我做错了什么吗?谢谢。

代码:

(有没有更好的粘贴代码的方法?因为当我粘贴它时,将其全部突出显示,然后按代码按钮,缩进会弄乱。)

import java.util.Arrays;

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

import org.xiph.speex.SpeexDecoder;
import org.xiph.speex.SpeexEncoder;

public class SpeexTest {

public static void main(String[] args) throws Exception {
int sample_rate = 8000;
int sample_size = 16;
int channels = 1;
AudioFormat format = new AudioFormat(sample_rate, sample_size,
channels, true, true);
TargetDataLine line_in;
DataLine.Info info_in = new DataLine.Info(TargetDataLine.class, format);
try {
line_in = (TargetDataLine) AudioSystem.getLine(info_in);
line_in.open(format);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
return;
}
DataLine.Info info_out = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine line_out;
try {
line_out = (SourceDataLine) AudioSystem.getLine(info_out);
line_out.open(format);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
return;
}
SpeexEncoder encoder = new SpeexEncoder();
SpeexDecoder decoder = new SpeexDecoder();
encoder.init(1, 1, sample_rate, channels);
decoder.init(1, sample_rate, channels, false);
int raw_block_size = encoder.getFrameSize() * channels
* (sample_size / 8);
byte[] buffer = new byte[raw_block_size * 2];
line_in.start();
line_out.start();
while (true) {
int read = line_in.read(buffer, 0, raw_block_size);
if (!encoder.processData(buffer, 0, raw_block_size)) {
System.err.println("Could not encode data!");
break;
}
int encoded = encoder.getProcessedData(buffer, 0);
System.out.println(encoded
+ " bytes resulted as a result of encoding " + read
+ " raw bytes.");
byte[] encoded_data = new byte[encoded];
System.arraycopy(buffer, 0, encoded_data, 0, encoded);
decoder.processData(encoded_data, 0, encoded);
byte[] decoded_data = new byte[decoder.getProcessedDataByteSize()];
int decoded = decoder.getProcessedData(decoded_data, 0);
System.out.println(decoded
+ " bytes resulted as a result of decoding " + encoded
+ " encoded bytes.");
line_out.write(decoded_data, 0, decoded);
}
}
}

这是一些输出(相同的总是重复):

15 bytes resulted as a result of encoding 640 raw bytes.
640 bytes resulted as a result of decoding 15 encoded bytes.

最佳答案

JSpeex 输入/输出是小端。要么在编码前/解码后交换字节,要么用

打开 DataLine
AudioFormat format = new AudioFormat(sample_rate, sample_size,
channels, true, false);

关于java - 播放解码的 Speex 数据时,我听到的都是静态的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15047108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com