如何在jFugue 5.0中转换模式?

时间:2017-06-29 05:53:52

标签: jfugue

在jFugue 4.0中,有一个很好的功能: 使用PatternTransformer转换模式

但是在jFugue 5.0中删除了所有模式转换器。我知道它必须被一些很酷的东西所取代。但是请在jFugue 5.0中做些什么?我不知道。我用谷歌搜索,但到目前为止没有结果。

1 个答案:

答案 0 :(得分:1)

“PatternTransformer”类已经消失了,但转换模式从未如此简单!

在旧版本的JFugue中,PatternTransformer和ParserListener之间实际上差别很小。较旧版本的JFugue也称为PatternTool,它就像一个Transformer,但它不是转换模式,而是仅仅测量它;例如,你可以写一个工具来告诉你在一件作品中使用了什么乐器。

要在JFugue中转换Pattern,只需创建一个实现ParserListener(或扩展ParserListenerAdapter)的类,并将其作为侦听器添加到解析器中 - 例如StaccatoParser:

例如,这是一个工具,可以找到一块中使用的乐器:

public class InstrumentTool extends ParserListenerAdapter 
{
    private List<String> instrumentNames;

    public InstrumentTool() {
        super();
        instrumentNames = new ArrayList<String>();
    }

    @Override
    public void onInstrumentParsed(byte instrument) {
        String instrumentName = MidiDictionary.INSTRUMENT_BYTE_TO_STRING.get(instrument);
        if (!instrumentNames.contains(instrumentName)) {
            instrumentNames.add(instrumentName);
        }

    }

    public List<String> getInstrumentNames() {
        return this.instrumentNames;
    }
}

以下是如何使用它:

MidiParser midiParser = new MidiParser();
InstrumentTool instrumentTool = new InstrumentTool();
midiParser.addParserListener(instrumentTool);
midiParser.parse(MidiSystem.getSequence(new File("filename")));
List<String> instrumentNames = instrumentTool.getInstrumentNames();
for (String name : instrumentNames) {
    System.out.println(name);
}

JFugue 5中有一个新类允许您将ParserListeners链接在一起。这将允许您创建一个侦听器链,每个侦听器在将事件发送到链中的下一个侦听器之前修改模式。例如,假设您有一个模式,并且您想要转换所有乐器(例如,将GUITAR更改为PIANO);那么你想拍任何与PIANO一起玩的音符并将它的持续时间延长两倍;然后你想要一个新的持续时间大于2.0(两个完整音符)的任何音符,你想要改变它的八度。这是一个疯狂的例子,但它表明需要一个“链接”系列的解析器监听器。

这是一个使用链接的演示示例。这个类读取MIDI模式;然后它会改变所有乐器,然后从原始MIDI创建一个Staccato模式。

public class ChainingParserListenerDemo {
    public static void main(String[] args) throws InvalidMidiDataException, IOException {
        MidiParser parser = new MidiParser();
        InstrumentChangingParserListener instrumentChanger = new InstrumentChangingParserListener();
        StaccatoParserListener staccatoListener = new StaccatoParserListener();
        instrumentChanger.addParserListener(staccatoListener);
        parser.addParserListener(instrumentChanger);
        parser.parse(MidiSystem.getSequence(new File("filename")));
        System.out.println("Changed "+instrumentChanger.counter+" Piano's to Guitar! "+ staccatoListener.getPattern().toString());
    }
}

class InstrumentChangingParserListener extends ChainingParserListenerAdapter {
    int counter = 0;

    @Override 
    public void onInstrumentParsed(byte instrument) {
        if (instrument == MidiDictionary.INSTRUMENT_STRING_TO_BYTE.get("PIANO")) {
            instrument = MidiDictionary.INSTRUMENT_STRING_TO_BYTE.get("GUITAR");
            counter++;
        }
        super.onInstrumentParsed(instrument);
    }
}