/*
 * -------------------------------------------------------------------------
 *      $Id: AccessWaveFile.java,v 1.4 2006/01/30 20:03:45 estewart Exp $
 * -------------------------------------------------------------------------
 *      Copyright (c) 1999 Visual Numerics Inc. All Rights Reserved.
 *
 *      This software is confidential information which is proprietary to
 *      and a trade secret of Visual Numerics, Inc.  Use, duplication or
 *      disclosure is subject to the terms of an appropriate license
 *      agreement.
 *
 *      VISUAL NUMERICS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 *      SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 *      BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 *      FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. VISUAL
 *      NUMERICS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 *      AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
 *      ITS DERIVATIVES.
 *--------------------------------------------------------------------------
 */

package com.imsl.demo.Harmonic;
import java.io.IOException;
import javax.sound.sampled.*;

/**
 *  Uses classes from javax.sound.sampled to obtain data
 *  from a .wave file.
 *
 * @author     R.B.E.Taylor
 * @created    October 23, 2002
 */
public class AccessWaveFile {
    java.net.URL fileURL;

    public AccessWaveFile(String fileName) throws UnsupportedAudioFileException {
        setInput(fileName);
    }

    public void setInput(String fileName) {
        this.fileURL = this.getClass().getResource("/com/imsl/demo/Harmonic/"+fileName);
    }

    private int availableBytes;
    private AudioFormat inputFormat;
    private float framesPerSec;
    private int bytesPerFrame;
    private float bytesPerSec;

    public byte[] getByteStream(int streamSize) throws IOException, UnsupportedAudioFileException {
        AudioInputStream input = AudioSystem.getAudioInputStream(fileURL);
        byte[] byteWaveForm = new byte[streamSize];

        // Deal with "unsigned-to-signed" issue and make "dc" 0
        input.read(byteWaveForm);
        for (int i=0; i<availableBytes; i++) byteWaveForm[i]+=128;
        // input.read() broken in Java 1.5
        //for (int i=0; i
        //    byteWaveForm[i] = (byte)(input.read() + 128);
        //}
        input.close();
        return byteWaveForm;
    }

    public float getBytesPerSec() throws IOException, UnsupportedAudioFileException {
        AudioInputStream input = AudioSystem.getAudioInputStream(fileURL);
        inputFormat = input.getFormat();
        framesPerSec = inputFormat.getFrameRate();
        bytesPerFrame = inputFormat.getFrameSize();
        input.close();

        // Test bytes per frame? Throw an exception if != 1 (as
        // demo expects it to be) ?

        return framesPerSec;
    }

    public int getAvailableBytes() throws IOException, UnsupportedAudioFileException {
        AudioInputStream input = AudioSystem.getAudioInputStream(fileURL);
        availableBytes = input.available();
        input.close();
        return availableBytes;
    }
}