/* MIDIVIDIN Mart Karu karu@metal.ee */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import javax.sound.midi.*; class Midiplayer extends JFrame { int tempo = 500; int[][] looData = new int[6][16]; Vector nupud = new Vector(); String[] instrumendid = {"Bassdrumm", "Soolotrumm", "Hat", "Taldrik", "Tom1", "Tom2"}; JComboBox inst = new JComboBox(instrumendid); JCheckBox cb1 = new JCheckBox(); JCheckBox cb2 = new JCheckBox(); JCheckBox cb3 = new JCheckBox(); JCheckBox cb4 = new JCheckBox(); JCheckBox cb5 = new JCheckBox(); JCheckBox cb6 = new JCheckBox(); JCheckBox cb7 = new JCheckBox(); JCheckBox cb8 = new JCheckBox(); JCheckBox cb9 = new JCheckBox(); JCheckBox cb10 = new JCheckBox(); JCheckBox cb11 = new JCheckBox(); JCheckBox cb12 = new JCheckBox(); JCheckBox cb13 = new JCheckBox(); JCheckBox cb14 = new JCheckBox(); JCheckBox cb15 = new JCheckBox(); JCheckBox cb16 = new JCheckBox(); Mangija m = null; public Midiplayer() { m = new Mangija(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); getContentPane().setLayout(null); cb1.setBounds(10,10,20,20); cb2.setBounds(30,10,20,20); cb3.setBounds(50,10,20,20); cb4.setBounds(70,10,20,20); cb5.setBounds(100,10,20,20); cb6.setBounds(120,10,20,20); cb7.setBounds(140,10,20,20); cb8.setBounds(160,10,20,20); cb9.setBounds(190,10,20,20); cb10.setBounds(210,10,20,20); cb11.setBounds(230,10,20,20); cb12.setBounds(250,10,20,20); cb13.setBounds(280,10,20,20); cb14.setBounds(300,10,20,20); cb15.setBounds(320,10,20,20); cb16.setBounds(340,10,20,20); getContentPane().add(cb1, BorderLayout.CENTER); getContentPane().add(cb2, BorderLayout.CENTER); getContentPane().add(cb3, BorderLayout.CENTER); getContentPane().add(cb4, BorderLayout.CENTER); getContentPane().add(cb5, BorderLayout.CENTER); getContentPane().add(cb6, BorderLayout.CENTER); getContentPane().add(cb7, BorderLayout.CENTER); getContentPane().add(cb8, BorderLayout.CENTER); getContentPane().add(cb9, BorderLayout.CENTER); getContentPane().add(cb10, BorderLayout.CENTER); getContentPane().add(cb11, BorderLayout.CENTER); getContentPane().add(cb12, BorderLayout.CENTER); getContentPane().add(cb13, BorderLayout.CENTER); getContentPane().add(cb14, BorderLayout.CENTER); getContentPane().add(cb15, BorderLayout.CENTER); getContentPane().add(cb16, BorderLayout.CENTER); nupud.add(cb1); nupud.add(cb2); nupud.add(cb3); nupud.add(cb4); nupud.add(cb5); nupud.add(cb6); nupud.add(cb7); nupud.add(cb8); nupud.add(cb9); nupud.add(cb10); nupud.add(cb11); nupud.add(cb12); nupud.add(cb13); nupud.add(cb14); nupud.add(cb15); nupud.add(cb16); CheckBoxListener myListener = new CheckBoxListener(this); cb1.addItemListener(myListener); cb2.addItemListener(myListener); cb3.addItemListener(myListener); cb4.addItemListener(myListener); cb5.addItemListener(myListener); cb6.addItemListener(myListener); cb7.addItemListener(myListener); cb8.addItemListener(myListener); cb9.addItemListener(myListener); cb10.addItemListener(myListener); cb11.addItemListener(myListener); cb12.addItemListener(myListener); cb13.addItemListener(myListener); cb14.addItemListener(myListener); cb15.addItemListener(myListener); cb16.addItemListener(myListener); inst.setBounds(10,40, 200, 20); inst.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox temp = (JComboBox)e.getSource(); vahetaInstrumenti(temp.getSelectedIndex()); } }); getContentPane().add(inst); JScrollBar SbTempo = new JScrollBar(JScrollBar.HORIZONTAL, 100, 100, 100, 1000); SbTempo.setBounds(220, 40, 300, 20); SbTempo.addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { JScrollBar temp = (JScrollBar)e.getSource(); tempo = temp.getValue(); } }); getContentPane().add(SbTempo); initLugu(); vahetaInstrumenti(0); m.start(); } void vahetaInstrumenti(int instrument) { for(int j=0;j<16;j++) ((JCheckBox)nupud.get(j)).setSelected(looData[instrument][j]==1); } void initLugu() { for(int i=0;i<6;i++) for(int j=0;j<16;j++) looData[i][j]=0; } void setNote(int note, int val) { looData[inst.getSelectedIndex()][note] = val; } public static void main(String args[]) { System.out.println("Käivitame midimängijat..."); Midiplayer mainFrame = new Midiplayer(); mainFrame.setSize(400, 400); mainFrame.setTitle("Midiplayer"); mainFrame.setVisible(true); } } class Mangija extends Thread{ Midiplayer mp = null; boolean isRunning = true; Synthesizer synt = null; MidiChannel kanal = null; int biit = 0; int[] instrud ={36, 40, 42, 49, 45, 47}; Mangija(Midiplayer mpIn) { try { synt=MidiSystem.getSynthesizer(); synt.open(); kanal=synt.getChannels()[9]; } catch(Exception e){} mp = mpIn; } public void run() { while(isRunning) { for(int i=0;i<6;i++) { if(mp.looData[i][biit]==1) { kanal.noteOn(instrud[i], 100); } } biit++; if (biit>15) biit = 0; try{Thread.sleep(mp.tempo);} catch (Exception e) {} } synt.close(); } } class CheckBoxListener implements ItemListener { Midiplayer mp = null; CheckBoxListener(Midiplayer mpIn) { mp = mpIn; } public void itemStateChanged(ItemEvent e) { Object source = e.getItemSelectable(); if (e.getStateChange() == ItemEvent.DESELECTED) { if (source == mp.cb1) mp.setNote(0, 0); else if (source == mp.cb2) mp.setNote(1,0); else if (source == mp.cb3) mp.setNote(2,0); else if (source == mp.cb4) mp.setNote(3,0); else if (source == mp.cb5) mp.setNote(4,0); else if (source == mp.cb6) mp.setNote(5,0); else if (source == mp.cb7) mp.setNote(6,0); else if (source == mp.cb8) mp.setNote(7,0); else if (source == mp.cb9) mp.setNote(8,0); else if (source == mp.cb10) mp.setNote(9,0); else if (source == mp.cb11) mp.setNote(10,0); else if (source == mp.cb12) mp.setNote(11,0); else if (source == mp.cb13) mp.setNote(12,0); else if (source == mp.cb14) mp.setNote(13,0); else if (source == mp.cb15) mp.setNote(14,0); else if (source == mp.cb16) mp.setNote(15,0); } else { if (source == mp.cb1) mp.setNote(0, 1); else if (source == mp.cb2) mp.setNote(1,1); else if (source == mp.cb3) mp.setNote(2,1); else if (source == mp.cb4) mp.setNote(3,1); else if (source == mp.cb5) mp.setNote(4,1); else if (source == mp.cb6) mp.setNote(5,1); else if (source == mp.cb7) mp.setNote(6,1); else if (source == mp.cb8) mp.setNote(7,1); else if (source == mp.cb9) mp.setNote(8,1); else if (source == mp.cb10) mp.setNote(9,1); else if (source == mp.cb11) mp.setNote(10,1); else if (source == mp.cb12) mp.setNote(11,1); else if (source == mp.cb13) mp.setNote(12,1); else if (source == mp.cb14) mp.setNote(13,1); else if (source == mp.cb15) mp.setNote(14,1); else if (source == mp.cb16) mp.setNote(15,1); } } }