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

Untitled

By: a guest on Feb 7th, 2013  |  syntax: Python  |  size: 2.61 KB  |  views: 37  |  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. import ctypes
  3. import wave
  4. replaygain = ctypes.CDLL('libreplaygain.so')
  5.  
  6.  
  7. def string_to_samples(string, channel_count):
  8.     """
  9.    Takes a byte string of raw PCM data and returns a float32 numpy array containing
  10.    audio data in range [-1, 1].
  11.    """
  12.     samples = np.fromstring(string, dtype='int16')
  13.     samples = samples / float(2**15)
  14.     samples.astype(np.float32)
  15.     frame_count = samples.size / channel_count
  16.     return samples.reshape([frame_count, channel_count])
  17.  
  18.  
  19. def read_wav(f, start=None, end=None):
  20.     # Opening the file and getting infos
  21.     raw = wave.open(f, 'rb')
  22.     channel_count = raw.getnchannels()
  23.     sample_width = raw.getsampwidth()       # Sample width in byte
  24.     if sample_width != 2: raise ValueError('Wave format not supported')
  25.     frame_rate = raw.getframerate()
  26.  
  27.     # Calculating start position and end position
  28.     # for reading the data
  29.     if start is None: start = 0
  30.     start_frame = start * frame_rate
  31.     if end is None: end_frame = raw.getnframes()
  32.     else: end_frame = end * frame_rate
  33.     frame_count = end_frame - start_frame
  34.  
  35.     # Reading only the data between `start` and `end`,
  36.     # putting this data to a numpy array
  37.     raw.setpos(int(start_frame))
  38.     data = raw.readframes(int(frame_count))
  39.     data = string_to_samples(data, channel_count)
  40.     return data, {'frame_rate': frame_rate, 'channel_count': channel_count}
  41.  
  42.  
  43. def calculate_replaygain(samples, frame_rate=44100):
  44.     """
  45.    pass in a generator that produces audio samples
  46.    returns the maximum sample.
  47.  
  48.    https://github.com/vontrapp/replaygain
  49.    """
  50.     replaygain.gain_init_analysis(frame_rate)
  51.     block_size = 10000
  52.     channel_count = samples.shape[1]
  53.     i = 0
  54.     samples = samples.astype(np.float64)
  55.  
  56.     while i * block_size < samples.shape[0]:
  57.         channel_left = samples[i*block_size:(i+1)*block_size,0]
  58.         channel_right = samples[i*block_size:(i+1)*block_size,1]
  59.  
  60.         samples_p_left = channel_left.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
  61.         samples_p_right = channel_right.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
  62.  
  63.         replaygain.gain_analyze_samples(samples_p_left, samples_p_right, channel_left.shape[0], channel_count)
  64.         i += 1
  65.    
  66.     return replaygain.gain_get_chapter()
  67.  
  68.  
  69. if __name__ == '__main__':
  70.     samples, infos = read_wav('directions.wav')
  71.     gain = calculate_replaygain(samples, frame_rate=infos['frame_rate'])
  72.     print "Recommended gain: %f dB" % gain
  73.     gain = calculate_replaygain(np.random.random((441000, 2)) * 2 - 1, frame_rate=44100)
  74.     print "Recommended gain: %f dB" % gain
clone this paste RAW Paste Data