首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用TarsosDSP将立体声转换为单声道不起作用

使用TarsosDSP将立体声转换为单声道不起作用
EN

Stack Overflow用户
提问于 2018-06-01 03:06:19
回答 1查看 243关注 0票数 1

我想在声音数据上使用TarsosDSP的一些功能。传入的数据是立体声,但Tarsos只支持单声道,所以我尝试将其转换为单声道,但结果听起来仍然像是解释为单声道的立体声数据,即通过MultichannelToMono转换似乎没有任何效果,尽管它的实现看起来很好。

代码语言:javascript
复制
@Test
public void testPlayStereoFile() throws IOException, UnsupportedAudioFileException, LineUnavailableException {
    AudioDispatcher dispatcher = AudioDispatcherFactory.fromFile(FILE,4096,0);
    dispatcher.addAudioProcessor(new MultichannelToMono(dispatcher.getFormat().getChannels(), false));
    dispatcher.addAudioProcessor(new AudioPlayer(dispatcher.getFormat()));
    dispatcher.run();
}

这里有什么地方我做错了吗?为什么MultichannelToMono处理器不将数据传输到单声道?

EN

回答 1

Stack Overflow用户

发布于 2018-06-02 20:56:49

我发现唯一有效的方法是在将数据发送到TarsosDSP之前使用Java Audio System执行此转换,它似乎不能正确转换帧大小

我在https://www.experts-exchange.com/questions/26925195/java-stereo-to-mono-conversion-unsupported-conversion-error.html上找到了下面这段代码,在使用TarsosDSP应用更高级的音频转换之前,我用它来转换成单声道。

代码语言:javascript
复制
public static AudioInputStream convertToMono(AudioInputStream sourceStream) {
    AudioFormat sourceFormat = sourceStream.getFormat();

    // is already mono?
    if(sourceFormat.getChannels() == 1) {
        return sourceStream;
    }

    AudioFormat targetFormat = new AudioFormat(
            sourceFormat.getEncoding(),
            sourceFormat.getSampleRate(),
            sourceFormat.getSampleSizeInBits(),
            1,
            // this is the important bit, the framesize needs to change as well,
            // for framesize 4, this calculation leads to new framesize 2
            (sourceFormat.getSampleSizeInBits() + 7) / 8,
            sourceFormat.getFrameRate(),
            sourceFormat.isBigEndian());
    return AudioSystem.getAudioInputStream(targetFormat, sourceStream);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50631179

复制
相关文章

相似问题

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