Skip to content

Commit 51fe9b7

Browse files
authored
Making sound split classes and functions private (#16606)
Following @seanbudd's suggestion in #16591 making classes and functions private. The rationale is that we would like to refactor sound split code so that more code is going to be reused in application volume adjuster, but #16591 will have to be postponed to v2024.3. So we need to make sound split API private to avoid breaking API changes.
1 parent f530805 commit 51fe9b7

File tree

4 files changed

+42
-42
lines changed

4 files changed

+42
-42
lines changed

source/audio/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
from .soundSplit import (
77
SoundSplitState,
8-
setSoundSplitState,
9-
toggleSoundSplitState,
8+
_setSoundSplitState,
9+
_toggleSoundSplitState,
1010
)
1111

1212
__all__ = [
1313
"SoundSplitState",
14-
"setSoundSplitState",
15-
"toggleSoundSplitState",
14+
"_setSoundSplitState",
15+
"_toggleSoundSplitState",
1616
]

source/audio/soundSplit.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,20 @@ def getNVDAVolume(self) -> VolumeTupleT:
8989
raise RuntimeError(f"Unexpected or unknown state {self=}")
9090

9191

92-
audioSessionManager: IAudioSessionManager2 | None = None
93-
activeCallback: AudioSessionNotification | None = None
92+
_audioSessionManager: IAudioSessionManager2 | None = None
93+
_activeCallback: AudioSessionNotification | None = None
9494

9595

9696
def initialize() -> None:
9797
if nvwave.usingWasapiWavePlayer():
98-
global audioSessionManager
98+
global _audioSessionManager
9999
try:
100-
audioSessionManager = AudioUtilities.GetAudioSessionManager()
100+
_audioSessionManager = AudioUtilities.GetAudioSessionManager()
101101
except COMError:
102102
log.exception("Could not initialize audio session manager")
103103
return
104104
state = SoundSplitState(config.conf["audio"]["soundSplitState"])
105-
setSoundSplitState(state, initial=True)
105+
_setSoundSplitState(state, initial=True)
106106
else:
107107
log.debug("Cannot initialize sound split as WASAPI is disabled")
108108

@@ -112,27 +112,27 @@ def terminate():
112112
if nvwave.usingWasapiWavePlayer():
113113
state = SoundSplitState(config.conf["audio"]["soundSplitState"])
114114
if state != SoundSplitState.OFF:
115-
setSoundSplitState(SoundSplitState.OFF)
116-
unregisterCallback()
115+
_setSoundSplitState(SoundSplitState.OFF)
116+
_unregisterCallback()
117117
else:
118118
log.debug("Skipping terminating sound split as WASAPI is disabled.")
119119

120120

121121
@dataclass(unsafe_hash=True)
122-
class AudioSessionNotificationWrapper(AudioSessionNotification):
122+
class _AudioSessionNotificationWrapper(AudioSessionNotification):
123123
listener: AudioSessionNotification
124124

125125
def on_session_created(self, new_session: AudioSession):
126126
pid = new_session.ProcessId
127-
with applicationExitCallbacksLock:
128-
if pid not in applicationExitCallbacks:
129-
volumeRestorer = VolumeRestorer(pid, new_session)
127+
with _applicationExitCallbacksLock:
128+
if pid not in _applicationExitCallbacks:
129+
volumeRestorer = _VolumeRestorer(pid, new_session)
130130
new_session.register_notification(volumeRestorer)
131-
applicationExitCallbacks[pid] = volumeRestorer
131+
_applicationExitCallbacks[pid] = volumeRestorer
132132
self.listener.on_session_created(new_session)
133133

134134

135-
def applyToAllAudioSessions(
135+
def _applyToAllAudioSessions(
136136
callback: AudioSessionNotification,
137137
applyToFuture: bool = True,
138138
) -> None:
@@ -141,30 +141,30 @@ def applyToAllAudioSessions(
141141
Additionally, if applyToFuture is True, then it will register a notification with audio session manager,
142142
which will execute the same callback for all future sessions as they are created.
143143
That notification will be active until next invokation of this function,
144-
or until unregisterCallback() is called.
144+
or until _unregisterCallback() is called.
145145
"""
146-
unregisterCallback()
147-
callback = AudioSessionNotificationWrapper(callback)
146+
_unregisterCallback()
147+
callback = _AudioSessionNotificationWrapper(callback)
148148
if applyToFuture:
149-
audioSessionManager.RegisterSessionNotification(callback)
149+
_audioSessionManager.RegisterSessionNotification(callback)
150150
# The following call is required to make callback to work:
151-
audioSessionManager.GetSessionEnumerator()
152-
global activeCallback
153-
activeCallback = callback
151+
_audioSessionManager.GetSessionEnumerator()
152+
global _activeCallback
153+
_activeCallback = callback
154154
sessions: list[AudioSession] = AudioUtilities.GetAllSessions()
155155
for session in sessions:
156156
callback.on_session_created(session)
157157

158158

159-
def unregisterCallback() -> None:
160-
global activeCallback
161-
if activeCallback is not None:
162-
audioSessionManager.UnregisterSessionNotification(activeCallback)
163-
activeCallback = None
159+
def _unregisterCallback() -> None:
160+
global _activeCallback
161+
if _activeCallback is not None:
162+
_audioSessionManager.UnregisterSessionNotification(_activeCallback)
163+
_activeCallback = None
164164

165165

166166
@dataclass(unsafe_hash=True)
167-
class VolumeSetter(AudioSessionNotification):
167+
class _VolumeSetter(AudioSessionNotification):
168168
leftVolume: float
169169
rightVolume: float
170170
leftNVDAVolume: float
@@ -187,7 +187,7 @@ def on_session_created(self, new_session: AudioSession):
187187
channelVolume.SetChannelVolume(1, self.rightNVDAVolume, None)
188188

189189

190-
def setSoundSplitState(state: SoundSplitState, initial: bool = False) -> dict:
190+
def _setSoundSplitState(state: SoundSplitState, initial: bool = False) -> dict:
191191
applyToFuture = True
192192
if state == SoundSplitState.OFF:
193193
if initial:
@@ -199,14 +199,14 @@ def setSoundSplitState(state: SoundSplitState, initial: bool = False) -> dict:
199199
applyToFuture = False
200200
leftVolume, rightVolume = state.getAppVolume()
201201
leftNVDAVolume, rightNVDAVolume = state.getNVDAVolume()
202-
volumeSetter = VolumeSetter(leftVolume, rightVolume, leftNVDAVolume, rightNVDAVolume)
203-
applyToAllAudioSessions(volumeSetter, applyToFuture=applyToFuture)
202+
volumeSetter = _VolumeSetter(leftVolume, rightVolume, leftNVDAVolume, rightNVDAVolume)
203+
_applyToAllAudioSessions(volumeSetter, applyToFuture=applyToFuture)
204204
return {
205205
"foundSessionWithNot2Channels": volumeSetter.foundSessionWithNot2Channels,
206206
}
207207

208208

209-
def toggleSoundSplitState() -> None:
209+
def _toggleSoundSplitState() -> None:
210210
if not nvwave.usingWasapiWavePlayer():
211211
message = _(
212212
# Translators: error message when wasapi is turned off.
@@ -224,7 +224,7 @@ def toggleSoundSplitState() -> None:
224224
i = -1
225225
i = (i + 1) % len(allowedStates)
226226
newState = SoundSplitState(allowedStates[i])
227-
result = setSoundSplitState(newState)
227+
result = _setSoundSplitState(newState)
228228
config.conf["audio"]["soundSplitState"] = newState.value
229229
ui.message(newState.displayString)
230230
if result["foundSessionWithNot2Channels"]:
@@ -238,7 +238,7 @@ def toggleSoundSplitState() -> None:
238238

239239

240240
@dataclass(unsafe_hash=True)
241-
class VolumeRestorer(AudioSessionEvents):
241+
class _VolumeRestorer(AudioSessionEvents):
242242
pid: int
243243
audioSession: AudioSession
244244

@@ -264,9 +264,9 @@ def restoreVolume(self):
264264
self.unregister()
265265

266266
def unregister(self):
267-
with applicationExitCallbacksLock:
267+
with _applicationExitCallbacksLock:
268268
try:
269-
del applicationExitCallbacks[self.pid]
269+
del _applicationExitCallbacks[self.pid]
270270
except KeyError:
271271
pass
272272
try:
@@ -275,5 +275,5 @@ def unregister(self):
275275
log.exception(f"Cannot unregister audio session for process {self.pid}")
276276

277277

278-
applicationExitCallbacksLock = Lock()
279-
applicationExitCallbacks: dict[int, VolumeRestorer] = {}
278+
_applicationExitCallbacksLock = Lock()
279+
_applicationExitCallbacks: dict[int, _VolumeRestorer] = {}

source/globalCommands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4509,7 +4509,7 @@ def script_cycleParagraphStyle(self, gesture: "inputCore.InputGesture") -> None:
45094509
gesture="kb:NVDA+alt+s",
45104510
)
45114511
def script_cycleSoundSplit(self, gesture: "inputCore.InputGesture") -> None:
4512-
audio.toggleSoundSplitState()
4512+
audio._toggleSoundSplitState()
45134513

45144514

45154515
#: The single global commands instance.

source/gui/settingsDialogs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2812,7 +2812,7 @@ def onSave(self):
28122812
index = self.soundSplitComboBox.GetSelection()
28132813
config.conf["audio"]["soundSplitState"] = index
28142814
if nvwave.usingWasapiWavePlayer():
2815-
audio.setSoundSplitState(audio.SoundSplitState(index))
2815+
audio._setSoundSplitState(audio.SoundSplitState(index))
28162816
config.conf["audio"]["includedSoundSplitModes"] = [
28172817
mIndex
28182818
for mIndex in range(len(self._allSoundSplitModes))

0 commit comments

Comments
 (0)