Skip to content

Commit

Permalink
refactor: pass output options as single struct
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Dec 17, 2024
1 parent 30b5765 commit 9f8ee67
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
20 changes: 10 additions & 10 deletions output/driver-alsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ type alsaOutput struct {
err chan error
}

func newAlsaOutput(reader librespot.Float32Reader, sampleRate int, channels int, device string, mixer string, control string, initialVolume float32, externalVolume bool, volumeUpdate chan float32) (*alsaOutput, error) {
func newAlsaOutput(opts *NewOutputOptions) (*alsaOutput, error) {
out := &alsaOutput{
reader: reader,
channels: channels,
sampleRate: sampleRate,
device: device,
mixer: mixer,
control: control,
volume: initialVolume,
reader: opts.Reader,
channels: opts.ChannelCount,
sampleRate: opts.SampleRate,
device: opts.Device,
mixer: opts.Mixer,
control: opts.Control,
volume: opts.InitialVolume,
err: make(chan error, 2),
externalVolume: externalVolume,
volumeUpdate: volumeUpdate,
externalVolume: opts.ExternalVolume,
volumeUpdate: opts.VolumeUpdate,
}

if err := out.setupMixer(); err != nil {
Expand Down
18 changes: 9 additions & 9 deletions output/driver-pulseaudio.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type pulseAudioOutput struct {
err chan error
}

func newPulseAudioOutput(reader librespot.Float32Reader, sampleRate int, channels int, externalVolumeUpdate chan float32) (*pulseAudioOutput, error) {
func newPulseAudioOutput(opts *NewOutputOptions) (*pulseAudioOutput, error) {
// Initialize the PulseAudio client.
// The device name is shown by PulseAudio volume controls (usually built
// into a desktop environment), so we might want to use device_name here.
Expand All @@ -32,21 +32,21 @@ func newPulseAudioOutput(reader librespot.Float32Reader, sampleRate int, channel
return nil, err
}
out := &pulseAudioOutput{
sampleRate: sampleRate,
reader: reader,
sampleRate: opts.SampleRate,
reader: opts.Reader,
client: client,
externalVolumeUpdate: externalVolumeUpdate,
externalVolumeUpdate: opts.VolumeUpdate,
err: make(chan error, 2),
}

// Create a new playback.
var channelOpt pulse.PlaybackOption
if channels == 1 {
if opts.ChannelCount == 1 {
channelOpt = pulse.PlaybackMono
} else if channels == 2 {
} else if opts.ChannelCount == 2 {
channelOpt = pulse.PlaybackStereo
} else {
return nil, fmt.Errorf("cannot play %d channels, pulse only supports mono and stereo", channels)
return nil, fmt.Errorf("cannot play %d channels, pulse only supports mono and stereo", opts.ChannelCount)
}
volumeUpdates := make(chan proto.ChannelVolumes, 1)
out.stream, err = out.client.NewPlayback(pulse.Float32Reader(out.float32Reader), pulse.PlaybackSampleRate(out.sampleRate), channelOpt, pulse.PlaybackVolumeChanges(volumeUpdates))
Expand All @@ -60,7 +60,7 @@ func newPulseAudioOutput(reader librespot.Float32Reader, sampleRate int, channel
// PulseAudio provided volume.
cvol, _ := out.stream.Volume()
out.volume = cvol.Avg()
sendVolumeUpdate(externalVolumeUpdate, float32(out.volume.Norm()))
sendVolumeUpdate(opts.VolumeUpdate, float32(out.volume.Norm()))

// Listen for volume changes (through the volume mixer application, usually
// built into the desktop environment), and send them back to Spotify.
Expand All @@ -70,7 +70,7 @@ func newPulseAudioOutput(reader librespot.Float32Reader, sampleRate int, channel

out.volumeLock.Lock()
if volume != out.volume {
sendVolumeUpdate(externalVolumeUpdate, float32(volume.Norm()))
sendVolumeUpdate(opts.VolumeUpdate, float32(volume.Norm()))
out.volume = volume
}
out.volumeLock.Unlock()
Expand Down
4 changes: 2 additions & 2 deletions output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ type NewOutputOptions struct {
func NewOutput(options *NewOutputOptions) (Output, error) {
switch options.Backend {
case "alsa":
out, err := newAlsaOutput(options.Reader, options.SampleRate, options.ChannelCount, options.Device, options.Mixer, options.Control, options.InitialVolume, options.ExternalVolume, options.VolumeUpdate)
out, err := newAlsaOutput(options)
if err != nil {
return nil, err
}
return out, nil
case "pulseaudio":
out, err := newPulseAudioOutput(options.Reader, options.SampleRate, options.ChannelCount, options.VolumeUpdate)
out, err := newPulseAudioOutput(options)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9f8ee67

Please sign in to comment.