使用libreplaygain.so和numpy无法获得明智的结果

时间:2013-02-07 19:46:10

标签: python numpy ctypes

我一直在尝试使用libreplaygain.soReplayGain是一种计算音频响度的算法。)来自python,从音频文件传递数据。这是libreplaygain的the header file。我对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>的结果进行比较。

1 个答案:

答案 0 :(得分:4)

gain_get_chapter()返回double,但the ctypes docs say&#34;默认情况下,假定函数返回C int类型。&#34;你应该做点什么

replaygain.gain_get_chapter.restype = ctypes.c_double

您还应该检查gain_init_analysisgain_analyze_samples的返回值;如果那些都不是1,那么其他东西就会出错。 (那些实际上是整数,所以你不应该在那里做任何其他事情。)