java - How setMessage() works and sound changes in each row? -
i new java , sound api, have written code , not able understand how different sound coming different rows have not changed instrument in code.
import java.awt.*; import javax.swing.*; import javax.sound.midi.*; import java.util.*; import java.awt.event.*; public class beatbox { jpanel mainpanel; arraylist<jcheckbox> checkboxlist; 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", "maracas", "whistle", "low conga", "cowbell", "vibraslap", "low-mid tom", "high agogo", "open hi conga"}; int[] instruments = {35,42,46,38,49,39,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"); theframe.setdefaultcloseoperation(jframe.exit_on_close); borderlayout layout = new borderlayout(); jpanel background = new jpanel(layout); background.setborder(borderfactory.createemptyborder(10,10,10,10)); checkboxlist = new arraylist<jcheckbox>(); 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("tempo up"); uptempo.addactionlistener(new myuptempolistener()); buttonbox.add(uptempo); jbutton downtempo = new jbutton("tempo down"); downtempo.addactionlistener(new mydowntempolistener()); buttonbox.add(downtempo); box namebox = new box(boxlayout.y_axis); (int = 0; < 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.sethgap(2); mainpanel = new jpanel(grid); background.add(borderlayout.center, mainpanel); (int = 0; < 256; i++) { jcheckbox c = new jcheckbox(); c.setselected(false); checkboxlist.add(c); mainpanel.add(c); } // end loop setupmidi(); theframe.setbounds(50,50,300,300); theframe.pack(); theframe.setvisible(true); } // close method 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();} } // close method public void buildtrackandstart() { int[] tracklist = null; sequence.deletetrack(track); track = sequence.createtrack(); (int = 0; < 16; i++) { tracklist = new int[16]; int key = instruments[i]; (int j = 0; j < 16; j++ ) { jcheckbox jc = (jcheckbox) checkboxlist.get(j + (16*i)); if ( jc.isselected()) { tracklist[j] = key; } else { tracklist[j] = 0; } } // close inner loop maketracks(tracklist); } // close outer try { sequencer.setsequence(sequence); sequencer.setloopcount(sequencer.loop_continuously); sequencer.start(); sequencer.settempoinbpm(120); } catch(exception e) {e.printstacktrace();} } // close buildtrackandstart method public class mystartlistener implements actionlistener { public void actionperformed(actionevent a) { buildtrackandstart(); } } // close inner class public class mystoplistener implements actionlistener { public void actionperformed(actionevent a) { sequencer.stop(); } } // close inner class public class myuptempolistener implements actionlistener { public void actionperformed(actionevent a) { float tempofactor = sequencer.gettempofactor(); sequencer.settempofactor((float)(tempofactor * 1.03)); } } // close inner class public class mydowntempolistener implements actionlistener { public void actionperformed(actionevent a) { float tempofactor = sequencer.gettempofactor(); sequencer.settempofactor((float)(tempofactor * .97)); } } // close inner class public void maketracks(int[] list) { (int = 0; < 16; i++) { int key = list[i]; if (key != 0) { track.add(makeevent(144,9,key, 100, i)); track.add(makeevent(128,9,key, 100, i+1)); } } } public midievent makeevent(int comd, int chan, int one, int two, int tick) { midievent event = null; try { shortmessage = new shortmessage(); a.setmessage(comd, chan, one, two); event = new midievent(a, tick); } catch(exception e) {e.printstacktrace(); } return event; } } // close class
above code , if can tell how setmessage()
works, , arguments great have seen documents , not able understand.
drum instruments form special case, not have specific pitch on piano or trumpet. specific midi channel used transmit playback of drum instruments. in general midi, channel 10, find synthesizers can programmed receive drums on channel.
in case of channel assigned drum instruments (and special sound effects included in synthesizers), note on , note off message information pitch in fact used select drum or sound effect play.
for instance, play bass drum instrument on channel 10, send note on message follows:
using channel 10 (coded 9) write 35 note number used acoustic bass drum in gm list above.
for more details refer [1]: https://www.cs.cmu.edu/~music/cmsip/readings/midi%20tutorial%20for%20programmers.html
Comments
Post a Comment