Re: Setting the sample rate in a WAV file

[email protected] Fri, 7 May 2021 09:14:39 +0800
Newsgroups gmane.comp.audio.supercollider.user
Message-ID <CAFniQ7WbzJu1vrK4-ZdxK7_xiBpUh1330Wdgg37HJLYeLvw+5Q@mail.gmail.com>
On Fri, May 7, 2021 at 3:23 AM <[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/