// MidiMaker.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.sound.midi.*; public class MidiMaker extends JApplet implements ActionListener { private Container container; private JLabel channelLabel, instrumentLabel, noteLabel, bpmLabel, beatsLabel; private JTextField channelTextField, instrumentTextField, noteTextField, bpmTextField, beatsTextField; private JButton channelUpButton, instrumentUpButton, noteUpButton, bpmUpButton, beatsUpButton; private JButton channelDownButton, instrumentDownButton, noteDownButton, bpmDownButton, beatsDownButton; private JButton playButton; Sequencer sequencer; Sequence sequence; Track track; public void init() { container = getContentPane(); container.setLayout( new GridLayout(5,4) ); container.setBackground(Color.CYAN); /* channelLabel = new JLabel( "Channel# (0 or 9 percussion)" ); container.add( channelLabel ); channelTextField = new JTextField( "0", 3 ); container.add( channelTextField ); channelTextField.addActionListener( this ); channelUpButton = new JButton( "+1" ); container.add( channelUpButton ); channelUpButton.addActionListener( this ); channelDownButton = new JButton( "-1" ); container.add( channelDownButton ); channelDownButton.addActionListener( this ); */ instrumentLabel = new JLabel( "Instrument# (0-127)" ); container.add( instrumentLabel ); instrumentTextField = new JTextField( "1", 3 ); container.add( instrumentTextField ); instrumentTextField.addActionListener( this ); instrumentUpButton = new JButton( "+1" ); container.add( instrumentUpButton ); instrumentUpButton.addActionListener( this ); instrumentDownButton = new JButton( "-1" ); container.add( instrumentDownButton ); instrumentDownButton.addActionListener( this ); noteLabel = new JLabel( "Note# (0-127)" ); container.add( noteLabel ); noteTextField = new JTextField( "50", 3 ); container.add( noteTextField ); noteTextField.addActionListener( this ); noteUpButton = new JButton( "+1" ); container.add( noteUpButton ); noteUpButton.addActionListener( this ); noteDownButton = new JButton( "-1" ); container.add( noteDownButton ); noteDownButton.addActionListener( this ); bpmLabel = new JLabel( "BPM tempo" ); container.add( bpmLabel ); bpmTextField = new JTextField( "120", 3 ); container.add( bpmTextField ); bpmTextField.addActionListener( this ); bpmUpButton = new JButton( "+1" ); container.add( bpmUpButton ); bpmUpButton.addActionListener( this ); bpmDownButton = new JButton( "-1" ); container.add( bpmDownButton ); bpmDownButton.addActionListener( this ); beatsLabel = new JLabel( "Duration (#beats)" ); container.add( beatsLabel ); beatsTextField = new JTextField( "5", 3 ); container.add( beatsTextField ); beatsTextField.addActionListener( this ); beatsUpButton = new JButton( "+1" ); container.add( beatsUpButton ); beatsUpButton.addActionListener( this ); beatsDownButton = new JButton( "-1" ); container.add( beatsDownButton ); beatsDownButton.addActionListener( this ); playButton = new JButton( "Play it" ); container.add( playButton ); playButton.addActionListener( this ); setUpMidi(); } public void actionPerformed( ActionEvent event ) { if ( event.getSource() == playButton ) { // int channel = Integer.parseInt(channelTextField.getText()); int instrument = Integer.parseInt(instrumentTextField.getText()); int note = Integer.parseInt(noteTextField.getText()); int bpm = Integer.parseInt(bpmTextField.getText()); int beats = Integer.parseInt(beatsTextField.getText()); // play(channel, instrument, note, bpm, beats); play(0, instrument, note, bpm, beats); } else if ( event.getSource() == channelUpButton ) channelTextField.setText(""+(Integer.parseInt(channelTextField.getText()) + 1)); else if ( event.getSource() == instrumentUpButton ) instrumentTextField.setText(""+(Integer.parseInt(instrumentTextField.getText()) + 1)); else if ( event.getSource() == noteUpButton ) noteTextField.setText(""+(Integer.parseInt(noteTextField.getText()) + 1)); else if ( event.getSource() == bpmUpButton ) bpmTextField.setText(""+(Integer.parseInt(bpmTextField.getText()) + 1)); else if ( event.getSource() == beatsUpButton ) beatsTextField.setText(""+(Integer.parseInt(beatsTextField.getText()) + 1)); else if ( event.getSource() == channelDownButton ) channelTextField.setText(""+(Integer.parseInt(channelTextField.getText()) - 1)); else if ( event.getSource() == instrumentDownButton ) instrumentTextField.setText(""+(Integer.parseInt(instrumentTextField.getText()) - 1)); else if ( event.getSource() == noteDownButton ) noteTextField.setText(""+(Integer.parseInt(noteTextField.getText()) - 1)); else if ( event.getSource() == bpmDownButton ) bpmTextField.setText(""+(Integer.parseInt(bpmTextField.getText()) - 1)); else if ( event.getSource() == beatsDownButton ) beatsTextField.setText(""+(Integer.parseInt(beatsTextField.getText()) - 1)); } public void setUpMidi() { try { sequencer = MidiSystem.getSequencer(); sequencer.open(); sequence = new Sequence(Sequence.PPQ,4); track = sequence.createTrack(); sequencer.setTempoInBPM(120); } catch(Exception e) {e.printStackTrace();} } public void play(int channel, int instrument, int note, int bpm, int beats) { try { sequence.deleteTrack(track); track = sequence.createTrack(); // if (channel != 9) // ?? track.add(makeEvent(192,channel,instrument,0,1)); //change instrument ?? track.add(makeEvent(144,channel,note,100,1)); //note on // track.add(makeEvent(176,1,127,0,i)); track.add(makeEvent(128,channel,note,100,beats)); //note off sequencer.setSequence(sequence); sequencer.setLoopCount(1); sequencer.start(); sequencer.setTempoInBPM(bpm); } catch (Exception ex) {ex.printStackTrace();} } public MidiEvent makeEvent(int comd, int chan, int note, int velocity, int tick) { MidiEvent event = null; try { ShortMessage a = new ShortMessage(); a.setMessage(comd, chan, note, velocity); // event = new MidiEvent(a, tick); }catch(Exception e) { } return event; } }