Re: Setting the sample rate in a WAV file

[email protected] Sat, 8 May 2021 08:42:27 +0800
Newsgroups gmane.comp.audio.supercollider.user
Message-ID <CAFniQ7XXzn2iS4oFeO8a4SF+1V0uZfEA3SGoeJZXsthMA75s+g@mail.gmail.com>
On Sat, May 8, 2021 at 5:52 AM <[email protected]> wrote:
> that’s helpful. As it happens, I don’t want to use loadtoFloatArray while the buffer is in use, because my client program is already very busy loading and dealing with multiple data streams in parallel from the server (using another custom mechanism).

loadToFloatArray uses a temporary file on disk, so it should be fast.

> In the end I did this: Once the oddball file is written and closed (with the server’s sample rate), I re-open it with SoundFile, and read it all into memory (it’s fairly small with a 100 Hz sample rate, and not so many channels). Then, I have to delete the disk file, or its header sample rate will take precedence when I write the data back (!?). Then I create a new SoundFile with the old name, and write the data back from memory, with the new sample rate. A bit clunky, but at least it works. It would have been nice to be able just to patch the file header. Oh well.

We use libsndfile to read and write audio files. If libsndfile
provides a function to update the header without touching the data
chunk, then it would be possible in theory to add a primitive into
sclang to call that function.

A cursory glance at https://libsndfile.github.io/libsndfile/api.html
doesn't reveal an obvious function for it, so that might not be
possible.

Here's what else you could do:

1. Look up the binary format for wav headers (or aiff or whichever
you're using).

2. Open the file with `File(path, "r+")`.

3. file.seek(byte_offset_to_sample_rate, 0) -- note, you'll have to
find this byte offset yourself, from the header format documentation

4. file.putInt8 a few times to write the correct bytes for the sample
rate (your responsibility to encode the number correctly)

5. file.close

Of course, if your code doesn't do it correctly, you might end up with
an unusable file.

hjh

_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/