<commit_before>#! /bin/env python

# Standard library imports
import os.path
import wave
import numpy

# Enthought library imports
from enthought.util.resource import find_resource

def wav_to_numeric( fname ):
  f = wave.open( fname, 'rb' )
  sampleRate = f.getframerate()
  channels = f.getnchannels()
  
  # get the first million samples...
  s = f.readframes(10000000)
  
  # I think we need to be a little more careful about type here.
  # We also may need to work with byteswap
  data = numpy.fromstring(s, numpy.dtype('uint8')).astype(numpy.float64) - 127.5
  index = numpy.arange(len(data)) * 1.0/sampleRate  
  return index, data
  
def test():
    sample_path = os.path.join('examples','data','sample.wav')
    alt_path = os.path.join('..','data','sample.wav')
    fname = find_resource('Chaco', sample_path, alt_path=alt_path,
        return_path=True)
    index, data = wav_to_numeric(fname)
    print data[:100]
    return index, data
    
if __name__== '__main__':
    test()
<commit_msg>wav_to_numeric now supports stereo and 16bit<commit_after>#! /bin/env python

# Standard library imports
import os.path
import wave
import numpy

# Enthought library imports
from enthought.util.resource import find_resource

def wav_to_numeric( fname, max_frames=-1 ):
  f = wave.open( fname, 'rb' )
  sampleRate = f.getframerate()
  channels = f.getnchannels()

  if max_frames < 0:
      max_frames = f.getnframes()

  frames = f.readframes(max_frames)
  
  if f.getsampwidth() == 2:
      data = numpy.fromstring(frames, numpy.uint16).astype(numpy.float64) - (2**15 - 0.5) 
  else:
      data = numpy.fromstring(frames, numpy.uint8).astype(numpy.float64) - 127.5 

  if channels == 2:
      left = data[0::2]
      right = data[1::2]

      data = left

  index = numpy.arange(len(data)) * 1.0/sampleRate

  return index, data
  
def test():
    sample_path = os.path.join('examples','data','sample.wav')
    alt_path = os.path.join('..','data','sample.wav')
    fname = find_resource('Chaco', sample_path, alt_path=alt_path,
        return_path=True)
    index, data = wav_to_numeric(fname)
    print data[:100]
    return index, data
    
if __name__== '__main__':
    test()

{'fraction_non_alphanumeric': 0.1310123683005039, 'fraction_numerical': 0.0233623453962437, 'mean_word_length': 3.493827160493827, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '115387', 'n_tokens_mistral': 805, 'n_tokens_neox': 764, 'n_words': 185}