0

我一直在一遍又一遍地尝试使用libreplaygain.soReplayGain是一种用于计算音频响度的算法。)来自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>

4

1 回答 1

4

gain_get_chapter()返回 a double,但ctypes 文档说“默认情况下假定函数返回 C int 类型。” 你应该做类似的事情

replaygain.gain_get_chapter.restype = ctypes.c_double

gain_init_analysis您还应该检查and的返回值gain_analyze_samples;如果两者都不是 1,则说明其他问题。(那些实际上是整数,所以你不应该在那里做任何其他事情。)

于 2013-02-07T20:25:47.603 回答