Uses 119K of libraries. Click here for Pure Java version (5126L/30K).
1 | lib 1400511 // VorbisJava (to unpack ogg file) |
2 | |
3 | import org.gagravarr.ogg.*; |
4 | |
5 | // replaces OggPacketReader |
6 | sclass OggStreamPacketReader_v2 { |
7 | InputStream inp; |
8 | Iterator<OggPacketData> it; |
9 | OggStreamPacket nextPacket; |
10 | |
11 | *(InputStream *inp) {} |
12 | |
13 | /** |
14 | * Returns the next packet in the file, or |
15 | * null if no more packets remain. |
16 | * Call {@link OggStreamPacket#isBeginningOfStream()} |
17 | * to detect if it is the first packet in the |
18 | * stream or not, and use |
19 | * {@link OggStreamPacket#getSid()} to track which |
20 | * stream it belongs to. |
21 | */ |
22 | public OggStreamPacket getNextPacket() throws IOException { |
23 | // If we skipped to a point in the stream, and |
24 | // have a packet waiting, return that |
25 | if(nextPacket != null) { |
26 | OggStreamPacket p = nextPacket; |
27 | nextPacket = null; |
28 | return p; |
29 | } |
30 | |
31 | // If we're already part way through a page, |
32 | // then fetch the next packet. If it's a |
33 | // full one, then we're done. |
34 | OggPacketData leftOver = null; |
35 | if(it != null && it.hasNext()) { |
36 | OggPacketData packet = it.next(); |
37 | if(packet instanceof OggStreamPacket) { |
38 | return (OggStreamPacket)packet; |
39 | } |
40 | leftOver = packet; |
41 | } |
42 | |
43 | // Find the next page, from which |
44 | // to get our next packet from |
45 | int searched = 0; |
46 | int pos = -1; |
47 | boolean found = false; |
48 | int r; |
49 | while (searched < 65536 && !found) { |
50 | r = inp.read(); |
51 | if(r == -1) { |
52 | // No more data |
53 | return null; |
54 | } |
55 | |
56 | switch(pos) { |
57 | case -1: |
58 | if(r == (int)'O') { |
59 | pos = 0; |
60 | } |
61 | break; |
62 | case 0: |
63 | if(r == (int)'g') { |
64 | pos = 1; |
65 | } else { |
66 | pos = -1; |
67 | } |
68 | break; |
69 | case 1: |
70 | if(r == (int)'g') { |
71 | pos = 2; |
72 | } else { |
73 | pos = -1; |
74 | } |
75 | break; |
76 | case 2: |
77 | if(r == (int)'S') { |
78 | found = true; |
79 | } else { |
80 | pos = -1; |
81 | } |
82 | break; |
83 | } |
84 | |
85 | if(!found) { |
86 | searched++; |
87 | } |
88 | } |
89 | |
90 | if(!found) { |
91 | throw new IOException("Next ogg packet header not found after searching " + searched + " bytes"); |
92 | } |
93 | |
94 | searched -= 3; // OggS |
95 | if(searched > 0) { |
96 | System.err.println("Warning - had to skip " + searched + " bytes of junk data before finding the next packet header"); |
97 | } |
98 | |
99 | // Create the page, and prime the iterator on it |
100 | |
101 | OggStreamPage page = new(inp); |
102 | if (!page.isChecksumValid()) { |
103 | warn("invalid checksum on page " + |
104 | page.getSequenceNumber() + " of stream " + |
105 | Integer.toHexString(page.getSid()) + " (" + |
106 | page.getSid() + ")"); |
107 | } |
108 | it = page.getPacketIterator(leftOver); |
109 | return getNextPacket(); |
110 | } |
111 | |
112 | /** |
113 | * Returns the next packet with the given SID (Stream ID), or |
114 | * null if no more packets remain. |
115 | * Any packets from other streams will be silently discarded. |
116 | */ |
117 | public OggStreamPacket getNextPacketWithSid(int sid) throws IOException { |
118 | OggStreamPacket p = null; |
119 | while( (p = getNextPacket()) != null ) { |
120 | if(p.getSid() == sid) { |
121 | return p; |
122 | } |
123 | } |
124 | return null; |
125 | } |
126 | |
127 | /** |
128 | * Un-reads a packet, leaving it ready to be feteched by the |
129 | * next call to {@link #getNextPacket()}. |
130 | * Only one packet may be unread. |
131 | * Normally used when identifying a stream, to leave the |
132 | * initial packet ready for a decoder |
133 | */ |
134 | public void unreadPacket(OggStreamPacket packet) { |
135 | if(nextPacket != null) { |
136 | throw new IllegalStateException("Can't un-read twice"); |
137 | } |
138 | nextPacket = packet; |
139 | } |
140 | |
141 | /** |
142 | * Skips forward until the first packet with a Sequence Number |
143 | * of equal or greater than that specified. Call {@link #getNextPacket()} |
144 | * to retrieve this packet. |
145 | * This method advances across all streams, but only searches the |
146 | * specified one. |
147 | * @param sid The ID of the stream who's packets we will search |
148 | * @param sequenceNumber The sequence number we're looking for |
149 | */ |
150 | public void skipToSequenceNumber(int sid, int sequenceNumber) throws IOException { |
151 | OggStreamPacket p = null; |
152 | while( (p = getNextPacket()) != null ) { |
153 | if(p.getSid() == sid && p.getSequenceNumber() >= sequenceNumber) { |
154 | nextPacket = p; |
155 | break; |
156 | } |
157 | } |
158 | } |
159 | |
160 | /** |
161 | * Skips forward until the first packet with a Granule Position |
162 | * of equal or greater than that specified. Call {@link #getNextPacket()} |
163 | * to retrieve this packet. |
164 | * This method advances across all streams, but only searches the |
165 | * specified one. |
166 | * @param sid The ID of the stream who's packets we will search |
167 | * @param granulePosition The granule position we're looking for |
168 | */ |
169 | public void skipToGranulePosition(int sid, long granulePosition) throws IOException { |
170 | OggStreamPacket p = null; |
171 | while( (p = getNextPacket()) != null ) { |
172 | if(p.getSid() == sid && p.getGranulePosition() >= granulePosition) { |
173 | nextPacket = p; |
174 | break; |
175 | } |
176 | } |
177 | } |
178 | } |
Began life as a copy of #1032910
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1032916 |
Snippet name: | OggStreamPacketReader_v2 |
Eternal ID of this version: | #1032916/2 |
Text MD5: | 1520432b8dc5b11e80021170bece43d6 |
Transpilation MD5: | 281c01bf08ebc7d8616f5a7f71394fdb |
Author: | stefan |
Category: | javax / audio |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-10-10 21:53:31 |
Source code size: | 5567 bytes / 178 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 136 / 243 |
Version history: | 1 change(s) |
Referenced in: | [show references] |