突发奇想,用java演奏钢琴曲!!

140 阅读3分钟

image.png

【背景】

突然想到用java代码编写一个钢琴曲演奏器。虽然没什么用但是还是比较好玩的。当然除了这个演奏钢琴曲外,还可以根据这个基础,利用java swing,生成一个钢琴模拟器,使用键盘就可以模拟弹奏钢琴,当然这个是下一期的内容。

下面先来欣赏一下java弹奏的钢琴曲《起风了》 由于这里目前不支持上传视频,如果想要查看最后实现效果,可以关注公众号【优码道】,查看历史推文。或者直接回复关键字【piano】获取可运行完整代码。

image.png

设计思路

主要是用到了Play类相关的功能,如下。

1、然后把主奏与伴奏,分为如下

“1--” ~ “7--”, “1-” ~ “7-”, “1” ~ “7”, “1+” ~ “7+”, “1++” ~ “7++” 分别代表倍低音、低音、中音、高音、倍高音一共35个音符

示例:小星星的音符如下

1, 1, 5, 5, 6, 6, 5, 0,4, 4, 3, 3, 2, 2, 1, 0,5, 5, 4, 4, 3, 3, 2, 0,5, 5, 4, 4, 3, 3, 2, 0,1, 1, 5, 5, 6, 6, 5, 0,4, 4, 3, 3, 2, 2, 1, 0,

2、把完整的曲子转化成上述的35个音符,用逗号间隔放入resource下的文件中,输入“0”表示延长音。

3、代码读取音符文件,解析后,通过匹配上预先设置好的音频,通过Play类播放出来,即可达到演奏钢琴曲的目的。

实现代码** **** **** **** **

先在pom中增加关健依赖

<dependency>
    <groupId>com.googlecode.soundlibs</groupId>
    <artifactId>mp3spi</artifactId>
    <version>1.9.5.4</version>
</dependency>

<!-- 如果需要解码播放flac文件则引入这个jar包 -->
<dependency>
    <groupId>org.jflac</groupId>
    <artifactId>jflac-codec</artifactId>
    <version>1.5.2</version>
</dependency>

关键的播放代码为AudioPlay中的run方法,如下:

public void run()
{
    try
    {
        int times = this.times;
        new Audio("audio/test.mp3").start();
        sleep(1000);
        for (int i = 0; i < notes.length; i++)
        {
            if (notes[i].length()<1){
                continue;
            }
            switch (notes[i])
            {
            case "1--":
                new Audio("audio/ll1.mp3").start();
                sleep(times / 2);
                break;
            case "2--":
                new Audio("audio/ll2.mp3").start();
                sleep(times / 2);
                break;
            case "3--":
                new Audio("audio/ll3.mp3").start();
                sleep(times / 2);
                break;
            case "4--":
                new Audio("audio/ll4.mp3").start();
                sleep(times / 2);
                break;
            case "5--":
                new Audio("audio/ll5.mp3").start();
                sleep(times / 2);
                break;
            case "6--":
                new Audio("audio/ll6.mp3").start();
                sleep(times / 2);
                break;
            case "7--":
                new Audio("audio/ll7.mp3").start();
                sleep(times / 2);
                break;
            case "1-":
                new Audio("audio/l1.mp3").start();
                sleep(times / 2);
                break;
            case "2-":
                new Audio("audio/l2.mp3").start();
                sleep(times / 2);
                break;
            case "3-":
                new Audio("audio/l3.mp3").start();
                sleep(times / 2);
                break;
            case "4-":
                new Audio("audio/l4.mp3").start();
                sleep(times / 2);
                break;
            case "5-":
                new Audio("audio/l5.mp3").start();
                sleep(times / 2);
                break;
            case "6-":
                new Audio("audio/l6.mp3").start();
                sleep(times / 2);
                break;
            case "7-":
                new Audio("audio/l7.mp3").start();
                sleep(times / 2);
                break;
            case "1":
                new Audio("audio/m1.mp3").start();
                sleep(times / 2);
                break;
            case "2":
                new Audio("audio/m2.mp3").start();
                sleep(times / 2);
                break;
            case "3":
                new Audio("audio/m3.mp3").start();
                sleep(times / 2);
                break;
            case "4":
                new Audio("audio/m4.mp3").start();
                sleep(times / 2);
                break;
            case "5":
                new Audio("audio/m5.mp3").start();
                sleep(times / 2);
                break;
            case "6":
                new Audio("audio/m6.mp3").start();
                sleep(times / 2);
                break;
            case "7":
                new Audio("audio/m7.mp3").start();
                sleep(times / 2);
                break;
            case "1+":
                new Audio("audio/h1.mp3").start();
                sleep(times / 2);
                break;
            case "2+":
                new Audio("audio/h2.mp3").start();
                sleep(times / 2);
                break;
            case "3+":
                new Audio("audio/h3.mp3").start();
                sleep(times / 2);
                break;
            case "4+":
                new Audio("audio/h4.mp3").start();
                sleep(times / 2);
                break;
            case "5+":
                new Audio("audio/h5.mp3").start();
                sleep(times / 2);
                break;
            case "6+":
                new Audio("audio/h6.mp3").start();
                sleep(times / 2);
                break;
            case "7+":
                new Audio("audio/h7.mp3").start();
                sleep(times / 2);
                break;
            case "1++":
                new Audio("audio/hh1.mp3").start();
                sleep(times / 2);
                break;
            case "2++":
                new Audio("audio/hh2.mp3").start();
                sleep(times / 2);
                break;
            case "3++":
                new Audio("audio/hh3.mp3").start();
                sleep(times / 2);
                break;
            case "4++":
                new Audio("audio/hh4.mp3").start();
                sleep(times / 2);
                break;
            case "5++":
                new Audio("audio/hh5.mp3").start();
                sleep(times / 2);
                break;
            case "6++":
                new Audio("audio/hh6.mp3").start();
                sleep(times / 2);
                break;
            case "7++":
                new Audio("audio/hh7.mp3").start();
                sleep(times / 2);
                break;
            case "0":
                sleep(times / 2);
                break;
            default:
                continue;
            }
            sleep(times / 2);
            times = this.times;
        }

    }
    catch (Exception e)
    {
        throw new RuntimeException(e);
    }

}

测试

我们先写一个小demo。点击运行即可开始演奏音乐

public class Test1 {
    public static void main(String[] args) {
        String path = new File("").getAbsolutePath() + File.separator + "src/main/resources/notes" + File.separator;

        String accompanimentPath = path + "起风了_180.accomp";
        String notePath = path + "起风了_180.notes";

        new AudioPlay(notePath,240).start();
        new AudioPlay(accompanimentPath,240).start();
        new Animation(notePath,240).start();
    }
}

需要完整代码的可以关注公众号【优码道】,后台回复【piano】获取

qrcode_for_gh_25ff3865cf68_258.jpg