移植 C++ 时我应该在 Android 中使用什么?用 libsndfile 编写的代码?

发布于 2024-09-29 19:50:22 字数 1618 浏览 4 评论 0原文

我正在将一个小型(<10 个类)C++ 项目移植到 Java。该项目操作声音文件,在 C++ 中使用 libsndfile 来执行此操作。该代码包括以下内容:

const int channels = audioFileInfo.channels;
...
sf_readf_double( audioFile, inputBuffer, MAX_ECHO );
...
sf_writef_double( outputAudioFile, &currentAudioBuffer[WINDOW_SIZE * channels], SEGMENTATION_LENGTH );

在 Java 中,在低级别上操作声音文件的最佳方法是什么?我正在谈论标准化、添加回声等内容。

进度报告

经过一番挖掘,我发现了javax.sound.sampled,看起来它可以完成这项工作。

编辑 2 经过仔细检查,它不会工作(或者至少不会以任何可用的方式),因为它依赖于 com.sun.sound 包。

编辑 3 经过更多检查和实验,com.sun.soundsun.misc 软件包在 GNU GPLv2 下发布,并且我已将它们下载到我的项目中。将 javax.sound.sampled 重命名为 imp.javax.sound.sampled 后,项目将进行编译,并且我可以毫无例外地创建 AudioFileFormat 对象被扔了,还没有。我还没有机会玩太多,但我会及时通知你。

编辑 4 好的,有些东西似乎适用于 javax.sound.sampled,而另一些则不然。例如,诸如以下的调用

AudioInputStream stream = AudioSystem.getAudioInputStream(waveFile));

不起作用,但是我可以通过执行以下操作来解决此问题:

WaveFileReader wfr = new WaveFileReader();
AudioInputStream stream = wfr.getAudioInputStream(waveFile);

一般来说,对诸如 AudioSystem.getAudioFileTypes() 之类的调用会返回空列表。我可以深入研究这些软件包,发现这与提供商有关,但我不知道如何解决这个问题。获得我的 stream 对象后,它确实正确地报告了其编码等,这是令人鼓舞的。

我目前的大问题是创建一个 Clip 对象。这需要使用 Line 对象创建,该对象通常来自 AudioSystem。有人能想办法解决这个问题吗?

I'm porting a small (<10 classes) C++ project to Java. The project manipulates sound files, and in C++ does this using libsndfile. The code includes stuff like:

const int channels = audioFileInfo.channels;
...
sf_readf_double( audioFile, inputBuffer, MAX_ECHO );
...
sf_writef_double( outputAudioFile, ¤tAudioBuffer[WINDOW_SIZE * channels], SEGMENTATION_LENGTH );

In Java, what's the best way to manipulate sound files on a low level? I'm talking about stuff like normalizing, adding echoes etc.

Progress Report

After a bit of digging I've found javax.sound.sampled, which looks like it might do the job.

Edit 2 On closer inspection, it won't work (or at least not in any usable way), since it relies on the com.sun.sound package.

Edit 3 On even more inspection, and experimentation, the com.sun.sound and sun.misc packages are released under the GNU GPLv2, and I've downloaded them into my project. Having renamed javax.sound.sampled to imp.javax.sound.sampled, the project compiles, and I can create AudioFileFormat objects without any exceptions being thrown, yet. I haven't had a chance to play around much yet but I'll keep you updated.

Edit 4 Ok, Some things appear to work with javax.sound.sampled, others do not. For example, calls such as:

AudioInputStream stream = AudioSystem.getAudioInputStream(waveFile));

do not work, however I can get around this by doing:

WaveFileReader wfr = new WaveFileReader();
AudioInputStream stream = wfr.getAudioInputStream(waveFile);

In general, calls to things like AudioSystem.getAudioFileTypes() return empty lists. I can delve into the packages and see it's something to do with providers, but I'm at a loss how to remedy this. Having got my stream object it does report its encoding etc. correctly, which is encouraging.

My big problem at the moment is creating a Clip object. This needs to be created with a Line object, which would normally come from AudioSystem. Can anyone think of a way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆流 2024-10-06 19:50:22

可以使用本机开发工具包为 Android 编译 libsndfile。一旦为 Android 编译了库,您应该能够使用 JNI 从 Java 访问它。

libsndfile can be compiled for Android using the Native Development Kit. Once you have the library compiled for Android, you should be able to use JNI to access it from Java.

栖迟 2024-10-06 19:50:22

为什么不将这段代码保留在 C++ 中并通过 JNI ?

Why don't you just keep this code in C++ and invoke it in your Java through JNI ?

鹊巢 2024-10-06 19:50:22

如果您可以使用 NDK 移植 libsndfile,为什么不直接使用 NDK 来移植您的 C++ 代码而不用担心将其移植到 java 呢?

If you can port libsndfile using the NDK, why not just use the NDK to port your C++ code directly and not worry about porting it to java?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文