码迷,mamicode.com
首页 > 编程语言 > 详细

java学习笔记_MIDI

时间:2016-09-10 23:48:38      阅读:503      评论:0      收藏:0      [点我收藏+]

标签:

 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

标签:

原文地址:http://www.cnblogs.com/ren-yu/p/5860591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!