在android中播放特定频率的声音

发布于 2024-10-07 07:10:39 字数 83 浏览 5 评论 0原文

我想根据用户输入在android中播放特定频率的声音,有点类似于吉他应用程序。谁能告诉我该怎么做?我需要将所有频率声音放在 res\raw 文件夹中吗?

I want to play a sound of a particular frequency in android based on user input, somewhat similar to guitar application. Can anyone tell me how to do that? Do i need to have all freq sounds in the res\raw folder?

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

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

发布评论

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

评论(3

猫卆 2024-10-14 07:10:39

不,您不需要将其存储在资源中。我认为更好的方法是实时合成音调。为此,您需要以下内容:

  • java.lang.Math 用于三角函数计算,以使用正确的音调填充缓冲区(从浮点转换为 PCM (int) 后)
  • android.media.AudioTrack 用于音频播放(从缓冲区)。我想静态模式将是您所需要的 - 更快的响应和更易于操作。然而,音频播放并不是一件简单的任务,因此您需要了解有关 Android 上音频系统的更多信息。当然,了解一些有关信号处理的基本知识也是非常受欢迎的。
  • 您甚至可以使用 android.media.audiofx 添加一些很酷的效果

No, you do not need to store that in the resources. I think better approach would be to synthesize the tones in a real time. For that purpose you would need the following:

  • java.lang.Math for trigonometric functions calculations to fill buffer (after conversion from float to PCM (int)) with correct tone
  • android.media.AudioTrack for audio playing (from buffer). I suppose static mode would be what you need - faster response and easier to operate. However, audio playing is not trivial task at all so you would need to learn more about audio system on Android. Of course, some basic knowledge about signal processing is more than welcome.
  • you may even add some cool effects using android.media.audiofx
始终不够 2024-10-14 07:10:39

将声音发送到 AudioTrack 实例并动态更改其采样率。

Send the sound to an AudioTrack instance and change its sample rate on the fly.

暖阳 2024-10-14 07:10:39

这是一个极其复杂的问题,尽管看起来并非如此。

当您说“声音”时,您只是指音调(例如正弦波、方波、三角波、锯齿波)还是您希望演奏“真实”乐器(例如吉他、钢琴等)的声音?

如果是第一个(简单的音调),那么 Desiderio 的答案可能是正确的,但如果你想要“真实”的乐器声音,那么你需要在 /res/raw 或 /assets 目录中有一个简短的“样本” 。

您可以将它们加载到 SoundPool 实例中,并使用它以不同的“速率”播放,例如,2 的“速率”将是速度的两倍和频率的两倍,从而增加一个八度。 0.5 的“速率”将是速度的一半,因此将声音降低一个八度。

This is an extremely complex question even though it doesn't seem like it.

When you say 'sound' do you simply mean a tone (sine, square, triangle, sawtooth waves for example) or do you wish to play sounds of 'real' instruments such as guitar, piano and so on?

If it's the first one (simple tones) then Desiderio's answer may be on the right track but if you want 'real' insrument sounds then you will need to have a short 'sample' in either /res/raw or in your /assets directory.

You can load them into a SoundPool instance and use it to playback at different 'rates', e.g., a 'rate' of 2 will be twice the speed and double the frequency thus increasing by an octave. A 'rate' of 0.5 will be half the speed so lowering the sound by an octave.

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