Skip to content

Commit

Permalink
accept input samplingrates other than 48kHz
Browse files Browse the repository at this point in the history
  • Loading branch information
dh1tw committed Dec 10, 2021
1 parent 7f46f61 commit caffd38
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 23 deletions.
10 changes: 0 additions & 10 deletions audio/sinks/pbWriter/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type Options struct {
DeviceName string
Encoder audiocodec.Encoder
Channels int
Samplerate float64
FramesPerBuffer int
UserID string
ToWireCb func([]byte)
Expand All @@ -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
Expand Down
13 changes: 6 additions & 7 deletions audio/sinks/pbWriter/pbWriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -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
Expand All @@ -75,7 +74,7 @@ func NewPbWriter(opts ...Option) (*PbWriter, error) {
}
pbw.src = src{
Src: srConv,
samplerate: pbw.options.Samplerate,
samplerate: 48000,
ratio: 1,
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/check_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
3 changes: 1 addition & 2 deletions cmd/client_nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand All @@ -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),
Expand Down
3 changes: 1 addition & 2 deletions cmd/server_nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand All @@ -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),
Expand Down

0 comments on commit caffd38

Please sign in to comment.