Mercurial > pylearn
view pylearn/io/wavread.py @ 1274:9d5905d6d879
hmc - changed updates to member fn from lambda for pickling
author | James Bergstra <bergstrj@iro.umontreal.ca> |
---|---|
date | Wed, 08 Sep 2010 13:17:45 -0400 |
parents | f3b7d6956209 |
children |
line wrap: on
line source
"""`WavRead` Op""" __docformat__ = "restructuredtext en" import numpy import theano import wave class WavRead(theano.Op): #TODO: add the samplerate as an output """Read a wave file input - the path to a wave file output - the contents of the wave file in pcm format, and the samplerate """ out_type = None """The type for the output of this op. Currently only wvector (aka int16) and dvector (aka double) are supported """ def __init__(self, out_type): self.out_type = out_type if out_type not in [theano.tensor.dvector, theano.tensor.wvector]: raise TypeError(out_type) def __eq__(self, other): return (type(self) == type(other)) and (self.out_type == other.out_type) def __hash__(self): return hash(type(self)) ^ hash(self.out_type) def make_node(self, path): return theano.Apply(self, [path], [self.out_type(), theano.tensor.dscalar()]) def perform(self, node, (path,), (out, sr)): w = wave.open(path) if w.getnchannels() != 1: raise NotImplementedError() if w.getsampwidth() != 2: #2 bytes means 16bit samples raise NotImplementedError() samples = numpy.frombuffer(w.readframes(w.getnframes()), dtype='int16') if self.out_type == theano.tensor.wvector: out[0] = samples elif self.out_type == theano.tensor.dvector: out[0] = samples * (1.0 / 2**15) else: raise NotImplementedError() sr[0] = numpy.asarray(w.getframerate(),dtype='float64') def grad(self, inputs, g_output): return [None for i in inputs] wav_read_int16 = WavRead(theano.tensor.wvector) wav_read_double = WavRead(theano.tensor.dvector)