3 Star 10 Fork 5

冯飞飞 / lumusic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

可编程式音乐描述集合(computer programming of music description toolkit,mdt)

目录
1、musicxml数字乐谱描述
2、musiccode乐谱可编程性
3、player乐谱播放器
4、musicscore乐谱绘图
5、GUI用户界面
6、MIDI和其它
图片替换文本

一、musicxml数字乐谱描述套件

在musicxml中,ScorePartwise是数字乐谱的顶层数据结构,其语法必须符合W3C规范:

package musicxml.example;

import musicxml.ScorePartwise;
import musicxml.part.Part;
import musicxml.part.measure.Measure;
import musicxml.part.measure.attributes.Attributes;
import musicxml.part.measure.attributes.Division;
import musicxml.part.measure.attributes.clef.Clef;
import musicxml.part.measure.attributes.key.Key;
import musicxml.part.measure.attributes.time.Time;
import musicxml.part.measure.note.Duration;
import musicxml.part.measure.note.Note;
import musicxml.part.measure.note.Staff;
import musicxml.part.measure.note.Type;
import musicxml.part.measure.note.pitch.Alter;
import musicxml.part.measure.note.pitch.Octave;
import musicxml.part.measure.note.pitch.Pitch;
import musicxml.part.measure.note.pitch.Step;

public class Music_1 {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {

        //创建乐谱
        ScorePartwise scorePartwise = new ScorePartwise("music1");

        //设置分谱和乐器名字
        scorePartwise.addScorePart("p1", "piano");

        //分谱
        Part part = scorePartwise.getPart("p1");

        //小节
        Measure measure = new Measure(1);
        Attributes attributes = new Attributes();
        //division要最先设置
        attributes.addDivision(Division.D256()).addKey(Key.MajorInC()).addTime(Time.Beat44());
        attributes.addStaves(2).addClef(Clef.G(1)).addClef(Clef.F(2));
        measure.addAttributes(attributes);
        part.addMeasure(measure);

        //音符
        Note note = new Note();
        note.addPitch(new Step("C"), new Alter(0), new Octave(4));
        note.addDuration(1024);
        note.addType(Type.TYPE_whole);
        note.addStaff(1);
        measure.addNote(note);

        //转换到xml格式
        String s = scorePartwise.toXML();
        scorePartwise.toFile("music1.xml");
        System.out.println(s);

    }
}

生成的音乐描述文件musicxml格式文件如下:

<score-partwise version='3'>                                                              <!-- 乐谱 -->
    <work>                                                                                <!-- 工作 -->
        <work-title>music1</work-title>                                                   <!-- 谱名 -->
    </work>
    <part-list>                                                                           <!-- 谱表 -->
        <score-part id='p1'>                                                              <!-- 声部 -->
            <part-name>piano</part-name>                                                  <!-- 名字 -->
        </score-part>
    </part-list>
    <part id='p1'>                                                                        <!-- 分谱 -->
        <measure number='1'>                                                              <!-- 小节 -->
            <attributes>                                                                  <!-- 属性 -->
                <divisions>256</divisions>                                                <!-- 时长 -->
                <key>                                                                     <!-- 调式 -->
                    <fifths>0</fifths>                                                    <!-- 五度 -->
                    <mode>major</mode>                                                    <!-- 类型 -->
                </key>
                <time>                                                                    <!-- 时间 -->
                    <beats>4</beats>                                                      <!-- 节奏 -->
                    <beat-type>4</beat-type>                                              <!-- 类型 -->
                </time>
                <staves>2</staves>                                                        <!-- 个数 -->
                <clef number='1'>                                                         <!-- 结点 -->
                    <sign>G</sign>                                                        <!-- 结点 -->
                    <line>2</line>                                                        <!-- 结点 -->
                </clef>
                <clef number='2'>                                                         <!-- 结点 -->
                    <sign>C</sign>                                                        <!-- 结点 -->
                    <line>4</line>                                                        <!-- 结点 -->
                </clef>
            </attributes>
            <note>                                                                        <!-- 结点 -->
                <pitch>                                                                   <!-- 结点 -->
                    <step>C</step>                                                        <!-- 音名 -->
                    <alter>0</alter>                                                      <!-- 结点 -->
                    <octave>4</octave>                                                    <!-- 结点 -->
                </pitch>
                <duration>1024</duration>                                                 <!-- 结点 -->
                <type>whole</type>                                                        <!-- 结点 -->
                <staff>1</staff>                                                          <!-- 结点 -->
            </note>
        </measure>
    </part>
</score-partwise>

用musescore软件渲染结果如下:

输入图片说明

二、midi文件格式生成

midi文件是存储乐器事件的数据结构,生成一个midi格式的文件如下:

package midi.test;

import midi.MidiFile;
import midi.mthd.MThd;
import midi.mtrk.MTrk;
import midi.mtrk.event.midievent.FinalEvent;
import midi.mtrk.event.midievent.KeyPressEvent;

import java.io.FileOutputStream;
import java.io.IOException;

public class MiDiFileTest {

    static MThd testMthd() {
        //midi文件头部
        MThd mThd = new MThd();

        //同步多音轨
        mThd.midiType = 1;

        //轨道数目
        mThd.mtrkNum = 1;

        //四分音符持续时间
        mThd.tickTime = 120;

        //内部字节
        byte[] bytes = mThd.getByte();

        //打印
        for (int i = 0; i <= bytes.length - 1; i = i + 1) {
            System.out.print(Integer.toHexString(bytes[i]) + " ");
        }

        return mThd;
    }

    static MTrk testMTrk() {

        //新建一条音轨
        MTrk mTrk = new MTrk();

        //添加事件
        mTrk.addEvent(new KeyPressEvent(240, 1, 66, 100));
        mTrk.addEvent(new KeyPressEvent(240, 1, 66, 100));
        mTrk.addEvent(new KeyPressEvent(240, 1, 66, 100));
        mTrk.addEvent(new KeyPressEvent(240, 1, 66, 100));
        mTrk.addEvent(new FinalEvent(240));

        //转换到字节
        byte[] bytes = mTrk.getByte();

        //打印
        for (int i = 0; i <= bytes.length - 1; i = i + 1) {
            System.out.print(Integer.toHexString(bytes[i]) + " ");
        }

        return mTrk;
    }

    static void midiTest() throws IOException {

        //创建MiDI文件
        MidiFile midiFile = new MidiFile();

        //设置头部
        midiFile.setmThd(testMthd());

        //添加音轨
        midiFile.addMTrk(testMTrk());

        //写出字节
        midiFile.write(new FileOutputStream("d:\\test.midi"));
    }

    public static void main(String[] args) throws IOException {

        midiTest();
    }
}

生成效果:

MIT License Copyright (c) 2022 冯飞飞 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

音乐描述格式(musicxml)、电子五线谱绘制及其自动播放系统 、MIDI乐器接口 等音乐编程性基础套件。 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/q568680472/lumusic.git
git@gitee.com:q568680472/lumusic.git
q568680472
lumusic
lumusic
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891