ResampleInputStreampublic final class ResampleInputStream extends InputStream
Fields Summary |
---|
private static final String | TAG | private InputStream | mInputStream | private final int | mRateIn | private final int | mRateOut | private byte[] | mBuf | private int | mBufCount | private static final int | mFirLength | private final byte[] | mOneByte |
Constructors Summary |
---|
public ResampleInputStream(InputStream inputStream, int rateIn, int rateOut)Create a new ResampleInputStream, which converts the sample rate
// only support 2:1 at the moment
if (rateIn != 2 * rateOut) throw new IllegalArgumentException("only support 2:1 at the moment");
rateIn = 2;
rateOut = 1;
mInputStream = inputStream;
mRateIn = rateIn;
mRateOut = rateOut;
|
Methods Summary |
---|
public void | close()
try {
if (mInputStream != null) mInputStream.close();
} finally {
mInputStream = null;
}
| protected void | finalize()
if (mInputStream != null) {
close();
throw new IllegalStateException("someone forgot to close ResampleInputStream");
}
| private static native void | fir21(byte[] in, int inOffset, byte[] out, int outOffset, int npoints)
| public int | read()
int rtn = read(mOneByte, 0, 1);
return rtn == 1 ? (0xff & mOneByte[0]) : -1;
| public int | read(byte[] b)
return read(b, 0, b.length);
| public int | read(byte[] b, int offset, int length)
if (mInputStream == null) throw new IllegalStateException("not open");
// ensure that mBuf is big enough to cover requested 'length'
int nIn = ((length / 2) * mRateIn / mRateOut + mFirLength) * 2;
if (mBuf == null) {
mBuf = new byte[nIn];
} else if (nIn > mBuf.length) {
byte[] bf = new byte[nIn];
System.arraycopy(mBuf, 0, bf, 0, mBufCount);
mBuf = bf;
}
// read until we have enough data for at least one output sample
while (true) {
int len = ((mBufCount / 2 - mFirLength) * mRateOut / mRateIn) * 2;
if (len > 0) {
length = len < length ? len : (length / 2) * 2;
break;
}
// TODO: should mBuf.length below be nIn instead?
int n = mInputStream.read(mBuf, mBufCount, mBuf.length - mBufCount);
if (n == -1) return -1;
mBufCount += n;
}
// resample input data
fir21(mBuf, 0, b, offset, length / 2);
// move any unused bytes to front of mBuf
int nFwd = length * mRateIn / mRateOut;
mBufCount -= nFwd;
if (mBufCount > 0) System.arraycopy(mBuf, nFwd, mBuf, 0, mBufCount);
return length;
|
|