简体   繁体   English

JAVA:控制音频输入流的音量

[英]JAVA: Controlling volume of an audioinputstream

I use in a java code a wav file that I load into an AudioInputStream using AudioInputStream ais = AudioSystem.getAudioInputStream("file.wav") 我在Java代码中使用了wav文件,该文件使用AudioInputStream ais = AudioSystem.getAudioInputStream(“ file.wav”)加载到AudioInputStream中

Once I have done that, I wish to basically pick up the n amount of seconds at the end (let's say the 5 last seconds) and "fade out" the volume (FloatControl.Type.MASTER_GAIN??). 完成此操作后,我希望基本上在末尾获得n秒钟的时间(假设最后5秒钟)并“淡出”音量(FloatControl.Type.MASTER_GAIN ??)。

Once done I could transfert my audiostreaminput back into a wav file using: AudioSystem.write(ais, Type.WAVE, file_output); 一旦完成,我可以使用以下方法将音频流输入传输回wav文件:AudioSystem.write(ais,Type.WAVE,file_output);

the result is a same wav file but with the last 5 seconds fading out (volume decreasing). 结果是相同的wav文件,但最后5秒钟逐渐消失(音量减小)。

Any idea on how to do this? 关于如何执行此操作的任何想法? I tried changing the ais into bytes[], or a sourcedataline... but didn't find what I wanted, as most examples are about changing volume of an audio "in-play" (I also saw things around using Clip which also seems to be dealing an audio file in-play) 我尝试将ais更改为bytes []或sourcedataline ...,但没有找到我想要的,因为大多数示例都是关于更改音频“播放中”的音量的(我也看到了Clip的使用,似乎正在播放音频文件)

Many thanks everyone 非常感谢大家

Start by turning the sound into a byte array. 首先将声音转换为字节数组。 Then turn the bytes into samples: you'll need to find a tutorial specifically for this, it's a little involved in Java ( http://www.jsresources.org/ is a good resource). 然后将字节转换为样本:您将需要找到专门用于此的教程,它与Java有点关系( http://www.jsresources.org/是很好的资源)。 Samples are the direct representation of the sound wave. 样本是声波的直接表示。

To decrease the volume, multiply all the samples by something less than 1, and then save them back to a byte array. 要减小音量,请将所有样本乘以小于1的值,然后将其保存回字节数组。 To fade out you'll need to multiply the last n samples by a decreasing function. 要淡出,您需要将最后n个样本乘以一个递减函数。 Then write out the file with the proper WAV headers. 然后用正确的WAV标头写出文件。

These are just a few pointers for a complex process, hopefully they will send you in the right direction. 这些只是一个复杂过程的一些指示,希望它们会向正确的方向发送信息。

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

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