java - 使用 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
处理器不将数据传输到单声道?
解决方案
我发现可行的唯一方法是在将数据发送到 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);
}
推荐阅读
- python - Python日志记录到日志文件的头部?
- oracle - Oracle 将按什么顺序使用同义词、视图和表对象?
- reactjs - 显示个人资料图片
- python - 当我按下一个按钮时,两个按钮被按下,pygame
- c# - .NET Core 2.2 将 cshtml 呈现为字符串
- java - Java x*0.01 和 x/100.0 不一样,为什么?
- javascript - 使用键盘存储以前的剑道下拉列表选项
- elasticsearch - 在 _reindex api 中的字段上使用 geo_point 数据类型
- ruby - 为什么 Ruby base64 编码的字符串与所有其他 base64 编码的字符串不同?
- mongodb-atlas - mongodb atlas alert 的调试流程