Copy of github.com/askrobots/network-computer (MIT): a desk you reach from anything. The original lives on GitHub.

2 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/demo/network-computer.git
HTTPS
https://gitstable.com/git/demo/network-computer.git
SSH
ssh://git@gitstable.com:2222/demo/network-computer.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

GitStable demo README: say this is a copy and where the original is cde82c2 13 hours, 42 minutes ago History
oggopus.go · 2.6 KB · 104 lines
1// Package oggopus reads individual Opus packets out of an Ogg stream.
2//
3// An Ogg page can hold several Opus packets, and a packet can span pages.
4// Sending a whole page as one RTP payload glues packets together into
5// something that is not a valid Opus packet, which the receiver decodes as
6// garbage and then has to conceal. RTP wants exactly one Opus packet each.
7package oggopus
8
9import (
10 "bufio"
11 "bytes"
12 "errors"
13 "io"
14 "time"
15)
16
17// Reader yields Opus packets in order, skipping the OpusHead and OpusTags
18// header packets.
19type Reader struct {
20 r *bufio.Reader
21 pending []byte // a packet still continuing onto the next page
22 queue [][]byte // complete packets not yet returned
23}
24
25func NewReader(r io.Reader) *Reader { return &Reader{r: bufio.NewReaderSize(r, 64<<10)} }
26
27// Next returns the next Opus packet.
28func (o *Reader) Next() ([]byte, error) {
29 for len(o.queue) == 0 {
30 if err := o.readPage(); err != nil {
31 return nil, err
32 }
33 }
34 p := o.queue[0]
35 o.queue = o.queue[1:]
36 return p, nil
37}
38
39var errNotOgg = errors.New("oggopus: not an Ogg page")
40
41func (o *Reader) readPage() error {
42 var hdr [27]byte
43 if _, err := io.ReadFull(o.r, hdr[:]); err != nil {
44 return err
45 }
46 if string(hdr[:4]) != "OggS" {
47 return errNotOgg
48 }
49 segs := make([]byte, int(hdr[26]))
50 if _, err := io.ReadFull(o.r, segs); err != nil {
51 return err
52 }
53 total := 0
54 for _, l := range segs {
55 total += int(l)
56 }
57 data := make([]byte, total)
58 if _, err := io.ReadFull(o.r, data); err != nil {
59 return err
60 }
61 off := 0
62 for _, l := range segs {
63 o.pending = append(o.pending, data[off:off+int(l)]...)
64 off += int(l)
65 if l < 255 { // a lacing value under 255 ends the packet
66 p := o.pending
67 o.pending = nil
68 if !bytes.HasPrefix(p, []byte("OpusHead")) && !bytes.HasPrefix(p, []byte("OpusTags")) && len(p) > 0 {
69 o.queue = append(o.queue, p)
70 }
71 }
72 }
73 return nil
74}
75
76// Duration is how much audio an Opus packet holds, from its TOC byte
77// (RFC 6716 section 3.1). Zero if the packet is malformed.
78func Duration(p []byte) time.Duration {
79 if len(p) == 0 {
80 return 0
81 }
82 toc := p[0]
83 cfg := toc >> 3
84 var frame time.Duration
85 switch {
86 case cfg < 12: // SILK: 10, 20, 40, 60 ms
87 frame = []time.Duration{10, 20, 40, 60}[cfg%4] * time.Millisecond
88 case cfg < 16: // Hybrid: 10, 20 ms
89 frame = []time.Duration{10, 20}[cfg%2] * time.Millisecond
90 default: // CELT: 2.5, 5, 10, 20 ms
91 frame = []time.Duration{2500, 5000, 10000, 20000}[cfg%4] * time.Microsecond
92 }
93 frames := 1
94 switch toc & 3 {
95 case 1, 2:
96 frames = 2
97 case 3:
98 if len(p) < 2 {
99 return 0
100 }
101 frames = int(p[1] & 0x3f)
102 }
103 return time.Duration(frames) * frame
104}