- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
- 55
- 56
- 57
- 58
import javax.sound.midi.*;
import java.io.*;
public class MusicTest2 {
public static void main(String[] args) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true) {
System.out.print("Введите номер инструмента: ");
int instrument = Integer.parseInt(reader.readLine());
if(instrument > 127 || instrument < 0){
System.out.println("Error! Недопустимое значение!");
break;
}
System.out.print("Введите номер ноты: ");
int nota = Integer.parseInt(reader.readLine());
if(nota > 127 || nota < 0){
System.out.println("Error! Недопустимое значение!");
break;
}
MusicTest2 testik = new MusicTest2();
testik.play(instrument, nota);
}
}
public void play(int instrument, int nota){
try{
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();
ShortMessage a = new ShortMessage();
a.setMessage(192, 1, instrument, 100);
MidiEvent changeInstrument = new MidiEvent(a, 1);
track.add(changeInstrument);
ShortMessage b = new ShortMessage();
b.setMessage(144, 1, nota, 100);
MidiEvent event = new MidiEvent(b, 1);
track.add(event);
ShortMessage c = new ShortMessage();
c.setMessage(128, 1, nota, 100);
MidiEvent event1 = new MidiEvent(c, 16);
track.add(event1);
sequencer.setSequence(seq);
sequencer.start();
}catch(Exception ex){
ex.printStackTrace();
}
}
}