RE: Setting the sample rate in a WAV file

[email protected] Fri, 7 May 2021 21:49:28 +0000
Newsgroups gmane.comp.audio.supercollider.user
Message-ID <[email protected]>
Thanks all,

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).

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.

sternsc

From: [email protected] <[email protected]> On Behalf Of Joseph Anderson
Sent: Friday, 7 May 2021 04:47
To: [email protected]
Subject: Re: [sc-users] Setting the sample rate in a WAV file

Ah... yes, that's right. You'll need to pull the contents of the Buffer into an Array. For use w/ SignalBox's write message, the Array will have to be converted to a Signal first.




Dr Joseph Anderson | Research Scientist

DXARTS, Box 353414

University of Washington

Seattle, WA 98195-3680



http://www.dxarts.washington.edu



Subscribe to our events list<https://dxarts.washington.edu/mailing-list> to receive email updates about lectures, performances, exhibitions and more.



On Thu, May 6, 2021 at 6:15 PM <[email protected]<mailto:[email protected]>> wrote:
On Fri, May 7, 2021 at 3:23 AM <[email protected]<mailto:[email protected]>> wrote:
> I have a process that creates signals at rates other than the server’s sample rate, and I want to save these signals using a custom UGen GatedDiskOut that writes a frame only on a trigger (very useful). But then I need to get the effective sample rate into the WAV file header, and I can’t figure out how. I have tried buffer.sampleRate, but buffer.write and buffer.close both seem to ignore that, writing the server’s samplerate to the WAV header instead.

That's right. `buffer.write` uses the server command /b_write to
create the file. This can use the server's sample rate only. There's
no way to override that.

> sf.sampleRate and sf.asBuffer

Converting a SoundFile to a buffer would imply reading from a file,
wouldn't it? I can't see how that would help you with writing.

In any case... if you get the data into the language client
(`loadToFloatArray` or `getToFloatArray`), then you can use SoundFile
to write it with any header information you specify.

If b is your buffer:

(
var path = "..... where to save it...";
var yourDesiredSampleRate = 1000;  // or whatever

b.loadToFloatArray(action: { |data|
    var file = SoundFile(path);

    file.sampleRate_(yourDesiredSampleRate)
    .numChannels_(b.numChannels)
    .sampleFormat_("int24")  // or int16 or float
    .headerFormat_("WAV");

    protect {
        if(file.openWrite) {
            file.writeData(data);
        } {
            "% could not be opened for writing".format(path.basename).warn;
        };
    } { file.close };
});
)

The Signal:write method from the SignalBox quark just packages the
writing part of the above example into a single method call. But it
won't get the data from the server for you (as far as I can see).

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/