Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 7th, 2013  |  syntax: Python  |  size: 1.36 KB  |  views: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import numpy as np
  2. from scipy.io import wavfile
  3. import ctypes
  4. replaygain = ctypes.CDLL('libreplaygain.so')
  5.  
  6.  
  7. def calculate_replaygain(samples, frame_rate=44100):
  8.     """
  9.    pass in a generator that produces audio samples
  10.    returns the maximum sample.
  11.  
  12.    https://github.com/vontrapp/replaygain
  13.    """
  14.     replaygain.gain_init_analysis(frame_rate)
  15.     block_size = 10000
  16.     channel_count = samples.shape[1]
  17.     i = 0
  18.     samples = samples.astype(np.float64)
  19.  
  20.     while i * block_size < samples.shape[0]:
  21.         channel_left = samples[i*block_size:(i+1)*block_size,0]
  22.         channel_right = samples[i*block_size:(i+1)*block_size,1]
  23.  
  24.         samples_p_left = channel_left.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
  25.         samples_p_right = channel_right.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
  26.  
  27.         replaygain.gain_analyze_samples(samples_p_left, samples_p_right, channel_left.shape[0], channel_count)
  28.         i += 1
  29.    
  30.     return replaygain.gain_get_chapter()
  31.  
  32.  
  33. if __name__ == '__main__':
  34.     frame_rate, samples = wavfile.read('directions.wav')
  35.     samples = samples.astype(np.float64) / 2**15
  36.     gain = calculate_replaygain(samples, frame_rate=frame_rate)
  37.     print "Recommended gain: %f dB" % gain
  38.     gain = calculate_replaygain(np.random.random((441000, 2)) * 2 - 1, frame_rate=44100)
  39.     print "Recommended gain: %f dB" % gain
clone this paste RAW Paste Data