Comments (4)
@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.
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.
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.
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)
- 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
- Reference for older methods HOT 4
- examples: how create reverse file? HOT 2
- Mix 2 wav files. HOT 2
- 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.