尝试将以下内容转换为def,但做一些可能不允许的事情......我做错了什么,怎么能做得更好?
# Same for both
import alsaaudio
l_input = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, card='default')
r_input = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, card='default')
#
l, data = l_input.read()
if l > 0:
# transform data to logarithmic scale
lin_vu = (math.log(float(max(audioop.max(data, 2),1)))-log_lo)/(log_hi-log_lo)
# Calculate value
lin_vu = (min(max(int(lin_vu*15),0),15))
l, data = r_input.read()
if l > 0:
# transform data to logarithmic scale
rin_vu = (math.log(float(max(audioop.max(data, 2),1)))-log_lo)/(log_hi-log_lo)
# Calculate value
rin_vu = (min(max(int(rin_vu*15),0),15))
我希望能做到这样的事情,因为我需要阅读4个值,而不仅仅是列出的两个值:
def readvu( src ):
l, data = src.read()
if l > 0:
# transform data to logarithmic scale
l_vu = (math.log(float(max(audioop.max(data, 2),1)))-log_lo)/(log_hi-log_lo)
# Calculate value
l_vu = (min(max(int(l_vu*15),0),15))
lin_vu = readvu( 'l_input' );
rin_vu = readvu( 'r_input' );
但是这会产生上述错误......
答案 0 :(得分:1)
解决方案很明显:如果您致电readvu('l_input')
,则src
变为'l_input'
且.read()
将失败。
电话应该像
lin_vu = readvu(l_input)
rin_vu = readvu(r_input)
传递实际变量,而不是字符串。
答案 1 :(得分:0)
思想的转变让我想到了以下几点:
# Convert log scale, calculate value & max
def vu_log(data,vu_max,max_t):
# transform data to logarithmic scale
vu = (math.log(float(max(audioop.max(data, 2),1)))-log_lo)/(log_hi-log_lo)
# Calculate value
vu = (min(max(int(vu*15),0),15))
if vu >= vu_max:
max_t = (3 * vu_fps) # keep max for 3 seconds
vu_max = vu
else:
max_t = max(0, max_t-1) # Reduce max timer by 1 until 0
return (vu,vu_max,max_t);
# Fetch Left input VU from ALSA
l, data = l_input.read()
if l > 0:
# Transform data to logarithmic scale and calculate value
lin = vu_log(data,lin_max,lin_t)
lin_vu = lin[0] # Desired value
lin_max = lin[1] # Maximum value (last 3 sec)
lin_t = lin[2] # Used for tracking age of lin_max
总是欢迎建设性的建议......:)