我住在一条大马路旁边。晚上打开窗户真是凉爽,而且时不时地发出很大的声音。如何根据内置麦克风输入自动调节音量?如果我将音量设置为在汽车经过时可以听到电影中的讲话,那么在其他时候声音会很大,并且对附近的人(外面和邻居)来说感觉很讨厌。
我的系统是 Debian Buster,尽管我可能可以获得通用的解决方案。如果没有可用的软件包可以执行此操作,则从默认麦克风提取响度的命令将有助于编写此脚本。
答案1
我编写了一个 Python 脚本来完成这项工作。剩下的问题是,我的笔记本电脑的麦克风当然也会拾取自己的扬声器。我认为“回声消除”可能就是我正在寻找的,但我不知道如何自己实现它。不过,使用外部麦克风可能会起作用。
不幸的是,由于python-alsaaudio
依赖关系,它是 python 2。
#!/usr/bin/env python
''' For noise cancellation:
$ pactl load-module module-echo-cancel
$ PULSE_PROP="filter.want=echo-cancel" ./this-script.py
'''
''' SETTINGS (you might want to keep presets for music and speech) '''
smoothing = 15 # Over how many samples should we compute?
step_size = 1 # maximum volume adjustment in percent points
# scale_xxx = (n, level) # At mic level n, scale to level% audio volume
scale_min = (4, 39)
scale_max = (19, 53)
''' CREDITS
https://stackoverflow.com/a/1937058
How get sound input from microphone in python, and process it on the fly?
Answer by jbochi
https://stackoverflow.com/a/10739764
How to programmatically change volume in Ubuntu
Answer by mata
'''
import alsaaudio, audioop, sys, os
bucket = [None for i in range(smoothing)]
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE)
inp.setchannels(1)
inp.setrate(8000)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(200)
print('Setting volume to minimum ({}%)'.format(scale_min[1]))
os.system('pactl set-sink-volume 0 {}%'.format(scale_min[1]))
i = 1
last_volume = scale_min[1]
while True:
l, data = inp.read()
if l:
val = audioop.max(data, 2)
bucket[i % smoothing] = val
if i % smoothing == 0:
m = min(bucket)
miclvl = float(m) / 50.0
if miclvl < scale_min[0]:
scale = scale_min[1]
elif miclvl > scale_max[0]:
scale = scale_max[1]
else:
miclvl_range = scale_max[0] - scale_min[0]
level_range = scale_max[1] - scale_min[1]
scale = (miclvl - scale_min[0]) / miclvl_range * level_range + scale_min[1]
scale = int(round(scale))
step = max(min(scale - last_volume, step_size), -step_size)
if step != 0:
last_volume += step
step = '+' + str(step) if step > 0 else str(step)
os.system('pactl set-sink-volume 0 {}%'.format(step))
miclvl = round(miclvl, 1)
miclvlpacing = ' ' * (4 - len(str(miclvl)))
stepspacing = ' ' * (2 - len(str(step)))
sys.stdout.write('mic lvl {}{} ideal scale {}% adjust {}{} now {} '.format(
miclvl, miclvlpacing, str(scale), step, stepspacing, last_volume))
print(int(round(last_volume - scale_min[1])) * 'x')
i += 1