コード例 #1
1
ファイル: MidiPlayer.java プロジェクト: ramp818/LightOuts2
 /**
  * Loads a sequence from an input stream. Returns null if an error occurs.
  *
  * @param is
  * @return
  */
 public Sequence getSequence(InputStream is) {
   try {
     if (!is.markSupported()) {
       is = new BufferedInputStream(is);
     }
     Sequence s = MidiSystem.getSequence(is);
     is.close();
     return s;
   } catch (InvalidMidiDataException ex) {
     ex.printStackTrace();
     return null;
   } catch (IOException ex) {
     ex.printStackTrace();
     return null;
   }
 }
コード例 #2
0
ファイル: Sequencer.java プロジェクト: rdvonz/SeqLife
  public Sequencer(int instrument, int tempo) {
    // Set up initial settings for the sequencer
    this.instrument = instrument;
    this.tempo = tempo;
    Synthesizer synth;
    ticks = 0;
    velocity = 64; // Mid volume

    try {
      // Setup values to create sequencer
      sequence = new Sequence(Sequence.PPQ, 16);
      sequencer = MidiSystem.getSequencer();
      sequencer.open();
      synth = MidiSystem.getSynthesizer();
      synth.open();
      sequencer.getTransmitter().setReceiver(synth.getReceiver());
      sequencer.setTempoInBPM(tempo);
      track = sequence.createTrack();

    } catch (InvalidMidiDataException e) {
      e.printStackTrace();
    } catch (MidiUnavailableException e) {
      e.printStackTrace();
    }
  }
コード例 #3
0
ファイル: Sequencer.java プロジェクト: rdvonz/SeqLife
  public void playSequence() {

    // Check if sequencer is stopped
    sequencer.addMetaEventListener(
        new MetaEventListener() {
          public void meta(MetaMessage m) {
            // A message of this type is automatically sent
            // when we reach the end of the track
            if (m.getType() == 47) {
              sequencer.setTempoInBPM(tempo);
              // start  the song at the last position
              sequencerFrame.doa = sequencerFrame.conway.nextStep();
              parseSequence(sequencerFrame.doa);
              sequencerFrame.refresh();

              sequencer.start();
            }
          }
        });
    // first start
    try {
      sequencer.setSequence(sequence);
      sequencer.start();

    } catch (InvalidMidiDataException e) {
      e.printStackTrace();
    }
  }
コード例 #4
0
ファイル: MidiExporter.java プロジェクト: jinwoopang/Fugue
 /**
  * Constructor. It breaks a Song into its MidiEvent components, and adds them together in order to
  * create a MIDI file.
  *
  * @param song The song to export
  */
 public MidiExporter() {
   // Create the Sequence that will contain everything
   try {
     sequence = new Sequence(Sequence.PPQ, 2);
   } catch (InvalidMidiDataException ex) {
     ex.printStackTrace();
     System.exit(1);
   }
 }
コード例 #5
0
ファイル: MidiPlayer.java プロジェクト: ramp818/LightOuts2
 /**
  * Plays a sequence, optionally looping. This method returns immediately. The sequence is not
  * played if it is invalid.
  *
  * @param sequence
  * @param loop
  */
 public void play(Sequence sequence, boolean loop) {
   if (sequencer != null && sequence != null && sequencer.isOpen()) {
     try {
       sequencer.setSequence(sequence);
       sequencer.start();
       this.loop = loop;
     } catch (InvalidMidiDataException ex) {
       ex.printStackTrace();
     }
   }
 }
コード例 #6
0
  /** Carrega a seqüência do sistema de arquivos. Retorna null se um erro ocorrer. */
  public Sequence getSequence(String name) {

    String filename = "/recursos/sons/" + name;
    try {
      return MidiSystem.getSequence(getClass().getResource((filename)));
    } catch (InvalidMidiDataException ex) {
      ex.printStackTrace();
      return null;
    } catch (IOException ex) {
      ex.printStackTrace();
      return null;
    }
  }
コード例 #7
0
ファイル: MidiExporter.java プロジェクト: jinwoopang/Fugue
  /**
   * Create the MidiEvent for a note, given the data.
   *
   * @param command The command value for the ShortMessage
   * @param note The MIDI value for the note to be played
   * @param eventTime When this event should occur
   * @param velocity The velocity of this note
   * @return The MidiEvent for the note
   */
  private MidiEvent createNoteEvent(int command, int note, int eventTime, int velocity) {
    // Create the message and set its parameters to the ones given.
    ShortMessage message = new ShortMessage();
    try {
      message.setMessage(command, 0, note, velocity);
    } catch (InvalidMidiDataException ex) {
      // Something went wrong.
      ex.printStackTrace();
      System.exit(1);
    }

    // Create the MidiEvent and return it.
    return new MidiEvent(message, eventTime);
  }
コード例 #8
0
ファイル: Sequencer.java プロジェクト: rdvonz/SeqLife
  public void parseSequence(boolean[][] grid) {
    ShortMessage on;
    this.grid = grid;
    ShortMessage off;
    int tickLength = 16;
    try {
      ShortMessage sm = new ShortMessage();
      sm.setMessage(ShortMessage.PROGRAM_CHANGE, 0, instrument, 0);
      track.add(new MidiEvent(sm, 0));
      for (boolean[] aGrid : grid) {
        for (int col = 0; col < aGrid.length; col++) {
          if (aGrid[col]) {
            if (other == 1) {
              other = 0;
            } else {
              other = 1;
            }
            off = new ShortMessage();
            off.setMessage(
                ShortMessage.NOTE_OFF,
                0,
                scale[(grid[0].length - col - 1 + other) % scale.length],
                velocity);
            on = new ShortMessage();
            on.setMessage(
                ShortMessage.NOTE_ON,
                0,
                scale[(grid[0].length - col - 1 + other) % scale.length],
                velocity);
            track.add(new MidiEvent(on, ticks));
            track.add(new MidiEvent(off, ticks + tickLength));
          }
        }
        ticks += tickLength;
      }

    } catch (InvalidMidiDataException e) {
      e.printStackTrace();
    }
  }
コード例 #9
0
  private InputStream getFileStream(int type, Sequence sequence) throws IOException {
    Track tracks[] = sequence.getTracks();
    int bytesBuilt = 0;
    int headerLength = 14;
    int length = 0;
    int timeFormat;
    float divtype;

    PipedOutputStream hpos = null;
    DataOutputStream hdos = null;
    PipedInputStream headerStream = null;

    InputStream trackStreams[] = null;
    InputStream trackStream = null;
    InputStream fStream = null;

    // Determine the filetype to write
    if (type == MIDI_TYPE_0) {
      if (tracks.length != 1) {
        return null;
      }
    } else if (type == MIDI_TYPE_1) {
      if (tracks.length < 1) { // $$jb: 05.31.99: we _can_ write TYPE_1 if tracks.length==1
        return null;
      }
    } else {
      if (tracks.length == 1) {
        type = MIDI_TYPE_0;
      } else if (tracks.length > 1) {
        type = MIDI_TYPE_1;
      } else {
        return null;
      }
    }

    // Now build the file one track at a time
    // Note that above we made sure that MIDI_TYPE_0 only happens
    // if tracks.length==1

    trackStreams = new InputStream[tracks.length];
    int trackCount = 0;
    for (int i = 0; i < tracks.length; i++) {
      try {
        trackStreams[trackCount] = writeTrack(tracks[i], type);
        trackCount++;
      } catch (InvalidMidiDataException e) {
        System.err.println("Exception in write: " + e.getMessage());
      }
      // bytesBuilt += trackStreams[i].getLength();
    }

    // Now seqence the track streams
    if (trackCount == 1) {
      trackStream = trackStreams[0];
    } else if (trackCount > 1) {
      trackStream = trackStreams[0];
      for (int i = 1; i < tracks.length; i++) {
        // fix for 5048381: NullPointerException when saving a MIDI sequence
        // don't include failed track streams
        if (trackStreams[i] != null) {
          trackStream = new SequenceInputStream(trackStream, trackStreams[i]);
        }
      }
    } else {
      throw new IllegalArgumentException("invalid MIDI data in sequence");
    }

    // Now build the header...
    hpos = new PipedOutputStream();
    hdos = new DataOutputStream(hpos);
    headerStream = new PipedInputStream(hpos);

    // Write the magic number
    hdos.writeInt(MThd_MAGIC);

    // Write the header length
    hdos.writeInt(headerLength - 8);

    // Write the filetype
    if (type == MIDI_TYPE_0) {
      hdos.writeShort(0);
    } else {
      // MIDI_TYPE_1
      hdos.writeShort(1);
    }

    // Write the number of tracks
    hdos.writeShort((short) trackCount);

    // Determine and write the timing format
    divtype = sequence.getDivisionType();
    if (divtype == Sequence.PPQ) {
      timeFormat = sequence.getResolution();
    } else if (divtype == Sequence.SMPTE_24) {
      timeFormat = (24 << 8) * -1;
      timeFormat += (sequence.getResolution() & 0xFF);
    } else if (divtype == Sequence.SMPTE_25) {
      timeFormat = (25 << 8) * -1;
      timeFormat += (sequence.getResolution() & 0xFF);
    } else if (divtype == Sequence.SMPTE_30DROP) {
      timeFormat = (29 << 8) * -1;
      timeFormat += (sequence.getResolution() & 0xFF);
    } else if (divtype == Sequence.SMPTE_30) {
      timeFormat = (30 << 8) * -1;
      timeFormat += (sequence.getResolution() & 0xFF);
    } else {
      // $$jb: 04.08.99: What to really do here?
      return null;
    }
    hdos.writeShort(timeFormat);

    // now construct an InputStream to become the FileStream
    fStream = new SequenceInputStream(headerStream, trackStream);
    hdos.close();

    length = bytesBuilt + headerLength;
    return fStream;
  }