Windows Core Audio & PyAudioWPatch Loopback
DualAudio taps into the Windows 11 Core Audio API via PyAudioWPatch to establish a zero-copy loopback stream of system playback at 44,100 Hz / 16-bit Stereo PCM. Audio frames (1024 chunks ~23.2ms) are captured before DAC conversion and written simultaneously to an in-memory ring buffer.
import pyaudiowpatch as pyaudio
# Locate default WASAPI loopback device
p = pyaudio.PyAudio()
wasapi_info = p.get_host_api_info_by_type(pyaudio.paWASAPI)
default_speakers = p.get_device_info_by_index(wasapi_info["defaultOutputDevice"])
# Open loopback stream at 44.1kHz Stereo PCM
stream = p.open(format=pyaudio.paInt16,
channels=2,
rate=44100,
input=True,
input_device_index=default_speakers["index"],
frames_per_buffer=1024)
Sonos HTTP Streaming via SoCo (UPnP / DLNA)
Sonos Play:1 does not feature Bluetooth or AUX inputs. DualAudio launches an embedded HTTP streaming endpoint generating continuous chunked WAV streams (with a synthetic 44-byte infinite-length RIFF header) and commands the Sonos device via UPnP SOAP requests using the Python soco library.
import soco
# Connect to Sonos Play:1 on subnet
sonos = soco.SoCo("192.168.1.105")
# Stream captured PC master audio via infinite HTTP WAV pipe
stream_url = "http://192.168.1.100:5000/stream.wav"
sonos.play_uri(stream_url, title="DualAudio Sync PC Stream")
Ring-Buffer Jitter Compensation & Echo Cancellation
Because Wi-Fi HTTP streaming introduces ~1,740ms of packet buffer latency inside the Sonos hardware DAC, playing Bluetooth directly results in an unlistenable echo chamber. DualAudio delays the local Citytek Bluetooth speaker buffer by exactly \(\Delta t\) milliseconds via a thread-safe circular ring buffer.
# Jitter buffer implementation with dynamic delay
delay_samples = int((target_delay_ms / 1000.0) * SAMPLE_RATE)
ring_buffer = deque(maxlen=delay_samples)
def playback_worker():
while True:
frame = input_queue.get()
ring_buffer.append(frame)
if len(ring_buffer) == delay_samples:
delayed_frame = ring_buffer.popleft()
output_citytek.write(delayed_frame)
Windows Notification Sound Shield
Prevents annoying Windows system sounds (clicks, error beeps, Teams alerts) from blasting over the Sonos speakers during music or movie playback by programmatically suppressing the Windows Audio Ducking session.
# PyCaw Windows Audio Session Controller
from pycaw.pycaw import AudioUtilities
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
if session.Process and session.Process.name() == "SystemSounds.exe":
volume = session.SimpleAudioVolume
volume.SetMute(1, None)