首页 > 解决方案 > 使用 TarsosDSP 将立体声转换为单声道不起作用

问题描述

我想在声音数据上使用 TarsosDSP 的一些功能。传入的数据是Stereo,但是Tarsos确实只支持mono,所以我尝试将其转成mono如下,但是结果听起来还是觉得立体声数据被解释为mono,即转换viaMultichannelToMono似乎没有任何效果,虽然乍一看,它的实现看起来不错。

@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处理器不将数据传输到单声道?

标签: javawavtarsosdsp

解决方案


我发现可行的唯一方法是在将数据发送到 TarsosDSP 之前使用 Java 音频系统执行此转换,它似乎没有正确转换帧大小

我在https://www.experts-exchange.com/questions/26925195/java-stereo-to-mono-conversion-unsupported-conversion-error.html找到了以下片段,我在应用更高级之前将其转换为单声道使用 TarsosDSP 进行音频转换。

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);
}

推荐阅读