Algum tempo estou estudando essa API do Java a Java Sound e estou começando a enrede-la, mas aos poucos vou indo… Estou implementando essa bateria meio tosca mesmo só pra aprender a lidar com ela (API) andei vendo vários exemplos pela internet, mas se alguém puder ajudar, contribuir serei grato.
import javax.sound.midi.*; import java.awt.Cursor; import java.awt.Font; import java.awt.Rectangle; import java.awt.event.*; import javax.swing.*; /** * This program the MIDI percussion channel with a Swing window. It monitors * keystrokes and mouse motion in the window and uses them to create music. * Keycodes between 35 and 81, inclusive, generate different percussive sounds. * See the VK_ constants in java.awt.event.KeyEvent, or just experiment. Mouse * position controls volume: move the mouse to the right of the window to * increase the volume. */ // http://www.onjava.com/pub/a/onjava/excerpt/jenut3_ch17/index1.html?page=2 public class Drummer extends JFrame { private static final long serialVersionUID = 1L; private static JLabel jlbInformations = null; private static JLabel jlbInstrument = null; private static JLabel Cymbal = null; private static JLabel Cymbal01 = null; private static JLabel Cymbal02 = null; private static JLabel Cymbal03 = null; private static JLabel Drum = null; private static JLabel Drum01 = null; private static JLabel Drum02 = null; private static JLabel Drum03 = null; // The channel we play on: 10 is for percussion MidiChannel channel; // Default volume is 50% int velocity = 64; public static void main(String[] args) throws MidiUnavailableException { // We don't need a Sequencer in this example, since we send MIDI // events directly to the Synthesizer instead. Synthesizer synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); JFrame frame = new Drummer(synthesizer); frame.add(jlbInformations, null); frame.add(jlbInstrument, null); frame.add(Cymbal, null); frame.add(Cymbal01, null); frame.add(Cymbal02, null); frame.add(Cymbal03, null); frame.add(Drum, null); frame.add(Drum01, null); frame.add(Drum02, null); frame.add(Drum03, null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // We use window width as volume control frame.setSize(800, 600); // Tamanho fixo do programa, sem alteração frame.setResizable(false); // Deixa o programa no meio da tela, centralizado frame.setLocationRelativeTo(null); frame.setVisible(true); } private void playCymbal() { // 120 = Reverse Cymbal int opcion = 120; try { // 60 = C "middle C"; start of 4th octave int listaNotas = 60; // Sintetizador midi é obtido Synthesizer sintetizador = MidiSystem.getSynthesizer(); // Abre o sintetizador para reproduzir sons sintetizador.open(); // Os canais do sintetizador são obtidos MidiChannel canales = sintetizador.getChannels()[0]; // Carrega-se o banco de som Soundbank bancoSonidos = sintetizador.getDefaultSoundbank(); // Carrega-se a lista de som sintetizador.loadAllInstruments(bancoSonidos); // Carrega-se os instrumentos Instrument[] listaInstrumentos = sintetizador .getLoadedInstruments(); // int upperRange = listaInstrumentos.length - 1; Instrument instrumento = listaInstrumentos[opcion]; Patch patch = instrumento.getPatch(); canales.programChange(patch.getBank(), patch.getProgram()); canales.noteOn(listaNotas, 100); canales.allNotesOff(); } catch (Exception exc) { exc.printStackTrace(); } } private void playSnare() { // 38 = Acoustic Snare int opcion = 38; try { // 60 = C "middle C"; start of 4th octave int listaNotas = 60; // Sintetizador midi é obtido Synthesizer sintetizador = MidiSystem.getSynthesizer(); // Abre o sintetizador para reproduzir sons sintetizador.open(); // Os canais do sintetizador são obtidos MidiChannel canales = sintetizador.getChannels()[9]; // Carrega-se o banco de som Soundbank bancoSonidos = sintetizador.getDefaultSoundbank(); // Carrega-se a lista de som sintetizador.loadAllInstruments(bancoSonidos); // Carrega-se os instrumentos Instrument[] listaInstrumentos = sintetizador .getLoadedInstruments(); // int upperRange = listaInstrumentos.length - 1; Instrument instrumento = listaInstrumentos[opcion]; Patch patch = instrumento.getPatch(); canales.programChange(patch.getBank(), patch.getProgram()); canales.noteOn(listaNotas, 100); canales.allNotesOff(); } catch (Exception exc) { exc.printStackTrace(); } } public Drummer(Synthesizer synth) { super("Java MIDI Sound Drums - J. Marcos B."); // Channel 10 is the GeneralMidi percussion channel. In Java code, we // number channels from 0 and use channel 9 instead. channel = synth.getChannels()[9]; addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key >= 35 && key <= 81) { channel.noteOn(key, velocity); try { Synthesizer synth = MidiSystem.getSynthesizer(); Instrument[] instruments = synth.getDefaultSoundbank() .getInstruments(); // for (int i = 0; i < instruments.length; i++) { Instrument instrument = instruments[key]; System.out.println(key + " Instrument: " + instrument.getName()); jlbInstrument.setText("Instrument: " + instrument.getName()); // } } catch (MidiUnavailableException e1) { e1.printStackTrace(); } } } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key >= 35 && key <= 81) channel.noteOff(key); } }); addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { velocity = e.getX(); } }); ImageIcon cymbal = new ImageIcon(getClass().getResource( "images/Cymbal.png")); Cymbal = new JLabel(cymbal); Cymbal.setBounds(new Rectangle(10, 90, 135, 150)); Cymbal.setCursor(new Cursor(Cursor.HAND_CURSOR)); Cymbal.setToolTipText("Cymbal"); Cymbal.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent e) { playCymbal(); } }); ImageIcon cymbal01 = new ImageIcon(getClass().getResource( "images/Cymbal.png")); Cymbal01 = new JLabel(cymbal01); Cymbal01.setBounds(new Rectangle(150, 50, 135, 150)); Cymbal01.setCursor(new Cursor(Cursor.HAND_CURSOR)); Cymbal01.setToolTipText("Cymbal"); Cymbal01.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent e) { playCymbal(); } }); ImageIcon cymbal02 = new ImageIcon(getClass().getResource( "images/Cymbal.png")); Cymbal02 = new JLabel(cymbal02); Cymbal02.setBounds(new Rectangle(450, 50, 135, 150)); Cymbal02.setCursor(new Cursor(Cursor.HAND_CURSOR)); Cymbal02.setToolTipText("Cymbal"); Cymbal02.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent e) { playCymbal(); } }); ImageIcon cymbal03 = new ImageIcon(getClass().getResource( "images/Cymbal.png")); Cymbal03 = new JLabel(cymbal03); Cymbal03.setBounds(new Rectangle(590, 90, 135, 150)); Cymbal03.setCursor(new Cursor(Cursor.HAND_CURSOR)); Cymbal03.setToolTipText("Cymbal"); Cymbal03.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent e) { playCymbal(); } }); // setBounds(coluna , linha , largura , altura ); ImageIcon drum = new ImageIcon(getClass().getResource( "images/Snare.png")); Drum = new JLabel(drum); Drum.setBounds(new Rectangle(245, 50, 135, 150)); Drum.setCursor(new Cursor(Cursor.HAND_CURSOR)); Drum.setToolTipText("Snare"); Drum.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent e) { playSnare(); } }); ImageIcon drum01 = new ImageIcon(getClass().getResource( "images/Snare.png")); Drum01 = new JLabel(drum01); Drum01.setBounds(new Rectangle(245, 165, 135, 150)); Drum01.setCursor(new Cursor(Cursor.HAND_CURSOR)); Drum01.setToolTipText("Snare"); Drum01.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent e) { playSnare(); } }); ImageIcon drum02 = new ImageIcon(getClass().getResource( "images/Snare.png")); Drum02 = new JLabel(drum02); Drum02.setBounds(new Rectangle(365, 50, 135, 150)); Drum02.setCursor(new Cursor(Cursor.HAND_CURSOR)); Drum02.setToolTipText("Snare"); Drum02.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent e) { playSnare(); } }); ImageIcon drum03 = new ImageIcon(getClass().getResource( "images/Snare.png")); Drum03 = new JLabel(drum03); Drum03.setBounds(new Rectangle(245, 265, 135, 150)); Drum03.setCursor(new Cursor(Cursor.HAND_CURSOR)); Drum03.setToolTipText("Snare"); Drum03.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent e) { playSnare(); } }); jlbInformations = new JLabel(); jlbInformations.setBounds(new Rectangle(30, 450, 800, 100)); jlbInformations .setText("<html>This the MIDI percussion channel program to create music generate different percussive sounds. " + "<br/>Just an experiment, press the following keys: 1234567890-= qeiopadfghjkl cbnm,.; Right/Left arrow keys, Up/Down arrow keys " + "<br/>and move the mouse to the right of the window to increase the volume.</html>"); jlbInstrument = new JLabel(); jlbInstrument.setBounds(new Rectangle(10, 350, 800, 100)); jlbInstrument.setHorizontalAlignment(SwingConstants.CENTER); jlbInstrument.setFont(new Font("Arial", Font.BOLD, 28)); } }
Gostei vou fazer mais coisas com ela. Sugestões?
Link para download:
Drummer.jar | 62 KB |