From caffd38e6d288f4ae50072cd30b99694cb7b026a Mon Sep 17 00:00:00 2001 From: "Tobias Wellnitz, DH1TW" Date: Fri, 10 Dec 2021 01:34:38 +0100 Subject: [PATCH] accept input samplingrates other than 48kHz --- audio/sinks/pbWriter/options.go | 10 ---------- audio/sinks/pbWriter/pbWriter.go | 13 ++++++------- cmd/check_parameters.go | 3 +-- cmd/client_nats.go | 3 +-- cmd/server_nats.go | 3 +-- 5 files changed, 9 insertions(+), 23 deletions(-) diff --git a/audio/sinks/pbWriter/options.go b/audio/sinks/pbWriter/options.go index 08890b8..c55f4b3 100644 --- a/audio/sinks/pbWriter/options.go +++ b/audio/sinks/pbWriter/options.go @@ -13,7 +13,6 @@ type Options struct { DeviceName string Encoder audiocodec.Encoder Channels int - Samplerate float64 FramesPerBuffer int UserID string ToWireCb func([]byte) @@ -28,15 +27,6 @@ func Channels(chs int) Option { } } -// Samplerate is a functional option to set the sampling rate of the -// audio device. Make sure your audio device supports the specified sampling -// rate. -func Samplerate(s float64) Option { - return func(args *Options) { - args.Samplerate = s - } -} - // FramesPerBuffer is a functional option which sets the amount of sample frames // our audio device will request / provide when executing the callback. // Example: A buffer with 960 frames at 48000kHz / stereo contains diff --git a/audio/sinks/pbWriter/pbWriter.go b/audio/sinks/pbWriter/pbWriter.go index 1ca9b28..68db619 100644 --- a/audio/sinks/pbWriter/pbWriter.go +++ b/audio/sinks/pbWriter/pbWriter.go @@ -42,9 +42,8 @@ func NewPbWriter(opts ...Option) (*PbWriter, error) { pbw := &PbWriter{ options: Options{ - DeviceName: "ProtoBufReader", + DeviceName: "ProtoBufWriter", Channels: 1, - Samplerate: 48000, FramesPerBuffer: 960, UserID: "myCallsign", }, @@ -59,7 +58,7 @@ func NewPbWriter(opts ...Option) (*PbWriter, error) { // if no encoder set, create the default encoder if pbw.options.Encoder == nil { encChannels := opus.Channels(pbw.options.Channels) - encSR := opus.Samplerate(pbw.options.Samplerate) + encSR := opus.Samplerate(48000) enc, err := opus.NewEncoder(encChannels, encSR) if err != nil { return nil, err @@ -75,7 +74,7 @@ func NewPbWriter(opts ...Option) (*PbWriter, error) { } pbw.src = src{ Src: srConv, - samplerate: pbw.options.Samplerate, + samplerate: 48000, ratio: 1, } @@ -161,11 +160,11 @@ func (pbw *PbWriter) Write(audioMsg audio.Msg) error { aData = audioMsg.Data } - if audioMsg.Samplerate != pbw.options.Samplerate { + if audioMsg.Samplerate != 48000 { if pbw.src.samplerate != audioMsg.Samplerate { pbw.src.Reset() pbw.src.samplerate = audioMsg.Samplerate - pbw.src.ratio = pbw.options.Samplerate / audioMsg.Samplerate + pbw.src.ratio = 48000 / audioMsg.Samplerate } aData, err = pbw.src.Process(aData, pbw.src.ratio, false) if err != nil { @@ -241,7 +240,7 @@ func (pbw *PbWriter) Write(audioMsg audio.Msg) error { BitDepth: 16, Codec: sbAudio.Codec_opus, FrameLength: int32(pbw.options.FramesPerBuffer), - SamplingRate: int32(pbw.options.Samplerate), + SamplingRate: 48000, UserId: pbw.options.UserID, } diff --git a/cmd/check_parameters.go b/cmd/check_parameters.go index 4010a55..88e21e5 100644 --- a/cmd/check_parameters.go +++ b/cmd/check_parameters.go @@ -55,8 +55,7 @@ func checkAudioParameterValues() error { } } - opusFrameLength := float64(viper.GetInt("audio.frame-length")) / - viper.GetFloat64("input-device.samplerate") + opusFrameLength := float64(viper.GetInt("audio.frame-length")) / 48000 if opusFrameLength != 0.0025 && opusFrameLength != 0.005 && opusFrameLength != 0.01 && diff --git a/cmd/client_nats.go b/cmd/client_nats.go index 36b84e6..afd597f 100644 --- a/cmd/client_nats.go +++ b/cmd/client_nats.go @@ -243,7 +243,7 @@ func natsAudioClient(cmd *cobra.Command, args []string) { opus.Bitrate(opusBitrate), opus.Complexity(opusComplexity), opus.Channels(iChannels), - opus.Samplerate(iSamplerate), + opus.Samplerate(48000), // opus only works well with 48kHz opus.Application(opusApplication), opus.MaxBandwidth(opusMaxBandwidth), ) @@ -253,7 +253,6 @@ func natsAudioClient(cmd *cobra.Command, args []string) { toNetwork, err := pbWriter.NewPbWriter( pbWriter.Encoder(opusEncoder), - pbWriter.Samplerate(iSamplerate), pbWriter.Channels(iChannels), pbWriter.FramesPerBuffer(audioFramesPerBuffer), pbWriter.UserID(natsUsername), diff --git a/cmd/server_nats.go b/cmd/server_nats.go index 8a77a10..82465e1 100644 --- a/cmd/server_nats.go +++ b/cmd/server_nats.go @@ -247,7 +247,7 @@ func natsAudioServer(cmd *cobra.Command, args []string) { opus.Bitrate(opusBitrate), opus.Complexity(opusComplexity), opus.Channels(iChannels), - opus.Samplerate(iSamplerate), + opus.Samplerate(48000), opus.Application(opusApplication), opus.MaxBandwidth(opusMaxBandwidth), ) @@ -259,7 +259,6 @@ func natsAudioServer(cmd *cobra.Command, args []string) { // and send it on the wire toNetwork, err := pbWriter.NewPbWriter( pbWriter.Encoder(opusEncoder), - pbWriter.Samplerate(iSamplerate), pbWriter.Channels(iChannels), pbWriter.FramesPerBuffer(audioFramesPerBuffer), pbWriter.ToWireCb(ns.toWireCb),