我想在声音数据上使用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处理器不将数据传输到单声道?
发布于 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应用更高级的音频转换之前,我用它来转换成单声道。
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);
}https://stackoverflow.com/questions/50631179
复制相似问题