Пример #1
0
    UlawCodecStream(AudioInputStream stream, AudioFormat outputFormat) {
      super(stream, outputFormat, AudioSystem.NOT_SPECIFIED);

      AudioFormat inputFormat = stream.getFormat();

      // throw an IllegalArgumentException if not ok
      if (!(isConversionSupported(outputFormat, inputFormat))) {
        throw new IllegalArgumentException(
            "Unsupported conversion: " + inputFormat.toString() + " to " + outputFormat.toString());
      }

      // $$fb 2002-07-18: fix for 4714846: JavaSound ULAW (8-bit) encoder erroneously depends on
      // endian-ness
      boolean PCMIsBigEndian;

      // determine whether we are encoding or decoding
      if (AudioFormat.Encoding.ULAW.equals(inputFormat.getEncoding())) {
        encode = false;
        encodeFormat = inputFormat;
        decodeFormat = outputFormat;
        PCMIsBigEndian = outputFormat.isBigEndian();
      } else {
        encode = true;
        encodeFormat = outputFormat;
        decodeFormat = inputFormat;
        PCMIsBigEndian = inputFormat.isBigEndian();
        tempBuffer = new byte[tempBufferSize];
      }

      // setup tables according to byte order
      if (PCMIsBigEndian) {
        tabByte1 = ULAW_TABH;
        tabByte2 = ULAW_TABL;
        highByte = 0;
        lowByte = 1;
      } else {
        tabByte1 = ULAW_TABL;
        tabByte2 = ULAW_TABH;
        highByte = 1;
        lowByte = 0;
      }

      // set the AudioInputStream length in frames if we know it
      if (stream instanceof AudioInputStream) {
        frameLength = ((AudioInputStream) stream).getFrameLength();
      }
      // set framePos to zero
      framePos = 0;
      frameSize = inputFormat.getFrameSize();
      if (frameSize == AudioSystem.NOT_SPECIFIED) {
        frameSize = 1;
      }
    }
Пример #2
0
  /**
   * Sets the InputStream from which this StreamDataSource reads.
   *
   * @param inputStream the InputStream from which audio data comes
   * @param streamName the name of the InputStream
   */
  public void setInputStream(AudioInputStream inputStream, String streamName) {
    dataStream = inputStream;
    streamEndReached = false;
    utteranceEndSent = false;
    utteranceStarted = false;

    AudioFormat format = inputStream.getFormat();
    sampleRate = (int) format.getSampleRate();
    bigEndian = format.isBigEndian();

    String s = format.toString();
    logger.finer("input format is " + s);

    if (format.getSampleSizeInBits() % 8 != 0)
      throw new Error("StreamDataSource: bits per sample must be a multiple of 8.");
    bytesPerValue = format.getSampleSizeInBits() / 8;

    // test whether all files in the stream have the same format

    AudioFormat.Encoding encoding = format.getEncoding();
    if (encoding.equals(AudioFormat.Encoding.PCM_SIGNED)) signedData = true;
    else if (encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED)) signedData = false;
    else throw new RuntimeException("used file encoding is not supported");

    totalValuesRead = 0;
  }
 /**
  * Store an AudioFormat
  *
  * @param audioFormat
  */
 public AudioFormatTransport(AudioFormat audioFormat) {
   _channels = audioFormat.getChannels();
   _encoding = audioFormat.getEncoding().toString();
   _frameRate = audioFormat.getFrameRate();
   _frameSize = audioFormat.getFrameSize();
   _sampleRate = audioFormat.getSampleRate();
   _sampleSizeInBits = audioFormat.getSampleSizeInBits();
   _isBigEndian = audioFormat.isBigEndian();
   _properties = audioFormat.properties();
 }
Пример #4
0
  public void open(AudioInputStream stream) throws IOException, LineUnavailableException {

    AudioInputStream is1;
    format = stream.getFormat();

    if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
      is1 = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, stream);
    } else {
      is1 = stream;
    }
    format = is1.getFormat();
    InputStream is2;
    if (parent != null) {
      ProgressMonitorInputStream pmis =
          new ProgressMonitorInputStream(parent, "Loading track..", is1);
      pmis.getProgressMonitor().setMillisToPopup(0);
      is2 = pmis;
    } else {
      is2 = is1;
    }

    byte[] buf = new byte[2 ^ 16];
    int totalRead = 0;
    int numRead = 0;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    numRead = is2.read(buf);
    while (numRead > -1) {
      baos.write(buf, 0, numRead);
      numRead = is2.read(buf, 0, buf.length);
      totalRead += numRead;
    }
    is2.close();
    audioData = baos.toByteArray();
    AudioFormat afTemp;
    if (format.getChannels() < 2) {
      afTemp =
          new AudioFormat(
              format.getEncoding(),
              format.getSampleRate(),
              format.getSampleSizeInBits(),
              2,
              format.getSampleSizeInBits() * 2 / 8, // calculate
              // frame
              // size
              format.getFrameRate(),
              format.isBigEndian());
    } else {
      afTemp = format;
    }

    setLoopPoints(0, audioData.length);
    dataLine = AudioSystem.getSourceDataLine(afTemp);
    dataLine.open();
    inputStream = new ByteArrayInputStream(audioData);
  }
Пример #5
0
  public AudioInputStream getAudioInputStream(
      AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream) {
    AudioFormat sourceFormat = sourceStream.getFormat();
    AudioFormat.Encoding sourceEncoding = sourceFormat.getEncoding();

    if (sourceEncoding.equals(targetEncoding)) {
      return sourceStream;
    } else {
      AudioFormat targetFormat = null;
      if (!isConversionSupported(targetEncoding, sourceStream.getFormat())) {
        throw new IllegalArgumentException(
            "Unsupported conversion: "
                + sourceStream.getFormat().toString()
                + " to "
                + targetEncoding.toString());
      }
      if (AudioFormat.Encoding.ULAW.equals(sourceEncoding)
          && AudioFormat.Encoding.PCM_SIGNED.equals(targetEncoding)) {
        targetFormat =
            new AudioFormat(
                targetEncoding,
                sourceFormat.getSampleRate(),
                16,
                sourceFormat.getChannels(),
                2 * sourceFormat.getChannels(),
                sourceFormat.getSampleRate(),
                sourceFormat.isBigEndian());
      } else if (AudioFormat.Encoding.PCM_SIGNED.equals(sourceEncoding)
          && AudioFormat.Encoding.ULAW.equals(targetEncoding)) {
        targetFormat =
            new AudioFormat(
                targetEncoding,
                sourceFormat.getSampleRate(),
                8,
                sourceFormat.getChannels(),
                sourceFormat.getChannels(),
                sourceFormat.getSampleRate(),
                false);
      } else {
        throw new IllegalArgumentException(
            "Unsupported conversion: "
                + sourceStream.getFormat().toString()
                + " to "
                + targetEncoding.toString());
      }

      return getAudioInputStream(targetFormat, sourceStream);
    }
  }
  /** Convert javax.sound.sampled.AudioFormat to javax.media.format.AudioFormat. */
  public static AudioFormat convertFormat(javax.sound.sampled.AudioFormat format) {

    Encoding encoding = format.getEncoding();
    int channels = format.getChannels();
    float frameRate = format.getFrameRate();
    int frameSize = format.getFrameSize() < 0 ? format.getFrameSize() : (format.getFrameSize() * 8);
    float sampleRate = format.getSampleRate();
    int sampleSize = format.getSampleSizeInBits();

    int endian = format.isBigEndian() ? AudioFormat.BIG_ENDIAN : AudioFormat.LITTLE_ENDIAN;

    int signed = AudioFormat.NOT_SPECIFIED;
    String encodingString = AudioFormat.LINEAR;

    if (encoding == Encoding.PCM_SIGNED) {
      signed = AudioFormat.SIGNED;
      encodingString = AudioFormat.LINEAR;
    } else if (encoding == Encoding.PCM_UNSIGNED) {
      signed = AudioFormat.UNSIGNED;
      encodingString = AudioFormat.LINEAR;
    } else if (encoding == Encoding.ALAW) {
      encodingString = AudioFormat.ALAW;
    } else if (encoding == Encoding.ULAW) {
      encodingString = AudioFormat.ULAW;
    } else {
      encodingString = encoding.toString();
    }

    AudioFormat jmfFormat =
        new AudioFormat(
            encodingString,
            (double) sampleRate,
            sampleSize,
            channels,
            endian,
            signed,
            frameSize,
            frameRate,
            AudioFormat.byteArray);

    return jmfFormat;
  }
 public float[] extractFloatDataFromAmplitudeByteArray(AudioFormat format, byte[] audioBytes) {
   // convert
   audioData = null;
   if (format.getSampleSizeInBits() == 16) {
     int nlengthInSamples = audioBytes.length / 2;
     audioData = new float[nlengthInSamples];
     if (format.isBigEndian()) {
       for (int i = 0; i < nlengthInSamples; i++) {
         /* First byte is MSB (high order) */
         int MSB = audioBytes[2 * i];
         /* Second byte is LSB (low order) */
         int LSB = audioBytes[2 * i + 1];
         audioData[i] = MSB << 8 | (255 & LSB);
       }
     } else {
       for (int i = 0; i < nlengthInSamples; i++) {
         /* First byte is LSB (low order) */
         int LSB = audioBytes[2 * i];
         /* Second byte is MSB (high order) */
         int MSB = audioBytes[2 * i + 1];
         audioData[i] = MSB << 8 | (255 & LSB);
       }
     }
   } else if (format.getSampleSizeInBits() == 8) {
     int nlengthInSamples = audioBytes.length;
     audioData = new float[nlengthInSamples];
     if (format.getEncoding().toString().startsWith("PCM_SIGN")) {
       for (int i = 0; i < audioBytes.length; i++) {
         audioData[i] = audioBytes[i];
       }
     } else {
       for (int i = 0; i < audioBytes.length; i++) {
         audioData[i] = audioBytes[i] - 128;
       }
     }
   } // end of if..else
   // System.out.println("PCM Returned===============" +
   // audioData.length);
   return audioData;
 }
Пример #8
0
  private void getAudioData() {

    if (format.getSampleSizeInBits() == 16) {
      nlengthInSamples = audioBytes.length / 2;
      audioData = new int[nlengthInSamples];
      if (format.isBigEndian()) {
        for (int i = 0; i < nlengthInSamples; i++) {
          // First byte is MSB (high order)
          int MSB = (int) audioBytes[2 * i];
          // Second byte is LSB (low order)
          int LSB = (int) audioBytes[2 * i + 1];
          audioData[i] = MSB << 8 | (255 & LSB);
        }
      } else {
        for (int i = 0; i < nlengthInSamples; i++) {
          // First byte is LSB (low order)
          int LSB = (int) audioBytes[2 * i];
          // Second byte is MSB (high order)
          int MSB = (int) audioBytes[2 * i + 1];
          audioData[i] = MSB << 8 | (255 & LSB);
        }
      }
    } else {
      if (format.getSampleSizeInBits() == 8) {
        nlengthInSamples = audioBytes.length;
        audioData = new int[nlengthInSamples];
        if (format.getEncoding().toString().startsWith("PCM_SIGN")) {
          for (int i = 0; i < audioBytes.length; i++) {
            audioData[i] = audioBytes[i];
          }
        } else {
          for (int i = 0; i < audioBytes.length; i++) {
            audioData[i] = audioBytes[i] - 128;
          }
        }
      }
    }
  }
Пример #9
0
  /**
   * Determines the single largest sample size of all channels of the current clip. This can be
   * handy for determining a fraction to scal visual representations.
   *
   * @return Double between 0 & 1 representing the maximum signal level of any channel.
   */
  public double getLargestSampleSize() {

    int largest = 0;
    int current;

    boolean signed = (format.getEncoding() == AudioFormat.Encoding.PCM_SIGNED);
    int bitDepth = format.getSampleSizeInBits();
    boolean bigEndian = format.isBigEndian();

    int samples = audioData.length * 8 / bitDepth;

    if (signed) {
      if (bitDepth / 8 == 2) {
        if (bigEndian) {
          for (int cc = 0; cc < samples; cc++) {
            current = (audioData[cc * 2] * 256 + (audioData[cc * 2 + 1] & 0xFF));
            if (Math.abs(current) > largest) {
              largest = Math.abs(current);
            }
          }
        } else {
          for (int cc = 0; cc < samples; cc++) {
            current = (audioData[cc * 2 + 1] * 256 + (audioData[cc * 2] & 0xFF));
            if (Math.abs(current) > largest) {
              largest = Math.abs(current);
            }
          }
        }
      } else {
        for (int cc = 0; cc < samples; cc++) {
          current = (audioData[cc] & 0xFF);
          if (Math.abs(current) > largest) {
            largest = Math.abs(current);
          }
        }
      }
    } else {
      if (bitDepth / 8 == 2) {
        if (bigEndian) {
          for (int cc = 0; cc < samples; cc++) {
            current = (audioData[cc * 2] * 256 + (audioData[cc * 2 + 1] - 0x80));
            if (Math.abs(current) > largest) {
              largest = Math.abs(current);
            }
          }
        } else {
          for (int cc = 0; cc < samples; cc++) {
            current = (audioData[cc * 2 + 1] * 256 + (audioData[cc * 2] - 0x80));
            if (Math.abs(current) > largest) {
              largest = Math.abs(current);
            }
          }
        }
      } else {
        for (int cc = 0; cc < samples; cc++) {
          if (audioData[cc] > 0) {
            current = (audioData[cc] - 0x80);
            if (Math.abs(current) > largest) {
              largest = Math.abs(current);
            }
          } else {
            current = (audioData[cc] + 0x80);
            if (Math.abs(current) > largest) {
              largest = Math.abs(current);
            }
          }
        }
      }
    }

    // audioData
    logger.log(Level.FINEST, "Max signal level: " + (double) largest / (Math.pow(2, bitDepth - 1)));
    return (double) largest / (Math.pow(2, bitDepth - 1));
  }
Пример #10
0
  private InputStream getFileStream(WaveFileFormat waveFileFormat, InputStream audioStream)
      throws IOException {
    // private method ... assumes audioFileFormat is a supported file type

    // WAVE header fields
    AudioFormat audioFormat = waveFileFormat.getFormat();
    int headerLength = waveFileFormat.getHeaderSize();
    int riffMagic = WaveFileFormat.RIFF_MAGIC;
    int waveMagic = WaveFileFormat.WAVE_MAGIC;
    int fmtMagic = WaveFileFormat.FMT_MAGIC;
    int fmtLength = WaveFileFormat.getFmtChunkSize(waveFileFormat.getWaveType());
    short wav_type = (short) waveFileFormat.getWaveType();
    short channels = (short) audioFormat.getChannels();
    short sampleSizeInBits = (short) audioFormat.getSampleSizeInBits();
    int sampleRate = (int) audioFormat.getSampleRate();
    int frameSizeInBytes = audioFormat.getFrameSize();
    int frameRate = (int) audioFormat.getFrameRate();
    int avgBytesPerSec = channels * sampleSizeInBits * sampleRate / 8;
    short blockAlign = (short) ((sampleSizeInBits / 8) * channels);
    int dataMagic = WaveFileFormat.DATA_MAGIC;
    int dataLength = waveFileFormat.getFrameLength() * frameSizeInBytes;
    int length = waveFileFormat.getByteLength();
    int riffLength = dataLength + headerLength - 8;

    byte header[] = null;
    ByteArrayInputStream headerStream = null;
    ByteArrayOutputStream baos = null;
    DataOutputStream dos = null;
    SequenceInputStream waveStream = null;

    AudioFormat audioStreamFormat = null;
    AudioFormat.Encoding encoding = null;
    InputStream codedAudioStream = audioStream;

    // if audioStream is an AudioInputStream and we need to convert, do it here...
    if (audioStream instanceof AudioInputStream) {
      audioStreamFormat = ((AudioInputStream) audioStream).getFormat();

      encoding = audioStreamFormat.getEncoding();

      if (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) {
        if (sampleSizeInBits == 8) {
          wav_type = WaveFileFormat.WAVE_FORMAT_PCM;
          // plug in the transcoder to convert from PCM_SIGNED to PCM_UNSIGNED
          codedAudioStream =
              AudioSystem.getAudioInputStream(
                  new AudioFormat(
                      AudioFormat.Encoding.PCM_UNSIGNED,
                      audioStreamFormat.getSampleRate(),
                      audioStreamFormat.getSampleSizeInBits(),
                      audioStreamFormat.getChannels(),
                      audioStreamFormat.getFrameSize(),
                      audioStreamFormat.getFrameRate(),
                      false),
                  (AudioInputStream) audioStream);
        }
      }
      if ((AudioFormat.Encoding.PCM_SIGNED.equals(encoding) && audioStreamFormat.isBigEndian())
          || (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)
              && !audioStreamFormat.isBigEndian())
          || (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)
              && audioStreamFormat.isBigEndian())) {
        if (sampleSizeInBits != 8) {
          wav_type = WaveFileFormat.WAVE_FORMAT_PCM;
          // plug in the transcoder to convert to PCM_SIGNED_LITTLE_ENDIAN
          codedAudioStream =
              AudioSystem.getAudioInputStream(
                  new AudioFormat(
                      AudioFormat.Encoding.PCM_SIGNED,
                      audioStreamFormat.getSampleRate(),
                      audioStreamFormat.getSampleSizeInBits(),
                      audioStreamFormat.getChannels(),
                      audioStreamFormat.getFrameSize(),
                      audioStreamFormat.getFrameRate(),
                      false),
                  (AudioInputStream) audioStream);
        }
      }
    }

    // Now push the header into a stream, concat, and return the new SequenceInputStream

    baos = new ByteArrayOutputStream();
    dos = new DataOutputStream(baos);

    // we write in littleendian...
    dos.writeInt(riffMagic);
    dos.writeInt(big2little(riffLength));
    dos.writeInt(waveMagic);
    dos.writeInt(fmtMagic);
    dos.writeInt(big2little(fmtLength));
    dos.writeShort(big2littleShort(wav_type));
    dos.writeShort(big2littleShort(channels));
    dos.writeInt(big2little(sampleRate));
    dos.writeInt(big2little(avgBytesPerSec));
    dos.writeShort(big2littleShort(blockAlign));
    dos.writeShort(big2littleShort(sampleSizeInBits));
    // $$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant
    if (wav_type != WaveFileFormat.WAVE_FORMAT_PCM) {
      // add length 0 for "codec specific data length"
      dos.writeShort(0);
    }

    dos.writeInt(dataMagic);
    dos.writeInt(big2little(dataLength));

    dos.close();
    header = baos.toByteArray();
    headerStream = new ByteArrayInputStream(header);
    waveStream = new SequenceInputStream(headerStream, new NoCloseInputStream(codedAudioStream));

    return waveStream;
  }
  /**
   * Encode the given input wav file to an output file.
   *
   * @param inputFile Input wav file to encode.
   * @param outputFile Output file to write FLAC stream to. If file exists, it will be overwritten
   *     without prompting.
   * @return Status flag for encode
   */
  public javaFlacEncoder.FLAC_FileEncoder.Status encode(File inputFile, File outputFile) {
    javaFlacEncoder.FLAC_FileEncoder.Status status =
        javaFlacEncoder.FLAC_FileEncoder.Status.FULL_ENCODE;
    this.outFile = outputFile;
    // take file and initial configuration.
    // open file
    AudioInputStream sin = null;
    AudioFormat format = null;
    // File inputFile = new File("encoderTest.wav");
    try {
      sin = AudioSystem.getAudioInputStream(inputFile);
    } catch (IOException e) {
      status = javaFlacEncoder.FLAC_FileEncoder.Status.FILE_IO_ERROR;
    } catch (UnsupportedAudioFileException e) {
      status = javaFlacEncoder.FLAC_FileEncoder.Status.UNSUPPORTED_FILE;
    } finally {
      if (status != javaFlacEncoder.FLAC_FileEncoder.Status.FULL_ENCODE) return status;
    }
    try {
      format = sin.getFormat();
      // sanitize and optimize configurations
      adjustConfigurations(format);
      // open stream
      openStream();
      int frameSize = format.getFrameSize();
      int sampleSize = format.getSampleSizeInBits();
      int bytesPerSample = sampleSize / 8;
      if (sampleSize % 8 != 0) {
        // end processing now
        Exception newEx = new Exception(Status.UNSUPPORTED_SAMPLE_SIZE.name());
        throw newEx;
      }
      int channels = format.getChannels();
      boolean bigEndian = format.isBigEndian();
      byte[] samplesIn = new byte[(int) MAX_READ];
      int samplesRead;
      int framesRead;
      int[] sampleData = new int[MAX_READ * channels / frameSize];
      int blockSize = sc.getMaxBlockSize();
      int unencodedSamples = 0;
      int totalSamples = 0;
      while ((samplesRead = sin.read(samplesIn, 0, MAX_READ)) != -1) {
        // System.err.println("Read: " + read);
        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;
            }
            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) // don't mask...we want
                // sign
                temp = ((samplesIn[bytesPerSample * i + x]) << upShift);
              else temp = ((samplesIn[bytesPerSample * i + x] & lower8Mask) << upShift);
              totalTemp = totalTemp | temp;
            }
            sampleData[i] = totalTemp;
          }
        }
        if (framesRead > 0) {
          flac.addSamples(sampleData, framesRead);
          unencodedSamples += framesRead;
        }
        // if(unencodedSamples > blockSize*100) {
        if (useThreads) // Thread.yield();//
        unencodedSamples -= flac.t_encodeSamples(unencodedSamples, false);
        else unencodedSamples -= flac.encodeSamples(unencodedSamples, false);
        totalSamples += unencodedSamples;
        // unencodedSamples = 0;

        // }
        // System.err.println("read : "+ samplesRead);
      }
      totalSamples += unencodedSamples;
      if (useThreads) unencodedSamples -= flac.t_encodeSamples(unencodedSamples, true);
      else unencodedSamples -= flac.encodeSamples(unencodedSamples, true);
      // unencodedSamples = 0;
      lastTotalSamples = totalSamples;
    } catch (IOException e) {
      status = javaFlacEncoder.FLAC_FileEncoder.Status.FILE_IO_ERROR;
    } catch (Exception e) {
      status = javaFlacEncoder.FLAC_FileEncoder.Status.GENERAL_ERROR;
      String message = e.getMessage();
      if (message == null) {
        e.printStackTrace();
      } else if (message.equals(
          javaFlacEncoder.FLAC_FileEncoder.Status.UNSUPPORTED_SAMPLE_SIZE.name()))
        status = javaFlacEncoder.FLAC_FileEncoder.Status.UNSUPPORTED_SAMPLE_SIZE;
    }

    // System.err.print("LastTotalSamples: "+lastTotalSamples);
    return status;
  }
Пример #12
0
  public boolean convertToTxt(short SIZE) {

    // WAV FILE ATTRIBUTE
    AudioInputStream audioinput;
    AudioFormat audioformat;
    long length_in_frames;
    float frameRate;
    int frameSize;
    int channels;
    float sampleRate;
    int sampleSize;
    boolean big_endian;
    System.out.println(wav_file_path);

    File wavfile = new File(wav_file_path);
    BufferedInputStream bufread;

    BufferedWriter wrL, wrR; // left channel file and right channel file;
    int max_mag_in_recording =
        0; // This is the place holder for the maximum magnitude in the WAV file (Used for
           // normalizing)
    byte[] buffer = new byte[SIZE];
    ArrayList write_list = new ArrayList();
    Integer[] write_buffer;
    int sampleCount = 0;
    boolean write_buffer_full = false;
    int num_bytes_read = 0;
    int valuecount = 0;
    try {
      audioinput = AudioSystem.getAudioInputStream(wavfile);
      bufread = new BufferedInputStream(audioinput);
      String[] tempstr = wav_file_path.split(".wav");
      left_channel_file = tempstr[0] + "LEFT.txt";
      right_channel_file = tempstr[0] + "RIGHT.txt";
      wrL = new BufferedWriter(new FileWriter(left_channel_file));
      wrR = new BufferedWriter(new FileWriter(right_channel_file));

      audioformat = audioinput.getFormat();
      length_in_frames = audioinput.getFrameLength();
      channels = audioformat.getChannels();
      frameRate = audioformat.getFrameRate();
      frameSize = audioformat.getFrameSize();
      sampleRate = audioformat.getSampleRate();
      sampling_rate = (int) sampleRate;
      sampleSize = audioformat.getSampleSizeInBits();
      big_endian = audioformat.isBigEndian();

      bufread = new BufferedInputStream(audioinput, SIZE);

      // THIS BYTE ARRAY WILL STORE THE FRAME
      short sampleSizeinBytes = (short) (sampleSize / 8);
      int[] frame = new int[sampleSizeinBytes];
      int frame_low = 0;
      int frame_high = sampleSizeinBytes - 1;

      /*
         frame[highest] + frame[highest-1] + .... + frame[0]
      */
      int tmp;
      int tempint = 0;
      short buffer_read_count = 0;
      boolean left_overs = false;
      ArrayList left_over_array = new ArrayList();
      short requires_more = 0;
      byte[] left_over_byte_array = new byte[SIZE];
      boolean number_negative = false;
      boolean write_to_file = false;
      int temp_left = 0;
      int temp_byte_to_int = 0;

      boolean right_channel = false;
      int negative_number_shift = 0;
      for (int i = sampleSizeinBytes; i < 4; i++) {
        negative_number_shift = negative_number_shift | 0xFF << (8 * i);
      }

      while ((num_bytes_read = bufread.read(buffer)) != -1) {
        buffer_read_count = -1;
        if (left_overs) {
          requires_more = (short) (sampleSizeinBytes - left_over_array.size());
          if (num_bytes_read >= requires_more) {
            for (short k = 1; k <= requires_more; k++) {
              buffer_read_count = (short) (buffer_read_count + 1);
              left_over_array.add(buffer[buffer_read_count]);
            }
          }
          left_overs = false;
          Byte[] t = new Byte[left_over_array.size()];
          left_over_array.toArray(t);
          temp_byte_to_int = 0;
          tempint = 0;
          temp_left = 0;
          for (int i = 0; i < left_over_array.size(); i++) {
            if (!((t[i]) >= 0 && (t[i] <= 127))) {
              if (i == left_over_array.size() - 1) number_negative = true;
              temp_byte_to_int = 256 + t[i];
            } else temp_byte_to_int = t[i];
            temp_left = temp_left + (temp_byte_to_int << (8 * (i)));
          }
          if (number_negative) {
            tempint = (negative_number_shift) | temp_left;
            number_negative = false;
          } else tempint = temp_left;
          write_list.add(tempint);
          sampleCount += 1;

          tempint = temp_left = temp_byte_to_int = 0;

          left_over_array.clear();
        }
        while ((buffer_read_count) < num_bytes_read - 1) {
          if (((num_bytes_read - 1) - (buffer_read_count)) >= sampleSizeinBytes) {

            for (short i = 1; i <= sampleSizeinBytes; i++) {
              buffer_read_count = (short) (buffer_read_count + 1);
              frame[i - 1] = buffer[buffer_read_count];
            }
            tempint = temp_left = temp_byte_to_int = 0;

            for (int m = 0; m < frame.length; m++) {
              if (!((frame[m]) >= 0 && (frame[m] <= 127))) {
                if (m == frame.length - 1) number_negative = true;
                temp_byte_to_int = 256 + frame[m];
              } else temp_byte_to_int = frame[m];
              temp_left = temp_left + (temp_byte_to_int << (8 * (m)));
            }
            if (number_negative) {
              tempint = (negative_number_shift) | temp_left;
              number_negative = false;
            } else tempint = temp_left;
            sampleCount += 1;
            write_list.add(tempint);

            tempint = temp_left = temp_byte_to_int = 0;
          } else {
            // save the left over bytes which are lesser in number than frame size in a tempbuffer
            // and turn the flag on. If flag==true then process that first and then process other.
            left_over_array.clear();
            short left_over_array_count = (short) (num_bytes_read - 1 - buffer_read_count);
            for (short l = 1; l <= left_over_array_count; l++) {
              buffer_read_count = (short) (buffer_read_count + 1);
              left_over_array.add(buffer[buffer_read_count]);
            }
            left_overs = true;
          }
        }
        write_to_file = true;
        if (write_to_file) {

          // store all the number in the write_buffer to the file
          write_buffer = new Integer[write_list.size()];
          write_list.toArray(write_buffer);
          for (int w = 0; w < write_list.size(); w++) {
            if (!right_channel) {
              int storeL = write_buffer[w].intValue();
              valuecount = valuecount + 1;

              if ((storeL > 0) && (maxValueL < storeL)) {
                maxValueL = storeL;
                sampleMaxL = valuecount;
              }
              if ((storeL < 0) && (minValueL > storeL)) {
                sampleMinL = valuecount;
                minValueL = storeL;
              }
              wrL.write(String.valueOf(storeL));
              wrL.newLine();
              right_channel = true;
            } else {
              valuecount = valuecount + 1;
              int storeR = write_buffer[w].intValue();
              if ((storeR > 0) && (maxValueR < storeR)) {
                sampleMaxR = valuecount;
                maxValueR = storeR;
              }
              if ((storeR < 0) && (minValueR > storeR)) {
                sampleMinR = valuecount;
                minValueR = storeR;
              }
              wrR.write(String.valueOf(storeR));
              wrR.newLine();
              right_channel = false;
            }
          }
          write_list.clear();
        }
      }
      wrR.close();
      wrL.close();
      bufread.close();
      System.out.println(sampleMaxL + "\n" + sampleMaxR + "\n" + sampleMinL + "\n" + sampleMinR);

    } catch (IOException e) {
      System.err.println(e);
    } catch (UnsupportedAudioFileException f) {
      System.err.println(f);
    }

    return true;
  }