I want be able just to get the PC internal sound received from a web site or a player in my speak device I dont want get sound from mic.
I found in the link post below an answer almost solve my problem, but there is a small detail.
How would i find the current decibel level and set it as a variable?
Modifing the code to adjust my needs the code works just when I put the flag: input=True and output=True also but this generate undesirable reverberation and noises from mic. If i remove the parameter input=True or set to False the input parameter in stream = p.open() method it throws a exception. Is there an alternative to this?
There is many example showing how to read a wave file through to speaker, but does not is what i need.
Would be mandatory in pyaudio audio wire callback between input and output? Always has a input true also?
in the documentation link shows that input flag can be set to false: https://people.csail.mit.edu/hubert/pyaudio/docs/#class-pyaudio-stream
My operating system is a Windows 11 and the output device is a Realtek.
follows the code, thanks!
import pyaudio
import time
import numpy as np
from math import log10
from scipy.io import wavfile
p = pyaudio.PyAudio()
WIDTH = 2
RATE = int(p.get_default_output_device_info()['defaultSampleRate'])
DEVICE = p.get_default_output_device_info()['index']
rms = 1
print(p.get_default_output_device_info())
def callback(in_data, frame_count, time_info, status):
global rms
# convert byte data to numpy array
in_data_ = np.frombuffer(in_data, dtype=np.int16)
# calculate rms
rms = np.sqrt(np.mean(np.square(in_data_))) / 32767
return in_data, pyaudio.paContinue
stream = p.open(format=p.get_format_from_width(WIDTH),
output_device_index=DEVICE,
channels=1,
rate=RATE,
input=False,
output=True,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
db = 20 * log10(max(rms, 1e-5)) # to avoid log0 error, use max(rms, 1e-5)
print(f"RMS: {rms} DB: {db}")
# refresh every 0.3 seconds
time.sleep(0.3)
stream.stop_stream()
stream.close()
p.terminate()