Skip to content

Commit

Permalink
feat: move mixer device name to configuration
Browse files Browse the repository at this point in the history
See #36
  • Loading branch information
devgianlu committed May 26, 2024
1 parent b2ec6d5 commit a178714
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
7 changes: 6 additions & 1 deletion cmd/daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (app *App) newAppPlayer(creds any) (_ *AppPlayer, err error) {
if appPlayer.player, err = player.NewPlayer(
appPlayer.sess.Spclient(), appPlayer.sess.AudioKey(),
!app.cfg.NormalisationDisabled, *app.cfg.NormalisationPregain,
appPlayer.countryCode, *app.cfg.AudioDevice,
appPlayer.countryCode, *app.cfg.AudioDevice, *app.cfg.MixerDevice,
*app.cfg.VolumeSteps, app.cfg.ExternalVolume,
); err != nil {
return nil, fmt.Errorf("failed initializing player: %w", err)
Expand Down Expand Up @@ -312,6 +312,7 @@ type Config struct {
DeviceType *string `yaml:"device_type"`
ClientToken *string `yaml:"client_token"`
AudioDevice *string `yaml:"audio_device"`
MixerDevice *string `yaml:"mixer_device"`
Bitrate *int `yaml:"bitrate"`
VolumeSteps *uint32 `yaml:"volume_steps"`
InitialVolume *uint32 `yaml:"initial_volume"`
Expand Down Expand Up @@ -368,6 +369,10 @@ func loadConfig(cfg *Config) error {
cfg.AudioDevice = new(string)
*cfg.AudioDevice = "default"
}
if cfg.MixerDevice == nil {
cfg.MixerDevice = new(string)
*cfg.MixerDevice = "default"
}
if cfg.Bitrate == nil {
cfg.Bitrate = new(int)
*cfg.Bitrate = 160
Expand Down
5 changes: 5 additions & 0 deletions config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
"description": "Which audio device should be used for playback, leave empty for default",
"default": "default"
},
"mixer_device": {
"type": "string",
"description": "Which audio mixer should be used for volume control, leave empty for default",
"default": "default"
},
"server": {
"type": "object",
"properties": {
Expand Down
15 changes: 11 additions & 4 deletions output/driver_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type output struct {
channels int
sampleRate int
device string
mixer string
reader librespot.Float32Reader

cond *sync.Cond
Expand All @@ -47,12 +48,13 @@ type output struct {
err chan error
}

func newOutput(reader librespot.Float32Reader, sampleRate int, channels int, device string, initialVolume float32) (*output, error) {
func newOutput(reader librespot.Float32Reader, sampleRate int, channels int, device string, mixer string, initialVolume float32) (*output, error) {
out := &output{
reader: reader,
channels: channels,
sampleRate: sampleRate,
device: device,
mixer: mixer,
volume: initialVolume,
err: make(chan error, 1),
cond: sync.NewCond(&sync.Mutex{}),
Expand Down Expand Up @@ -141,13 +143,18 @@ func (out *output) openAndSetup() error {
}

func (out *output) openAndSetupMixer() error {
if len(out.mixer) == 0 {
out.mixerEnabled = false
return nil
}

if err := C.snd_mixer_open(&out.mixerHandle, 0); err < 0 {
return out.alsaError("snd_mixer_open", err)
}

cdevice := C.CString(out.device)
defer C.free(unsafe.Pointer(cdevice))
if err := C.snd_mixer_attach(out.mixerHandle, cdevice); err < 0 {
cmixer := C.CString(out.mixer)
defer C.free(unsafe.Pointer(cmixer))
if err := C.snd_mixer_attach(out.mixerHandle, cmixer); err < 0 {
return out.alsaError("snd_mixer_attach", err)
}

Expand Down
7 changes: 6 additions & 1 deletion output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ type NewOutputOptions struct {
// This feature is support only for the unix driver.
Device string

// Mixer specifies the audio mixer name.
//
// This feature is support only for the unix driver.
Mixer string

// InitialVolume specifies the initial output volume.
InitialVolume float32
}

func NewOutput(options *NewOutputOptions) (*Output, error) {
out, err := newOutput(options.Reader, options.SampleRate, options.ChannelCount, options.Device, options.InitialVolume)
out, err := newOutput(options.Reader, options.SampleRate, options.ChannelCount, options.Device, options.Mixer, options.InitialVolume)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type playerCmdDataSet struct {
paused bool
}

func NewPlayer(sp *spclient.Spclient, audioKey *audio.KeyProvider, normalisationEnabled bool, normalisationPregain float32, countryCode *string, device string, volumeSteps uint32, externalVolume bool) (*Player, error) {
func NewPlayer(sp *spclient.Spclient, audioKey *audio.KeyProvider, normalisationEnabled bool, normalisationPregain float32, countryCode *string, device, mixer string, volumeSteps uint32, externalVolume bool) (*Player, error) {
p := &Player{
sp: sp,
audioKey: audioKey,
Expand All @@ -75,6 +75,7 @@ func NewPlayer(sp *spclient.Spclient, audioKey *audio.KeyProvider, normalisation
SampleRate: SampleRate,
ChannelCount: Channels,
Device: device,
Mixer: mixer,
InitialVolume: volume,
})
},
Expand Down

0 comments on commit a178714

Please sign in to comment.