Please copy and paste the following into the debug console, then paste the output you get back here:
#!/usr/bin/env python3
"""
Minimal reproducible example for debugging QAudioSource.start() returning None
Copy and paste this into Anki's debug console to test audio initialization
"""
from PyQt6.QtMultimedia import QAudioDevice, QAudioFormat, QAudioSource, QMediaDevices
from PyQt6.QtWidgets import QApplication, QWidget
def test_qaudio_source():
"""Test QAudioSource initialization and debug available formats"""
try:
print("=== QAudioSource Debug Test ===")
# Create a minimal widget as parent (required for QAudioSource)
app = QApplication.instance()
if app is None:
print("No QApplication found - this test needs to run within Anki")
return
parent = QWidget()
# Get available audio devices
print("\n1. Available Audio Input Devices:")
input_devices = QMediaDevices.audioInputs()
for i, device in enumerate(input_devices):
print(f" Device {i}: {device.description()}")
print(f" - ID: {device.id()}")
print(f" - Default: {device.isDefault()}")
if not input_devices:
print(" No audio input devices found!")
return
# Get the default audio device
default_device: QAudioDevice = QMediaDevices.defaultAudioInput()
print(f"\n2. Default Audio Input Device:")
print(f" Description: {default_device.description()}")
print(f" ID: {default_device.id()}")
# Check supported formats for default device
print(f"\n3. Supported formats for default device:")
supported_sample_rates = default_device.supportedSampleFormats()
supported_channel_counts = (default_device.minimumChannelCount(), default_device.maximumChannelCount())
supported_formats = default_device.supportedSampleFormats()
print(f" Sample rates: {supported_sample_rates}")
print(f" Channel counts: {supported_channel_counts}")
print(f" Sample formats: {[fmt.name for fmt in supported_formats]}")
# Create the format (same as in the original code)
print(f"\n4. Creating QAudioFormat:")
format = QAudioFormat()
format.setChannelCount(2)
format.setSampleRate(44100)
format.setSampleFormat(QAudioFormat.SampleFormat.Int16)
print(f" Requested - Channels: {format.channelCount()}, Rate: {format.sampleRate()}, Format: {format.sampleFormat().name}")
# Check if the format is supported
is_supported = default_device.isFormatSupported(format)
print(f" Format supported by default device: {is_supported}")
if not is_supported:
print(" Trying to find a preferred format...")
preferred_format = default_device.preferredFormat()
print(f" Preferred - Channels: {preferred_format.channelCount()}, Rate: {preferred_format.sampleRate()}, Format: {preferred_format.sampleFormat().name}")
# Create QAudioSource with default device (same as original code)
print(f"\n5. Creating QAudioSource:")
source = QAudioSource(format, parent)
# Check the actual format that will be used
actual_format = source.format()
print(f" Actual format - Channels: {actual_format.channelCount()}, Rate: {actual_format.sampleRate()}, Format: {actual_format.sampleFormat().name}")
# Get audio source state and error before starting
print(f"\n6. Audio source state before start():")
print(f" State: {source.state().name}")
print(f" Error: {source.error().name}")
print(f" Buffer size: {source.bufferSize()}")
# Try starting the audio source - THIS IS THE KEY TEST
print(f"\n7. Calling source.start():")
iodevice = source.start()
# Check the return value
print(f" Return value: {iodevice}")
print(f" Return value type: {type(iodevice)}")
print(f" Is None: {iodevice is None}")
# Check state and error after start
print(f"\n8. Audio source state after start():")
print(f" State: {source.state().name}")
print(f" Error: {source.error().name}")
if iodevice is not None:
print(f" IODevice: {iodevice}")
print(f" IODevice type: {type(iodevice)}")
print(f" IODevice open: {iodevice.isOpen()}")
print(f" IODevice readable: {iodevice.isReadable()}")
# Clean up
if iodevice is not None:
source.stop()
parent.deleteLater()
print(f"\n=== Test Complete ===")
except ImportError as e:
print(f"Import error: {e}")
print("Make sure PyQt6.QtMultimedia is available")
except Exception as e:
print(f"Error during test: {e}")
import traceback
traceback.print_exc()
# Run the test
test_qaudio_source()