Giter Site home page Giter Site logo

Rewind the IO object about wavefile HOT 4 OPEN

jstrait avatar jstrait commented on May 28, 2024
Rewind the IO object

from wavefile.

Comments (4)

jstrait avatar jstrait commented on May 28, 2024

@alxx Unfortunately, there's currently no way to rewind a Reader, or to seek in general. Besides creating multiple Reader instances like you mentioned, I suppose another option if your file is small enough would be to read it into one large buffer and loop through that.

I’ve been planning to add something like a Reader#seekToSampleFrame() method but haven’t gotten around to it yet. The idea is that it would work similarly to IO#seek() but instead of seeking by individual bytes, would allow seeking by sample frame. For example, to go back to the first sample frame you would use my_reader.seekToSampleFrame(0).

I’ll take a look and see in more detail what would be involved in adding that functionality.

from wavefile.

alxx avatar alxx commented on May 28, 2024

Well, first my solution has been to create new Reader instances and trust the garbage collector:

# Rewrite the exception handling to loop the same file continuously
module WaveFile
  class Reader
    def read(sample_frame_count)
      if @closed
        raise ReaderClosedError
      end

      begin
        @data_chunk_reader.read(sample_frame_count)
      rescue # always read from the start when reaching the end
        @io.rewind
        riff_reader        = ChunkReaders::RiffReader.new(@io, format)
        @data_chunk_reader = riff_reader.data_chunk_reader
        @sample_chunk      = riff_reader.sample_chunk
      end
    end
  end
end

Then I noticed that the Reader was anyway too slow for real-time operations (feeding the samples into a PortAudio stream on a pretty powerful iMac) causing hick-ups every few seconds when I was reading chunks as "large" as 4096 samples at a time. So I reverted to reading the entire file (220500 samples) into a buffer and looping through that, as you guessed.

Thanks for looking into this! :)

from wavefile.

jstrait avatar jstrait commented on May 28, 2024

Oh interesting! I’ve never tried using the gem for real-time stuff, so unfortunately I don’t have any particular insights to add about that. How did you determine garbage collection was causing the hiccups? It makes sense why it would, but interested to know how to recreate. How are you sending the samples to PortAudio?

If garbage collection is a blocker, it sounds like a seekToSampleFrame() method wouldn't fix your problem, although it would have removed the need to create a custom version of read().

from wavefile.

alxx avatar alxx commented on May 28, 2024

I'm not sure it was the GC itself, actually the GC is operating out-of-band as far as I know, so it's not a likely culprit (though I may be wrong).

I use ffi-portaudio, a Ruby implementation where I keep supplying sample buffers to a C library that speaks to the sound card. If I don't supply samples in time, it causes hiccups. I prepare buffers in advance in a Ruby Queue but when there's just no data (for example because Wavefile can't read them fast enough) then I'm forced to just supply zeroes. Then it's another kind of hiccup :))

Man, streaming is really not easy...

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.