ogginputstream.java
来自「java 3d game jme 工程开发源代码」· Java 代码 · 共 556 行 · 第 1/2 页
JAVA
556 行
// (which is guaranteed to be small and only contain the Vorbis
// stream initial header) We need the first page to get the stream
// serialno.
// submit a 4k block to libvorbis' Ogg layer
int index = syncState.buffer(4096);
byte buffer[] = syncState.data;
int bytes = in.read(buffer, index, 4096);
syncState.wrote(bytes);
// Get the first page.
if (syncState.pageout(page) != 1) {
// have we simply run out of data? If so, we're done.
if (bytes < 4096)
return;//break;
// error case. Must not be Vorbis data
throw new Exception("Input does not appear to be an Ogg bitstream.");
}
// Get the serial number and set up the rest of decode.
// serialno first; use it to set up a logical stream
streamState.init(page.serialno());
// extract the initial header from the first page and verify that the
// Ogg bitstream is in fact Vorbis data
// I handle the initial header first instead of just having the code
// read all three Vorbis headers at once because reading the initial
// header is an easy way to identify a Vorbis bitstream and it's
// useful to see that functionality seperated out.
info.init();
comment.init();
if (streamState.pagein(page) < 0) {
// error; stream version mismatch perhaps
throw new Exception("Error reading first page of Ogg bitstream data.");
}
if (streamState.packetout(packet) != 1) {
// no page? must not be vorbis
throw new Exception("Error reading initial header packet.");
}
if (info.synthesis_headerin(comment, packet) < 0) {
// error case; not a vorbis header
throw new Exception("This Ogg bitstream does not contain Vorbis audio data.");
}
// At this point, we're sure we're Vorbis. We've set up the logical
// (Ogg) bitstream decoder. Get the comment and codebook headers and
// set up the Vorbis decoder
// The next two packets in order are the comment and codebook headers.
// They're likely large and may span multiple pages. Thus we read
// and submit data until we get our two packets, watching that no
// pages are missing. If a page is missing, error out; losing a
// header page is the only place where missing data is fatal.
int i = 0;
while (i < 2) {
while (i < 2) {
int result = syncState.pageout(page);
if (result == 0)
break; // Need more data
// Don't complain about missing or corrupt data yet. We'll
// catch it at the packet output phase
if (result == 1) {
streamState.pagein(page); // we can ignore any errors here
// as they'll also become apparent
// at packetout
while (i < 2) {
result = streamState.packetout(packet);
if (result == 0) {
break;
}
if (result == -1) {
// Uh oh; data at some point was corrupted or missing!
// We can't tolerate that in a header. Die.
throw new Exception("Corrupt secondary header. Exiting.");
}
info.synthesis_headerin(comment, packet);
i++;
}
}
}
// no harm in not checking before adding more
index = syncState.buffer(4096);
buffer = syncState.data;
bytes = in.read(buffer, index, 4096);
// NOTE: This is a bugfix. read will return -1 which will mess up syncState.
if (bytes < 0 ) {
bytes = 0;
}
if (bytes == 0 && i < 2) {
throw new Exception("End of file before finding all Vorbis headers!");
}
syncState.wrote(bytes);
}
convsize = 4096 / info.channels;
// OK, got and parsed all three headers. Initialize the Vorbis
// packet->PCM decoder.
dspState.synthesis_init(info); // central decode state
block.init(dspState); // local state for most of the decode
// so multiple block decodes can
// proceed in parallel. We could init
// multiple vorbis_block structures
// for vd here
}
/**
* Decodes a packet.
*/
private int decodePacket(Packet packet) {
// check the endianes of the computer.
final boolean bigEndian = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
if (block.synthesis(packet) == 0) {
// test for success!
dspState.synthesis_blockin(block);
}
// **pcm is a multichannel float vector. In stereo, for
// example, pcm[0] is left, and pcm[1] is right. samples is
// the size of each channel. Convert the float values
// (-1.<=range<=1.) to whatever PCM format and write it out
int convOff = 0;
int samples;
while ((samples = dspState.synthesis_pcmout(_pcm, _index)) > 0) {
//logger.info("while() 4");
float[][] pcm = _pcm[0];
int bout = (samples < convsize ? samples : convsize);
// convert floats to 16 bit signed ints (host order) and interleave
for (int i = 0; i < info.channels; i++) {
int ptr = (i << 1) + convOff;
int mono = _index[i];
for (int j = 0; j < bout; j++) {
int val = (int) (pcm[i][mono + j] * 32767.);
// might as well guard against clipping
val = Math.max(-32768, Math.min(32767, val));
val |= (val < 0 ? 0x8000 : 0);
convbuffer[ptr + 0] = (byte) (bigEndian ? val >>> 8 : val);
convbuffer[ptr + 1] = (byte) (bigEndian ? val : val >>> 8);
ptr += (info.channels << 1);
}
}
convOff += 2 * info.channels * bout;
// Tell orbis how many samples were consumed
dspState.synthesis_read(bout);
}
return convOff;
}
/**
* Decodes the next packet.
* @return bytes read into convbuffer of -1 if end of file
*/
private int lazyDecodePacket() throws IOException {
int result = getNextPacket(packet);
if (result == -1) {
return -1;
}
// we have a packet. Decode it
return decodePacket(packet);
}
/**
* @param packet where to put the packet.
*/
private int getNextPacket(Packet packet) throws IOException {
// get next packet.
boolean fetchedPacket = false;
while (!eos && !fetchedPacket) {
int result1 = streamState.packetout(packet);
if (result1 == 0) {
// no more packets in page. Fetch new page.
int result2 = 0;
while (!eos && result2 == 0) {
result2 = syncState.pageout(page);
if (result2 == 0) {
fetchData();
}
}
// return if we have reaced end of file.
if ((result2 == 0) && (page.eos() != 0)) {
return -1;
}
if (result2 == 0) {
// need more data fetching page..
fetchData();
} else if (result2 == -1) {
logger.info("syncState.pageout(page) result == -1");
return -1;
} else {
// int result3 =
streamState.pagein(page);
}
} else if (result1 == -1) {
logger.info("streamState.packetout(packet) result == -1");
return -1;
} else {
fetchedPacket = true;
}
}
return 0;
}
/**
* Copys data from input stream to syncState.
*/
private void fetchData() throws IOException {
if (!eos) {
// copy 4096 bytes from compressed stream to syncState.
int index = syncState.buffer(4096);
if (index < 0) {
eos = true;
return;
}
int bytes = in.read(syncState.data, index, 4096);
syncState.wrote(bytes);
if (bytes == 0) {
eos = true;
}
}
}
/**
* Gets information on the ogg.
*/
public String toString() {
String s = "";
s = s + "version " + info.version + "\n";
s = s + "channels " + info.channels + "\n";
s = s + "rate (hz) " + info.rate ;
return s;
}
@Override
public int getChannelCount() {
return info.channels;
}
@Override
public OggInputStream makeNew() throws IOException {
return new OggInputStream(getResource(), getLength());
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?