Java Midi - 如何从Java Synth到Java Sequencer创建Midi事件/记录?

时间:2013-04-26 00:22:09

标签: java midi

我将首先明确说明我的主要问题:在使用Java合成器记录到Java音序器时,使用正确的微秒位置从ShortMessages创建正确的'tick'来创建midi事件的正确方法是什么?

我一直试图解决这个问题好几天了。我的小midi程序很简单,或者至少应该是。有钢琴布局和三个按钮。使用合成器的钢琴效果很好;你可以用鼠标或电脑键盘演奏音符。

我已经向前和向前阅读了Oracle Docs说明(以及许多论坛帖子),但显然我错过了一些东西。

目前我可以通过手动创建ShortMessage,MidiEvent并将它们发送到音序器/将它们添加到音轨来获取音序器,但它只会记录一次。回放也通常在错误的时机播放。这是执行这些代码的代码:(如果您希望我发布其他或所有代码,请告知)。

单击录制/停止/按钮时发生的代码:

    public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand() == "Record")
    {

        mySeq.deleteTrack(track);
        track = mySeq.createTrack();
        try {
            seq.setSequence(mySeq);
        } catch (InvalidMidiDataException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        seq.recordEnable(track, ChannelNum);
        seq.startRecording();
    }

    else if(e.getActionCommand() == "Stop")
    {
        seq.stop();
    }

    else if(e.getActionCommand() == "Play")
    {
        mySeq.deleteTrack(track);
        track = mySeq.createTrack();
        addEvents(track);
        seq.setTickPosition(10);
        seq.start();
    }

}

这是用户在钢琴上弹奏音符时的代码:

    // The Mouse presses a key, the note on the channel is turned on
    // A MidiEvent and ShortMessage are created using the CreateOnEvent method
    public void mousePressed (MouseEvent e) {
        Key key = (Key) e.getSource ();
        channel.noteOn (key.getNote (), 127);
        CreateOnEvent(key);
    }

    public void mouseReleased (MouseEvent e) {
        Key key = (Key) e.getSource ();
        channel.noteOff (key.getNote ());
        CreateOffEvent(key);
    }

    public void mouseClicked (MouseEvent e) { }
    public void mouseEntered (MouseEvent e) { }
    public void mouseExited (MouseEvent e) { }

最后这里是CreateOnEvent方法:

    // I originally was sending the events directly to the sequencer
    // but here I'm adding the events to an ArrayList of MidiEvents to attempt a work around
    // I can then add those events to a track to play them
    public void CreateOnEvent(Key key)
{
    if(seq.isRecording())
    {
        ShortMessage myMsg = new ShortMessage();
        try {
            myMsg.setMessage(ShortMessage.NOTE_ON, ChannelNum, key.getNote(), 127);
        } catch (InvalidMidiDataException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        long timeStamp = synth.getMicrosecondPosition();
        long tick = seq.getTickPosition();
        event = new MidiEvent(myMsg, tick);
        seqReceiver.send(myMsg, timeStamp);
        Events.add(event);
    }

}

0 个答案:

没有答案