合成采样的声音

发布于 2024-10-16 20:38:53 字数 252 浏览 9 评论 0原文

我正在尝试将整数数组转换为声音表示。目前,我将每个值作为单独的短音播放,但我想根据信息创建单个采样波并播放它,如下所示:

image

每个红点代表一个数据点。理想情况下,我希望最终得到一个简单的方法,例如 playSound(int[] array); 。我查看了 javax.sound.sampled 包,但我不知道从哪里开始。

I am trying to convert an array of integers into a sound representation. Currently, I'm playing each value as an individual short tone but I would like to create a single sampled wave from the information and play it back, like so:

image

Each red dot representing a data point. Ideally I'd like to end up with a simple method such as playSound(int[] array);. I've looked through the javax.sound.sampled package but I dont know where to start.

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

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

发布评论

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

评论(1

夏见 2024-10-23 20:38:53

首先,样本数组不足以提供播放声音的信息。

  • 您还需要指定采样率。 (例如,如果以 25 个样本/秒记录,重复 -1、+1 等的 50 个样本可能是 50 Hz 正弦波,如果以 50 个样本/秒记录,则可能是 12.5 Hz 波(我的数学可能不正确))。

  • 需要的另一件事是如何存储声音。 MAX_INT 是您的最高音量还是 255 是您的峰值音量?

  • 您还需要指定编码。您可能需要 PCM_SIGNED 或 PCM_UNSIGNED(取决于您是否有负样本),或者甚至可能需要使用另一种编码。

javax.sound.sampled 包在 AudioFormat 类。

您需要构建一个音频格式来反映音频采样的方法。例如,如果您的样本占据了完整的整数范围,并且您用一个整数表示每个样本,并且您以每秒 22,000 个样本的速度进行采样,并且它是单声道声音,您将构造以下 AudioFormat...

AudioFormat desiredFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,22000,32,1,32,22000,true);

一旦您拥有了 AudioFormat,您就可以打开一行并将音频转储到其中。 此页面演示了如何做到这一点(他们作弊并播放音频文件,以便他们获得 AudioFormat从文件中)。

当然,您可能无法打开所有格式的行,因此您可能需要进行一些转换。综上所述,可能有 Java 声音库可以使这变得容易得多。

To start with, an array of samples is not enough information to play a sound.

  • You also need to specify the sample rate. (e.g. 50 samples repeating -1,+1,etc. could be a 50 hz sine wave if recorded at 25 samples/second or a 12.5 hz wave if recorded at 50 samples/second (my math may be off)).

  • Another thing that is needed is how the sound is stored. Is MAX_INT your highest sound or is 255 your peak volume?

  • You also need to specify an encoding. You probably want either PCM_SIGNED or PCM_UNSIGNED (depending on if you have negative samples) or there may be even another encoding you want to use.

The javax.sound.sampled package represents this information in the AudioFormat class.

You will need to construct an audio format that reflects the method in which your audio was samples. For example, if your samples took up the full integer range and you represented each sample with an integer and you sampled at 22,000 samples per second and it was a mono sound you would construct the following AudioFormat...

AudioFormat desiredFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,22000,32,1,32,22000,true);

Once you have an AudioFormat you can open a line and dump audio to it. This page demonstrates how to do that (they cheat and are playing an audio file so they get the AudioFormat from the file).

Of course, you might not be able to open a line for all formats so you might have to do some conversion. All of this said, there may be Java sound libraries out there which can make this a lot easier.

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