今天给大家介绍一下Java实现钢琴的小程序,程序虽小,功能挺多,支持循环播放,录音等功能,首先简单介绍下源码结构:
先看看钢琴界面实现,添加相应滴监听事件:
/*
* @(#)MidiSynth.java 1.15 99/12/03
*
* Copyright (c) 1999 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.sound.midi.*;
import java.util.Vector;
import java.io.File;
import java.io.IOException;
/**
* Illustrates general MIDI melody instruments and MIDI controllers.
*
* @version @(#)MidiSynth.java 1.15 99/12/03
* @author Brian Lichtenwalter
*/
public class MidiSynth extends JPanel implements ControlContext {
final int PROGRAM = 192;
final int NOTEON = 144;
final int NOTEOFF = 128;
final int SUSTAIN = 64;
final int REVERB = 91;
final int ON = 0, OFF = 1;
final Color jfcBlue = new Color(204, 204, 255);
final Color pink = new Color(255, 175, 175);
Sequencer sequencer;
Sequence sequence;
Synthesizer synthesizer;
Instrument instruments[];
ChannelData channels[];
ChannelData cc; // current channel
JCheckBox mouseOverCB = new JCheckBox("mouseOver", true);
JSlider veloS, presS, bendS, revbS;
JCheckBox soloCB, monoCB, muteCB, sustCB;
Vector keys = new Vector();
Vector whiteKeys = new Vector();
JTable table;
Piano piano;
boolean record;
Track track;
long startTime;
RecordFrame recordFrame;
Controls controls;
public MidiSynth() {
setLayout(new BorderLayout());
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
EmptyBorder eb = new EmptyBorder(5,5,5,5);
BevelBorder bb = new BevelBorder(BevelBorder.LOWERED);
CompoundBorder cb = new CompoundBorder(eb,bb);
p.setBorder(new CompoundBorder(cb,eb));
JPanel pp = new JPanel(new BorderLayout());
pp.setBorder(new EmptyBorder(10,20,10,5));
pp.add(piano = new Piano());
p.add(pp);
p.add(controls = new Controls());
p.add(new InstrumentsTable());
add(p);
}
public void open() {
try {
if (synthesizer == null) {
if ((synthesizer = MidiSystem.getSynthesizer()) == null) {
System.out.println("getSynthesizer() failed!");
return;
}
}
synthesizer.open();
sequencer = MidiSystem.getSequencer();
sequence = new Sequence(Sequence.PPQ, 10);
} catch (Exception ex) { ex.printStackTrace(); return; }
Soundbank sb = synthesizer.getDefaultSoundbank();
if (sb != null) {
instruments = synthesizer.getDefaultSoundbank().getInstruments();
synthesizer.loadInstrument(instruments[0]);
}
MidiChannel midiChannels[] = synthesizer.getChannels();
channels = new ChannelData[midiChannels.length];
for (int i = 0; i < channels.length; i++) {
channels[i] = new ChannelData(midiChannels[i], i);
}
cc = channels[0];
ListSelectionModel lsm = table.getSelectionModel();
lsm.setSelectionInterval(0,0);
lsm = table.getColumnModel().getSelectionModel();
lsm.setSelectionInterval(0,0);
}
public void close() {
if (synthesizer != null) {
synthesizer.close();
}
if (sequencer != null) {
sequencer.close();
}
sequencer = null;
synthesizer = null;
instruments = null;
channels = null;
if (recordFrame != null) {
recordFrame.dispose();
recordFrame = null;
}
}
/**
* given 120 bpm:
* (120 bpm) / (60 seconds per minute) = 2 beats per second
* 2 / 1000 beats per millisecond
* (2 * resolution) ticks per second