Giter Site home page Giter Site logo

Comments (3)

kylekyle avatar kylekyle commented on May 24, 2024 1

Excellent! Thanks!

from wavefile.

jstrait avatar jstrait commented on May 24, 2024

Hey @kylekyle, thanks for the feedback!

I think the original reason I made each_buffer close the file was so that you could read an entire file by doing this:

Reader.new("my_file.wav").each_buffer do |buffer|
  # Do something with buffer
end

If it didn't automatically close the file, you would have to do this instead:

Reader.new("my_file.wav") do |reader|
  reader.each_buffer do |buffer|
    # Do something with buffer
  end
end

Or this:

reader = Reader.new("my_file.wav")
reader.each_buffer do |buffer|
  # Do something with buffer
end
reader.close

(Both examples currently don't work, as you pointed out).

It's possible that optimizing for succinctness here is the wrong trade-off, because it reduces the flexibility to use each_buffer in different ways, and it could also be confusing. To be honest it's not something I've thought about for awhile, but as I look at it now the current interface does look a little weird to me.

I see a couple of options:

  1. Change Reader.close to no longer raise an error if the Reader is already closed, as you pointed out.
  2. Change each_buffer to no longer close the file when the block exits, as you pointed out.
  3. Add a new way of opening a file for buffered reading. E.g. something like:
    WaveFile::Reader.read_buffered("my_file.wav", <optional buffer size>) do |buffer|
      # Do something with buffer
    end
    

1.) Seems like the most appealing option. I originally made Reader.close raise an error if the file is already closed to match the behavior of IO.close. However, it looks like IO.close no longer raises an error as of Ruby 2.3, and I'm not sure of another reason why this should raise an error in that scenario. It seems like changing this behavior would allow the examples you gave to work.

I'm wary of changing each_buffer to no longer close the file because it would cause unexpected behavior in existing code that uses the Reader.new.each_block {} pattern - if you updated the gem without changing you code, your code would silently change to leave the file open forever. I'd have to think more about the trade-off between a potentially better interface vs. breaking existing code.

I'd have to think more about 3.)

Finally, is there a specific use case that you aren't able to do based on the current behavior? I.e., one work-around would be to use the Reader.new.each_buffer {} pattern, unless what you are trying to do isn't possible with that.

from wavefile.

jstrait avatar jstrait commented on May 24, 2024

Hey @kylekyle, I released v1.0.0 of the gem a few days ago, and as of that release Reader.close and Writer.close no longer raise an error if the Reader/Writer is already closed. I think that should allow the code in your examples to work.

I’ll keep in mind the other suggestions you made as possible changes for the future, but I’m not planning to make any immediate changes.


Thanks for raising this issue!

from wavefile.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.