Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

132
LINES

< > BotCompany Repo | #1032915 // OggStreamPacket

JavaX fragment (include) [tags: use-pretranspiled]

Uses 119K of libraries. Click here for Pure Java version (4897L/29K).

1  
!include once #1032914 // VorbisJava
2  
3  
/*
4  
 * Licensed under the Apache License, Version 2.0 (the "License");
5  
 * you may not use this file except in compliance with the License.
6  
 * You may obtain a copy of the License at
7  
 *
8  
 *     http://www.apache.org/licenses/LICENSE-2.0
9  
 *
10  
 * Unless required by applicable law or agreed to in writing, software
11  
 * distributed under the License is distributed on an "AS IS" BASIS,
12  
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  
 * See the License for the specific language governing permissions and
14  
 * limitations under the License.
15  
 */
16  
17  
/**
18  
 * Represents a logical group of data.
19  
 * RFC3533 suggests that these should usually be
20  
 *  around 50-200 bytes long.
21  
 */
22  
sclass OggStreamPacket extends OggPacketData {
23  
    private OggStreamPage parent; // Last page if split
24  
    private boolean bos;
25  
    private boolean eos;
26  
27  
    /**
28  
     * Creates a new Ogg Packet based on data read
29  
     *  from within an Ogg Page.
30  
     */
31  
    *(OggStreamPage parent, byte[] data, boolean bos, boolean eos) {
32  
        super(data);
33  
        this.parent = parent;
34  
        this.bos = bos;
35  
        this.eos = eos;
36  
    }
37  
    /**
38  
     * Creates a new Ogg Packet filled with data to
39  
     *  be later written.
40  
     * The Sid, and begin/end flags will be available
41  
     *  after the packet has been flushed.
42  
     */
43  
    *(byte[] data) {
44  
        super(data);
45  
    }
46  
47  
    protected void setParent(OggStreamPage parent) {
48  
        this.parent = parent;
49  
    }
50  
    protected void setIsBOS() {
51  
        this.bos = true;
52  
    }
53  
    protected void setIsEOS() {
54  
        this.eos = true;
55  
    }
56  
57  
    /** Unit tests only! */
58  
    protected OggStreamPage _getParent() {
59  
        return parent;
60  
    }
61  
62  
    /**
63  
     * Returns the Stream ID (Sid) that
64  
     *  this packet belongs to.
65  
     */
66  
    public int getSid() {
67  
        return parent.getSid();
68  
    }
69  
    /**
70  
     * Returns the granule position of the page
71  
     *  that this packet belongs to. The meaning
72  
     *  of the granule depends on the codec.
73  
     */
74  
    public long getGranulePosition() {
75  
        return parent.getGranulePosition();
76  
    }
77  
    /**
78  
     * Returns the sequence number within the stream
79  
     *  of the page that this packet belongs to.
80  
     * You can use this to detect when pages have
81  
     *  been lost.
82  
     */
83  
    public int getSequenceNumber() {
84  
        return parent.getSequenceNumber();
85  
    }
86  
87  
    /**
88  
     * Returns the number of bytes overhead of the {@link OggStreamPage}
89  
     *  we belong to, if we're the only packet in the page, or
90  
     *  a rough guess if we span multiple pages / share a page.
91  
     */
92  
    public int getOverheadBytes() {
93  
        if (parent == null) return 0;
94  
95  
        double ourShare = 1.0;
96  
        int ourDataLen = getData().length;
97  
        int pageDataLen = parent.getDataSize();
98  
        if (pageDataLen != ourDataLen) {
99  
            // We don't have a page to ourselves, so we can't come up
100  
            //  with a fully accurate overhead at this stage, so get close
101  
102  
            // Do we span multiple pages?
103  
            if (ourDataLen > pageDataLen) {
104  
                // Take a best guess, rounding up
105  
                int approxPages = (int)Math.ceil(ourDataLen/pageDataLen);
106  
                ourShare = approxPages;
107  
            } else {
108  
                // We're probably just a part of a larger page
109  
                // Take a rough guess
110  
                ourShare = ((double)ourDataLen) / pageDataLen;
111  
            }
112  
        }
113  
        // Take the current page's overhead, scale as needed, and return
114  
        return (int)Math.rint(ourShare * (parent.getPageSize() - pageDataLen));
115  
    }
116  
117  
    /**
118  
     * Is this the first packet in the stream?
119  
     * If so, the data should hold the magic
120  
     *  information required to identify which
121  
     *  decoder will be needed.
122  
     */
123  
    public boolean isBeginningOfStream() {
124  
        return bos;
125  
    }
126  
    /**
127  
     * Is this the last packet in the stream?
128  
     */
129  
    public boolean isEndOfStream() {
130  
        return eos;
131  
    }
132  
}

Author comment

Began life as a copy of #1032913

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032915
Snippet name: OggStreamPacket
Eternal ID of this version: #1032915/2
Text MD5: 5c04cb000b20c7e4d6d6c200c4258e79
Transpilation MD5: fe38e5a22baa73e15888f02890750f9a
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:28:42
Source code size: 4119 bytes / 132 lines
Pitched / IR pitched: No / No
Views / Downloads: 82 / 379
Version history: 1 change(s)
Referenced in: [show references]