Reputation: 147
Trying to convert the following to a def but doing something that's probably not allowed... What am I doing wrong and how could this be done better?
# 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))
I was hoping to do something like this as I need to read 4 values, not just the two listed:
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' );
But that yields the mentioned error...
Upvotes: 0
Views: 1495
Reputation: 147
Change of thought brought me to the following:
# 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
Constructive suggestions always welcome... :)
Upvotes: 0
Reputation: 91119
The solution is obvious: if you call readvu('l_input')
, your src
becomes 'l_input'
and .read()
ing from it will fail.
The call should be like
lin_vu = readvu(l_input)
rin_vu = readvu(r_input)
which passes the actual variables, not the strings.
Upvotes: 1