public static int encodeAudioInputStream(AudioInputStream sin, int maxRead,
      FLACEncoder flac, boolean useThreads) throws IOException, IllegalArgumentException {
    AudioFormat format = sin.getFormat();
    int frameSize = format.getFrameSize();
    int sampleSize = format.getSampleSizeInBits();
    int bytesPerSample = sampleSize/8;
    if(sampleSize %8 != 0) {
      //end processing now
      throw new IllegalArgumentException("Unsupported Sample Size: size = "+sampleSize);
    }
    int channels = format.getChannels();
    boolean bigEndian = format.isBigEndian();
    boolean isSigned = format.getEncoding() == AudioFormat.Encoding.PCM_SIGNED;
    byte[] samplesIn = new byte[(int)maxRead];
    int samplesRead;
    int framesRead;
    int[] sampleData = new int[maxRead*channels/frameSize];
    int unencodedSamples = 0;
    int totalSamples = 0;
    while((samplesRead = sin.read(samplesIn, 0, maxRead)) > 0 ) {
      framesRead = samplesRead/(frameSize);
      if(bigEndian) {
        for(int i = 0; i < framesRead*channels; i++) {
          int lower8Mask = 255;
          int temp = 0;
          int totalTemp = 0;
          for(int x = bytesPerSample-1; x >= 0; x++) {
            int upShift = 8*x;
            if(x == 0)//don't mask...we want sign
              temp = ((samplesIn[bytesPerSample*i+x]) << upShift);
            else
              temp = ((samplesIn[bytesPerSample*i+x] & lower8Mask) << upShift);
            totalTemp = totalTemp | temp;
          }
          if(!isSigned) {
            int reducer = 1 << (bytesPerSample*8-1);
            totalTemp -= reducer;
          }
          sampleData[i] = totalTemp;
        }
      }
      else {
        for(int i = 0; i < framesRead*channels; i++) {
          int lower8Mask = 255;
          int temp = 0;
          int totalTemp = 0;
          for(int x = 0; x < bytesPerSample; x++) {
            int upShift = 8*x;
            if(x == bytesPerSample-1 && isSigned)//don't mask...we want sign
              temp = ((samplesIn[bytesPerSample*i+x]) << upShift);
            else
              temp = ((samplesIn[bytesPerSample*i+x] & lower8Mask) << upShift);
            totalTemp = totalTemp | temp;
          }
          if(!isSigned) {
            int reducer = 1 << (bytesPerSample*8-1);
            totalTemp -= reducer;
          }
          sampleData[i] = totalTemp;
        }
      }
      if(framesRead > 0) {
        flac.addSamples(sampleData, framesRead);
        unencodedSamples += framesRead;
      }

      if(useThreads)
        unencodedSamples -= flac.t_encodeSamples(unencodedSamples, false, 5);
      else
        unencodedSamples -= flac.encodeSamples(unencodedSamples, false);
      totalSamples += unencodedSamples;
    }
    totalSamples += unencodedSamples;
    if(useThreads)
      unencodedSamples -= flac.t_encodeSamples(unencodedSamples, true, 5);
    else
      unencodedSamples -= flac.encodeSamples(unencodedSamples, true);
    return totalSamples;
  }
public static int encodeAudioInputStream ( AudioInputStream sin , int maxRead , FLACEncoder flac , boolean useThreads ) throws IOException , IllegalArgumentException { AudioFormat format = sin . getFormat ( ) ; int frameSize = format . getFrameSize ( ) ; int sampleSize = format . getSampleSizeInBits ( ) ; int bytesPerSample = sampleSize / 8 ; if ( sampleSize % 8 != 0 ) { throw new IllegalArgumentException ( "Unsupported Sample Size: size = " + sampleSize ) ; } int channels = format . getChannels ( ) ; boolean bigEndian = format . isBigEndian ( ) ; boolean isSigned = format . getEncoding ( ) == AudioFormat . Encoding . PCM_SIGNED ; byte [ ] samplesIn = new byte [ ( int ) maxRead ] ; int samplesRead ; int framesRead ; int [ ] sampleData = new int [ maxRead * channels / frameSize ] ; int unencodedSamples = 0 ; int totalSamples = 0 ; while ( ( samplesRead = sin . read ( samplesIn , 0 , maxRead ) ) > 0 ) { framesRead = samplesRead / ( frameSize ) ; if ( bigEndian ) { for ( int i = 0 ; i < framesRead * channels ; i ++ ) { int lower8Mask = 255 ; int temp = 0 ; int totalTemp = 0 ; for ( int x = bytesPerSample - 1 ; x >= 0 ; x ++ ) { int upShift = 8 * x ; if ( x == 0 ) temp = ( ( samplesIn [ bytesPerSample * i + x ] ) << upShift ) ; else temp = ( ( samplesIn [ bytesPerSample * i + x ] & lower8Mask ) << upShift ) ; totalTemp = totalTemp | temp ; } if ( ! isSigned ) { int reducer = 1 << ( bytesPerSample * 8 - 1 ) ; totalTemp -= reducer ; } sampleData [ i ] = totalTemp ; } } else { for ( int i = 0 ; i < framesRead * channels ; i ++ ) { int lower8Mask = 255 ; int temp = 0 ; int totalTemp = 0 ; for ( int x = 0 ; x < bytesPerSample ; x ++ ) { int upShift = 8 * x ; if ( x == bytesPerSample - 1 && isSigned ) temp = ( ( samplesIn [ bytesPerSample * i + x ] ) << upShift ) ; else temp = ( ( samplesIn [ bytesPerSample * i + x ] & lower8Mask ) << upShift ) ; totalTemp = totalTemp | temp ; } if ( ! isSigned ) { int reducer = 1 << ( bytesPerSample * 8 - 1 ) ; totalTemp -= reducer ; } sampleData [ i ] = totalTemp ; } } if ( framesRead > 0 ) { flac . addSamples ( sampleData , framesRead ) ; unencodedSamples += framesRead ; } if ( useThreads ) unencodedSamples -= flac . t_encodeSamples ( unencodedSamples , false , 5 ) ; else unencodedSamples -= flac . encodeSamples ( unencodedSamples , false ) ; totalSamples += unencodedSamples ; } totalSamples += unencodedSamples ; if ( useThreads ) unencodedSamples -= flac . t_encodeSamples ( unencodedSamples , true , 5 ) ; else unencodedSamples -= flac . encodeSamples ( unencodedSamples , true ) ; return totalSamples ; }
Encodes the given AudioInputStream, using the given FLACEncoder.
FLACEncoder must be in a state to accept samples and encode(FLACOutputStream,
EncodingConfiguration, and StreamConfiguration have been set, and FLAC stream
has been opened).
@param sin
@return
@throws IOException
@throws IllegalArgumentException thrown if input sample size is not supported