首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当播放解码的Speex数据时,我听到的只是静态的。

当播放解码的Speex数据时,我听到的只是静态的。
EN

Stack Overflow用户
提问于 2013-02-23 23:51:50
回答 1查看 1.2K关注 0票数 2

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

代码:

(有更好的方法粘贴代码吗?因为当我粘贴它,高亮显示它,并按下Code按钮时,缩进就会变得混乱。)

代码语言:javascript
复制
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);
    }
    }
}

下面是一些输出(总是重复的):

代码语言:javascript
复制
15 bytes resulted as a result of encoding 640 raw bytes.
640 bytes resulted as a result of decoding 15 encoded bytes.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-24 16:41:38

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

代码语言:javascript
复制
AudioFormat format = new AudioFormat(sample_rate, sample_size,
            channels, true, false);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15047108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档