Skip to content

2-dimensional indexing on SoundFile objects? #15

@mgeier

Description

@mgeier

As mentioned in #12, it would be nice to select certain channels by indexing.

I wanted to create a pull request, but I have still a few questions which make me think maybe it's not such a good idea after all.

Should data which was read before be somehow cached?

If a channel is requested from an interleaved file, all channels have to be read anyway, so if another channel is requested afterwards it would make sense to re-use the data.
OTOH, if only one of many channels is ever needed, the memory for all other channels will not be freed up.

Should indexing be possible when writing files?
In this case the affected frames would have to be read first, only to overwrite a part of them afterwards.
But I guess this doesn't make sense in any case ...

All-in-all, maybe the current read() and write() methods are enough, and indexing isn't needed at all?

Maybe instead, to be able to be as succinct as possible, there should be some convenience functions à la Octave's wavread() (to avoid explicit creation of the SoundFile object)?
Because then, indexing could be used like this:

from pysoundfile import readfile
wave = readfile('existing_file.wav')[:, 1]

And if someone doesn't want to read the whole file, they'll have to fall back to read():

from pysoundfile import SoundFile
with SoundFile('existing_file.wav') as f:
    f.seek_absolute(22050)
    wave = f.read(44100)[:, 1]

I guess this wouldn't be much worse than:

from pysoundfile import SoundFile
wave = SoundFile('existing_file.wav')[22050:66150, 1]

(Although latter may have the __del__() problem discussed in #13)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions