Giter Site home page Giter Site logo

Reference for older methods about wavefile HOT 4 CLOSED

jstrait avatar jstrait commented on May 28, 2024
Reference for older methods

from wavefile.

Comments (4)

jstrait avatar jstrait commented on May 28, 2024

Thanks for the question - after looking at the gist, I think I might see the problem.

It seems like the gist relies on having the sample data for the entire file in the sample_data array. Older versions of the WaveFile gem read the entire file into memory, which meant that the sample_data field would contain the file's entire sample data. However, more recent versions instead allow reading a *.wav file in a buffered way. Calling each_buffer will normally only return a portion of the file's sample data at a time.

If you want to read the entire file with the current version of the WaveFile gem, you could do:

all_samples_in_file = reader.read(reader.total_sample_frames)

Or:

reader.each_buffer(reader.total_sample_frames) do |buffer|
  # `buffer` will contain all samples in the file
end

Hope this helps!

from wavefile.

arjunmenon avatar arjunmenon commented on May 28, 2024

Ok, so if I omit reader.total_sample_frames does that mean we can use it for continuous stream, like from a mic or from a streaming source?

from wavefile.

jstrait avatar jstrait commented on May 28, 2024

In theory I think it should be possible to read sample data from a *.wav file in a streaming way, using Reader.read, if you construct it with the relevant IO instance. However, this will only work if the sample data being read from the IO instance is wrapped in a *.wav file. If the stream only contains raw sample data, then the WaveFile gem won't know how to read it.

Raw sample data coming from a microphone will probably not be wrapped inside a *.wav file.

If you're trying to get sample data in real time from a microphone or other sound source, you might want to look into something like a PortAudio wrapper gem for Ruby, such as https://rubygems.org/gems/ffi-portaudio or https://rubygems.org/gems/easy_audio. I haven't used these myself, but I found them from searching on rubygems.org.

Here's an example of how to read a *.wav file from a stream using the WaveFile gem, adapted from the example at http://wavefilegem.com/examples.html:

SAMPLES_PER_BUFFER = 4096
streaming_io = <construct IO instance here>

reader = Reader.new(streaming_io)
begin
  while true do
    buffer = reader.read(SAMPLES_PER_BUFFER)
    puts "Read #{buffer.samples.length} samples."
  end
rescue EOFError
  reader.close
end

Hope this helps!

from wavefile.

jstrait avatar jstrait commented on May 28, 2024

Closing this issue, hope this helped!

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.