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

时间:2013-02-23 23:51:51

标签: java voip jspeex

我正在学习如何将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.

1 个答案:

答案 0 :(得分:2)

JSpeex输入/输出是little-endian。在编码之前/解码之后交换字节,或者用

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