1

我正在创建一个 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();
}
}
4

1 回答 1

0

我可能在这里误解了您的代码,但您似乎只是在增加setDuration()方法中注释的长度。它不应该看起来像这样吗?

public void setDuration(int duration) {
    noteOff.setTick(noteOn.getTick() + duration);
}
于 2012-12-05T09:35:47.547 回答