Я пытаюсь создать простой живой счетчик децибел в питоне, используя
sounddevice и Numpy библиотеки. Идея состоит в том, чтобы перечислить устройства ввода, пусть пользователь выбирает одну, а затем непрерывно печатать уровни DB с устройства. Это просто молча уходит или кажется застрявшим. Я подозреваю, что цикл Decibel не начинается, но я не понимаю, почему. < /P>
Вот сценарий: < /p>
import sounddevice as sd
import numpy as np
import time
# --- Settings ---
SAMPLERATE = 48000 # sample rate (in Hz)
INTERVAL = 0.5 # measure each 0.5 seconds
def db_from_rms(rms):
"""Calculate dezibels from RMS"""
if rms == 0:
return -np.inf
return 20 * np.log10(rms)
def live_db_meter(device_id):
"""Record live system audio from microphone and display decibels"""
with sd.InputStream(
device=device_id, # input device (e.g. microphone)
channels=1, # mono channel for the microphone (1 channel)
samplerate=SAMPLERATE,
dtype='float32',
latency='low'
) as stream:
print("Starting decibel measurement... (Press CTRL+C to stop)\n")
try:
while True:
audio, _ = stream.read(int(SAMPLERATE * INTERVAL)) # Read the audio data
rms = np.sqrt(np.mean(audio ** 2)) # Calculate the RMS value
db = db_from_rms(rms) # Calculate decibels from RMS
print(f"Decibels: {db:.2f} dB", end="\r") # Show the decibel value in the same line
time.sleep(0.01)
except KeyboardInterrupt:
print("\nMeasurement stopped.")
# List and select devices
def list_devices():
"""Show all available audio devices"""
devices = sd.query_devices()
if len(devices) == 0:
print("No devices found!")
else:
print("Available devices:\n")
for idx, device in enumerate(devices):
print(f"{idx}: {device['name']}")
# Main part of the program
if __name__ == "_main_":
print("Loading available devices...\n")
list_devices() # Zeige alle verfügbaren Geräte an
try:
device_id = int(input("\nEnter the device number: ")) # Ask user for device ID
live_db_meter(device_id) # Start the decibel measurement
except ValueError:
print("Invalid input! Please enter a valid device ID.")
Подробнее здесь: https://stackoverflow.com/questions/796 ... ice-python
Как я могу найти децибел музыки или видео, которое выходит из моего устройства Python ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как я могу найти децибел музыки или видео, которое выходит из моего устройства Python
Anonymous » » в форуме Python - 0 Ответы
- 4 Просмотры
-
Последнее сообщение Anonymous
-