java学习笔记_MIDI

Posted Ren.Yu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java学习笔记_MIDI相关的知识,希望对你有一定的参考价值。

 1 import javax.sound.midi.*;
 2 
 3 public class Midi {
 4     public void play(int instrument, int note) {
 5         try {
 6             Sequencer player = MidiSystem.getSequencer();
 7             player.open();
 8             
 9             Sequence seq = new Sequence(Sequence.PPQ, 4);//divisionType, resolution
10             
11             Track track = seq.createTrack();
12             
13             ShortMessage first = new ShortMessage();
14             first.setMessage(192, 1, instrument, 0);
15             MidiEvent change = new MidiEvent(first, 1);
16             track.add(change);
17             
18             ShortMessage a = new ShortMessage();
19             a.setMessage(144, 1, note, 100);//command channel data1 data2
20                                            //144 -note on 128 -note off
21                                            //1 - 频道
22                                            //44 - 音符 0-127
23                                            //100 - 音道 
24             MidiEvent noteOn = new MidiEvent(a, 1); //tick - the time-stamp for the event, in MIDI ticks
25             track.add(noteOn);
26             
27             ShortMessage b = new ShortMessage();
28             b.setMessage(128, 1, note, 100);//command channel data1 data2
29             MidiEvent noteOff = new MidiEvent(b, 16); //tick - the time-stamp for the event, in MIDI ticks
30             track.add(noteOff);
31             
32             player.setSequence(seq);
33             player.start();
34             //while( player.isRunning() ) {
35             //    try {
36             //        Thread.sleep(1000);
37             //    } catch (Exception e) {}
38             //}
39             //player.close();
40             
41         } catch( Exception ex) {
42             ex.printStackTrace();
43         }
44     }
45     
46     public static void main(String[] args) {
47         if (args.length < 2) {
48             System.out.println("Don‘t forget the instrument and note args");
49         }
50         else {
51             int instrument = Integer.parseInt(args[0]);
52             int note = Integer.parseInt(args[1]);
53             Midi midi = new Midi();
54             midi.play(instrument, note);
55         }        
56     }
57 }

 

以上是关于java学习笔记_MIDI的主要内容,如果未能解决你的问题,请参考以下文章

[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段

笔记本电脑退出休眠后 Java MIDI 音频延迟

如何在 Java 中播放(MIDI)序列中的音频剪辑?

学习笔记:python3,代码片段(2017)

JSP 学习笔记

java学习笔记_内存分析