Comments (4)
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.
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.
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.
Closing this issue, hope this helped!
from wavefile.
Related Issues (20)
- Possible bug when no block is given to the writer? HOT 2
- No high-level duration info HOT 2
- Example here - https://github.com/jstrait/wavefile/wiki/WaveFile-Tutorial#copying-a-wave-file-to-different-format working correctly? HOT 5
- fyi: ruby-wavefile now packaged for Debian HOT 1
- support reading from a file or stream HOT 5
- Duration does not override equality HOT 2
- UnsupportedFormatError HOT 8
- Method to obtain markers/cue points HOT 6
- each_buffer causes ReaderClosedError HOT 3
- examples: how create reverse file? HOT 2
- Mix 2 wav files. HOT 2
- Rewind the IO object HOT 4
- Format Chunks With Extra Bytes at the End Sometimes Cause `InvalidFormatError` to be Raised HOT 1
- `Reader` instances can be created for WAVE_FORMAT_EXTENSIBLE files that have an incomplete/missing format chunk extension HOT 1
- Bufer from bytes & mulaw HOT 2
- WaveFile::Reader doesn't work with pipe IO HOT 2
- Sample Data Can't Be Read From a WAVE_FORMAT_EXTENSIBLE File With an Oversized Format Chunk Extension HOT 1
- How i can play file HOT 1
- Misleading error message if "fmt " chunk extension is too large to fit into chunk HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wavefile.