~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to examples/demo/zoomed_plot/wav_to_numeric.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2014-06-01 17:04:08 UTC
  • mfrom: (7.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140601170408-m86xvdjd83a4qon0
Tags: 4.4.1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
 - Let the binary-predeb target work on the usr/lib/python* directory
   as we don't have usr/share/pyshared anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /bin/env python
2
 
 
3
 
# Standard library imports
4
 
import os.path
5
 
import wave
6
 
import numpy
7
 
 
8
 
# Enthought library imports
9
 
from traits.util.resource import find_resource
10
 
 
11
 
def wav_to_numeric( fname, max_frames=-1 ):
12
 
  f = wave.open( fname, 'rb' )
13
 
  sampleRate = f.getframerate()
14
 
  channels = f.getnchannels()
15
 
 
16
 
  if max_frames < 0:
17
 
      max_frames = f.getnframes()
18
 
 
19
 
  frames = f.readframes(max_frames)
20
 
 
21
 
  if f.getsampwidth() == 2:
22
 
      data = numpy.fromstring(frames, numpy.uint16).astype(numpy.float64) - (2**15 - 0.5)
23
 
  else:
24
 
      data = numpy.fromstring(frames, numpy.uint8).astype(numpy.float64) - 127.5
25
 
 
26
 
  if channels == 2:
27
 
      left = data[0::2]
28
 
      right = data[1::2]
29
 
 
30
 
      data = left
31
 
 
32
 
  index = numpy.arange(len(data)) * 1.0/sampleRate
33
 
 
34
 
  return index, data
35
 
 
36
 
def test():
37
 
    sample_path = os.path.join('examples','data','sample.wav')
38
 
    alt_path = os.path.join('..','data','sample.wav')
39
 
    fname = find_resource('Chaco', sample_path, alt_path=alt_path,
40
 
        return_path=True)
41
 
    index, data = wav_to_numeric(fname)
42
 
    print data[:100]
43
 
    return index, data
44
 
 
45
 
if __name__== '__main__':
46
 
    test()
47