Checkpoint
This commit is contained in:
125 files changed
+59376
No files matched your search
@@ -0,0 +1,44 @@
|
||||
package audio
|
||||
|
||||
// Complex128 represents a slice of 64-bit floating-point linear PCM encoded
|
||||
// audio samples, in the range of -1 to +1.
|
||||
type Complex128 []complex128
|
||||
|
||||
// Len implements the Slice interface.
|
||||
func (p Complex128) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
|
||||
// Cap implements the Slice interface.
|
||||
func (p Complex128) Cap() int {
|
||||
return cap(p)
|
||||
}
|
||||
|
||||
// At implements the Slice interface.
|
||||
func (p Complex128) At(i int) float64 {
|
||||
return real(p[i])
|
||||
}
|
||||
|
||||
// Set implements the Slice interface.
|
||||
func (p Complex128) Set(i int, s float64) {
|
||||
p[i] = complex(s, 0)
|
||||
}
|
||||
|
||||
// Slice implements the Slice interface.
|
||||
func (p Complex128) Slice(low, high int) Slice {
|
||||
return p[low:high]
|
||||
}
|
||||
|
||||
// Make implements the Slice interface.
|
||||
func (p Complex128) Make(length, capacity int) Slice {
|
||||
return make(Float64, length, capacity)
|
||||
}
|
||||
|
||||
// CopyTo implements the Slice interface.
|
||||
func (p Complex128) CopyTo(dst Slice) int {
|
||||
d, ok := dst.(Complex128)
|
||||
if ok {
|
||||
return copy(d, p)
|
||||
}
|
||||
return sliceCopy(dst, p)
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package audio
|
||||
|
||||
// Float32 represents a slice of 32-bit floating-point linear PCM encoded
|
||||
// audio samples, in the range of -1 to +1.
|
||||
type Float32 []float32
|
||||
|
||||
// Len implements the Slice interface.
|
||||
func (p Float32) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
|
||||
// Cap implements the Slice interface.
|
||||
func (p Float32) Cap() int {
|
||||
return cap(p)
|
||||
}
|
||||
|
||||
// At implements the Slice interface.
|
||||
func (p Float32) At(i int) float64 {
|
||||
return float64(p[i])
|
||||
}
|
||||
|
||||
// Set implements the Slice interface.
|
||||
func (p Float32) Set(i int, s float64) {
|
||||
p[i] = float32(s)
|
||||
}
|
||||
|
||||
// Slice implements the Slice interface.
|
||||
func (p Float32) Slice(low, high int) Slice {
|
||||
return p[low:high]
|
||||
}
|
||||
|
||||
// Make implements the Slice interface.
|
||||
func (p Float32) Make(length, capacity int) Slice {
|
||||
return make(Float32, length, capacity)
|
||||
}
|
||||
|
||||
// CopyTo implements the Slice interface.
|
||||
func (p Float32) CopyTo(dst Slice) int {
|
||||
d, ok := dst.(Float32)
|
||||
if ok {
|
||||
return copy(d, p)
|
||||
}
|
||||
return sliceCopy(dst, p)
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package audio
|
||||
|
||||
// Float64 represents a slice of 64-bit floating-point linear PCM encoded
|
||||
// audio samples, in the range of -1 to +1.
|
||||
type Float64 []float64
|
||||
|
||||
// Len implements the Slice interface.
|
||||
func (p Float64) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
|
||||
// Cap implements the Slice interface.
|
||||
func (p Float64) Cap() int {
|
||||
return cap(p)
|
||||
}
|
||||
|
||||
// At implements the Slice interface.
|
||||
func (p Float64) At(i int) float64 {
|
||||
return p[i]
|
||||
}
|
||||
|
||||
// Set implements the Slice interface.
|
||||
func (p Float64) Set(i int, s float64) {
|
||||
p[i] = s
|
||||
}
|
||||
|
||||
// Slice implements the Slice interface.
|
||||
func (p Float64) Slice(low, high int) Slice {
|
||||
return p[low:high]
|
||||
}
|
||||
|
||||
// Make implements the Slice interface.
|
||||
func (p Float64) Make(length, capacity int) Slice {
|
||||
return make(Float64, length, capacity)
|
||||
}
|
||||
|
||||
// CopyTo implements the Slice interface.
|
||||
func (p Float64) CopyTo(dst Slice) int {
|
||||
d, ok := dst.(Float64)
|
||||
if ok {
|
||||
return copy(d, p)
|
||||
}
|
||||
return sliceCopy(dst, p)
|
||||
}
|
||||
|
||||
// ToFloat64 converts src to a Float64 slice.
|
||||
func ToFloat64(src Slice) Float64 {
|
||||
if s, ok := src.(Float64); ok {
|
||||
return s
|
||||
}
|
||||
|
||||
dst := make(Float64, src.Len())
|
||||
for i, l := 0, src.Len(); i < l; i++ {
|
||||
dst[i] = src.At(i)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// Int16 represents a signed 16-bit linear PCM encoded audio sample.
|
||||
type Int16 []int16
|
||||
|
||||
// Int16ToFloat64 converts a Int16 encoded audio sample to Float64.
|
||||
func Int16ToFloat64(s int16) float64 {
|
||||
return float64(s) / float64(math.MaxInt16)
|
||||
}
|
||||
|
||||
// Float64ToInt16 converts a Float64 encoded audio sample to Int16.
|
||||
func Float64ToInt16(s float64) int16 {
|
||||
return int16(math.Floor((s * float64(math.MaxInt16)) + 0.5))
|
||||
}
|
||||
|
||||
// Len implements the Slice interface.
|
||||
func (p Int16) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
|
||||
// Cap implements the Slice interface.
|
||||
func (p Int16) Cap() int {
|
||||
return cap(p)
|
||||
}
|
||||
|
||||
// At implements the Slice interface.
|
||||
func (p Int16) At(i int) float64 {
|
||||
return Int16ToFloat64(p[i])
|
||||
}
|
||||
|
||||
// Set implements the Slice interface.
|
||||
func (p Int16) Set(i int, s float64) {
|
||||
p[i] = Float64ToInt16(s)
|
||||
}
|
||||
|
||||
// Slice implements the Slice interface.
|
||||
func (p Int16) Slice(low, high int) Slice {
|
||||
return p[low:high]
|
||||
}
|
||||
|
||||
// Make implements the Slice interface.
|
||||
func (p Int16) Make(length, capacity int) Slice {
|
||||
return make(Int16, length, capacity)
|
||||
}
|
||||
|
||||
// CopyTo implements the Slice interface.
|
||||
func (p Int16) CopyTo(dst Slice) int {
|
||||
if d, ok := dst.(Int16); ok {
|
||||
return copy(d, p)
|
||||
}
|
||||
if d, ok := dst.(Int8); ok {
|
||||
for i, v := range p {
|
||||
d[i] = int8(v >> 8)
|
||||
}
|
||||
return p.Len()
|
||||
}
|
||||
return sliceCopy(dst, p)
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package audio
|
||||
|
||||
import "math"
|
||||
|
||||
// Int8 represents a signed 8-bit linear PCM encoded audio sample.
|
||||
type Int8 []int8
|
||||
|
||||
// Int8ToFloat64 converts a Int8 encoded audio sample to Float64.
|
||||
func Int8ToFloat64(s int8) float64 {
|
||||
return float64(s) / float64(math.MaxInt8)
|
||||
}
|
||||
|
||||
// Float64ToInt8 converts a Float64 encoded audio sample to Int8.
|
||||
func Float64ToInt8(s float64) int8 {
|
||||
return int8(math.Floor((s * float64(math.MaxInt8)) + 0.5))
|
||||
}
|
||||
|
||||
// Len implements the Slice interface.
|
||||
func (p Int8) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
|
||||
// Cap implements the Slice interface.
|
||||
func (p Int8) Cap() int {
|
||||
return cap(p)
|
||||
}
|
||||
|
||||
// At implements the Slice interface.
|
||||
func (p Int8) At(i int) float64 {
|
||||
return Int8ToFloat64(p[i])
|
||||
}
|
||||
|
||||
// Set implements the Slice interface.
|
||||
func (p Int8) Set(i int, s float64) {
|
||||
p[i] = Float64ToInt8(s)
|
||||
}
|
||||
|
||||
// Slice implements the Slice interface.
|
||||
func (p Int8) Slice(low, high int) Slice {
|
||||
return p[low:high]
|
||||
}
|
||||
|
||||
// Make implements the Slice interface.
|
||||
func (p Int8) Make(length, capacity int) Slice {
|
||||
return make(Int8, length, capacity)
|
||||
}
|
||||
|
||||
// CopyTo implements the Slice interface.
|
||||
func (p Int8) CopyTo(dst Slice) int {
|
||||
if d, ok := dst.(Int8); ok {
|
||||
return copy(d, p)
|
||||
}
|
||||
if d, ok := dst.(Int16); ok {
|
||||
for i, v := range p {
|
||||
d[i] = int16(v) * 255
|
||||
}
|
||||
return p.Len()
|
||||
}
|
||||
return sliceCopy(dst, p)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package audio
|
||||
|
||||
func Deinterleave[T any](dst [][]T, src []T) {
|
||||
if len(src) == 0 {
|
||||
return
|
||||
} else if len(dst) == 1 {
|
||||
copy(dst[0], src)
|
||||
return
|
||||
}
|
||||
|
||||
c := len(dst)
|
||||
for i, s := range src {
|
||||
dst[i%c][i/c] = s
|
||||
}
|
||||
}
|
||||
|
||||
func Interleave[T any](dst []T, src [][]T) {
|
||||
if len(src) == 0 {
|
||||
return
|
||||
} else if len(src) == 1 {
|
||||
copy(dst, src[0])
|
||||
return
|
||||
}
|
||||
|
||||
c := len(src)
|
||||
for i := range dst {
|
||||
dst[i] = src[i%c][i/c]
|
||||
}
|
||||
}
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"git.maze.io/go/bragi/internal/resample"
|
||||
"periph.io/x/conn/v3/physic"
|
||||
)
|
||||
|
||||
// Slice is a generic audio slice, it can conceptually be thought of as a
|
||||
// slice of some audio encoding type.
|
||||
//
|
||||
// Conversion between two encoded audio slices is as simple as:
|
||||
//
|
||||
// dst, ok := src.(MuLaw)
|
||||
// if !ok {
|
||||
// // Create a new slice of the target encoding and copy the samples over
|
||||
// // because src is not MuLaw encoded.
|
||||
// dst = make(MuLaw, src.Len())
|
||||
// src.CopyTo(dst)
|
||||
// }
|
||||
type Slice interface {
|
||||
// Len returns the number of elements in the slice.
|
||||
//
|
||||
// Equivalent slice syntax:
|
||||
//
|
||||
// len(b)
|
||||
Len() int
|
||||
|
||||
// Cap returns the number of elements in the slice.
|
||||
//
|
||||
// Equivalent slice syntax:
|
||||
//
|
||||
// cap(b)
|
||||
Cap() int
|
||||
|
||||
// Set sets the specified index in the slice to the specified Float64
|
||||
// encoded audio sample, s.
|
||||
//
|
||||
// If the slice's audio samples are not stored in Float64 encoding, then
|
||||
// the sample should be converted to the slice's internal format and then
|
||||
// stored.
|
||||
//
|
||||
// Just like slices, slice indices must be non-negative; and no greater
|
||||
// than (Len() - 1), or else a panic may occur.
|
||||
//
|
||||
// Equivalent slice syntax:
|
||||
//
|
||||
// b[index] = s
|
||||
// -> b.Set(index, s)
|
||||
//
|
||||
Set(index int, s float64)
|
||||
|
||||
// At returns the Float64 encoded audio sample at the specified index in
|
||||
// the slice.
|
||||
//
|
||||
// If the slice's audio samples are not stored in Float64 encoding, then
|
||||
// the sample should be converted to Float64 encoding, and subsequently
|
||||
// returned.
|
||||
//
|
||||
// Just like slices, slice indices must be non-negative; and no greater
|
||||
// than (Len() - 1), or else a panic may occur.
|
||||
//
|
||||
// Equivalent slice syntax:
|
||||
//
|
||||
// b[index]
|
||||
// -> b.At(index)
|
||||
//
|
||||
At(index int) float64
|
||||
|
||||
// Slice returns a new slice of the slice, using the low and high
|
||||
// parameters.
|
||||
//
|
||||
// Equivalent slice syntax:
|
||||
//
|
||||
// b[low:high]
|
||||
// -> b.Slice(low, high)
|
||||
//
|
||||
// b[2:]
|
||||
// -> b.Slice(2, a.Len())
|
||||
//
|
||||
// b[:3]
|
||||
// -> b.Slice(0, 3)
|
||||
//
|
||||
// b[:]
|
||||
// -> b.Slice(0, a.Len())
|
||||
//
|
||||
Slice(low, high int) Slice
|
||||
|
||||
// Make creates and returns a new slice of this slices type. This allows
|
||||
// allocating a new slice of exactly the same type for lossless copying of
|
||||
// data without knowing about the underlying type.
|
||||
//
|
||||
// It is exactly the same syntax as the make builtin:
|
||||
//
|
||||
// make(MuLaw, len, cap)
|
||||
//
|
||||
// Where cap cannot be less than len.
|
||||
Make(length, capacity int) Slice
|
||||
|
||||
// CopyTo operates exactly like the copy builtin, but this slice is always
|
||||
// the source operand. Equivalent slice syntax:
|
||||
//
|
||||
// copy(dst, src)
|
||||
// -> src.CopyTo(dst)
|
||||
//
|
||||
CopyTo(dst Slice) int
|
||||
}
|
||||
|
||||
// sliceCopy copies copies audio samples from the source slice to the
|
||||
// destination slice. Returns the number of elements copied, which is the
|
||||
// minimum of the dst.Len() and src.Len() values.
|
||||
func sliceCopy(dst, src Slice) int {
|
||||
var i int
|
||||
for i = 0; i < src.Len() && i < dst.Len(); i++ {
|
||||
dst.Set(i, src.At(i))
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
// Min returns the minimum value in the Slice.
|
||||
func Min(s Slice) float64 {
|
||||
if s.Len() == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
min := math.MaxFloat64
|
||||
for i, l := 0, s.Len(); i < l; i++ {
|
||||
if v := s.At(i); v < min {
|
||||
min = v
|
||||
}
|
||||
}
|
||||
return min
|
||||
}
|
||||
|
||||
// Max returns the minimum value in the Slice.
|
||||
func Max(s Slice) float64 {
|
||||
if s.Len() == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
max := -math.MaxFloat64
|
||||
for i, l := 0, s.Len(); i < l; i++ {
|
||||
if v := s.At(i); v > max {
|
||||
max = v
|
||||
}
|
||||
}
|
||||
return max
|
||||
}
|
||||
|
||||
// Limits returns the minimum and maximum value in the Slice.
|
||||
func Limits(s Slice) (min, max float64) {
|
||||
if s.Len() == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
min = +math.MaxFloat64
|
||||
max = -math.MaxFloat64
|
||||
for i, l := 0, s.Len(); i < l; i++ {
|
||||
v := s.At(i)
|
||||
if v < min {
|
||||
min = v
|
||||
}
|
||||
if v > max {
|
||||
max = v
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Normalize the samples in the buffer.
|
||||
func Normalize(s Slice) {
|
||||
min, max := Limits(s)
|
||||
for i, l := 0, s.Len(); i < l; i++ {
|
||||
val := s.At(i)
|
||||
s.Set(i, 2*((val-min)/(max-min))-1)
|
||||
}
|
||||
}
|
||||
|
||||
// Saturate the samples in the buffer to fit in [-1, 1].
|
||||
func Saturate(s Slice) {
|
||||
for i, l := 0, s.Len(); i < l; i++ {
|
||||
v := s.At(i)
|
||||
if v > 1 {
|
||||
s.Set(i, 1)
|
||||
} else if v < -1 {
|
||||
s.Set(i, -1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Resample an audio slice.
|
||||
func Resample(dst, src Slice, dstRate, srcRate physic.Frequency, channels, quality int) (read, written int) {
|
||||
d := ToFloat64(dst)
|
||||
s := ToFloat64(src)
|
||||
r := resample.NewWithSkipZeros(channels, int(srcRate/physic.Hertz), int(dstRate/physic.Hertz), quality)
|
||||
read, written = r.ProcessFloat64(0, s, d)
|
||||
d.CopyTo(dst)
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package audio
|
||||
|
||||
// InterleavedStereoToMono converts interleaved stereo to mono samples.
|
||||
//
|
||||
// The size of src should be twice the size of dst. No bounds checking
|
||||
// is performed while writing to dst.
|
||||
func InterleavedStereoToMono(dst, src Slice) {
|
||||
var (
|
||||
i, j int
|
||||
l = src.Len() / 2
|
||||
)
|
||||
for ; i < l; i, j = i+1, j+2 {
|
||||
dst.Set(i, (src.At(j)+src.At(j+1))/2)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package audio
|
||||
|
||||
import "math"
|
||||
|
||||
// Uint8 represents a unsigned 8-bit linear PCM encoded audio sample.
|
||||
type Uint8 []uint8
|
||||
|
||||
// Uint8ToFloat64 converts a Uint8 encoded audio sample to Float64.
|
||||
func Uint8ToFloat64(s uint8) float64 {
|
||||
// In 0 to 1 range
|
||||
f := float64(s) / float64(math.MaxUint8)
|
||||
|
||||
// Switch to -1 to +1 range
|
||||
f *= 2
|
||||
f--
|
||||
return f
|
||||
}
|
||||
|
||||
// Float64ToUint8 converts a Float64 encoded audio sample to Uint8.
|
||||
func Float64ToUint8(s float64) uint8 {
|
||||
// In -1 to +1 range, switch to 0 to 1
|
||||
s++
|
||||
s /= 2
|
||||
return uint8(math.Floor((s * float64(math.MaxUint8)) + 0.5))
|
||||
}
|
||||
|
||||
// Len implements the Slice interface.
|
||||
func (p Uint8) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
|
||||
// Cap implements the Slice interface.
|
||||
func (p Uint8) Cap() int {
|
||||
return cap(p)
|
||||
}
|
||||
|
||||
// At implements the Slice interface.
|
||||
func (p Uint8) At(i int) float64 {
|
||||
return Uint8ToFloat64(p[i])
|
||||
}
|
||||
|
||||
// Set implements the Slice interface.
|
||||
func (p Uint8) Set(i int, s float64) {
|
||||
p[i] = Float64ToUint8(s)
|
||||
}
|
||||
|
||||
// Slice implements the Slice interface.
|
||||
func (p Uint8) Slice(low, high int) Slice {
|
||||
return p[low:high]
|
||||
}
|
||||
|
||||
// Make implements the Slice interface.
|
||||
func (p Uint8) Make(length, capacity int) Slice {
|
||||
return make(Uint8, length, capacity)
|
||||
}
|
||||
|
||||
// CopyTo implements the Slice interface.
|
||||
func (p Uint8) CopyTo(dst Slice) int {
|
||||
d, ok := dst.(Uint8)
|
||||
if ok {
|
||||
return copy(d, p)
|
||||
}
|
||||
return sliceCopy(dst, p)
|
||||
}
|
||||
Reference in new issue
Block a user