java 声音,fadeIn 效果,使用 FloatControl
我正在尝试为我的 MP3 播放器实现淡入淡出效果。 我正在使用 FloatControl volumeControl = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN); 因为 FloatControl.Type.VOLUME 抛出异常(控制不可用)我不知道为什么。 我需要一些关于算法的帮助,因为它不能正常工作。 这是代码:
public class FloatControlFader
{
public static void fadeIn(final FloatControl control, final float from,
final float to, final int seconds)
{
final float vps = ((to-from) / (seconds*10));//Volume incrased/100millisecond
control.setValue(from);
Thread t = new Thread(new Runnable(){
public void run() {
for(int i=0; i < seconds*10; i++)
{
try
{
Thread.sleep(100);
}
catch (InterruptedException ex)
{
}
System.out.println(control.getValue()); //for DEBUG
control.setValue(control.getValue() + vps);
}
}
});
t.start();
}
}
希望得到任何帮助,谢谢!
Im trying to implement fade in effect to my mp3 player.
I´m using
FloatControl volumeControl = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
because FloatControl.Type.VOLUME throws an exception(Unavaliable Control) I dont know why.
I need some help with the algorithm, because its not working ok.
Heres the code:
public class FloatControlFader
{
public static void fadeIn(final FloatControl control, final float from,
final float to, final int seconds)
{
final float vps = ((to-from) / (seconds*10));//Volume incrased/100millisecond
control.setValue(from);
Thread t = new Thread(new Runnable(){
public void run() {
for(int i=0; i < seconds*10; i++)
{
try
{
Thread.sleep(100);
}
catch (InterruptedException ex)
{
}
System.out.println(control.getValue()); //for DEBUG
control.setValue(control.getValue() + vps);
}
}
});
t.start();
}
}
Would appreciate any help, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住,人耳的听觉不是线性的,因此以稳定的 X vps 增加听起来不会像平滑的淡入淡出。您需要在那里放置一个日志功能。然后您需要将线性增加映射到对数值。当然,这一切都是假设音量控制单位不是分贝。如果你增加 db 那就没问题了。
Remember the human ear does not hear linearly so increasing by a steady X vps is not going to sound like a smooth fade. You need to put a log function in there. Then you need to map the linear increases to log values. This is of course all assuming the volume control units are not in decibels. If you're increasing by db then you're fine.