java 声音,fadeIn 效果,使用 FloatControl

发布于 2024-09-17 09:34:41 字数 1102 浏览 5 评论 0原文

我正在尝试为我的 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 技术交流群。

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

发布评论

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

评论(1

水中月 2024-09-24 09:35:01

请记住,人耳的听觉不是线性的,因此以稳定的 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.

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