Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from scipy.io import wavfile
- import ctypes
- replaygain = ctypes.CDLL('libreplaygain.so')
- def calculate_replaygain(samples, frame_rate=44100):
- """
- pass in a generator that produces audio samples
- returns the maximum sample.
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement