New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to append to existing wav without reading the wav data first? #22
Comments
That’s an interesting problem, and one that I do not have a good answer to currently. To clarify, having a long-running process that writes the file in one go is not an option for you? I can imagine adding something like WavWriter::append(writer: W, spec: WavSpec) -> Result<WavWriter<W>>
where W: Read + Write + Seek that would assert that the current header agrees with the spec, seeks to the end, and behaves as if the As a temporary workaround, you could open the file in append mode, and append samples manually using |
Unfortunately it's not an option because the data would be multiple GB and it has to run as a 32-bit process (and the rest of the application already uses a lot of RAM). And the stream session length doesn't have an upper bound, and it should be possible to run this for a long time (even days) with constant RAM usage.. But this Btw, I'm also concerned about losing already written wav data when the application crashes after some samples were written before the writer was finalized to write the header.. Or let's say I open this corrupted wav file again with let sample_bytes = file_size - header_size;
let sample_total_size = self.bytes_per_sample * self.spec.channels;
let seek_pos = sample_bytes / sample_total_size * sample_total_size; // round down
// delete remaining bytes after seek_pos and seek to there to continue writing from there Then after this writer is finalized it will have repaired the corrupt wav file without losing the samples that were recorded previously.. |
This should be possible even with the normal writer, it uses constant memory, and only few bytes by itself. If you use an underlying
I think it would make sense to add a
I would say in general the safe thing to do is to return an I will try to have a go at this next week. |
Ah right, the |
I have a work in progress in the Hound can read more kinds of files than it can write, so appending will not always be possible. I still have to handle that properly and return |
Thanks :) I think it's ok to just read the spec from the existing file. Btw, now after opening a |
The writer does store the spec, so it could have a getter, good idea! I’ll add one. |
I pushed another update that makes the implementation more robust. (It will not corrupt files that were not written by Hound.) Does the current API suit your needs @Boscop? If it does I will make a new release shortly. |
Yes, thanks :) Btw, I see that you are using a custom |
Hound 3.4.0 with append support is now on crates.io! I renamed
The reason is mostly historical; I introduced the trait in March 2015 (c61733a), analogous to And now that all of the code is already there and working fine, there is little advantage to switching to an external crate. There are no code size advantages because all of the functions are inlined, and there is a significant cost to dependencies that is often underestimated. |
I want to record an audio stream from a live performance to wav, in chunks every few seconds.
How to append to an existing wav file with hound without having to read the existing wav data first?
(The recording session could be many hours long.)
The text was updated successfully, but these errors were encountered: