Skip to content

Several changes to the read() and write() methods (alternative)#37

Closed
bastibe wants to merge 24 commits intomasterfrom
open-read-write-alternative
Closed

Several changes to the read() and write() methods (alternative)#37
bastibe wants to merge 24 commits intomasterfrom
open-read-write-alternative

Conversation

@bastibe
Copy link
Owner

@bastibe bastibe commented May 9, 2014

This implements several changes to the read() and write() methods.

  • Add always_2d argument to read()
  • Add out argument to read()
  • Add fill_value argument to read()
  • Add read() and write() functions

mgeier and others added 18 commits April 21, 2014 23:17
This is the combination of a few commits from #18.
See also #14.
Replace ffi.new() with np.empty() and np.ascontiguousarray()

Remove dicts for readers/writers

read():
* reserve only as much memory as needed (if 'frames' is too large)
* check return value of sf_readf_*()

write():
* avoid copy if data has already the correct memory layout
* check return value of sf_writef_*()
* don't return the number of written frames!
  This is for symmetry with read() and it's redundant information anyway

As the written frames are now checked in the write() method, the same
check was removed from the write() *function*.
If less frames are left in the file than would fit into out and
fill_value=None, a smaller view into out is returned that contains only
the valid frames.
- pysoundfile is not imported into the global namespace any longer, since
that would overwrite `open`.
- Split the test class into subclasses
- removed `channels_first`.
- split read into `read` and `readinto`.
- removed many helper functions that were not needed any longer.
- added tests for `fill_value` and EOF behavior.
readinto can thus be turned into a private method
@bastibe
Copy link
Owner Author

bastibe commented May 9, 2014

  • you call self.seek(0, SEEK_CUR, 'r') three times (reducing readability/maintainability, the performance hit is of course negligible)
  • in some cases too much memory is allocated
  • I don't like how you create out and then conditionally change its shape. I prefer to get the correct shape in the first place and then create an array with it. Also self.channels == 1 is more readable than out.shape[1] == 1.
  • fill_value is assigned even if it's None and the view to assign to is actually empty. This doesn't (yet) work on PyPy.

These issues should be addressed now.

This is a fork of #34, which does not need the _check_frames_and_channels helper method and is more compact in general.

Also, this fork is mostly tested.

@mgeier
Copy link
Contributor

mgeier commented May 11, 2014

This is starting to get confusing ...

This is a fork of #34

#34 is quite outdated, because I created #36 (following your suggestion, @bastibe).
I thought we wanted to merge the changes to the methods first, and then tackle the functions separately.

Currently it's hard to see the differences regarding the functions.
Do you want me to re-combine #34 and #36 or do you want to remove the functions from #37?

pysoundfile.py Outdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the third time that self.frames - current_frame is calculated (the others being in line 622 and 624). I guess this is a code smell.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You said the same somewhere else. I'll amend it.

@bastibe
Copy link
Owner Author

bastibe commented May 11, 2014

I fixed most of your comments. Thank you!

BTW, that test suite is invaluable!

@bastibe
Copy link
Owner Author

bastibe commented May 11, 2014

Sorry about the confusion with #34, #36, and #37. This pull request was supposed to be for discussion purposes mostly. I think we did settle on an API for SoundFile.read and SoundFile.write. Now we have to come up with an implementation. Please ignore the read and write functions for the time being if you want.

@mgeier
Copy link
Contributor

mgeier commented May 12, 2014

OK, I can ignore the functions.

I found this quite interesting:

git difftool origin/open-read-write-alternative  origin/read-write pysoundfile.py

@bastibe
Copy link
Owner Author

bastibe commented May 12, 2014

Very interesting indeed. I missed the fact that there are many similarities between the error checking in SoundFile.write and SoundFile.read. Thus, I kind of re-implemented your _check_frames_and_channels, though this time only for error checking.

Actually, this made SoundFile.read quite a bit shorter. What do you think about this approach?

@mgeier
Copy link
Contributor

mgeier commented May 17, 2014

I like the idea of re-factoring out common functionality of read() and write() to make them both shorter.

However, I found a different (but overlapping) set of functionality to factor out.
See d73ef60 in #36.

I guess the name could be improved, though (_read_or_write()) ...

@bastibe
Copy link
Owner Author

bastibe commented May 27, 2014

This has been superceded by #36

@bastibe bastibe closed this May 27, 2014
bastibe added a commit that referenced this pull request May 27, 2014
@bastibe bastibe mentioned this pull request May 27, 2014
mgeier added a commit that referenced this pull request Dec 17, 2014
See also #16

If less frames are left in the file than would fit into out and
fill_value=None, a smaller view into out is returned that contains only
the valid frames.

This includes several suggestions by @bastibe from #37.
bastibe added a commit that referenced this pull request Dec 17, 2014
@bastibe bastibe deleted the open-read-write-alternative branch April 25, 2016 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants