예제 #1
0
  /** Open an audio channel. */
  public synchronized void openChannel(InputStream in) {

    if (DEBUG) {
      System.out.println("AudioDevice: openChannel");
      System.out.println("input stream =" + in);
    }

    Info info = null;

    // is this already playing?  if so, then just return
    for (int i = 0; i < infos.size(); i++) {
      info = (AudioDevice.Info) infos.elementAt(i);
      if (info.in == in) {

        return;
      }
    }

    AudioInputStream as = null;

    if (in instanceof AudioStream) {

      if (((AudioStream) in).midiformat != null) {

        // it's a midi file
        try {
          startMidi(((AudioStream) in).stream, in);
        } catch (Exception e) {
          return;
        }

      } else if (((AudioStream) in).ais != null) {

        // it's sampled audio
        try {
          startSampled(((AudioStream) in).ais, in);
        } catch (Exception e) {
          return;
        }
      }
    } else if (in instanceof AudioDataStream) {
      if (in instanceof ContinuousAudioDataStream) {
        try {
          AudioInputStream ais =
              new AudioInputStream(
                  in, ((AudioDataStream) in).getAudioData().format, AudioSystem.NOT_SPECIFIED);
          startSampled(ais, in);
        } catch (Exception e) {
          return;
        }
      } else {
        try {
          AudioInputStream ais =
              new AudioInputStream(
                  in,
                  ((AudioDataStream) in).getAudioData().format,
                  ((AudioDataStream) in).getAudioData().buffer.length);
          startSampled(ais, in);
        } catch (Exception e) {
          return;
        }
      }
    } else {
      BufferedInputStream bis = new BufferedInputStream(in, 1024);

      try {

        try {
          as = AudioSystem.getAudioInputStream(bis);
        } catch (IOException ioe) {
          return;
        }

        startSampled(as, in);

      } catch (UnsupportedAudioFileException e) {

        try {
          try {
            MidiFileFormat mff = MidiSystem.getMidiFileFormat(bis);
          } catch (IOException ioe1) {
            return;
          }

          startMidi(bis, in);

        } catch (InvalidMidiDataException e1) {

          // $$jb:08.01.99: adding this section to make some of our other
          // legacy classes work.....
          // not MIDI either, special case handling for all others

          AudioFormat defformat =
              new AudioFormat(AudioFormat.Encoding.ULAW, 8000, 8, 1, 1, 8000, true);
          try {
            AudioInputStream defaif =
                new AudioInputStream(bis, defformat, AudioSystem.NOT_SPECIFIED);
            startSampled(defaif, in);
          } catch (UnsupportedAudioFileException es) {
            return;
          } catch (LineUnavailableException es2) {
            return;
          }

        } catch (MidiUnavailableException e2) {

          // could not open sequence
          return;
        }

      } catch (LineUnavailableException e) {

        return;
      }
    }

    // don't forget adjust for a new stream.
    notify();
  }
예제 #2
0
 /**
  * Parses a MIDI file and returns a Pattern. This is an excellent example of JFugue's
  * Parser-Renderer architecture:
  *
  * <pre>
  * MidiParser parser = new MidiParser();
  * MusicStringRenderer renderer = new MusicStringRenderer();
  * parser.addParserListener(renderer);
  * parser.parse(sequence);
  * </pre>
  *
  * @param filename The name of the MIDI file
  * @return a Pattern containing the MusicString representing the MIDI music
  * @throws IOException If there is a problem opening the MIDI file
  * @throws InvalidMidiDataException If there is a problem obtaining MIDI resources
  */
 public Pattern loadMidi(File file) throws IOException, InvalidMidiDataException {
   MidiFileFormat format = MidiSystem.getMidiFileFormat(file);
   this.sequenceTiming = format.getDivisionType();
   this.resolution = format.getResolution();
   return Pattern.loadMidi(file);
 }