import java.applet.Applet; import java.awt.event.*; import java.awt.*; import java.io.*; import javax.sound.midi.*; import java.util.Date; import java.util.StringTokenizer; public class Klaver1 extends Applet implements KeyListener{ BufferedReader klaviatuur=new BufferedReader( new InputStreamReader(System.in) ); Sequence sequence; Track track; ShortMessage kinnic; ShortMessage lahtic; ShortMessage kinnicis; ShortMessage lahticis; ShortMessage kinnid; ShortMessage lahtid; ShortMessage kinnidis; ShortMessage lahtidis; ShortMessage kinnie; ShortMessage lahtie; ShortMessage kinnif; ShortMessage lahtif; ShortMessage kinnifis; ShortMessage lahtifis; ShortMessage kinnig; ShortMessage lahtig; ShortMessage kinnigis; ShortMessage lahtigis; ShortMessage kinnia; ShortMessage lahtia; ShortMessage kinniais; ShortMessage lahtiais; ShortMessage kinnih; ShortMessage lahtih; ShortMessage kinnic1; ShortMessage lahtic1; MidiChannel kanal; int valjus=93; int C=60; int CIS=61; int D=62; int DIS=63; int E=64; int F=65; int FIS=66; int G=67; int GIS=68; int A=69; int AIS=70; int H=71; int C1=72; long algaeg=new Date().getTime(); public Klaver1(){ try{ sequence=new Sequence(Sequence.PPQ, 4); sequence.createTrack(); track=sequence.createTrack(); ShortMessage lahti = new ShortMessage(); ShortMessage kinni = new ShortMessage(); ShortMessage algus = new ShortMessage(); algus.setMessage(ShortMessage.PROGRAM_CHANGE, 0, 16, 0); track.add(new MidiEvent(algus, 0)); lahtic = new ShortMessage(); kinnic = new ShortMessage(); lahtic.setMessage(ShortMessage.NOTE_ON, 0, C, 93); kinnic.setMessage(ShortMessage.NOTE_OFF, 0, C, 93); lahticis = new ShortMessage(); kinnicis = new ShortMessage(); lahticis.setMessage(ShortMessage.NOTE_ON, 0, CIS, 93); kinnicis.setMessage(ShortMessage.NOTE_OFF, 0, CIS, 93); lahtid = new ShortMessage(); kinnid = new ShortMessage(); lahtid.setMessage(ShortMessage.NOTE_ON,0, D, 93); kinnid.setMessage(ShortMessage.NOTE_OFF, 0, D, 93); lahtidis = new ShortMessage(); kinnidis = new ShortMessage(); lahtidis.setMessage(ShortMessage.NOTE_ON, 0, DIS, 93); kinnidis.setMessage(ShortMessage.NOTE_OFF, 0, DIS, 93); lahtie = new ShortMessage(); kinnie = new ShortMessage(); lahtie.setMessage(ShortMessage.NOTE_ON, 0, E, 93); kinnie.setMessage(ShortMessage.NOTE_OFF, 0, E, 93); lahtif = new ShortMessage(); kinnif = new ShortMessage(); lahtif.setMessage(ShortMessage.NOTE_ON, 0, F, 93); kinnif.setMessage(ShortMessage.NOTE_OFF, 0, F, 93); lahtifis = new ShortMessage(); kinnifis = new ShortMessage(); lahtifis.setMessage(ShortMessage.NOTE_ON, 0, FIS, 93); kinnifis.setMessage(ShortMessage.NOTE_OFF, 0, FIS, 93); lahtig = new ShortMessage(); kinnig = new ShortMessage(); lahtig.setMessage(ShortMessage.NOTE_ON, 0, G, 93); kinnig.setMessage(ShortMessage.NOTE_OFF, 0, G, 93); lahtigis = new ShortMessage(); kinnigis = new ShortMessage(); lahtigis.setMessage(ShortMessage.NOTE_ON, 0, GIS, 93); kinnigis.setMessage(ShortMessage.NOTE_OFF, 0, GIS, 93); lahtia = new ShortMessage(); kinnia = new ShortMessage(); lahtia.setMessage(ShortMessage.NOTE_ON, 0, A, 93); kinnia.setMessage(ShortMessage.NOTE_OFF, 0, A, 93); lahtiais = new ShortMessage(); kinniais = new ShortMessage(); lahtiais.setMessage(ShortMessage.NOTE_ON, 0, AIS, 93); kinniais.setMessage(ShortMessage.NOTE_OFF, 0, AIS, 93); lahtih = new ShortMessage(); kinnih = new ShortMessage(); lahtih.setMessage(ShortMessage.NOTE_ON, 0, H, 93); kinnih.setMessage(ShortMessage.NOTE_OFF, 0, H, 93); lahtic1 = new ShortMessage(); kinnic1 = new ShortMessage(); lahtic1.setMessage(ShortMessage.NOTE_ON, 0, C1, 93); kinnic1.setMessage(ShortMessage.NOTE_OFF, 0, C1, 93); Synthesizer synthesizer=MidiSystem.getSynthesizer(); synthesizer.open(); kanal=synthesizer.getChannels()[0]; //kanal 0; init(); }catch(Exception viga){ viga.printStackTrace(); } } public void init(){ addKeyListener(this); requestFocus(); } public void paint(Graphics g){ Image pilt=Toolkit.getDefaultToolkit().getImage("Klaver.jpg"); g.drawImage(pilt, 0, 50, this); } public void keyPressed(KeyEvent e){ try{ int kood=e.getKeyCode(); long vajutusaeg=new Date().getTime(); System.out.println(vajutusaeg-algaeg); if(kood==KeyEvent.VK_C){ kanal.noteOn(C, valjus); track.add(new MidiEvent(lahtic, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_1){ kanal.noteOn(CIS, valjus); track.add(new MidiEvent(lahticis, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_D){ kanal.noteOn(D, valjus); track.add(new MidiEvent(lahtid, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_2){ kanal.noteOn(DIS, valjus); track.add(new MidiEvent(lahtidis, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_E){ kanal.noteOn(E, valjus); track.add(new MidiEvent(lahtie, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_F){ kanal.noteOn(F, valjus); track.add(new MidiEvent(lahtif, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_3){ kanal.noteOn(FIS, valjus); track.add(new MidiEvent(lahtifis, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_G){ kanal.noteOn(G, valjus); track.add(new MidiEvent(lahtig, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_4){ kanal.noteOn(GIS, valjus); track.add(new MidiEvent(lahtigis, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_A){ kanal.noteOn(A, valjus); track.add(new MidiEvent(lahtia, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_5){ kanal.noteOn(AIS, valjus); track.add(new MidiEvent(lahtiais, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_H){ kanal.noteOn(H, valjus); track.add(new MidiEvent(lahtih, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_6){ kanal.noteOn(C1, valjus); track.add(new MidiEvent(lahtic1, (vajutusaeg-algaeg)/100)); } String sisestus=""; if(kood==KeyEvent.VK_U){ try{ System.out.println("Sisesta muusikafaili nimi, kuhu salvestan:"); sisestus=klaviatuur.readLine(); }catch(Exception viga){ viga.printStackTrace(); } try{ Sequencer sequencer=MidiSystem.getSequencer(); sequencer.open(); sequencer.setSequence(sequence); sequencer.start(); FileOutputStream fos; MidiSystem.write(sequence, 1, fos=new FileOutputStream(sisestus)); fos.close(); }catch(Exception viga){ viga.printStackTrace(); } } }catch(Exception probleem){ probleem.printStackTrace(); } repaint(); } public void keyReleased(KeyEvent e){ try{ int kood=e.getKeyCode(); long vajutusaeg=new Date().getTime(); // ShortMessage lahtic = new ShortMessage(); // ShortMessage kinnic = new ShortMessage(); // lahtic.setMessage(ShortMessage.NOTE_ON, 0, 65, 93); // 0 kanal, 65 noot, 93 valjus // kinnic.setMessage(ShortMessage.NOTE_OFF, 0, 60, 93); if(kood==KeyEvent.VK_C){ kanal.noteOff(C); System.out.println(track); track.add(new MidiEvent(kinnic, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_1){ kanal.noteOff(CIS); System.out.println(track); track.add(new MidiEvent(kinnicis, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_D){ kanal.noteOff(D); System.out.println(track); track.add(new MidiEvent(kinnid, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_2){ kanal.noteOff(DIS); System.out.println(track); track.add(new MidiEvent(kinnidis, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_E){ kanal.noteOff(E); System.out.println(track); track.add(new MidiEvent(kinnie, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_F){ kanal.noteOff(F); System.out.println(track); track.add(new MidiEvent(kinnif, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_3){ kanal.noteOff(FIS); System.out.println(track); track.add(new MidiEvent(kinnifis, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_G){ kanal.noteOff(G); System.out.println(track); track.add(new MidiEvent(kinnig, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_4){ kanal.noteOff(GIS); System.out.println(track); track.add(new MidiEvent(kinnigis, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_A){ kanal.noteOff(A); System.out.println(track); track.add(new MidiEvent(kinnia, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_5){ kanal.noteOff(AIS); System.out.println(track); track.add(new MidiEvent(kinniais, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_H){ kanal.noteOff(H); System.out.println(track); track.add(new MidiEvent(kinnih, (vajutusaeg-algaeg)/100)); } if(kood==KeyEvent.VK_6){ kanal.noteOff(C1); System.out.println(track); track.add(new MidiEvent(kinnic1, (vajutusaeg-algaeg)/100)); } }catch(Exception viga){ viga.printStackTrace(); } kanal.noteOff(D); kanal.noteOff(E); kanal.noteOff(F); kanal.noteOff(G); kanal.noteOff(A); kanal.noteOff(H); kanal.noteOff(CIS); kanal.noteOff(DIS); kanal.noteOff(GIS); kanal.noteOff(AIS); kanal.noteOff(FIS); kanal.noteOff(C1); } public void keyTyped(KeyEvent e){} public static void main (String argumendid[]){ Frame f=new Frame(); f.add(new Klaver1()); f.setSize(670, 500); f.setVisible(true); } }