1

我想使用 jFugue 在小程序中播放一些 MIDI 音乐。MIDI 模式有一个类——Pattern加载模式的唯一方法是从文件中加载。现在,我不知道小程序如何加载文件以及不知道什么,但我正在使用一个框架(PulpCore),它使加载资产成为一项简单的任务。如果我需要从 ZIP 目录中获取资产,我可以使用Assets提供方法get()的类getAsStream()get()将给定资产作为 a 返回ByteArray,另一个作为a 返回InputStream

我需要 jFugue 从ByteArray或加载模式InputStream。在伪代码中,我想这样做:

Pattern.load(new File(Assets.get("mymidifile.midi")));

但是,没有可以采用 ByteArray 的 File 构造函数。请给点建议好吗?

4

5 回答 5

2

确实,jFugue 不允许加载文件以外的任何内容,这很遗憾,因为没有任何东西可以阻止使用任何其他类型的流:

public static final String TITLE = "Title";

public static Pattern loadPattern(File file) throws IOException {
    InputStream in = new FileInputStream(file);
    try {
        return loadPattern(in);
    } finally {
        in.close();
    }
}

public static Pattern loadPattern(URL url) throws IOException {
    InputStream in = url.openStream();
    try {
        return loadPattern(in);
    } finally {
        in.close();
    }
}

public static Pattern loadPattern(InputStream in) throws IOException {
    return loadPattern(new InputStreamReader(in, "UTF-8")); // or ISO-8859-1 ?
}

public static Pattern loadPattern(Reader reader) throws IOException {
    if (reader instanceof BufferedReader) {
        return loadPattern(reader);
    } else {
        return loadPattern(new BufferedReader(reader));
    }
}

public static Pattern loadPattern(BufferedReader bread) throws IOException {
    StringBuffer buffy = new StringBuffer();

    Pattern pattern = new Pattern();
    while (bread.ready()) {
        String s = bread.readLine();
        if ((s != null) && (s.length() > 1)) {
            if (s.charAt(0) != '#') {
                buffy.append(" ");
                buffy.append(s);
            } else {
                String key = s.substring(1, s.indexOf(':')).trim();
                String value = s.substring(s.indexOf(':')+1, s.length()).trim();
                if (key.equalsIgnoreCase(TITLE)) {
                    pattern.setTitle(value);
                } else {
                    pattern.setProperty(key, value);
                }
            }
        }
    }
    return pattern;
}

更新(对于 loadMidi)

public static Pattern loadMidi(InputStream in) throws IOException, InvalidMidiDataException
{
    MidiParser parser = new MidiParser();
    MusicStringRenderer renderer = new MusicStringRenderer();
    parser.addParserListener(renderer);
    parser.parse(MidiSystem.getSequence(in));
    Pattern pattern = new Pattern(renderer.getPattern().getMusicString());
    return pattern;
}

public static Pattern loadMidi(URL url) throws IOException, InvalidMidiDataException
{
    MidiParser parser = new MidiParser();
    MusicStringRenderer renderer = new MusicStringRenderer();
    parser.addParserListener(renderer);
    parser.parse(MidiSystem.getSequence(url));
    Pattern pattern = new Pattern(renderer.getPattern().getMusicString());
    return pattern;
}
于 2010-04-24T11:21:23.943 回答
1

如果我没记错的话,模式文件包含纯文本。使用 getAsStream() 加载文件,然后使用

BufferedReader br = new BufferedReader(new InputStreamReader(yourStream));
//...
String pattern = convertToString(br); // you should implement convertToString yourself. It's easy. Read the java.io APIs.

其中 yourStream 是 getAsStream() 返回的 InputStream。然后使用 add(String... patterns) 方法加载模式:

add(pattern);
于 2010-04-24T11:07:09.603 回答
1

您可以使用此代码(取自Pattern.loadPattern()方法的实现):

    InputStream is = ...; // Get a stream from the Asset object

    // Prepare a pattern object
    Pattern pattern = new Pattern();

    // Now start reaing from the stream
    StringBuffer buffy = new StringBuffer();
    BufferedReader bread = new BufferedReader(new InputStreamReader(is));
    while (bread.ready()) {
        String s = bread.readLine();
        if ((s != null) && (s.length() > 1)) {
            if (s.charAt(0) != '#') {
                buffy.append(" ");
                buffy.append(s);
            } else {
                String key = s.substring(1, s.indexOf(':')).trim();
                String value = s.substring(s.indexOf(':')+1, s.length()).trim();
                if (key.equalsIgnoreCase(TITLE)) {
                    pattern.setTitle(value);
                } else {
                    pattern.setProperty(key, value);
                }
            }
        }
    }
    bread.close();
    pattern.setMusicString(buffy.toString());

    // Your pattern is now ready
于 2010-04-24T11:12:42.297 回答
-1

您可以读取字节数组并将其转换为字符串。

问题将是 InputStream。有一个 StringBufferInputStream,但它已被弃用,取而代之的是 StringReader。

byte [] b = Assets.get();
InputStream is = new StringBufferInputStream(new String(b));
Pattern.load(is);
于 2010-04-24T10:54:37.113 回答
-2

你不想用File,​​你想用java.io.ByteArrayInputStream

于 2010-04-24T11:06:13.997 回答