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



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

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

我发现唯一有效的方法是在将数据发送到 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);
}

最新更新