feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
This commit is contained in:
1 parent
2fb4a8ea79
commit
6c0ee61750
2538 files changed
+675779
-4792
No files matched your search
Vendored
+203
@@ -0,0 +1,203 @@
|
||||
// Package aac decodes AAC-LC for package audio, with
|
||||
// github.com/tphakala/go-aac: in MP4 (.m4a, and fragmented .m4s as DASH
|
||||
// streams it), through package audio/mp4, and in ADTS (.aac). Import it
|
||||
// for its side effect:
|
||||
//
|
||||
// import _ "github.com/egoist/mygo/audio/aac"
|
||||
//
|
||||
// HE-AAC (AAC+, with SBR) and HE-AAC v2 it does not decode: their files
|
||||
// fail to open, and mp4.Track's Decodable says so of them.
|
||||
//
|
||||
// go-aac is under the LGPL 2.1 or later, unlike MyGo, whose license is
|
||||
// MIT: an app that imports this package links it, and takes on the LGPL's
|
||||
// terms for it.
|
||||
package aac
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/egoist/mygo/audio"
|
||||
"github.com/egoist/mygo/audio/mp4"
|
||||
aacpcm "github.com/tphakala/go-aac/pcm"
|
||||
)
|
||||
|
||||
func init() {
|
||||
mp4.RegisterCodec("mp4a", func(config []byte) (mp4.Codec, error) { return NewCodec(config) })
|
||||
open := func(r io.ReadSeeker) (audio.Decoder, error) { return NewADTSDecoder(r) }
|
||||
// The sync word of an ADTS header, of MPEG-4 or MPEG-2, with a CRC or
|
||||
// without; MP3's differ in their layer.
|
||||
for _, b := range []string{"\xff\xf1", "\xff\xf0", "\xff\xf9", "\xff\xf8"} {
|
||||
audio.RegisterFormat("aac", b, open)
|
||||
}
|
||||
}
|
||||
|
||||
// FrameLength is how many frames each AAC packet decodes to.
|
||||
const FrameLength = 1024
|
||||
|
||||
// codec decodes AAC packets, raw as in an MP4 or each with its ADTS header.
|
||||
type codec struct {
|
||||
fd *aacpcm.FrameDecoder
|
||||
pcm []byte
|
||||
}
|
||||
|
||||
// NewCodec returns a decoder of the raw AAC packets of a track configured
|
||||
// by asc, its AudioSpecificConfig: an MP4's, for package mp4.
|
||||
func NewCodec(asc []byte) (mp4.Codec, error) {
|
||||
fd, err := aacpcm.NewRawDecoder(asc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &codec{fd: fd}, nil
|
||||
}
|
||||
|
||||
func (c *codec) SampleRate() int { return c.fd.SampleRate() }
|
||||
func (c *codec) Reset() { c.fd.Reset() }
|
||||
func (c *codec) Preroll() int { return 1 }
|
||||
func (c *codec) PacketFrames() int { return FrameLength }
|
||||
|
||||
func (c *codec) Decode(dst []float32, packet []byte) ([]float32, error) {
|
||||
pcm, n, err := c.fd.DecodeFrame(c.pcm[:0], packet)
|
||||
if err != nil {
|
||||
return dst, err
|
||||
}
|
||||
c.pcm = pcm
|
||||
ch := c.fd.Channels()
|
||||
right := 0
|
||||
if ch > 1 {
|
||||
right = 2
|
||||
}
|
||||
for i := range n {
|
||||
f := pcm[i*ch*2:]
|
||||
dst = append(dst,
|
||||
float32(int16(binary.LittleEndian.Uint16(f)))/(1<<15),
|
||||
float32(int16(binary.LittleEndian.Uint16(f[right:])))/(1<<15))
|
||||
}
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
var adtsRates = [...]int{96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350}
|
||||
|
||||
// adtsHeader reads an ADTS frame's header: its length in bytes, and its
|
||||
// sample rate.
|
||||
func adtsHeader(h []byte) (length, rate int, ok bool) {
|
||||
if len(h) < 7 || h[0] != 0xff || h[1]&0xf6 != 0xf0 {
|
||||
return 0, 0, false
|
||||
}
|
||||
i := int(h[2] >> 2 & 15)
|
||||
length = int(h[3]&3)<<11 | int(h[4])<<3 | int(h[5]>>5)
|
||||
if i >= len(adtsRates) || length < 7 {
|
||||
return 0, 0, false
|
||||
}
|
||||
return length, adtsRates[i], true
|
||||
}
|
||||
|
||||
// adts decodes AAC in ADTS frames, each with a header saying how long it
|
||||
// is, which it finds all of as it opens, for the length and to seek.
|
||||
type adts struct {
|
||||
r io.ReadSeeker
|
||||
c *codec
|
||||
rate int
|
||||
offs []int64
|
||||
sizes []int
|
||||
next int // the packet decoded next
|
||||
skip int64 // frames to drop before the frame Read returns next
|
||||
buf []byte
|
||||
out []float32
|
||||
pend []float32
|
||||
}
|
||||
|
||||
// NewADTSDecoder returns a decoder of the AAC in ADTS frames r holds, as
|
||||
// an .aac file, after an ID3 tag if one is first. It decodes to stereo.
|
||||
func NewADTSDecoder(r io.ReadSeeker) (audio.Decoder, error) {
|
||||
start, err := skipID3(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
d := &adts{r: r, c: &codec{fd: aacpcm.NewADTSDecoder()}}
|
||||
var h [7]byte
|
||||
for off := start; ; {
|
||||
if _, err := r.Seek(off, io.SeekStart); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := io.ReadFull(r, h[:]); err != nil {
|
||||
break
|
||||
}
|
||||
length, rate, ok := adtsHeader(h[:])
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if d.rate == 0 {
|
||||
d.rate = rate
|
||||
}
|
||||
d.offs = append(d.offs, off)
|
||||
d.sizes = append(d.sizes, length)
|
||||
off += int64(length)
|
||||
}
|
||||
if len(d.offs) == 0 {
|
||||
return nil, errors.New("aac: not an ADTS stream")
|
||||
}
|
||||
return d, d.SetPosition(0)
|
||||
}
|
||||
|
||||
// skipID3 returns where r's audio starts: after an ID3 tag, if r starts
|
||||
// with one.
|
||||
func skipID3(r io.ReadSeeker) (int64, error) {
|
||||
var b [10]byte
|
||||
if _, err := r.Seek(0, io.SeekStart); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if _, err := io.ReadFull(r, b[:]); err != nil || string(b[:3]) != "ID3" {
|
||||
return 0, nil
|
||||
}
|
||||
return 10 + int64(b[6]&0x7f)<<21 | int64(b[7]&0x7f)<<14 | int64(b[8]&0x7f)<<7 | int64(b[9]&0x7f), nil
|
||||
}
|
||||
|
||||
func (d *adts) SampleRate() int { return d.rate }
|
||||
func (d *adts) Channels() int { return 2 }
|
||||
func (d *adts) Len() int64 { return int64(len(d.offs)) * FrameLength }
|
||||
|
||||
func (d *adts) Read(dst []float32) (int, error) {
|
||||
for len(d.pend) == 0 {
|
||||
if d.next >= len(d.offs) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
size := d.sizes[d.next]
|
||||
if cap(d.buf) < size {
|
||||
d.buf = make([]byte, size)
|
||||
}
|
||||
d.buf = d.buf[:size]
|
||||
if _, err := d.r.Seek(d.offs[d.next], io.SeekStart); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if _, err := io.ReadFull(d.r, d.buf); err != nil {
|
||||
return 0, io.EOF
|
||||
}
|
||||
d.next++
|
||||
out, err := d.c.Decode(d.out[:0], d.buf)
|
||||
d.out = out
|
||||
if err != nil {
|
||||
continue // a packet gone bad: on to the next
|
||||
}
|
||||
drop := min(d.skip, int64(len(out)/2))
|
||||
d.skip -= drop
|
||||
d.pend = out[drop*2:]
|
||||
}
|
||||
n := copy(dst[:len(dst)/2*2], d.pend)
|
||||
d.pend = d.pend[n:]
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// SetPosition goes to the packet holding frame, from the one before, whose
|
||||
// sound overlaps it, and drops what comes before frame.
|
||||
func (d *adts) SetPosition(frame int64) error {
|
||||
frame = min(max(frame, 0), d.Len())
|
||||
i := int(frame / FrameLength)
|
||||
start := max(0, i-1)
|
||||
d.c.Reset()
|
||||
d.next = start
|
||||
d.skip = frame - int64(start)*FrameLength
|
||||
d.pend = nil
|
||||
return nil
|
||||
}
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
package aac_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
_ "github.com/egoist/mygo/audio/aac"
|
||||
_ "github.com/egoist/mygo/audio/flac"
|
||||
"github.com/egoist/mygo/audio/internal/audiotest"
|
||||
"github.com/egoist/mygo/audio/mp4"
|
||||
)
|
||||
|
||||
// The source the files were encoded from.
|
||||
func source(t *testing.T) []float32 {
|
||||
return audiotest.ReadAll(t, audiotest.Open(t, audiotest.Read(t, "../flac/testdata/tone.flac"), "flac"))
|
||||
}
|
||||
|
||||
// AAC's encoder adds a packet of priming before the sound.
|
||||
const prime = 1024
|
||||
|
||||
// An AAC .m4a drops the encoder's priming, as its edit list says, so it
|
||||
// lines up with what it was encoded from and is as long. A seek decodes the
|
||||
// packet before, whose sound overlaps the one sought to.
|
||||
func TestMP4(t *testing.T) {
|
||||
b := audiotest.Read(t, "testdata/tone.m4a")
|
||||
tr, err := mp4.ReadTrack(bytes.NewReader(b), int64(len(b)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tr.Codec != "mp4a" || tr.Duration != 2*time.Second || !tr.Decodable() {
|
||||
t.Fatalf("%+v", tr)
|
||||
}
|
||||
d := audiotest.Open(t, b, "mp4")
|
||||
if d.SampleRate() != 44100 || d.Channels() != 2 || d.Len() != 88200 {
|
||||
t.Fatalf("%d Hz, %d channels, %d frames", d.SampleRate(), d.Channels(), d.Len())
|
||||
}
|
||||
got := audiotest.ReadAll(t, d)
|
||||
if len(got) != 88200*2 {
|
||||
t.Fatalf("decoded %d samples", len(got))
|
||||
}
|
||||
if db := audiotest.SNR(got, source(t)); db < 20 {
|
||||
t.Errorf("%.1f dB from the source", db)
|
||||
}
|
||||
// Noise the encoder substituted is random each time it's decoded.
|
||||
audiotest.SeekLands(t, d, got, 0.05, 0, 1, 4096, 50000, 77777)
|
||||
}
|
||||
|
||||
// An AAC .m4a cut into fragments, as a stream for DASH is, plays what it
|
||||
// was encoded from too, with its length from its segment index. Its movie
|
||||
// header has no edit list, so the encoder's priming plays; the padding of
|
||||
// its last packet doesn't, as its last packet's duration says.
|
||||
func TestFragmented(t *testing.T) {
|
||||
b := audiotest.Read(t, "testdata/fragmented.m4a")
|
||||
d := audiotest.Open(t, b, "mp4")
|
||||
length := d.Len()
|
||||
got := audiotest.ReadAll(t, d)
|
||||
src := source(t)
|
||||
if len(got) != len(src)+prime*2 {
|
||||
t.Fatalf("decoded %d samples, the source %d", len(got), len(src))
|
||||
}
|
||||
if length*2 != int64(len(got)) || d.Len() != length {
|
||||
t.Errorf("length %d, then %d, though %d frames decode", length, d.Len(), len(got)/2)
|
||||
}
|
||||
if db := audiotest.SNR(got[prime*2:], src); db < 20 {
|
||||
t.Errorf("%.1f dB from the source", db)
|
||||
}
|
||||
audiotest.SeekLands(t, d, got, 0.05, 0, 1, 4096, 50000, 77777)
|
||||
}
|
||||
|
||||
// AAC's packets each decode to 1024 frames, whatever the file says they
|
||||
// last: a seek lands where decoding straight through has the frame. Some
|
||||
// files have a packet near the start a few frames shorter, which would put
|
||||
// every seek past it that much off.
|
||||
func TestPacketsOfOtherDurations(t *testing.T) {
|
||||
b := bytes.Clone(audiotest.Read(t, "testdata/fragmented.m4a"))
|
||||
// The first fragment's packets last 1016 frames, its header says.
|
||||
at := bytes.Index(b, []byte("tfhd"))
|
||||
flags := binary.BigEndian.Uint32(b[at+4:]) & 0xffffff
|
||||
if flags&0x08 == 0 {
|
||||
t.Fatal("the fragment has no default duration")
|
||||
}
|
||||
at += 4 + 8
|
||||
if flags&0x01 != 0 {
|
||||
at += 8
|
||||
}
|
||||
if flags&0x02 != 0 {
|
||||
at += 4
|
||||
}
|
||||
if binary.BigEndian.Uint32(b[at:]) != 1024 {
|
||||
t.Fatal("no default duration of 1024 where expected")
|
||||
}
|
||||
binary.BigEndian.PutUint32(b[at:], 1016)
|
||||
d := audiotest.Open(t, b, "mp4")
|
||||
got := audiotest.ReadAll(t, d)
|
||||
for _, frame := range []int64{30000, 60000, 60001} {
|
||||
if err := d.SetPosition(frame); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var read []float32
|
||||
buf := make([]float32, 4096)
|
||||
for len(read) < 44100 {
|
||||
n, err := d.Read(buf)
|
||||
read = append(read, buf[:n]...)
|
||||
if err != nil || n == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if db := audiotest.SNR(read, got[frame*2:]); db < 20 {
|
||||
t.Errorf("seek to %d: %.1f dB from decoding straight through", frame, db)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// An .aac of ADTS frames plays what it was encoded from, after the
|
||||
// encoder's priming, which nothing says to drop.
|
||||
func TestADTS(t *testing.T) {
|
||||
b := audiotest.Read(t, "testdata/tone.aac")
|
||||
d := audiotest.Open(t, b, "aac")
|
||||
if d.SampleRate() != 44100 || d.Channels() != 2 {
|
||||
t.Fatalf("%d Hz, %d channels", d.SampleRate(), d.Channels())
|
||||
}
|
||||
got := audiotest.ReadAll(t, d)
|
||||
if int64(len(got)) != d.Len()*2 || len(got) < (88200+prime)*2 {
|
||||
t.Fatalf("decoded %d samples, length %d", len(got), d.Len())
|
||||
}
|
||||
if db := audiotest.SNR(got[prime*2:], source(t)); db < 20 {
|
||||
t.Errorf("%.1f dB from the source", db)
|
||||
}
|
||||
audiotest.SeekLands(t, d, got, 0.05, 0, 1, 4096, 50000, 77777)
|
||||
|
||||
// After an ID3 tag too.
|
||||
tag := append([]byte("ID3\x04\x00\x00\x00\x00\x00\x20"), make([]byte, 32)...)
|
||||
d = audiotest.Open(t, append(tag, b...), "aac")
|
||||
if again := audiotest.ReadAll(t, d); len(again) != len(got) {
|
||||
t.Errorf("decoded %d samples after an ID3 tag, %d without", len(again), len(got))
|
||||
}
|
||||
}
|
||||
|
||||
// HE-AAC, with SBR, isn't decoded: a track of it says so.
|
||||
func TestHEAAC(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
config []byte
|
||||
want bool
|
||||
}{
|
||||
{"AAC-LC", []byte{0x12, 0x10}, true},
|
||||
{"HE-AAC", []byte{0x2b, 0x12, 0x08}, false},
|
||||
} {
|
||||
tr := mp4.Track{Codec: "mp4a", Config: tt.config}
|
||||
if got := tr.Decodable(); got != tt.want {
|
||||
t.Errorf("%s: decodable %v", tt.name, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
Reference in new issue
Block a user