Skip to content

Commit caffd38

Browse files
committed
accept input samplingrates other than 48kHz
1 parent 7f46f61 commit caffd38

File tree

5 files changed

+9
-23
lines changed

5 files changed

+9
-23
lines changed

audio/sinks/pbWriter/options.go

-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ type Options struct {
1313
DeviceName string
1414
Encoder audiocodec.Encoder
1515
Channels int
16-
Samplerate float64
1716
FramesPerBuffer int
1817
UserID string
1918
ToWireCb func([]byte)
@@ -28,15 +27,6 @@ func Channels(chs int) Option {
2827
}
2928
}
3029

31-
// Samplerate is a functional option to set the sampling rate of the
32-
// audio device. Make sure your audio device supports the specified sampling
33-
// rate.
34-
func Samplerate(s float64) Option {
35-
return func(args *Options) {
36-
args.Samplerate = s
37-
}
38-
}
39-
4030
// FramesPerBuffer is a functional option which sets the amount of sample frames
4131
// our audio device will request / provide when executing the callback.
4232
// Example: A buffer with 960 frames at 48000kHz / stereo contains

audio/sinks/pbWriter/pbWriter.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ func NewPbWriter(opts ...Option) (*PbWriter, error) {
4242

4343
pbw := &PbWriter{
4444
options: Options{
45-
DeviceName: "ProtoBufReader",
45+
DeviceName: "ProtoBufWriter",
4646
Channels: 1,
47-
Samplerate: 48000,
4847
FramesPerBuffer: 960,
4948
UserID: "myCallsign",
5049
},
@@ -59,7 +58,7 @@ func NewPbWriter(opts ...Option) (*PbWriter, error) {
5958
// if no encoder set, create the default encoder
6059
if pbw.options.Encoder == nil {
6160
encChannels := opus.Channels(pbw.options.Channels)
62-
encSR := opus.Samplerate(pbw.options.Samplerate)
61+
encSR := opus.Samplerate(48000)
6362
enc, err := opus.NewEncoder(encChannels, encSR)
6463
if err != nil {
6564
return nil, err
@@ -75,7 +74,7 @@ func NewPbWriter(opts ...Option) (*PbWriter, error) {
7574
}
7675
pbw.src = src{
7776
Src: srConv,
78-
samplerate: pbw.options.Samplerate,
77+
samplerate: 48000,
7978
ratio: 1,
8079
}
8180

@@ -161,11 +160,11 @@ func (pbw *PbWriter) Write(audioMsg audio.Msg) error {
161160
aData = audioMsg.Data
162161
}
163162

164-
if audioMsg.Samplerate != pbw.options.Samplerate {
163+
if audioMsg.Samplerate != 48000 {
165164
if pbw.src.samplerate != audioMsg.Samplerate {
166165
pbw.src.Reset()
167166
pbw.src.samplerate = audioMsg.Samplerate
168-
pbw.src.ratio = pbw.options.Samplerate / audioMsg.Samplerate
167+
pbw.src.ratio = 48000 / audioMsg.Samplerate
169168
}
170169
aData, err = pbw.src.Process(aData, pbw.src.ratio, false)
171170
if err != nil {
@@ -241,7 +240,7 @@ func (pbw *PbWriter) Write(audioMsg audio.Msg) error {
241240
BitDepth: 16,
242241
Codec: sbAudio.Codec_opus,
243242
FrameLength: int32(pbw.options.FramesPerBuffer),
244-
SamplingRate: int32(pbw.options.Samplerate),
243+
SamplingRate: 48000,
245244
UserId: pbw.options.UserID,
246245
}
247246

cmd/check_parameters.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ func checkAudioParameterValues() error {
5555
}
5656
}
5757

58-
opusFrameLength := float64(viper.GetInt("audio.frame-length")) /
59-
viper.GetFloat64("input-device.samplerate")
58+
opusFrameLength := float64(viper.GetInt("audio.frame-length")) / 48000
6059
if opusFrameLength != 0.0025 &&
6160
opusFrameLength != 0.005 &&
6261
opusFrameLength != 0.01 &&

cmd/client_nats.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func natsAudioClient(cmd *cobra.Command, args []string) {
243243
opus.Bitrate(opusBitrate),
244244
opus.Complexity(opusComplexity),
245245
opus.Channels(iChannels),
246-
opus.Samplerate(iSamplerate),
246+
opus.Samplerate(48000), // opus only works well with 48kHz
247247
opus.Application(opusApplication),
248248
opus.MaxBandwidth(opusMaxBandwidth),
249249
)
@@ -253,7 +253,6 @@ func natsAudioClient(cmd *cobra.Command, args []string) {
253253

254254
toNetwork, err := pbWriter.NewPbWriter(
255255
pbWriter.Encoder(opusEncoder),
256-
pbWriter.Samplerate(iSamplerate),
257256
pbWriter.Channels(iChannels),
258257
pbWriter.FramesPerBuffer(audioFramesPerBuffer),
259258
pbWriter.UserID(natsUsername),

cmd/server_nats.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func natsAudioServer(cmd *cobra.Command, args []string) {
247247
opus.Bitrate(opusBitrate),
248248
opus.Complexity(opusComplexity),
249249
opus.Channels(iChannels),
250-
opus.Samplerate(iSamplerate),
250+
opus.Samplerate(48000),
251251
opus.Application(opusApplication),
252252
opus.MaxBandwidth(opusMaxBandwidth),
253253
)
@@ -259,7 +259,6 @@ func natsAudioServer(cmd *cobra.Command, args []string) {
259259
// and send it on the wire
260260
toNetwork, err := pbWriter.NewPbWriter(
261261
pbWriter.Encoder(opusEncoder),
262-
pbWriter.Samplerate(iSamplerate),
263262
pbWriter.Channels(iChannels),
264263
pbWriter.FramesPerBuffer(audioFramesPerBuffer),
265264
pbWriter.ToWireCb(ns.toWireCb),

0 commit comments

Comments
 (0)