Ejemplo n.º 1
1
  /**
   * @param filename the
   * @param is
   * @return
   */
  AudioInputStream getAudioInputStream(String filename) {
    AudioInputStream ais = null;
    BufferedInputStream bis = null;
    if (filename.startsWith("http")) {
      try {
        ais = getAudioInputStream(new URL(filename));
      } catch (MalformedURLException e) {
        error("Bad URL: " + e.getMessage());
      } catch (UnsupportedAudioFileException e) {
        error("URL is in an unsupported audio file format: " + e.getMessage());
      } catch (IOException e) {
        Sound.error("Error reading the URL: " + e.getMessage());
      }
    } else {
      try {
        // InputStream is = createInput(filename);
        InputStream is =
            new FileInputStream(filename) {

              @Override
              public int read() throws IOException {
                // TODO Auto-generated method stub
                return 0;
              }
            };

        debug("Base input stream is: " + is.toString());
        bis = new BufferedInputStream(is);
        ais = getAudioInputStream(bis);
        // don't mark it like this because it means the entire
        // file will be loaded into memory as it plays. this
        // will cause out-of-memory problems with very large files.
        // ais.mark((int)ais.available());
        debug(
            "Acquired AudioInputStream.\n"
                + "It is "
                + ais.getFrameLength()
                + " frames long.\n"
                + "Marking support: "
                + ais.markSupported());
      } catch (IOException ioe) {
        error("IOException: " + ioe.getMessage());
      } catch (UnsupportedAudioFileException uafe) {
        error("Unsupported Audio File: " + uafe.getMessage());
      }
    }
    return ais;
  }
Ejemplo n.º 2
0
 private Map<String, Object> getID3Tags(String filename) {
   debug("Getting the properties.");
   Map<String, Object> props = new HashMap<String, Object>();
   try {
     MpegAudioFileReader reader = new MpegAudioFileReader(this);
     // InputStream stream = createInput(filename);
     InputStream stream = new FileInputStream(filename);
     AudioFileFormat baseFileFormat = reader.getAudioFileFormat(stream, stream.available());
     stream.close();
     if (baseFileFormat instanceof TAudioFileFormat) {
       TAudioFileFormat fileFormat = (TAudioFileFormat) baseFileFormat;
       props = fileFormat.properties();
       if (props.size() == 0) {
         error("No file properties available for " + filename + ".");
       } else {
         debug("File properties: " + props.toString());
       }
     }
   } catch (UnsupportedAudioFileException e) {
     error("Couldn't get the file format for " + filename + ": " + e.getMessage());
   } catch (IOException e) {
     error("Couldn't access " + filename + ": " + e.getMessage());
   }
   return props;
 }
Ejemplo n.º 3
0
 public AudioRecordingStream getAudioRecordingStream(String filename, int bufferSize) {
   AudioRecordingStream mstream = null;
   if (getAudioInputStream(filename) == null)
     debug("Reading from " + getAudioInputStream(filename).getClass().toString());
   if (getAudioInputStream(filename) != null) {
     debug("File format is: " + getAudioInputStream(filename).getFormat().toString());
     AudioFormat format = getAudioInputStream(filename).getFormat();
     // special handling for mp3 files because
     // they need to be converted to PCM
     if (format instanceof MpegAudioFormat) {
       AudioFormat baseFormat = format;
       format =
           new AudioFormat(
               AudioFormat.Encoding.PCM_SIGNED,
               baseFormat.getSampleRate(),
               16,
               baseFormat.getChannels(),
               baseFormat.getChannels() * 2,
               baseFormat.getSampleRate(),
               false);
       // converts the stream to PCM audio from mp3 audio
       AudioInputStream decAis = getAudioInputStream(format, getAudioInputStream(filename));
       // source data line is for sending the file audio out to the
       // speakers
       SourceDataLine line = getSourceDataLine(format, bufferSize);
       if (decAis != null && line != null) {
         Map<String, Object> props = getID3Tags(filename);
         long lengthInMillis = -1;
         if (props.containsKey("duration")) {
           Long dur = (Long) props.get("duration");
           if (dur.longValue() > 0) {
             lengthInMillis = dur.longValue() / 1000;
           }
         }
         MP3MetaData meta = new MP3MetaData(filename, lengthInMillis, props);
         mstream =
             new JSMPEGAudioRecordingStream(
                 this, meta, getAudioInputStream(filename), decAis, line, bufferSize);
       }
     } // format instanceof MpegAudioFormat
     else {
       // source data line is for sending the file audio out to the
       // speakers
       SourceDataLine line = getSourceDataLine(format, bufferSize);
       if (line != null) {
         long length =
             AudioUtils.frames2Millis(getAudioInputStream(filename).getFrameLength(), format);
         BasicMetaData meta = new BasicMetaData(filename, length);
         mstream =
             new JSPCMAudioRecordingStream(
                 this, meta, getAudioInputStream(filename), line, bufferSize);
       }
     } // else
   } // ais != null
   return mstream;
 }
Ejemplo n.º 4
0
 TargetDataLine getTargetDataLine(AudioFormat format, int bufferSize) {
   TargetDataLine line = null;
   DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
   if (AudioSystem.isLineSupported(info)) {
     try {
       if (inputMixer == null) {
         line = (TargetDataLine) AudioSystem.getLine(info);
       } else {
         line = (TargetDataLine) inputMixer.getLine(info);
       }
       line.open(format, bufferSize * format.getFrameSize());
       debug(
           "TargetDataLine buffer size is "
               + line.getBufferSize()
               + "\n"
               + "TargetDataLine format is "
               + line.getFormat().toString()
               + "\n"
               + "TargetDataLine info is "
               + line.getLineInfo().toString());
     } catch (Exception e) {
       error("Error acquiring TargetDataLine: " + e.getMessage());
     }
   } else {
     error("Unable to return a TargetDataLine: unsupported format - " + format.toString());
   }
   return line;
 }
Ejemplo n.º 5
0
 SourceDataLine getSourceDataLine(AudioFormat format, int bufferSize) {
   SourceDataLine line = null;
   DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
   if (AudioSystem.isLineSupported(info)) {
     try {
       if (outputMixer == null) {
         line = (SourceDataLine) AudioSystem.getLine(info);
       } else {
         line = (SourceDataLine) outputMixer.getLine(info);
       }
       // remember that time you spent, like, an entire afternoon fussing
       // with this buffer size to try to get the latency decent on Linux?
       // Yah, don't fuss with this anymore, ok?
       line.open(format, bufferSize * format.getFrameSize() * 4);
       if (line.isOpen()) {
         debug(
             "SourceDataLine is "
                 + line.getClass().toString()
                 + "\n"
                 + "Buffer size is "
                 + line.getBufferSize()
                 + " bytes.\n"
                 + "Format is "
                 + line.getFormat().toString()
                 + ".");
         return line;
       }
     } catch (LineUnavailableException e) {
       error("Couldn't open the line: " + e.getMessage());
     }
   }
   error("Unable to return a SourceDataLine: unsupported format - " + format.toString());
   return line;
 }
Ejemplo n.º 6
0
 public SampleRecorder getSampleRecorder(Recordable source, String fileName, boolean buffered) {
   String ext = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
   debug("createRecorder: file extension is " + ext + ".");
   AudioFileFormat.Type fileType = null;
   if (ext.equals(Sound.WAV.getExtension())) {
     fileType = Sound.WAV;
   } else if (ext.equals(Sound.AIFF.getExtension()) || ext.equals("aif")) {
     fileType = Sound.AIFF;
   } else if (ext.equals(Sound.AIFC.getExtension())) {
     fileType = Sound.AIFC;
   } else if (ext.equals(Sound.AU.getExtension())) {
     fileType = Sound.AU;
   } else if (ext.equals(Sound.SND.getExtension())) {
     fileType = Sound.SND;
   } else {
     error("The extension " + ext + " is not a recognized audio file type.");
     return null;
   }
   SampleRecorder recorder = null;
   if (buffered) {
     recorder =
         new JSBufferedSampleRecorder(
             this, sketchPath(fileName), fileType, source.getFormat(), source.bufferSize());
   } else {
     recorder =
         new JSStreamingSampleRecorder(
             this, sketchPath(fileName), fileType, source.getFormat(), source.bufferSize());
   }
   return recorder;
 }
Ejemplo n.º 7
0
 /**
  * This method is a replacement for AudioSystem.getAudioInputStream(InputStream), which includes
  * workaround for getting an mp3 AudioInputStream when sketch is running in an applet. The
  * workaround was developed by the Tritonus team and originally comes from the package
  * javazoom.jlgui.basicplayer
  *
  * @param is The stream to convert to an AudioInputStream
  * @return an AudioInputStream that will read from is
  * @throws UnsupportedAudioFileException
  * @throws IOException
  */
 AudioInputStream getAudioInputStream(InputStream is)
     throws UnsupportedAudioFileException, IOException {
   try {
     return AudioSystem.getAudioInputStream(is);
   } catch (Exception iae) {
     debug("Using AppletMpegSPIWorkaround to get codec");
     return new MpegAudioFileReader(this).getAudioInputStream(is);
   }
 }
Ejemplo n.º 8
0
 /**
  * This method is a replacement for AudioSystem.getAudioInputStream(AudioFormat,
  * AudioInputStream), which is used for audio format conversion at the stream level. This method
  * includes a workaround for converting from an mp3 AudioInputStream when the sketch is running in
  * an applet. The workaround was developed by the Tritonus team and originally comes from the
  * package javazoom.jlgui.basicplayer
  *
  * @param targetFormat the AudioFormat to convert the stream to
  * @param sourceStream the stream containing the unconverted audio
  * @return an AudioInputStream in the target format
  */
 AudioInputStream getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream) {
   try {
     return AudioSystem.getAudioInputStream(targetFormat, sourceStream);
   } catch (IllegalArgumentException iae) {
     debug("Using AppletMpegSPIWorkaround to get codec");
     try {
       Class.forName("javazoom.spi.mpeg.sampled.convert.MpegFormatConversionProvider");
       return new javazoom.spi.mpeg.sampled.convert.MpegFormatConversionProvider()
           .getAudioInputStream(targetFormat, sourceStream);
     } catch (ClassNotFoundException cnfe) {
       throw new IllegalArgumentException("Mpeg codec not properly installed");
     }
   }
 }
Ejemplo n.º 9
0
 private byte[] loadByteAudio(AudioInputStream ais, int toRead) {
   int totalRead = 0;
   byte[] rawBytes = new byte[toRead];
   try {
     // we have to read in chunks because the decoded stream won't
     // read more than about 2000 bytes at a time
     while (totalRead < toRead) {
       int actualRead = ais.read(rawBytes, totalRead, toRead - totalRead);
       if (actualRead < 1) break;
       totalRead += actualRead;
     }
     ais.close();
   } catch (Exception ioe) {
     error("Error loading file into memory: " + ioe.getMessage());
   }
   debug("Needed to read " + toRead + " actually read " + totalRead);
   return rawBytes;
 }
Ejemplo n.º 10
0
 private FloatSampleBuffer loadFloatAudio(AudioInputStream ais, int toRead) {
   FloatSampleBuffer samples = new FloatSampleBuffer();
   int totalRead = 0;
   byte[] rawBytes = new byte[toRead];
   try {
     // we have to read in chunks because the decoded stream won't
     // read more than about 2000 bytes at a time
     while (totalRead < toRead) {
       int actualRead = ais.read(rawBytes, totalRead, toRead - totalRead);
       if (actualRead < 1) {
         break;
       }
       totalRead += actualRead;
     }
     ais.close();
   } catch (Exception ioe) {
     error("Error loading file into memory: " + ioe.getMessage());
   }
   debug("Needed to read " + toRead + " actually read " + totalRead);
   samples.initFromByteArray(rawBytes, 0, totalRead, ais.getFormat());
   return samples;
 }