Update with 120ms latency and stereo

This commit is contained in:
Lukas Herman
2021-07-24 13:33:41 -07:00
parent 1d496f796e
commit 32c78be33b
3 changed files with 20 additions and 6 deletions

8
examples/file/Makefile Normal file
View File

@@ -0,0 +1,8 @@
record:
gst-launch-1.0 -v pulsesrc ! audio/x-raw, format=S16LE, rate=48000, channels=2 ! filesink location=audio.raw
play: audio.raw
gst-launch-1.0 filesrc location=$^ ! rawaudioparse use-sink-caps=false \
format=pcm pcm-format=s16le sample-rate=48000 num-channels=2 ! \
audioconvert ! audioresample ! autoaudiosink

Binary file not shown.

View File

@@ -15,6 +15,12 @@ import (
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
) )
const (
sampleRate = 48000
channels = 2
sampleSize = 2
)
type AudioFile struct { type AudioFile struct {
rawReader *os.File rawReader *os.File
bufferedReader *bufio.Reader bufferedReader *bufio.Reader
@@ -25,18 +31,18 @@ type AudioFile struct {
func NewAudioFile(path string) (*AudioFile, error) { func NewAudioFile(path string) (*AudioFile, error) {
// Assume 48000 sample rate, mono channel, and S16LE interleaved // Assume 48000 sample rate, mono channel, and S16LE interleaved
latency := time.Millisecond * 20 latency := time.Millisecond * 120
readFrequency := time.Second / latency readFrequency := time.Second / latency
readLen := 48000 / readFrequency * 1 * 2 readLen := sampleRate * channels * sampleSize / int(readFrequency)
decoder, err := wave.NewDecoder(&wave.RawFormat{ decoder, err := wave.NewDecoder(&wave.RawFormat{
SampleSize: 2, SampleSize: sampleSize,
IsFloat: false, IsFloat: false,
Interleaved: true, Interleaved: true,
}) })
fmt.Printf(` fmt.Printf(`
Latency: %s Latency: %s
Read Delay: %d Hz Read Frequency: %d Hz
Buffer Len: %d bytes Buffer Len: %d bytes
`, latency, readFrequency, readLen) `, latency, readFrequency, readLen)
if err != nil { if err != nil {
@@ -68,13 +74,13 @@ func (file *AudioFile) Read() (chunk wave.Audio, release func(), err error) {
} }
} }
chunk, err = file.decoder.Decode(binary.LittleEndian, file.rawBuffer, 1) chunk, err = file.decoder.Decode(binary.LittleEndian, file.rawBuffer, channels)
if err != nil { if err != nil {
return return
} }
int16Chunk := chunk.(*wave.Int16Interleaved) int16Chunk := chunk.(*wave.Int16Interleaved)
int16Chunk.Size.SamplingRate = 48000 int16Chunk.Size.SamplingRate = sampleRate
// Slow down reading so that it matches 48 KHz // Slow down reading so that it matches 48 KHz
<-file.ticker.C <-file.ticker.C