media: support voice messages
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
go test fuzz v1
|
||||
[]byte("00")
|
||||
@@ -0,0 +1,66 @@
|
||||
// Package waveform implements encoding and decoding of a Telegram waveform.
|
||||
//
|
||||
// Telegram waveforms consist of packed 5-bit values. The values are packed
|
||||
// into a byte stream, meaning that the actual values cross the byte boundary.
|
||||
//
|
||||
// The following diagram explains the format:
|
||||
//
|
||||
// [210|43210][0|43210|43][3210|4321][10|43210|4]...
|
||||
// [111|00000][3|22222|11][4444|3333][66|55555|4]...
|
||||
//
|
||||
// Explanation of diagram:
|
||||
// - The []'s enclose byte boundaries.
|
||||
// - The |s represent separation between waveform values.
|
||||
// - The numbers in the first row indicate the binary power.
|
||||
// - The numbers in the second row indicate the corresponding waveform index.
|
||||
package waveform
|
||||
|
||||
import "math"
|
||||
|
||||
// NormalizeWaveform normalizes a waveform by bounding the values to the range
|
||||
// [0, 32] which is required for the encoding to work.
|
||||
func NormalizeWaveform(waveform []int) (normalized []byte) {
|
||||
normalized = make([]byte, len(waveform))
|
||||
var waveformMax int
|
||||
for _, v := range waveform {
|
||||
waveformMax = max(waveformMax, v)
|
||||
}
|
||||
for i, v := range waveform {
|
||||
normalized[i] = byte(math.Round(float64(v) / float64(max(waveformMax/256, 1))))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Encode normalizes and encodes the input Matrix waveform into a Telegram
|
||||
// waveform.
|
||||
func Encode(waveform []int) []byte {
|
||||
bytesCount := (len(waveform)*5 + 7) / 8
|
||||
result := make([]byte, bytesCount+1)
|
||||
|
||||
var bitShift int
|
||||
for i, v := range NormalizeWaveform(waveform) {
|
||||
result[i*5/8] |= v << bitShift
|
||||
result[i*5/8+1] |= v >> (8 - bitShift)
|
||||
bitShift = (bitShift + 5) % 8
|
||||
}
|
||||
return result[:bytesCount]
|
||||
}
|
||||
|
||||
// Decode decodes a Telegram waveform into a waveform usable by Matrix.
|
||||
func Decode(waveform []byte) []int {
|
||||
numValues := len(waveform) * 8 / 5
|
||||
result := make([]int, numValues)
|
||||
|
||||
var bitShift int
|
||||
for i := 0; i < numValues; i++ {
|
||||
var val byte
|
||||
val |= waveform[i*5/8] >> bitShift
|
||||
if i*5/8+1 < len(waveform) {
|
||||
val |= waveform[i*5/8+1] << (8 - bitShift)
|
||||
}
|
||||
result[i] = int(val) & 0b00011111
|
||||
bitShift = (bitShift + 5) % 8
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package waveform_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/connector/waveform"
|
||||
)
|
||||
|
||||
func TestEncode(t *testing.T) {
|
||||
assert.Equal(t, []byte{0x01}, waveform.Encode([]int{1}))
|
||||
assert.Equal(t, []byte{0xff, 0x03}, waveform.Encode([]int{31, 31}))
|
||||
assert.Equal(t, []byte{0x41, 0x0c, 0x52, 0xcc, 0x41}, waveform.Encode([]int{1, 2, 3, 4, 5, 6, 7, 8}))
|
||||
assert.Equal(t, []byte{0xff, 0xff, 0xff, 0xff, 0xff}, waveform.Encode([]int{31, 31, 31, 31, 31, 31, 31, 31}))
|
||||
}
|
||||
|
||||
func TestDecode(t *testing.T) {
|
||||
// assert.Equal(t, []int{0x01}, waveform.Decode([]byte{1}))
|
||||
// assert.Equal(t, []int{0x01, 0x10, 0x00}, waveform.Decode([]byte{1, 2}))
|
||||
// assert.Equal(t, []int{0x01, 0x10, 0x00, 0x06, 0x00, 0x02, 0x14, 0x00}, waveform.Decode([]byte{1, 2, 3, 4, 5}))
|
||||
}
|
||||
|
||||
func FuzzRoundtrip(f *testing.F) {
|
||||
f.Add([]byte{0x01})
|
||||
|
||||
f.Fuzz(func(t *testing.T, w []byte) {
|
||||
wf := make([]int, len(w))
|
||||
for i, v := range waveform.NormalizeWaveform(wf) {
|
||||
wf[i] = int(v)
|
||||
}
|
||||
encoded := waveform.Encode(wf)
|
||||
decoded := waveform.Decode(encoded)
|
||||
|
||||
// Sometimes, the decoded wavefeorm might have an extra value if the
|
||||
// last value of the encoded waveform is packed into the 3
|
||||
// least-significant bits of the last byte. In that case, it's unclear
|
||||
// whether the waveform contains a 0b00000 as the last byte or if there
|
||||
// shouldn't have been anything there.
|
||||
if len(wf) != len(decoded) {
|
||||
assert.Len(t, decoded, len(wf)+1)
|
||||
wf = append(wf, 0x00)
|
||||
}
|
||||
assert.Equal(t, wf, decoded)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user