-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniMusicCmdLine.java
More file actions
54 lines (41 loc) · 1.26 KB
/
MiniMusicCmdLine.java
File metadata and controls
54 lines (41 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package BeatBox;
import javax.sound.midi.*;
public class MiniMusicCmdLine {
public static void main(String[] args) {
args = new String[] {"30" , "80"};
MiniMusicCmdLine mini = new MiniMusicCmdLine();
if(args.length < 2){
System.out.println("Don't forget the instrument and note args");
}
else{
int instrument = Integer.parseInt(args[0]);
int note = Integer.parseInt(args[1]);
mini.play(instrument, note);
}
}
public void play(int instrument, int note) {
try{
Sequencer player = MidiSystem.getSequencer();
player.open();
Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();
ShortMessage first = new ShortMessage();
first.setMessage(192,1,instrument,0);
MidiEvent changeInstrument = new MidiEvent(first, 1);
track.add(changeInstrument);
ShortMessage a = new ShortMessage();
a.setMessage(144,1,note,100);
MidiEvent noteon = new MidiEvent(a, 1);
track.add(noteon);
ShortMessage b = new ShortMessage();
b.setMessage(128,1,note,100);
MidiEvent noteoff = new MidiEvent(b, 16);
track.add(noteoff);
player.setSequence(seq);
player.start();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}