예제 #1
0
  /** Signals that a PooledThread has started. Creates the Thread's line and buffer. */
  protected void threadStarted() {
    // wait for the SoundManager constructor to finish
    synchronized (this) {
      try {
        wait();
      } catch (InterruptedException ex) {
      }
    }

    // use a short, 100ms (1/10th sec) buffer for filters that
    // change in real-time
    int bufferSize =
        playbackFormat.getFrameSize() * Math.round(playbackFormat.getSampleRate() / 10);

    // create, open, and start the line
    SourceDataLine line;
    DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, playbackFormat);
    try {
      line = (SourceDataLine) AudioSystem.getLine(lineInfo);
      line.open(playbackFormat, bufferSize);
    } catch (LineUnavailableException ex) {
      // the line is unavailable - signal to end this thread
      Thread.currentThread().interrupt();
      return;
    }

    line.start();

    // create the buffer
    byte[] buffer = new byte[bufferSize];

    // set this thread's locals
    localLine.set(line);
    localBuffer.set(buffer);
  }
예제 #2
0
  private void sleepUntil(long startTime, long clock) throws InterruptedException {
    // convert clock to timestamp in nanos

    long targetTime = (NS_PER_CLOCK * clock) + startTime;
    long totalTime = targetTime - System.nanoTime();
    if (totalTime < 0L) {
      return;
    }
    long millis = totalTime / 1000000L;
    long nanos = totalTime % 1000000L;

    Thread.sleep(millis, (int) nanos);
  }
예제 #3
0
  public void play(int instrument, int note) {

    try {

      Sequencer player = MidiSystem.getSequencer();
      player.open();

      Sequence seq = new Sequence(Sequence.PPQ, 4);
      Track track = seq.createTrack();

      // MidiEvent event = null;

      ShortMessage first = new ShortMessage();
      first.setMessage(192, 1, instrument, 0);
      MidiEvent changeInstrument = new MidiEvent(first, 1);
      track.add(changeInstrument);

      ShortMessage a = new ShortMessage();
      a.setMessage(144, 1, note, 100);
      MidiEvent noteOn = new MidiEvent(a, 1);
      track.add(noteOn);

      ShortMessage b = new ShortMessage();
      b.setMessage(128, 1, note, 100);
      MidiEvent noteOff = new MidiEvent(b, 16);
      track.add(noteOff);
      player.setSequence(seq);
      player.start();

      Thread.sleep(2000);
      player.close();
      System.exit(0);

    } catch (Exception ex) {
      System.out.println(ex.getMessage());
      ex.printStackTrace();
    }
  } // close play
예제 #4
0
 public void run() {
   try {
     if (waitfor != null) waitfor.join();
     Resource res = Loading.waitforint(this.res);
     try {
       seq = MidiSystem.getSequencer(false);
       synth = MidiSystem.getSynthesizer();
       seq.open();
       seq.setSequence(res.layer(Resource.Music.class).seq);
       synth.open();
       seq.getTransmitter().setReceiver(synth.getReceiver());
     } catch (MidiUnavailableException e) {
       return;
     } catch (InvalidMidiDataException e) {
       return;
     } catch (IllegalArgumentException e) {
       /* The soft synthesizer appears to be throwing
        * non-checked exceptions through from the sampled
        * audio system. Ignore them and only them. */
       if (e.getMessage().startsWith("No line matching")) return;
       throw (e);
     }
     seq.addMetaEventListener(
         new MetaEventListener() {
           public void meta(MetaMessage msg) {
             debug("Meta " + msg.getType());
             if (msg.getType() == 47) {
               synchronized (Player.this) {
                 done = true;
                 Player.this.notifyAll();
               }
             }
           }
         });
     do {
       debug("Start loop");
       done = false;
       seq.start();
       synchronized (this) {
         while (!done) this.wait();
       }
       seq.setTickPosition(0);
     } while (loop);
   } catch (InterruptedException e) {
   } finally {
     try {
       debug("Exit player");
       if (seq != null) seq.close();
       try {
         if (synth != null) synth.close();
       } catch (Throwable e2) {
         if (e2 instanceof InterruptedException) {
           /* XXX: There appears to be a bug in Sun's
            * software MIDI implementation that throws back
            * an unchecked InterruptedException here when two
            * interrupts come close together (such as in the
            * case when the current player is first stopped,
            * and then another started immediately afterwards
            * on a new song before the first one has had time
            * to terminate entirely). */
         } else {
           throw (new RuntimeException(e2));
         }
       }
     } finally {
       synchronized (Music.class) {
         if (player == this) player = null;
       }
     }
   }
 }