我正在制作midi钢琴卷编辑器。 Note类创建并包含一个NOTE_ON对象和一个关联的NOTE_OFF对象以及一个用户在屏幕上操作以操纵音符的音高,时间和持续时间的矩形。下面是这个类的代码减去矩形的代码。我无法弄清楚为什么它不能正常工作。测试程序创建其中五个Note对象并将其显示在钢琴卷轴上,并且它们可以正常播放。当向上或向下拖动矩形时,改变音高的方法也能正常工作。但是当我打电话给方法改变时间或持续时间时,笔记开始行为不端。首先,它们不会在移动时被告知它们的位置,然后如果将音符拖到彼此之上或者相互延伸,则移动的音符将阻止其下方的音符播放。当arg设置为16时,我的移动单位发送到这些方法,因此音符将始终捕捉到第16拍位置。任何人都可以发现我的代码有什么问题吗?
public class Note {
MidiEvent noteOn;
MidiEvent noteOff;
private int channel;
private int pitch;
private int vel;
// Constructor calls methods to create NOTE_ON, NOTE_OFF, and graphical rectangle
public Note(MidiMessage on, long tickPos) {
noteOn = createNoteOn(on, tickPos);
ShortMessage shortMessage = (ShortMessage) on;
noteOff = createNoteOff(shortMessage.getChannel(), shortMessage.getData1(), tickPos);
}
public MidiEvent createNoteOn(MidiMessage on, long tickPos) {
noteOn = new MidiEvent(on, tickPos);
return noteOn;
}
public MidiEvent createNoteOff(int chan, int pitch, long tickPos) {
try {
noteOff = new MidiEvent(new ShortMessage(ShortMessage.NOTE_OFF, chan, pitch, 0), tickPos + 2);
} catch (InvalidMidiDataException e) {
e.printStackTrace();
}
return noteOff;
}
// Method for moving musical pitch of this note up or down for both NOTE_ON and NOTE_OFF
public void setPitch(int pitchUpOrDown) {
MidiMessage message = noteOn.getMessage();
ShortMessage shortMessage = (ShortMessage) message;
channel = shortMessage.getChannel();
pitch = shortMessage.getData1();
vel = shortMessage.getData2();
try {
shortMessage.setMessage(ShortMessage.NOTE_ON, channel, pitch + pitchUpOrDown, vel);
} catch (InvalidMidiDataException e) {
e.printStackTrace();
}
message = noteOff.getMessage();
shortMessage = (ShortMessage) message;
try {
shortMessage.setMessage(ShortMessage.NOTE_OFF, channel, pitch + pitchUpOrDown, 0);
} catch (InvalidMidiDataException e) {
e.printStackTrace();
}
}
// Moves entire note without changing duration of note
public void shiftLocation(int diff) {
noteOn.setTick(noteOn.getTick() + diff);
noteOff.setTick(noteOff.getTick() + diff);
}
// Moves start time of note while leaving end time in place, changes duration of note
public void setStartTime(long start) {
noteOn.setTick(start);
}
// Moves end time of note while leaving start time in place, changes duration of note
public void setDuration(int duration) {
noteOff.setTick(noteOff.getTick() + duration);
}
MIDI音序器和合成器:
import javax.sound.midi.*;
public class MusicEngine {
Sequencer sequencer;
Sequence sequence;
Synthesizer synthesizer;
Track track;
MidiEvent event;
// PPQ, or ticks per beat
int ticksPerBeat = 16;
int tempoBPM = 120;
// Constructor
public MusicEngine() {
createMidi();
}
// Get sequencer and sequence and track
public void createMidi() {
try {
sequencer = MidiSystem.getSequencer();
if (sequencer == null) {
System.out.println("Cannot get a sequencer");
System.exit(0);
}
sequencer.open();
// If sequencer is not the same as synthesizer, link the two
// (required in J2SE 5.0)
if (!(sequencer instanceof Synthesizer)) {
System.out.println("Linking the sequencer to the synthesizer");
synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
Receiver synthReceiver = synthesizer.getReceiver();
Transmitter seqTransmitter = sequencer.getTransmitter();
seqTransmitter.setReceiver(synthReceiver);
} else
synthesizer = (Synthesizer)sequencer;
sequence = new Sequence(Sequence.PPQ, ticksPerBeat);
track = sequence.createTrack();
sequencer.setTempoInBPM(tempoBPM);
} catch(MidiUnavailableException e) {
System.out.println("No sequencer available");
System.exit(0);
} catch(Exception e) {
e.printStackTrace();
System.exit(0);
}
}
// Create an individual MIDI event
public MidiEvent createEvent(int command, int channel, int one, int two, int tick) {
event = null;
try {
ShortMessage a = new ShortMessage();
a.setMessage(command, channel, one, two);
event = new MidiEvent(a, tick);
} catch(InvalidMidiDataException e) {
e.printStackTrace();
}
return event;
}
public void add(MidiEvent event) {
track.add(event);
}
public void playSong(int tempo) {
try {
sequencer.setSequence(sequence);
}
catch (InvalidMidiDataException e) {
e.printStackTrace();
}
sequencer.start();
}
public void stopSong() {
sequencer.stop();
}
}
答案 0 :(得分:0)
我可能在这里误解了你的代码,但似乎你只是在setDuration()
方法中增加了笔记的长度。它不应该看起来像这样吗?
public void setDuration(int duration) {
noteOff.setTick(noteOn.getTick() + duration);
}