-
Notifications
You must be signed in to change notification settings - Fork 153
Description
Getting this error from a L/R switched I'm uploading to github -
File "x\Desktop\Coding\Github Shit\Desktop Audio LR Flipper\scripttest.py", line 33, in
start_stream()
~~~~~~~~~~~~^^
File "x\Desktop\Coding\Github Shit\Desktop Audio LR Flipper\scripttest.py", line 19, in start_stream
stream = sd.Stream(
samplerate=48000,
...<4 lines>...
callback=callback
)
File "x\AppData\Local\Programs\Python\Python313\Lib\site-packages\sounddevice.py", line 1837, in init
_StreamBase.init(self, kind='duplex', wrap_callback='array',
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
**_remove_self(locals()))
^^^^^^^^^^^^^^^^^^^^^^^^^
File "x\AppData\Local\Programs\Python\Python313\Lib\site-packages\sounddevice.py", line 909, in init
_check(_lib.Pa_OpenStream(self._ptr, iparameters, oparameters,
~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
samplerate, blocksize, stream_flags,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
callback_ptr, userdata),
^^^^^^^^^^^^^^^^^^^^^^^^
f'Error opening {self.class.name}')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "x\AppData\Local\Programs\Python\Python313\Lib\site-packages\sounddevice.py", line 2823, in _check
raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Error opening Stream: Invalid number of channels [PaErrorCode -9998]
This is the code that came from the error -
import sounddevice as sd
import numpy as np
import threading
import pystray
from PIL import Image, ImageDraw
INPUT_ID = 7 # CABLE Input (replace with your actual ID)
OUTPUT_ID = 6 # Real Speakers (replace with your actual ID)
def callback(indata, outdata, frames, time, status):
if status:
print("Status:", status)
# Ensure stereo
if indata.shape[1] == 2:
# Flip L <-> R
outdata[:] = indata[:, ::-1]
else:
# If mono, duplicate channel
outdata[:] = np.repeat(indata, 2, axis=1)
def start_stream():
stream = sd.Stream(
samplerate=48000,
blocksize=1024,
dtype="float32",
channels=2,
device=(INPUT_ID, OUTPUT_ID), # Force specific devices
callback=callback
)
stream.start()
print("Stream running. Ctrl+C to stop.")
while True:
pass
def stop_stream():
global stream
if stream is not None:
stream.stop()
stream.close()
stream = None
def toggle(icon, item):
global running
running = not running
def quit_app(icon, item):
stop_stream()
icon.stop()
def make_icon():
img = Image.new("RGB", (64, 64), "black")
draw = ImageDraw.Draw(img)
draw.polygon([(10, 20), (54, 20), (32, 44)], fill="white")
return img
def tray():
icon = pystray.Icon(
"Audio Flip",
make_icon(),
"Audio Flip",
menu=pystray.Menu(
pystray.MenuItem("Toggle", toggle),
pystray.MenuItem("Quit", quit_app)
)
)
icon.run()
if name == "main":
start_stream()
t = threading.Thread(target=tray)
t.daemon = True
t.start()
#this is only here for the github issue, its wrapping it for some reason
try:
while True:
pass
except KeyboardInterrupt:
stop_stream()