简体   繁体   English

寻找一种控制音量和声像的方法

[英]Looking for a way to control sound volume and panning

I've been creating a Java project where I'd like to have multiple sounds playing at once, and how to control each sound's volume and panning. 我一直在创建一个Java项目,希望同时播放多个声音,以及如何控制每个声音的音量和声像。 I've Googled around and read about 3 different ways; 我已经在Google周围搜索并了解了3种不同的方式; the thing is, I can't seem to find any tutorials or examples of how to use them. 问题是,我似乎找不到任何使用它们的教程或示例。 They are: 他们是:

 import java.applet.AudioClip;
 import javax.sound.sampled;
 import sun.audio.SomethingOrOther; // Sorry, don't recall off-hand.  It was an "un-documented" site

As I continued to read, I found out that AudioClip was only for a certain kind of applciation ("applet"?) and the third one was only mentioned on one of a hundred sites. 当我继续阅读时,我发现AudioClip仅用于某种​​应用程序(“ applet”?),而第三个仅在一百个站点中被提及。 The second one was brought up on this site, but it dealt with a different topic altogether (something about "lines"). 第二个是在此站点上提出的,但它总共处理了另一个主题(有关“行”的问题)。 Anyway, I'm familiar enough with Java syntax to get the idea, but I haven't been able to find anything presented in a straight-forward way; 无论如何,我对Java语法已经足够熟悉了,但是我一直无法找到简单明了的方法。 so if anyone could point me in the right direction on this it would be greatly appreciated. 因此,如果有人能指出我正确的方向,将不胜感激。

Take a look at this trail in the Java tutorials: Trail: Sound , specifically Processing Audio with Controls 在Java教程中了解以下内容: Trail:Sound ,特别是使用控件处理音频

As suggested in Andrews comment you should look at FloatControl.Type (PAN for example). 如安德鲁斯评论中所建议,您应该查看FloatControl.Type (例如PAN)。

It seems to me the only real option for stereo mixing is to access the sound data at the frame (ie, per sample) level. 在我看来,立体声混音的唯一真正选择是在帧(即每个样本)级别访问声音数据。 The built-in controls for pan and volume and such are kind of dubious, and having multiple SourceDataLines all executing concurrently won't even work on some OS's (some Linux). 用于pan和volume等的内置控件有点可疑,并且同时执行多个SourceDataLines甚至无法在某些OS(某些Linux)上运行。

To do so, there are two options: write your own or use a library. 为此,有两种选择:自己编写或使用库。

The Java Tutorials "Trail:Sound" lightly mentions the direct manipulation option at the end of the tutorial on "Controls". Java教程“ Trail:Sound”在“控件”教程的结尾处轻轻提及了直接操纵选项。 The explanations they give ARE helpful for figuring out how to access the individual sound frames. 他们给出的解释有助于弄清楚如何访问各个声音帧。 It is a big job learning this stuff if you are coming into it for the first time (and it sounds like you are, if you are using AudioClips). 如果您是初次接触这些东西,这是一项艰巨的工作(如果您正在使用AudioClips,听起来就像您一样)。

I suspect the best option for you will be to use a library such as TinySound . 我怀疑最好的选择是使用TinySound这样的库。 You can read more about it at Java-Gaming.org Another library to investigate is JAudioLibs . 您可以在Java-Gaming.org上了解有关它的更多信息。要研究的另一个库是JAudioLibs

I'm writing my own stereo mixer for game coding, and have implemented a rough-and-ready ability to control the volume and panning. 我正在编写自己的立体声混音器来进行游戏编码,并且已经实现了控制音量和声像的大致能力。 In doing so, I discovered that Java does not give any real-time guarantees other than that it will try to keep the output sound continuous. 这样做时,我发现Java除了会尝试保持输出声音连续之外,不提供任何实时保证。 Things like garbage collection, thread-slicing, playing from byte-code vs memory, all these things work against real-time consistency. 诸如垃圾收集,线程切片,从字节码还是内存播放之类的事情,所有这些事情都违反了实时一致性。 See this article for more gory details: REAL-TIME, LOW LATENCY AUDIO PROCESSING IN JAVA . 有关更多详细信息,请参见本文: JAVA中的实时,低延迟音频处理

On the plus side, Java's core implementation (eg, things like AudioInputStream and SourceDataLine) are very powerful, and it is possible to do a LOT of real-time processing. 从好的方面来说,Java的核心实现(例如AudioInputStream和SourceDataLine之类的东西)非常强大,并且可以进行很多实时处理。 But it takes some careful coding. 但是需要一些仔细的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM