我想创建一个由这些注释组成的midi文件。我从网上取了这个代码。如果我能找出这个问题,我就能走得更远。当我运行这个应用程序时,我只得到一个错误调用"javaresult1"错误。我需要的是,在我运行这个之后,应该会生成一个由我传递的注释组成的midi文件。完整的代码可用。
public class CreateSequence{
private static final int VELOCITY = 64;
public static void main(String[] args) {
if (args.length != 1) {
printUsageAndExit();
}
File outputFile = new File(args[0]);
Sequence sequence = null;
try {
sequence = new Sequence(Sequence.PPQ, 1);
} catch (InvalidMidiDataException e) {
e.printStackTrace();
System.exit(1);
}
Track track = sequence.createTrack();
// first chord: C major
track.add(createNoteOnEvent(60, 0));
track.add(createNoteOnEvent(64, 0));
track.add(createNoteOnEvent(67, 0));
track.add(createNoteOnEvent(72, 0));
track.add(createNoteOffEvent(60, 1));
track.add(createNoteOffEvent(64, 1));
track.add(createNoteOffEvent(67, 1));
track.add(createNoteOffEvent(72, 1));
// second chord: f minor N
track.add(createNoteOnEvent(53, 1));
track.add(createNoteOnEvent(65, 1));
track.add(createNoteOnEvent(68, 1));
track.add(createNoteOnEvent(73, 1));
track.add(createNoteOffEvent(63, 2));
track.add(createNoteOffEvent(65, 2));
track.add(createNoteOffEvent(68, 2));
track.add(createNoteOffEvent(73, 2));
// third chord: C major 6-4
track.add(createNoteOnEvent(55, 2));
track.add(createNoteOnEvent(64, 2));
track.add(createNoteOnEvent(67, 2));
track.add(createNoteOnEvent(72, 2));
track.add(createNoteOffEvent(64, 3));
track.add(createNoteOffEvent(72, 3));
// forth chord: G major 7
track.add(createNoteOnEvent(65, 3));
track.add(createNoteOnEvent(71, 3));
track.add(createNoteOffEvent(55, 4));
track.add(createNoteOffEvent(65, 4));
track.add(createNoteOffEvent(67, 4));
track.add(createNoteOffEvent(71, 4));
// fifth chord: C major
track.add(createNoteOnEvent(48, 4));
track.add(createNoteOnEvent(64, 4));
track.add(createNoteOnEvent(67, 4));
track.add(createNoteOnEvent(72, 4));
track.add(createNoteOffEvent(48, 8));
track.add(createNoteOffEvent(64, 8));
track.add(createNoteOffEvent(67, 8));
track.add(createNoteOffEvent(72, 8));
//Now we just save the Sequence to the file we specified.
//The '0' (second parameter) means saving as SMF type 0.
//Since we have only one Track, this is actually the only option
//(type 1 is for multiple tracks).
try {
MidiSystem.write(sequence, 0, outputFile);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
private static MidiEvent createNoteOnEvent(int nKey, long lTick) {
return createNoteEvent(ShortMessage.NOTE_ON, nKey, VELOCITY, lTick);
}
private static MidiEvent createNoteOffEvent(int nKey, long lTick) {
return createNoteEvent(ShortMessage.NOTE_OFF, nKey, 0, lTick);
}
private static MidiEvent createNoteEvent(int nCommand, int nKey,
int nVelocity, long lTick) {
ShortMessage message = new ShortMessage();
try {
message.setMessage(nCommand, 0, nKey, nVelocity); // 0 always on channel 1
} catch (InvalidMidiDataException e) {
e.printStackTrace();
System.exit(1);
}
MidiEvent event = new MidiEvent(message, lTick);
return event;
}
private static void printUsageAndExit(){
out("usage:");
out("java CreateSequence <midifile>");
System.exit(1);
}
private static void out(String strMessage) {
System.out.println(strMessage);
}
}
这是输出
usage:
java CreateSequence <midifile>
Java Result: 1
您需要将输出文件名作为arg传递。它将以代码1退出,因为如果您没有使用正确的arg计数运行它,它将调用printUsafeAndExit()。尝试将其运行为:
java CreateSequence output.mid
如果您在传递args时遇到困难(如果您使用Eclipse这样的IDE,初学者可能会感到困惑),只需删除以下检查:
if (args.length != 1) {
printUsageAndExit();
}
并对输出文件进行硬编码:
File outputFile = new File("output.mid");
您是如何调用这个Java程序的?该代码要求在命令行中传递一个参数。从输出来看,它看起来像方法
private static void printUsageAndExit()
{
out("usage:");
out("java CreateSequence <midifile>");
System.exit(1);
}
正在被调用。运行程序时,您需要将文件名传递(我相信)给程序