jstrait / wavefile

A Ruby gem for reading and writing sound files in Wave format (*.wav)
https://wavefilegem.com
MIT License
209 stars 24 forks source link

support reading from a file or stream #21

Closed taf2 closed 7 years ago

taf2 commented 8 years ago

It would be nice if the interface would allow streaming when reading a wave file.

e.g.

wav_reader = WaveFile::Reader.new(File.open("some.wav"))

or even more generically from any Ruby IO object:

wav_reader = WaveFile::Reader.new(StringIO.new(".... wave bytes..."))
jstrait commented 8 years ago

Sorry for taking awhile to respond. I can see the benefit of this in theory - is there a specific use case where this would be helpful for you?

Passing in an IO object to the new constructors instead of a pathname (as in your examples above) is nice in that it makes the gem agnostic to where it is reading/writing. However, what I don't like as much is that it puts more work on the caller to manually open the file. For my own use, I've only ever wanted to read/write to a file, so this seems like adding extra work without clear gain. Maybe there's a use case I'm not thinking of.

Another option could be to add a different method that allows constructing an instance from an IO object.

taf2 commented 8 years ago

@jstrait - I have a need to avoid writing to disk where were are dealing with a large number wave files and converting them to different formats, downloading the wave file and upload the resulting mp3 for example. With the IO object we can achieve this and avoid touching the disk in the process which improves our throughput.

jstrait commented 7 years ago

@taf2 thanks for the additional context, that makes sense.

I've added support for this on the v0.8.0 branch, and it will be included as a feature in that release. I expect it to be released soon.

Out of curiosity, is this still a feature that would be helpful for you for the reasons you mentioned above? It won't change whether this feature gets added either way, I'm just curious how people are using this gem.

Thanks for opening https://github.com/jstrait/wavefile/pull/22 - I appreciate it! I decided to go with that API, where the argument to Reader/Writer constructor can either be a String for the file name, or an IO object. I like that because it's backwards compatible with the existing interface.

I ended up building a different implementation though, and added some additional changes:

Check out the v0.8.0 branch if you're interested to see the current implementation or try it out. Please let me know if you have any feedback.

Because of the alternate implementation, I'll also close https://github.com/jstrait/wavefile/pull/22.

taf2 commented 7 years ago

@jstrait - excellent, yeah your implementation is better to leave control of the IO object to the client since they created it. Backwards compatibility is always the way to go thanks!

jstrait commented 7 years ago

v0.8.0 has been released, so this feature should now be available. Let me know if you run into any issues!