def mu_law_encode_nonlinear(audio, quantization_channels=256):
'''
Compress the waveform amplitudes using mu-law non-linearity.
NOTE: This mu-law functions as a non-linear function as opposed to
quantization.
'''
with tf.name_scope('encode'):
mu = tf.to_float(quantization_channels - 1)
# Perform mu-law companding transformation (ITU-T, 1988).
# Minimum operation is here to deal with rare large amplitudes caused
# by resampling.
safe_audio_abs = tf.minimum(tf.abs(audio), 1.0)
magnitude = tf.log1p(mu * safe_audio_abs) / tf.log1p(mu)
signal = tf.multiply(tf.sign(audio), magnitude, name='mulaw')
# Quantize signal to the specified number of levels.
# return tf.to_int32((signal + 1) / 2 * mu + 0.5)
return signal
评论列表
文章目录