I am trying to measure the sound level of my speaker in Python. The idea is to open a browser which will play a sound and, since the browser outputs the audio to the default speaker, measure that sound with sounddevice
However, I cannot get it to work. This is the code I have so far
import sounddevice as sd
import numpy as np
def print_sound(outdata, frames, time, status):
volume_norm = np.linalg.norm(outdata)*10
print ("|" * int(volume_norm))
deviceInfo = sd.query_devices(kind='output')
print(deviceInfo)
sd.default.device = deviceInfo
with sd.OutputStream(callback=print_sound,device=deviceInfo['index']):
sd.sleep(10000)
I get thrown this error:
{'name': 'Speakers (Realtek High Definiti', 'hostapi': 0, 'max_input_channels': 0, 'max_output_channels': 2, 'default_low_input_latency': 0.09, 'default_low_output_latency': 0.09, 'default_high_input_latency': 0.18, 'default_high_output_latency': 0.18, 'default_samplerate': 44100.0}
Traceback (most recent call last):
File "c:UsersMyUserDesktopMyProjectsMainRecorderScriptvenvlibsite-packagessounddevice.py", line 2710, in _split
invalue, outvalue = value
ValueError: too many values to unpack (expected 2)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:UsersMyUserDesktopMyProjectsMainRecorderScriptsound_device_test.py", line 11, in <module>
sd.default.device = deviceInfo
File "c:UsersMyUserDesktopMyProjectsMainRecorderScriptvenvlibsite-packagessounddevice.py", line 2170, in __setattr__
getattr(self, name)._pair[:] = _split(value)
File "c:UsersMyUserDesktopMyProjectsMainRecorderScriptvenvlibsite-packagessounddevice.py", line 2714, in _split
raise ValueError('Only single values and pairs are allowed') from e
ValueError: Only single values and pairs are allowed
I do not want to specify a string to name the default speaker because this will move from PC to PC so I am hoping to capture the default speaker. There will be no microphone plugged in
Edit: For reference, I tried modifying this script How to read realtime microphone audio volume in python and ffmpeg or similar
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…