Saya telah membuat skrip Python untuk melakukan pekerjaan itu. Masalah yang tersisa adalah bahwa mikrofon laptop saya akan, tentu saja, juga mengambil speaker sendiri. Saya pikir 'pembatalan gema' mungkin yang saya cari, tapi saya tidak tahu bagaimana menerapkannya sendiri. Menggunakan mikrofon eksternal mungkin berfungsi.
Sayangnya, ini adalah python 2 karena python-alsaaudio
ketergantungan.
#!/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
sox
untuk menghitung kenyaringan rata-rata (IIRC ada pertanyaan lain tentang stackexchange tentang ini), danpacmd
untuk mengubah volume untuk Pulseaudio.