AI作曲软件Java版,代码免费下载

前言

友情提示:本文技术含量不高,大佬请直接忽略本文!

由于本人热爱音乐,当前又是人工智能的潮流,因此本人设想,如果人工智能可以作曲就好了。无奈水平有限,虽然大概看过一些人工智能的书籍,然而还是感觉似懂非懂。以下是本人编写的最低级的AI作曲代码,基本毫无技术含量,大佬一笑而过即可。(如何训练AI写出好听的音乐,我也很想知道,找些好听的谱,让AI参考后自由发挥?各抄一段拼起来肯定不行;本人对作曲一窍不通,如果有专业的作曲家会编程,也许能想到怎么办吧)

代码

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class radomMusic {

    static ArrayList list = new ArrayList();
    static char cl[] = {'1','2','3','4','5','6','7','#',' ','(',')','[',']'};

    static int count = 1000;

    static boolean needNum = false;
    static boolean needJingAndNum = false;


    static boolean canXiaoKuoHao = true;
    static boolean canZhongKuoHao = true;

    static boolean canZuoXiaoKuoHao = true;
    static boolean canZuoZhongKuoHao = true;

    static boolean canYouXiaoKuoHao = false;
    static boolean canYouZhongKuoHao = false;


    public static void main(String argv[]){
        System.out.println("启动");
        try{
            count = Integer.parseInt(argv[0]);
        }
        catch (Exception e){
            count = 1000;
        }

        initList();
        FileWriter fw = getFileWriter();
        for(int i=0; i< count; i++) {
            radomWrite(fw);
        }
        closeFR(fw);
        System.out.println("");
        System.out.println("结束");
    }

    public static void  initList(){
        for(char c: cl){
            list.add(c);
        }
    }
    public static void  radomWrite(FileWriter fw){
        int length = list.size();
        int target = (int)(Math.random()*length);
        try {
            String str = writeRule(String.valueOf(list.get(target)));
            System.out.print(str);

            writeToFile(fw, str);

        }catch (Exception e){
            System.out.println("出现错误,可能下标越界!");
        }

    }

    public static String reRadom(int begin, int end){
        String str = null;
        int i = (int) (Math.random()*(end-begin) + begin);
        try{
            str = String.valueOf(list.get(i));
        }catch (Exception e){
            System.out.println("重随机错误,可能越界!");
        }
        return str;
    }

    /**
     * 要求:
     * #后一位只能加数字;
     * (后不能出现[和]和(
     * [后不能出现(和)和[
     * )后不能出现)和]
     * ]后不能出现]和)
     *
     * 必须先出现一次(才能出现)
     * 必须先出现一次[才能出现]
     *
     * (后需要#或数字
     * [后需要#或数字
     *
     * //list中,0-6为1-7
     * 7为#
     * 8为空格
     * 9为(
     * 10为)
     * 11为[
     * 12为]
     *
     * @param input
     * @return
     */
    public static String writeRule(String input){
        String output = input;

        if(needNum){
            needNum = false;
            return reRadom(0,6);
        }
        if(needJingAndNum){
            needJingAndNum = false;
            return reRadom(0,7);
        }

        if("#".equals(input)){
            needNum = true;
        }else if("(".equals(input)){
            if(!canZuoXiaoKuoHao){
                return " ";
            }
            if(!canXiaoKuoHao){
                return " ";
            }
            canZhongKuoHao = false;
            canYouXiaoKuoHao = true;
            needJingAndNum = true;
            canZuoXiaoKuoHao = false;

        }else if(")".equals(input)){
            if(!canXiaoKuoHao){
                return " ";
            }
            if(!canYouXiaoKuoHao){
                return " ";
            }
            canZhongKuoHao = true;
            canYouXiaoKuoHao = false;
            canZuoXiaoKuoHao = true;
            canZuoZhongKuoHao = true;

        }else if("[".equals(input)){
            if(!canZuoZhongKuoHao){
                return " ";
            }
            if(!canZhongKuoHao){
                return " ";
            }
            canXiaoKuoHao = false;
            canYouZhongKuoHao = true;
            needJingAndNum = true;
            canZuoZhongKuoHao = false;
        }else if("]".equals(input)){
            if(!canZhongKuoHao){
                return " ";
            }
            if(!canYouZhongKuoHao){
                return " ";
            }
            canXiaoKuoHao = true;
            canYouZhongKuoHao = false;
            canZuoXiaoKuoHao = true;
            canZuoZhongKuoHao = true;

        }else if(" ".equals(input)){

        }
        else{

        }

        return output;
    }

    public static FileWriter getFileWriter(){

        //URL path = radomMusic.class.getClassLoader().getResource("");
        String url = new File("").getAbsolutePath();
        url = url + "/tm.txt";
        System.out.println(url);
        File f = new File(url);

        if(!f.exists()){
            try {
                f.createNewFile();
            } catch (IOException e) {
                System.out.println(e.toString());

            }
        }

        try {
            FileWriter fw = new FileWriter(url);
            return fw;
        } catch (IOException e) {
            System.out.println(e.toString());

        }
        return null;
    }

    public static void writeToFile(FileWriter fw, String str){
        try {
            fw.write(str);
        } catch (IOException e) {
            System.out.println(e.toString());

        }
    }

    public static void closeFR(FileWriter fw){
        try {
            fw.flush();
            fw.close();
        } catch (IOException e) {
            System.out.println(e.toString());

        }
    }
}

输出样例

2 #1 4 (4 #676 2) 57#17#2 675[21](166   527 #2)465[2 17 114275164]2(16)12 (31652146 362#43#42 5 541 463 2557  )4(5 77 #6 13 16 63  2) 67(3 24327 327#24426 57  4  1631 3)#4[4#63  #324 22 7 3 5 5327 3  565256  47]166 61  3   

总结

其实本文主要是用来存代码的;这段代码打成jar包运行,可以在当前目录下的tm.txt中生成AI随机完成的简谱;结合本人之前的TXT音乐播放器,还是能听着玩的(不知道能不能听出啥作曲灵感来)。没啥技术含量,就不讲解了。还请大佬勿喷。

原文地址:https://www.cnblogs.com/codeToSuccess/p/13906245.html