Ejemplo n.º 1
0
 public AudioSample getAudioSample(
     float[] left, float[] right, AudioFormat format, int bufferSize) {
   FloatSampleBuffer sample = new FloatSampleBuffer(2, left.length, format.getSampleRate());
   System.arraycopy(left, 0, sample.getChannel(0), 0, left.length);
   System.arraycopy(right, 0, sample.getChannel(1), 0, right.length);
   return getAudioSampleImp(sample, format, bufferSize);
 }
Ejemplo n.º 2
0
 public AudioSample getAudioSample(String filename, int bufferSize) {
   AudioInputStream ais = getAudioInputStream(filename);
   if (ais != null) {
     AudioMetaData meta = null;
     AudioFormat format = ais.getFormat();
     FloatSampleBuffer samples = null;
     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
       ais = getAudioInputStream(format, ais);
       // get a map of properties so we can find out how long it is
       Map<String, Object> props = getID3Tags(filename);
       // there is a property called mp3.length.bytes, but that is
       // the length in bytes of the mp3 file, which will of course
       // be much shorter than the decoded version. so we use the
       // duration of the file to figure out how many bytes the
       // decoded file will be.
       long dur = ((Long) props.get("duration")).longValue();
       int toRead = (int) AudioUtils.millis2Bytes(dur / 1000, format);
       samples = loadFloatAudio(ais, toRead);
       meta = new MP3MetaData(filename, dur / 1000, props);
     } else {
       samples = loadFloatAudio(ais, (int) ais.getFrameLength() * format.getFrameSize());
       long length = AudioUtils.frames2Millis(samples.getSampleCount(), format);
       meta = new BasicMetaData(filename, length);
     }
     AudioSynthesizer out =
         getAudioSynthesizer(
             format.getChannels(),
             bufferSize,
             format.getSampleRate(),
             format.getSampleSizeInBits());
     if (out != null) {
       SampleSignal ssig = new SampleSignal(samples);
       out.setAudioSignal(ssig);
       return new JSAudioSample(meta, ssig, out);
     } else {
       error("Couldn't acquire an output.");
     }
   }
   return null;
 }
Ejemplo n.º 3
0
  private JSAudioSample getAudioSampleImp(
      FloatSampleBuffer samples, AudioFormat format, int bufferSize) {
    AudioSynthesizer out =
        getAudioSynthesizer(
            samples.getChannelCount(),
            bufferSize,
            format.getSampleRate(),
            format.getSampleSizeInBits());
    if (out != null) {
      SampleSignal ssig = new SampleSignal(samples);
      out.setAudioSignal(ssig);
      long length = AudioUtils.frames2Millis(samples.getSampleCount(), format);
      BasicMetaData meta = new BasicMetaData(samples.toString(), length);
      return new JSAudioSample(meta, ssig, out);
    } else {
      error("Couldn't acquire an output.");
    }

    return null;
  }
Ejemplo n.º 4
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;
 }
Ejemplo n.º 5
0
 public AudioSample getAudioSample(float[] samples, AudioFormat format, int bufferSize) {
   FloatSampleBuffer sample = new FloatSampleBuffer(1, samples.length, format.getSampleRate());
   System.arraycopy(samples, 0, sample.getChannel(0), 0, samples.length);
   return getAudioSampleImp(sample, format, bufferSize);
 }