我一直在一遍又一遍地尝试使用libreplaygain.so
(ReplayGain
是一种用于计算音频响度的算法。)来自python,从音频文件中传递数据。这是libreplaygain的头文件。我一般不太了解ctypes
或 C,所以我希望这可能是我愚蠢的问题,对其他人来说非常明显!这是我正在使用的脚本:
import numpy as np
from scipy.io import wavfile
import ctypes
replaygain = ctypes.CDLL('libreplaygain.so')
def calculate_replaygain(samples, frame_rate=44100):
"""
inspired from https://github.com/vontrapp/replaygain
"""
replaygain.gain_init_analysis(frame_rate)
block_size = 10000
channel_count = samples.shape[1]
i = 0
samples = samples.astype(np.float64)
while i * block_size < samples.shape[0]:
channel_left = samples[i*block_size:(i+1)*block_size,0]
channel_right = samples[i*block_size:(i+1)*block_size,1]
samples_p_left = channel_left.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
samples_p_right = channel_right.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
replaygain.gain_analyze_samples(samples_p_left, samples_p_right, channel_left.shape[0], channel_count)
i += 1
return replaygain.gain_get_chapter()
if __name__ == '__main__':
frame_rate, samples = wavfile.read('directions.wav')
samples = samples.astype(np.float64) / 2**15
gain = calculate_replaygain(samples, frame_rate=frame_rate)
print "Recommended gain: %f dB" % gain
gain = calculate_replaygain(np.random.random((441000, 2)) * 2 - 1, frame_rate=44100)
print "Recommended gain: %f dB" % gain
脚本运行,但我无法获得与命令行工具相同的值replaygain
。事实上我总是得到80.0
. 要尝试,您可以用任何声音文件替换“directions.wav”...并将结果与命令的结果进行比较replaygain <soundfile.wav>
。