WaveHeaderpublic class WaveHeader extends Object This class represents the header of a WAVE format audio file, which usually
have a .wav suffix. The following integer valued fields are contained:
- format - usually PCM, ALAW or ULAW.
- numChannels - 1 for mono, 2 for stereo.
- sampleRate - usually 8000, 11025, 16000, 22050, or 44100 hz.
- bitsPerSample - usually 16 for PCM, 8 for ALAW, or 8 for ULAW.
- numBytes - size of audio data after this header, in bytes.
Not yet ready to be supported, so |
Fields Summary |
---|
private static final String | TAG | private static final int | HEADER_LENGTH | public static final short | FORMAT_PCMIndicates PCM format. | public static final short | FORMAT_ALAWIndicates ALAW format. | public static final short | FORMAT_ULAWIndicates ULAW format. | private short | mFormat | private short | mNumChannels | private int | mSampleRate | private short | mBitsPerSample | private int | mNumBytes |
Constructors Summary |
---|
public WaveHeader()Construct a WaveHeader, with all fields defaulting to zero.
| public WaveHeader(short format, short numChannels, int sampleRate, short bitsPerSample, int numBytes)Construct a WaveHeader, with fields initialized.
mFormat = format;
mSampleRate = sampleRate;
mNumChannels = numChannels;
mBitsPerSample = bitsPerSample;
mNumBytes = numBytes;
|
Methods Summary |
---|
public short | getBitsPerSample()Get the number of bits per sample.
return mBitsPerSample;
| public short | getFormat()Get the format field.
return mFormat;
| public int | getNumBytes()Get the size of audio data after this header, in bytes.
return mNumBytes;
| public short | getNumChannels()Get the number of channels.
return mNumChannels;
| public int | getSampleRate()Get the sample rate.
return mSampleRate;
| public int | read(java.io.InputStream in)Read and initialize a WaveHeader.
/* RIFF header */
readId(in, "RIFF");
int numBytes = readInt(in) - 36;
readId(in, "WAVE");
/* fmt chunk */
readId(in, "fmt ");
if (16 != readInt(in)) throw new IOException("fmt chunk length not 16");
mFormat = readShort(in);
mNumChannels = readShort(in);
mSampleRate = readInt(in);
int byteRate = readInt(in);
short blockAlign = readShort(in);
mBitsPerSample = readShort(in);
if (byteRate != mNumChannels * mSampleRate * mBitsPerSample / 8) {
throw new IOException("fmt.ByteRate field inconsistent");
}
if (blockAlign != mNumChannels * mBitsPerSample / 8) {
throw new IOException("fmt.BlockAlign field inconsistent");
}
/* data chunk */
readId(in, "data");
mNumBytes = readInt(in);
return HEADER_LENGTH;
| private static void | readId(java.io.InputStream in, java.lang.String id)
for (int i = 0; i < id.length(); i++) {
if (id.charAt(i) != in.read()) throw new IOException( id + " tag not present");
}
| private static int | readInt(java.io.InputStream in)
return in.read() | (in.read() << 8) | (in.read() << 16) | (in.read() << 24);
| private static short | readShort(java.io.InputStream in)
return (short)(in.read() | (in.read() << 8));
| public android.speech.srec.WaveHeader | setBitsPerSample(short bitsPerSample)Set the number of bits per sample.
mBitsPerSample = bitsPerSample;
return this;
| public android.speech.srec.WaveHeader | setFormat(short format)Set the format field.
mFormat = format;
return this;
| public android.speech.srec.WaveHeader | setNumBytes(int numBytes)Set the size of audio data after this header, in bytes.
mNumBytes = numBytes;
return this;
| public android.speech.srec.WaveHeader | setNumChannels(short numChannels)Set the number of channels.
mNumChannels = numChannels;
return this;
| public android.speech.srec.WaveHeader | setSampleRate(int sampleRate)Set the sample rate.
mSampleRate = sampleRate;
return this;
| public java.lang.String | toString()
return String.format(
"WaveHeader format=%d numChannels=%d sampleRate=%d bitsPerSample=%d numBytes=%d",
mFormat, mNumChannels, mSampleRate, mBitsPerSample, mNumBytes);
| public int | write(java.io.OutputStream out)Write a WAVE file header.
/* RIFF header */
writeId(out, "RIFF");
writeInt(out, 36 + mNumBytes);
writeId(out, "WAVE");
/* fmt chunk */
writeId(out, "fmt ");
writeInt(out, 16);
writeShort(out, mFormat);
writeShort(out, mNumChannels);
writeInt(out, mSampleRate);
writeInt(out, mNumChannels * mSampleRate * mBitsPerSample / 8);
writeShort(out, (short)(mNumChannels * mBitsPerSample / 8));
writeShort(out, mBitsPerSample);
/* data chunk */
writeId(out, "data");
writeInt(out, mNumBytes);
return HEADER_LENGTH;
| private static void | writeId(java.io.OutputStream out, java.lang.String id)
for (int i = 0; i < id.length(); i++) out.write(id.charAt(i));
| private static void | writeInt(java.io.OutputStream out, int val)
out.write(val >> 0);
out.write(val >> 8);
out.write(val >> 16);
out.write(val >> 24);
| private static void | writeShort(java.io.OutputStream out, short val)
out.write(val >> 0);
out.write(val >> 8);
|
|