源数据线格式支持的问题



我有一个用Java编写的应用程序,我需要在其中播放音频。我使用OpenAL(带有java-openal库(来完成任务,但是我想使用OpenAL不直接支持的WSOLA。我找到了一个名为TarsosDSP的不错的Java原生库,它支持WSOLA。

该库使用标准 Java API 进行音频输出。在源数据线安装过程中会出现此问题:

IllegalArgumentException: No line matching interface SourceDataLine supporting format PCM_UNSIGNED 16000.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian is supported.

我确保问题不是由缺少权限引起的(在 Linux 上以 root 身份运行它 + 在 Windows 10 上尝试过(,并且项目中没有使用其他 SourceDataLines。

在修改格式后,我发现当格式从PCM_UNSIGNED更改为PCM_SIGNED时,该格式是可以接受的。这似乎是一个小问题,因为只有将无符号的字节范围形式移动到有符号应该很容易。然而,奇怪的是它本身不受支持。

那么,是否有一些解决方案可以让我不必修改源数据?

谢谢,一月

您不必手动移动字节范围。创建音频输入流后,创建另一个具有签名格式并连接到第一个未签名流的音频输入流。如果随后使用签名流读取数据,声音 API 会自动转换格式。这样,您无需修改源数据。

File fileWithUnsignedFormat;
AudioInputStream sourceInputStream;
AudioInputStream targetInputStream;
AudioFormat sourceFormat;
AudioFormat targetFormat;
SourceDataLine sourceDataLine;
sourceInputStream = AudioSystem.getAudioInputStream(fileWithUnsignedFormat);
sourceFormat = sourceInputStream.getFormat();
targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 
sourceFormat.getSampleRate(), 
sourceFormat.getSampleSizeInBits(), 
sourceFormat.getChannels(), 
sourceFormat.getFrameSize(), 
sourceFormat.getFrameRate(), 
false);
targetInputStream = AudioSystem.getAudioInputStream(targetFormat, sourceInputStream);
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, targetFormat);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(targetFormat);
sourceLine.start();

// schematic
targetInputStream.read(byteArray, 0, byteArray.length);
sourceDataLine.write(byteArray, 0, byteArray.length);

相关内容

最新更新