Results 1 to 3 of 3
Thread: [help] my beatbox prgramming
- 09-24-2009, 02:23 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
[help] my beatbox prgramming
I run it on Eclipse but when i press button "play" , it shows error. Can you try to run it to help me? Thanks for your care!
Java Code:import java.awt.*; import javax.swing.*; import javax.sound.midi.*; import java.util.*; import java.awt.event.*; public class BeatBox implements MetaEventListener { JPanel mainPanel; ArrayList checkboxList; int bpm; Sequencer sequencer; Sequence sequence; Track track; JFrame theFrame; String[] instrumentNames= { "Bass Drum","Closed Hi-hat","Open Hi-hat","Acoustic Snare","Crash Cymbal" ,"Hand Clap","High Tom","Hi Bongo","Macaras","Whistle","Low Conga","Cowbell","Vibraslap" ,"Low-mid Tom","High Agogo","Open Hi Conga"}; int[] instruments= {35,42,46,38,49,50,60,70,72,64,56,58,47,67,63}; public static void main(String[] args) { new BeatBox().buildGUI(); } public void buildGUI() { theFrame=new JFrame("Cyber BeatBox"); BorderLayout layout=new BorderLayout(); JPanel background=new JPanel(layout); background.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); checkboxList=new ArrayList(); Box buttonBox=new Box(BoxLayout.Y_AXIS); JButton start=new JButton("start"); start.addActionListener(new MyStartListener()); buttonBox.add(start); JButton stop=new JButton("stop"); stop.addActionListener(new MyStopListener()); buttonBox.add(stop); JButton upTempo=new JButton("upTempo"); upTempo.addActionListener(new MyUpTempoListener()); buttonBox.add(upTempo); JButton downTempo=new JButton("downTempo"); downTempo.addActionListener(new MyDownTempoListener()); buttonBox.add(downTempo); Box nameBox=new Box(BoxLayout.Y_AXIS); for(int i=0;i<16;i++) { nameBox.add(new Label(instrumentNames[i])); } background.add(BorderLayout.EAST,buttonBox); background.add(BorderLayout.WEST,nameBox); theFrame.getContentPane().add(background); GridLayout grid=new GridLayout(16,16); grid.setVgap(1); grid.setVgap(2); mainPanel=new JPanel(grid); background.add(BorderLayout.CENTER,mainPanel); for(int i=0;i<256;i++) { JCheckBox c=new JCheckBox(); c.setSelected(false); checkboxList.add(c); mainPanel.add(c); } setUpMidi(); theFrame.setBounds(50,50,300,300); theFrame.pack(); theFrame.setVisible(true); } public void setUpMidi() { try{ sequencer =MidiSystem.getSequencer(); sequencer.open(); sequencer.addMetaEventListener(this); sequence=new Sequence(Sequence.PPQ,4); track=sequence.createTrack(); sequencer.setTempoInBPM(bpm); } catch(Exception e) { e.printStackTrace(); } } public void buildTrackAndStart() { int[] trackList=null; int j,key; sequence.deleteTrack(track); track =sequence.createTrack(); for(int i=0;i<16;i++) { trackList=new int[16]; key=instruments[i]; for(j=0;j<16;j++) { JCheckBox jc=(JCheckBox)checkboxList.get(j+(16*i)); if(jc.isSelected()) { trackList[j]=key; } else { trackList[j]=0; } } makeTracks(trackList); } track.add(makeEvent(192,9,1,0,15)); try { sequencer.setSequence(sequence); sequencer.start(); sequencer.setTempoInBPM(bpm); } catch(Exception e) { e.printStackTrace(); } } public class MyStartListener implements ActionListener { public void actionPerformed(ActionEvent a) { buildTrackAndStart(); } } public class MyStopListener implements ActionListener { public void actionPerformed(ActionEvent a) { sequencer.stop(); } } public class MyUpTempoListener implements ActionListener { public void actionPerformed(ActionEvent a) { bpm+=3; } } public class MyDownTempoListener implements ActionListener { public void actionPerformed(ActionEvent a) { bpm-=3; } } public void makeTracks(int[] list) { int key; for(int i=0;i<16;i++) { key=list[i]; if(key!=0) { track.add(makeEvent(144,9,key,100,i)); track.add(makeEvent(128,9,key,100,i+1)); } } } public static MidiEvent makeEvent(int comd,int chan,int one,int two,int tick) { MidiEvent event=null; try{ ShortMessage a=new ShortMessage(); a.setMessage(comd, chan, one, two); event=new MidiEvent(a,tick); } catch(Exception e){ e.printStackTrace(); } return event; } public void meta(MetaMessage message) { if(message.getType()==47) { sequencer.start(); sequencer.setTempoInBPM(bpm); } } }
- 09-24-2009, 05:47 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Why don't you post the error that you got?
- 09-24-2009, 06:38 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
this is it.i'm sorry .
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks