Guilherme Polo <ggp...@gmail.com> added the comment:

1) wave.py doesn't do assumptions about what the user wants, so I don't
think it is the place to put the DC (0 hz) assumption.

3) writesamples would raise an exception in the case of the current
number of channels set being wrong.

4) Well, lets fix a format then. I said list of lists of integers, or it
could use generators, and you didn't disagree here so it seems to be
fine. The problem in the current code is that you are making mono
channels special by being the one where a list of lists of integers is
not returned, but instead a single is returned. This is troublesome for
the caller to set the number of channels then, it is also a different
format then when something with 2 channels or other configuration is
used. With that in mind I have simplified some of your code as this:


def readsamples(self, nframes) :
    """Return a list of lists of integers.

    The number of these inner lists is given by the number of channels in
    the wave file. Each list contains the channel samples formatted as
    integers.
    """
    wav = self.readframes(nframes)

    sampwidth = self.getsampwidth()
    wav = struct.unpack(
            '<%d%s' % (len(wav) / sampwidth, wave._array_fmts[sampwidth]),
            wav)

    nc = self.getnchannels()
    if nc > 1:
        wavs = []
        for c in xrange(nc):
            wavs.append([wav[si] for si in xrange(c, len(wav), nc)])
    else:
        wavs = [[wav]]

    return wavs

def writesamples(self, *wavs) :
    """Write samples to the wave file.

    wavs must follow the structure returned by readsamples.
    """
    if self.getnchannels() != len(wavs):
        raise wave.Error("# of channels != # of samples")

    wav = []
    for w in zip(*wavs):
        wav.extend(w)

    ws = array.array(wave._array_fmts[self.getsampwidth()], wav)
    ws = ws.tostring()

    # we want all the samples in writeframes() format so that _convert
    # can be called on them
    self.writeframes(ws)


You can monkey patch wave then by doing:

wave.Wave_write.writesamples = writesamples
wave.Wave_read.readsamples = readsamples

And then change some other parts of your code.

5) There is a very small test for wave in Python's source,
Lib/test/test_wave.py

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue4913>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to