好的,这是我在这里的第一个问题,所以对我来说很容易。我正在尝试使用MidiChannel类中的noteOn和内部Java Synthesizer动态生成声音。它产生完美的声音。我的问题是将声音输出到我的音序器,所以我可以将它保存到midi文件。目前,我的程序创建了文件,但它是空的。我的发射器/接收器设置发生断开。当我试图获得合成器的发射器时,它会产生可爱的MidiUnavailbleException。
我在这里有一堆代码,所以我只是给你一些亮点。完整的课程将在最底层。
public Piano()
{
try
{
//previous code not shown//
sequence = new Sequence(Sequence.PPQ, 4); //I know. Just let it be.
sequence.createTrack();
sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(sequence);
sequencer.recordEnable(sequencer.getSequence().getTracks()[0], 1);
receiver = sequencer.getReceiver();
transmitter = synth.getTransmitter();
transmitter.setReceiver(receiver);
}
catch (Exception e)
{
e.printStackTrace();
}
}
当它到达transmitter = synth.getTransmitter();
时,我得到了MidiUnavailableException。关于如何将MidiEvents从我的合成器送到音序器的任何想法?
package main;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import javax.imageio.ImageIO;
import javax.sound.midi.*;
import javax.sound.midi.MidiDevice.Info;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.TargetDataLine;
import javax.swing.*;
import org.jfugue.DeviceThatWillReceiveMidi;
import org.jfugue.DeviceThatWillTransmitMidi;
public class Piano implements ActionListener
![enter image description here][2]{
KeyEventDispatcher dispatcher = new KeyEventDispatcher()
{
public boolean dispatchKeyEvent(KeyEvent e)
{
if(e.getID()==401)
{
if(e.getKeyCode()==KeyEvent.VK_SPACE)
{
sustain();
}
if(e.getKeyCode()==KeyEvent.VK_PERIOD)
{
octaveUp();
}
if(e.getKeyCode()==KeyEvent.VK_COMMA)
{
octaveDown();
}
if(e.getKeyCode()==KeyEvent.VK_A)
{
start(freq);
}
if(e.getKeyCode()==KeyEvent.VK_W)
{
start(freq+1);
}
if(e.getKeyCode()==KeyEvent.VK_S)
{
start(freq+2);
}
if(e.getKeyCode()==KeyEvent.VK_E)
{
start(freq+3);
}
if(e.getKeyCode()==KeyEvent.VK_D)
{
start(freq+4);
}
if(e.getKeyCode()==KeyEvent.VK_F)
{
start(freq+5);
}
if(e.getKeyCode()==KeyEvent.VK_T)
{
start(freq+6);
}
if(e.getKeyCode()==KeyEvent.VK_G)
{
start(freq+7);
}
if(e.getKeyCode()==KeyEvent.VK_Y)
{
start(freq+8);
}
if(e.getKeyCode()==KeyEvent.VK_H)
{
start(freq+9);
}
if(e.getKeyCode()==KeyEvent.VK_U)
{
start(freq+10);
}
if(e.getKeyCode()==KeyEvent.VK_J)
{
start(freq+11);
}
if(e.getKeyCode()==KeyEvent.VK_K)
{
start(freq+12);
}
if(e.getKeyCode()==KeyEvent.VK_O)
{
start(freq+13);
}
if(e.getKeyCode()==KeyEvent.VK_L)
{
start(freq+14);
}
if(e.getKeyCode()==KeyEvent.VK_P)
{
start(freq+15);
}
if(e.getKeyCode()==KeyEvent.VK_SEMICOLON)
{
start(freq+16);
}
}
if(e.getID()==402)
{
if(!sustain)
{
if(e.getKeyCode()==KeyEvent.VK_A)
{
end(freq);
}
if(e.getKeyCode()==KeyEvent.VK_W)
{
end(freq+1);
}
if(e.getKeyCode()==KeyEvent.VK_S)
{
end(freq+2);
}
if(e.getKeyCode()==KeyEvent.VK_E)
{
end(freq+3);
}
if(e.getKeyCode()==KeyEvent.VK_D)
{
end(freq+4);
}
if(e.getKeyCode()==KeyEvent.VK_F)
{
end(freq+5);
}
if(e.getKeyCode()==KeyEvent.VK_T)
{
end(freq+6);
}
if(e.getKeyCode()==KeyEvent.VK_G)
{
end(freq+7);
}
if(e.getKeyCode()==KeyEvent.VK_Y)
{
end(freq+8);
}
if(e.getKeyCode()==KeyEvent.VK_H)
{
end(freq+9);
}
if(e.getKeyCode()==KeyEvent.VK_U)
{
end(freq+10);
}
if(e.getKeyCode()==KeyEvent.VK_J)
{
end(freq+11);
}
if(e.getKeyCode()==KeyEvent.VK_K)
{
end(freq+12);
}
if(e.getKeyCode()==KeyEvent.VK_O)
{
end(freq+13);
}
if(e.getKeyCode()==KeyEvent.VK_L)
{
end(freq+14);
}
if(e.getKeyCode()==KeyEvent.VK_P)
{
end(freq+15);
}
if(e.getKeyCode()==KeyEvent.VK_SEMICOLON)
{
end(freq+16);
}
}
else
{
if(e.getKeyCode()==KeyEvent.VK_A)
{
endSoft(freq);
}
if(e.getKeyCode()==KeyEvent.VK_W)
{
endSoft(freq+1);
}
if(e.getKeyCode()==KeyEvent.VK_S)
{
endSoft(freq+2);
}
if(e.getKeyCode()==KeyEvent.VK_E)
{
endSoft(freq+3);
}
if(e.getKeyCode()==KeyEvent.VK_D)
{
endSoft(freq+4);
}
if(e.getKeyCode()==KeyEvent.VK_F)
{
endSoft(freq+5);
}
if(e.getKeyCode()==KeyEvent.VK_T)
{
endSoft(freq+6);
}
if(e.getKeyCode()==KeyEvent.VK_G)
{
endSoft(freq+7);
}
if(e.getKeyCode()==KeyEvent.VK_Y)
{
endSoft(freq+8);
}
if(e.getKeyCode()==KeyEvent.VK_H)
{
endSoft(freq+9);
}
if(e.getKeyCode()==KeyEvent.VK_U)
{
endSoft(freq+10);
}
if(e.getKeyCode()==KeyEvent.VK_J)
{
endSoft(freq+11);
}
if(e.getKeyCode()==KeyEvent.VK_K)
{
endSoft(freq+12);
}
if(e.getKeyCode()==KeyEvent.VK_O)
{
endSoft(freq+13);
}
if(e.getKeyCode()==KeyEvent.VK_L)
{
endSoft(freq+14);
}
if(e.getKeyCode()==KeyEvent.VK_P)
{
endSoft(freq+15);
}
if(e.getKeyCode()==KeyEvent.VK_SEMICOLON)
{
endSoft(freq+16);
}
}
}
return false;
}
};
final int VOLUME_MAX = 120;
int freq = 60;
int volume = VOLUME_MAX;
int instr;
int delay = 1000;
int velocity = 1000;
int count = 0;
boolean sustain = false;
boolean recording = false;
DecimalFormat df;
List<Icon> icons;
AnimatedIcon aIcon;
Synthesizer synth;
Sequence sequence;
Sequencer sequencer;
MidiChannel[] mc;
Instrument[] instruments;
Instrument instrument;
Transmitter transmitter;
Receiver receiver;
File file;
JFrame mainFrame;
JPanel mainPanel;
ImagePanel iPanel;
JButton rec;
JButton stop;
JButton load;
JButton sustainB;
JButton octave;
JButton up;
JButton down;
JButton volumeB;
JButton vUp;
JButton vDown;
OctavePanel oPanel;
VolumePanel vPanel;
GridBagLayout gridBag;
GridBagConstraints c;
Image redCir;
Image greenCir;
Image recCir;
public Piano()
{
DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().
addKeyEventDispatcher(dispatcher);
InputMap im = (InputMap)UIManager.get("Button.focusInputMap");
im.put(KeyStroke.getKeyStroke("pressed SPACE"), "none");
im.put(KeyStroke.getKeyStroke("released SPACE"), "none");
try
{
synth = MidiSystem.getSynthesizer();
synth.open();
FileInputStream stream = new FileInputStream(this.getClass().getResource("resources/soundbank1.gm").toString().substring(6));
Soundbank sBank = MidiSystem.getSoundbank(stream);
instruments = sBank.getInstruments();
synth.loadAllInstruments(sBank);
mc = synth.getChannels();
instrument = (Instrument)JOptionPane.showInputDialog(null, "Choose an instrument", "", JOptionPane.PLAIN_MESSAGE, null, instruments, instruments[0]);
mc[0].programChange(instrument.getPatch().getProgram());
greenCir = ImageIO.read(getClass().getResource("resources/greenCir.gif"));
redCir = ImageIO.read(getClass().getResource("resources/redCir.gif"));
recCir = ImageIO.read(getClass().getResource("resources/rec.gif"));
sequence = new Sequence(Sequence.PPQ, 4);
sequence.createTrack();
sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(sequence);
sequencer.recordEnable(sequencer.getSequence().getTracks()[0], 1);
df = new DecimalFormat("000");
receiver = sequencer.getReceiver();
transmitter = synth.getTransmitter();
transmitter.setReceiver(receiver);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void sustain()
{
sustain = !sustain;
if(sustain)
{
sustainB.setIcon(new ImageIcon(greenCir));
}
else
{
sustainB.setIcon(new ImageIcon(redCir));
}
}
public void octaveUp()
{
freq += 12;
octave.setText("Octave: " + (freq-60)/12);
}
public void octaveDown()
{
freq -= 12;
octave.setText("Octave: " + (freq-60)/12);
}
public void vUp()
{
if(volume < 120)
volume += 12;
volumeB.setText(setVText(volume));
}
public void vDown()
{
if(volume > 0)
volume -= 12;
volumeB.setText(setVText(volume));
}
public void resetVolume()
{
volume = VOLUME_MAX;
volumeB.setText(setVText(volume));
}
public String setVText(int v)
{
return "Volume " + volume/12 * 10 + "%";
}
public void selectInstrument()
{
instrument = (Instrument)JOptionPane.showInputDialog(null, "Choose an instrument", "", JOptionPane.PLAIN_MESSAGE, null, instruments, instruments[0]);
mc[0].programChange(instrument.getPatch().getProgram());
}
public BufferedImage getPiano()
{
URL url = this.getClass().getResource("piano.gif");
try
{
BufferedImage buffImage = ImageIO.read(url);
return buffImage;
}
catch (IOException e)
{
System.out.println("Error");
return null;
}
}
public void start(int note)
{
mc[0].noteOn(note, volume);
}
public void end(int note)
{
mc[0].noteOff(note);
}
public void endSoft(int note)
{
//mc[0].noteOff(note, velocity);
}
public void createAndShowGUI()
{
mainFrame = new JFrame("Piano Maestro");
mainFrame.setDefaultCloseOperation(0);
mainFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {System.exit(0);}
});
setupFrame(mainFrame.getContentPane());
mainFrame.pack();
mainFrame.setMinimumSize(new Dimension(500, 300));
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
mainFrame.setMinimumSize(new Dimension(iPanel.getWidth()+20, mainFrame.getHeight()));
mainFrame.setResizable(false);
}
public void setupFrame(Container pane)
{
gridBag = new GridBagLayout();
pane.setLayout(gridBag);
c = new GridBagConstraints();
sustainB = new JButton("Sustain");
sustainB.addActionListener(this);
sustainB.setActionCommand("sustain");
up = new JButton();
up.addActionListener(this);
up.setActionCommand("up");
down = new JButton();
down.addActionListener(this);
down.setActionCommand("down");
octave = new JButton("Octave: 0");
octave.addActionListener(this);
octave.setActionCommand("resetOctave");
stop = new JButton("Stop");
stop.addActionListener(this);
stop.setActionCommand("stop");
rec = new JButton("rec");
rec.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){rec();}});
load = new JButton("Load Instr.");
load.addActionListener(this);
load.setActionCommand("load");
volumeB = new JButton(setVText(volume));
volumeB.addActionListener(this);
volumeB.setActionCommand("resetVolume");
vUp = new JButton();
vUp.addActionListener(this);
vUp.setActionCommand("vUp");
vDown = new JButton();
vDown.addActionListener(this);
vDown.setActionCommand("vDown");
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.ipady = 10;
vPanel = new VolumePanel(vDown,volumeB,vUp);
pane.add(vPanel,c);
c.gridy = 1;
oPanel = new OctavePanel(down,octave,up);
pane.add(oPanel,c);
c.gridx = 1;
c.gridy = 0;
pane.add(sustainB,c);
c.gridx = 2;
pane.add(rec,c);
c.gridx = 1;
c.gridy = 1;
pane.add(load,c);
c.gridx = 2;
pane.add(stop,c);
c.gridy = 2;
c.gridx = 0;
c.gridwidth = 3;
try
{
sustainB.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("resources/redCir.gif"))));
up.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("resources/up.gif"))));
vUp.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("resources/up.gif"))));
down.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("resources/down.gif"))));
vDown.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("resources/down.gif"))));
stop.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("resources/square.gif"))));
rec.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("resources/rec.gif"))));
iPanel = new ImagePanel("resources/piano.gif");
pane.add(iPanel,c);
}
catch (Exception e)
{
e.printStackTrace();
}
aIcon = new AnimatedIcon(rec,750,1,new ImageIcon(this.getClass().getResource("resources/recOn.gif")),new ImageIcon(this.getClass().getResource("resources/recOff.gif")));
load.setPreferredSize(sustainB.getPreferredSize());
rec.setPreferredSize(stop.getPreferredSize());
octave.setPreferredSize(volumeB.getPreferredSize());
volumeB.setPreferredSize(volumeB.getPreferredSize());
volumeB.setText(setVText(volume));
}
public void rec()
{
recording = true;
rec.setIcon(aIcon);
aIcon.start();
sequencer.startRecording();
}
public void stop()
{
if(recording)
{
aIcon.stop();
rec.setIcon(new ImageIcon(recCir));
sequencer.stop();
if(sequence!=null)
{
file = new File("Song" + df.format(count) + ".midi");
while(file.exists())
{
count++;
file = new File("Song" + df.format(count) + ".midi");
}
try
{
MidiSystem.write(sequence, 1, file);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "Your system is unable to write files.","Error",2);
}
}
else
{
JOptionPane.showMessageDialog(null, "Your system is unable to write files.","Error",2);
}
}
mc[0].allNotesOff();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("sustain"))
{
sustain();
}
if(e.getActionCommand().equals("up"))
{
octaveUp();
}
if(e.getActionCommand().equals("down"))
{
octaveDown();
}
if(e.getActionCommand().equals("resetOctave"))
{
freq = 60;
octave.setText("Octave: " + (freq-60)/12);
}
if(e.getActionCommand().equals("load"))
{
this.selectInstrument();
}
if(e.getActionCommand().equals("piano"))
{
mc[0].programChange(instruments[0].getPatch().getProgram());
}
if(e.getActionCommand().equals("stop"))
{
stop();
}
if(e.getActionCommand().equals("vUp"))
{
vUp();
}
if(e.getActionCommand().equals("vDown"))
{
vDown();
}
if(e.getActionCommand().equals("resetVolume"))
{
resetVolume();
}
}
}