Skip to content

Commit

Permalink
Fix naming of audio devices under PipeWire
Browse files Browse the repository at this point in the history
Under PulseAudio, the user-friendly name for each audio device was
stored under its `device.description` property. Under PipeWire,
`device.description` is the name of the sound card, not the source/sink.
This results in all source devices showing their sound card's name in
NoiseTorch. Since I have multiple sources on the same sound card, it
meant all my microphones had the same name, making NoiseTorch unusable.

This commit changes the assignment of device names. When running under
PipeWire, the `pulseaudio.Source.Description` field holds the correct
device name, while the
`pulseaudio.Source.PropList["device.description"]` field that was being
used previously is the sound card's description.

The NoiseTorch context must now be passed to the `getSources` and
`getSinks` functions so they can check whether the program is running
under PulseAudio or PipeWire.
  • Loading branch information
kdkasad committed Nov 12, 2022
1 parent dd5c141 commit 0f361e2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
8 changes: 4 additions & 4 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ func doCLI(opt CLIOpts, config *config, librnnoise string) {

if opt.list {
fmt.Println("Sources:")
sources := getSources(paClient)
sources := getSources(&ctx, paClient)
for i := range sources {
fmt.Printf("\tDevice Name: %s\n\tDevice ID: %s\n\n", sources[i].Name, sources[i].ID)
}

fmt.Println("Sinks:")
sinks := getSinks(paClient)
sinks := getSinks(&ctx, paClient)
for i := range sinks {
fmt.Printf("\tDevice Name: %s\n\tDevice ID: %s\n\n", sinks[i].Name, sinks[i].ID)
}
Expand All @@ -121,7 +121,7 @@ func doCLI(opt CLIOpts, config *config, librnnoise string) {
}

if opt.loadInput {
sources := getSources(paClient)
sources := getSources(&ctx, paClient)

if opt.sinkName == "" {
defaultSource, err := getDefaultSourceID(paClient)
Expand All @@ -147,7 +147,7 @@ func doCLI(opt CLIOpts, config *config, librnnoise string) {

}
if opt.loadOutput {
sinks := getSinks(paClient)
sinks := getSinks(&ctx, paClient)

if opt.sinkName == "" {
defaultSink, err := getDefaultSinkID(paClient)
Expand Down
20 changes: 14 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func removeLib(file string) {
log.Printf("Deleted temp librnnoise: %s\n", file)
}

func getSources(client *pulseaudio.Client) []device {
func getSources(ctx *ntcontext, client *pulseaudio.Client) []device {
sources, err := client.Sources()
if err != nil {
log.Printf("Couldn't fetch sources from pulseaudio\n")
Expand All @@ -138,7 +138,11 @@ func getSources(client *pulseaudio.Client) []device {
var inp device

inp.ID = sources[i].Name
inp.Name = sources[i].PropList["device.description"]
if ctx.serverInfo.servertype == servertype_pulse {
inp.Name = sources[i].PropList["device.description"]
} else {
inp.Name = sources[i].Description
}
inp.isMonitor = (sources[i].MonitorSourceIndex != 0xffffffff)
inp.rate = sources[i].SampleSpec.Rate

Expand All @@ -151,7 +155,7 @@ func getSources(client *pulseaudio.Client) []device {
return outputs
}

func getSinks(client *pulseaudio.Client) []device {
func getSinks(ctx *ntcontext, client *pulseaudio.Client) []device {
sources, err := client.Sinks()
if err != nil {
log.Printf("Couldn't fetch sources from pulseaudio\n")
Expand All @@ -168,7 +172,11 @@ func getSinks(client *pulseaudio.Client) []device {
var inp device

inp.ID = sources[i].Name
inp.Name = sources[i].PropList["device.description"]
if ctx.serverInfo.servertype == servertype_pulse {
inp.Name = sources[i].PropList["device.description"]
} else {
inp.Name = sources[i].Description
}
inp.rate = sources[i].SampleSpec.Rate

// PA_SINK_DYNAMIC_LATENCY = 0x0080U
Expand Down Expand Up @@ -207,8 +215,8 @@ func paConnectionWatchdog(ctx *ntcontext) {
ctx.paClient = paClient
go updateNoiseSupressorLoaded(ctx)

ctx.inputList = preselectDevice(ctx, getSources(ctx.paClient), ctx.config.LastUsedInput, getDefaultSourceID)
ctx.outputList = preselectDevice(ctx, getSinks(paClient), ctx.config.LastUsedOutput, getDefaultSinkID)
ctx.inputList = preselectDevice(ctx, getSources(ctx, paClient), ctx.config.LastUsedInput, getDefaultSourceID)
ctx.outputList = preselectDevice(ctx, getSinks(ctx, paClient), ctx.config.LastUsedOutput, getDefaultSinkID)

resetUI(ctx)
(*ctx.masterWindow).Changed()
Expand Down

0 comments on commit 0f361e2

Please sign in to comment.