2

I wanted to create a midi file which consist of these notes. I took this code from internet. If i can find out the problem to this i can go further. When i run this application i only get an error call "java result 1" error. What i need is after i run this there there should be a midi file generated which consist of the notes which i have passed. The complete code is available.

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);
  }
}

This is the output

   usage:
   java CreateSequence <midifile>
   Java Result: 1
M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
user3805160
  • 87
  • 2
  • 11
  • 1
    Paste the exact output that you get when you run it. – chiastic-security Oct 30 '14 at 15:57
  • 1
    There's no such thing as a "Java result 1" error. The only thing this can mean in your context is that the application is terminating with a status code of 1, which means it's hitting one of the `System.exit(1)` lines of your code. But without further information, it's impossible to say why that would be happening. – chiastic-security Oct 30 '14 at 15:58
  • I'm pretty sure that it want hit any `System.exit(1)` – user3805160 Oct 31 '14 at 04:43

2 Answers2

1

You need to pass the output file name as an arg. It's exiting with Code 1 because it calls printUsafeAndExit() if you don't run it with the correct arg count. Try running it as:

java CreateSequence output.mid

If you have trouble passing args (can be confusing for beginners if you're using an IDE like Eclipse), just dele this check:

if (args.length != 1) {
    printUsageAndExit();
}

And hardcode the output file:

File outputFile = new File("output.mid");
alexroussos
  • 2,671
  • 1
  • 25
  • 38
0

How are you invoking this Java program? The code expects one argument to be passed at the command line. Looking at the output it looks like the method

private static void printUsageAndExit()
{
    out("usage:");
    out("java CreateSequence <midifile>");
    System.exit(1);
}

is getting invoked. You need to pass (i believe) the file name to the program when you run it

Saifuddin Merchant
  • 1,071
  • 7
  • 13