Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 230 additions & 2 deletions audio1/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"math"
"os"
"os/exec"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -63,6 +64,8 @@ const (
dsgKeyBluezModeDefault = "bluezModeDefault"
dsgKeyMonoEnabled = "monoEnabled"
dsgKeyReduceNoiseEnabled = "reduceNoiseEnabled" // 降噪配置,保存用户数据
dsgKeyAiReduceNoiseEnabled = "aiReduceNoiseEnabled"
rnnoiseSourceName = "effect_output.rnnoise"

dsgKeyFirstRun = "firstRun"
dsgKeyInputVolume = "inputVolume"
Expand Down Expand Up @@ -95,7 +98,7 @@ const (
AudioStateChanging = false
)

//go:generate dbusutil-gen -type Audio,Sink,SinkInput,Source,Meter -import github.com/godbus/dbus audio.go sink.go sinkinput.go source.go meter.go
//go:generate dbusutil-gen -type Audio,Sink,SinkInput,Source,Meter -import github.com/godbus/dbus/v5 audio.go sink.go sinkinput.go source.go meter.go
//go:generate dbusutil-gen em -type Audio,Sink,SinkInput,Source,Meter

func objectPathSliceEqual(v1, v2 []dbus.ObjectPath) bool {
Expand Down Expand Up @@ -151,6 +154,12 @@ type Audio struct {

ReduceNoise bool `prop:"access:rw"`

AiReduceNoise bool `prop:"access:rw"`

AiNoiseSourcePath dbus.ObjectPath

aiNoiseSourceName string

defaultPaCfg defaultPaConfig

// 最大音量
Expand Down Expand Up @@ -240,6 +249,10 @@ func newAudio(service *dbusutil.Service) *Audio {
a.PausePlayer = false
a.ReduceNoise = false
a.emitPropChangedReduceNoise(a.ReduceNoise)
a.AiReduceNoise = false
a.emitPropChangedAiReduceNoise(a.AiReduceNoise)
a.AiNoiseSourcePath = "/"
a.emitPropChangedAiNoiseSourcePath(a.AiNoiseSourcePath)
a.CurrentAudioServer = a.getCurrentAudioServer()
a.headphoneUnplugAutoPause, err = a.audioDConfig.GetValueBool(dsgKeyHeadphoneUnplugAutoPause)
if err != nil {
Expand Down Expand Up @@ -804,6 +817,10 @@ func (a *Audio) init() error {

// 更新本地数据
a.refresh()

// After refresh, run AI noise-reduction init with both DConfig and
// source list available.
a.initAiReduceNoise()
logger.Debug("init cards")
a.PropsMu.Lock()
a.setPropCards(a.cards.string())
Expand Down Expand Up @@ -945,6 +962,29 @@ func (a *Audio) findSource(cardId uint32, activePortName string) *Source {
return nil
}

// getSourcePathByName returns the D-Bus object path of the source whose Name
// matches, or "/" if not found. Used to expose the physical mic's Source path
// to control center while AI noise reduction is enabled.
func (a *Audio) getSourcePathByName(name string) dbus.ObjectPath {
if name == "" {
return "/"
}
a.mu.Lock()
defer a.mu.Unlock()
for _, source := range a.sources {
if source.Name == name {
return source.getPath()
}
}
return "/"
}

// updateAiNoiseSourcePath refreshes the AiNoiseSourcePath D-Bus property from
// aiNoiseSourceName. Call under PropsMu.
func (a *Audio) updateAiNoiseSourcePath() {
a.setPropAiNoiseSourcePath(a.getSourcePathByName(a.aiNoiseSourceName))
}

func (a *Audio) SetPortEnabled(cardId uint32, portName string, enabled bool) *dbus.Error {
logger.Infof("dbus call SetPortEnabled with cardId %d, portName %s and enabled %t", cardId, portName, enabled)

Expand Down Expand Up @@ -1372,7 +1412,7 @@ func (a *Audio) resumeSourceConfig(s *Source) {
}

s.setMute(GetConfigKeeper().Mute.MuteInput || !portConfig.Enabled)
s.setReduceNoise(a.ReduceNoise)
s.audio.setAiReduceNoise(a.AiReduceNoise, "")
}

func (a *Audio) refreshBluetoothOpts() {
Expand Down Expand Up @@ -1527,6 +1567,14 @@ func (a *Audio) updateDefaultSource(sourceName string) {

a.PropsMu.Lock()
a.setPropDefaultSource(defaultSourcePath)
if a.AiReduceNoise && source != nil {
a.aiNoiseSourceName = source.Name
if isBluezAudio(source.Name) {
a.setPropAiNoiseSourcePath("/")
} else {
a.updateAiNoiseSourcePath()
}
}
a.PropsMu.Unlock()

logger.Debug("set prop default source:", defaultSourcePath)
Expand Down Expand Up @@ -1594,6 +1642,31 @@ func (a *Audio) getMasterNameFromVirtualDevice(device string) string {
}
}
}
if strings.Contains(device, "rnnoise") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加一个注释,为什么要使用这个方式去处理

out, err := exec.Command("pw-link", "-l").Output()
if err != nil {
return ""
}
lines := strings.Split(string(out), "\n")
for i, line := range lines {
if !strings.Contains(line, "effect_input.rnnoise:input") {
continue
}
for _, s := range lines[i+1:] {
s = strings.TrimSpace(s)
if strings.HasPrefix(s, "|<-") {
name := strings.Fields(s)[1]
if idx := strings.LastIndex(name, ":"); idx > 0 {
return name[:idx]
}
return name
}
if s == "" || !strings.HasPrefix(s, "|") {
break
}
}
}
}
return ""
}

Expand Down Expand Up @@ -1872,6 +1945,19 @@ func (a *Audio) initDsgProp() error {
}
getReduceNoiseEnabled()

getAiReduceNoiseEnabled := func() {
aiReduceNoiseEnabled, err := a.audioDConfig.GetValueBool(dsgKeyAiReduceNoiseEnabled)
if err != nil {
logger.Warningf("aiReduceNoise DConfig: GetValueBool failed: %v", err)
return
}
logger.Infof("aiReduceNoise: DConfig=%v, calling setAiReduceNoise", aiReduceNoiseEnabled)
a.setAiReduceNoise(aiReduceNoiseEnabled, "")
}
// NOTE: do NOT call getAiReduceNoiseEnabled() during initDsgProp — it runs
// before refresh() and would get an empty default-source name. The actual
// init is done in initAiReduceNoise() after refresh().

a.audioDConfig.ConnectValueChanged(func(key string) {
switch key {
case dsgKeyAutoSwitchPort:
Expand All @@ -1888,6 +1974,8 @@ func (a *Audio) initDsgProp() error {
getMonoEnabled()
case dsgKeyVolumeIncrease:
a.handleVolumeIncrease()
case dsgKeyAiReduceNoiseEnabled:
getAiReduceNoiseEnabled()
}
})

Expand Down Expand Up @@ -2013,3 +2101,143 @@ func (a *Audio) isModuleExist(name string) bool {
}
return false
}

func (a *Audio) setAiReduceNoise(enable bool, sourceName string) error {
a.PropsMu.Lock()
if !enable && a.AiReduceNoise == enable {
// Already disabled, nothing to do.
a.PropsMu.Unlock()
return nil
}

var micName string
if enable {
micName = sourceName
if micName == "" {
micName = a.getDefaultSourceName()
}
if micName == rnnoiseSourceName {
micName = a.getMasterNameFromVirtualDevice(micName)
}
a.aiNoiseSourceName = micName
} else {
micName = a.aiNoiseSourceName
}
a.PropsMu.Unlock()

// Phase 2: Execute PA operations (outside PropsMu to avoid blocking
// PA event handlers like updateDefaultSource).

// Check current rnnoise status first.
statusCmd := exec.Command("rnnoise-toggle", "status")
statusOut, statusErr := statusCmd.Output()
rnnoiseStatus := ""
if statusErr == nil {
rnnoiseStatus = strings.TrimSpace(string(statusOut))
}

if enable {
if rnnoiseStatus == "on" {
logger.Infof("rnnoise-toggle status is on, skip enable")
} else {
cmd := exec.Command("rnnoise-toggle", "-q", "on")
err := cmd.Run()
if err != nil {
logger.Warning("failed to enable ai reduce noise:", err)
return err
}
}
} else {
if rnnoiseStatus == "off" {
logger.Infof("rnnoise-toggle status is off, skip disable")
} else {
args := []string{"-q", "off"}
if micName != "" {
args = append(args, micName)
}
cmd := exec.Command("rnnoise-toggle", args...)
err := cmd.Run()
if err != nil {
logger.Warning("failed to disable ai reduce noise:", err)
return err
}
}
}

// Phase 3: Update property and persist DConfig (only on success).
a.PropsMu.Lock()
a.setPropAiReduceNoise(enable)
if enable {
// AiNoiseSourceName was computed in Phase 1; refresh the path so
// control center can bind the volume slider to the physical mic.
a.updateAiNoiseSourcePath()
} else {
// Clear the path — DefaultSource is the physical mic again.
if a.AiNoiseSourcePath != "/" {
a.AiNoiseSourcePath = "/"
a.emitPropChangedAiNoiseSourcePath(a.AiNoiseSourcePath)
}
}
a.PropsMu.Unlock()
if err := a.audioDConfig.SetValue(dsgKeyAiReduceNoiseEnabled, enable); err != nil {
logger.Warning("failed to persist aiReduceNoiseEnabled to dconfig:", err)
}
return nil
}

func (a *Audio) initAiReduceNoise() {
logger.Debug("initAiReduceNoise")

// 1. Check if rnnoise source already exists in the source list.
a.mu.Lock()
rnnoiseFound := false
for _, src := range a.sources {
if src.Name == rnnoiseSourceName {
rnnoiseFound = true
break
}
}
a.mu.Unlock()

// 2. Check DConfig value.
aiReduceNoiseEnabled, err := a.audioDConfig.GetValueBool(dsgKeyAiReduceNoiseEnabled)
if err != nil {
logger.Warningf("initAiReduceNoise: DConfig GetValueBool failed: %v", err)
}

shouldEnable := aiReduceNoiseEnabled
if err != nil && rnnoiseFound {
shouldEnable = true
}
if !shouldEnable {
logger.Infof("initAiReduceNoise: DConfig=%v (err=%v), rnnoiseFound=%v, nothing to do",
aiReduceNoiseEnabled, err != nil, rnnoiseFound)
return
}

logger.Infof("initAiReduceNoise: DConfig=%v, rnnoiseFound=%v, calling setAiReduceNoise(true)",
aiReduceNoiseEnabled, rnnoiseFound)
if err := a.setAiReduceNoise(true, ""); err != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释一下,传入空的时候使用默认的输入源作为开启降噪后的源

logger.Warning("initAiReduceNoise: setAiReduceNoise(true) failed:", err)
return
}

a.PropsMu.RLock()
micName := a.aiNoiseSourceName
a.PropsMu.RUnlock()
if micName != "" {
a.mu.Lock()
var micSource *Source
for _, s := range a.sources {
if s.Name == micName {
micSource = s
break
}
}
a.mu.Unlock()
if micSource != nil {
logger.Infof("initAiReduceNoise: resumeSourceConfig for %s", micName)
a.resumeSourceConfig(micSource)
}
}
}
28 changes: 27 additions & 1 deletion audio1/audio_dbusutil.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions audio1/audio_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func (a *Audio) handleStateChanged() {
select {
case state := <-a.stateChan:
switch state {
case pulse.ContextStateReady:
logger.Debug("re-checking AI noise reduction")
a.initAiReduceNoise()
case pulse.ContextStateFailed:
logger.Warning("pulseaudio context state failed")
a.destroyCtxRelated()
Expand Down Expand Up @@ -747,6 +750,7 @@ func isPhysicalDevice(deviceName string) bool {
"Echo-Cancel",
"remap-sink-mono",
"null-sink",
"effect_output.rnnoise",
} {
if strings.Contains(deviceName, virtualDeviceKey) {
return false
Expand Down Expand Up @@ -781,6 +785,16 @@ func (a *Audio) writeReduceNoise(write *dbusutil.PropertyWrite) *dbus.Error {
return nil
}

// 外部修改AiReduceNoise时触发回调
func (a *Audio) writeAiReduceNoise(write *dbusutil.PropertyWrite) *dbus.Error {
reduce, ok := write.Value.(bool)
if !ok {
logger.Warning("type is not bool")
return dbusutil.ToError(errors.New("type is not bool"))
}
return dbusutil.ToError(a.setAiReduceNoise(reduce, ""))
}

func (a *Audio) writeKeyPausePlayer(write *dbusutil.PropertyWrite) *dbus.Error {
return dbusutil.ToError(func() error {
a.PausePlayer = write.Value.(bool)
Expand Down
Loading
Loading