[LAU] Library for simple audio programming
James Harkins
jamshark70 at gmail.com
Thu Jan 17 03:09:55 UTC 2013
On 01/16/2013 07:28 PM, Kjetil Matheussen wrote:
> James Harkins:
>> FWIW, this task is incredibly easy in SuperCollider:
>
> Perhaps it's easier than writing C, but there
> should be many alternatives that are easier than your example.
>
> For instance SND:
>
> (define reader (make-readin "filename.wav"))
> (call-with-output-file "outfile.txt"
> (lambda (out)
> (let loop ((i 0))
> (if (< i (mus-length reader))
> (begin
> (format out "~A\n" (readin reader))
> (loop (1+ i)))))))
>
I would debate that SND is "easier." Mine appeared to be more verbose
because I commented heavily to explain everything. Without comments:
f = SoundFile.openRead(Platform.resourceDir +/+ "sounds/a11wlk01.wav");
f.seek(4410); // 0.1 seconds in
f.readData(d = FloatArray.newClear(50));
f.close;
o = File("~/samples.txt", "w");
d.do { |sample| o << sample << Char.nl };
o.close;
... SC actually clocks in with one less line.
Not many people use SC for commandline scripting, but it is possible
(attached).
sclang sf-to-text.scd ~/share/SC/sounds/drums/kix/aphexbd.aif
~/tmp/testout.txt 100 50
hjh
-------------- next part --------------
// commandline args: input file, output file, start frame, num frames to copy
var inpath, outpath, startFrame, numFrames, infile, outfile, data, chunkSize = 65536;
#inpath, outpath, startFrame, numFrames = thisProcess.argv;
startFrame = (startFrame ? 0).asInteger;
numFrames = (numFrames ? -1).asInteger;
protect {
infile = SoundFile.openRead(inpath.standardizePath);
if(infile.notNil) {
if(numFrames <= 0) { numFrames = infile.numFrames };
outfile = File(outpath.standardizePath, "w");
if(outfile.isOpen) {
while {
numFrames > 0 and: {
data = FloatArray.newClear(min(numFrames, chunkSize));
infile.readData(data);
data.size > 0
}
} {
data.do { |sample| outfile << sample << Char.nl };
numFrames = numFrames - data.size;
};
} {
"ERROR: Couldn't open % for writing".format(outpath).postln;
-4.exit
}
} {
"ERROR: Couldn't open % for reading".format(inpath).postln;
-2.exit
};
} { |error|
// cleanup function: always runs
// so this always tidies up the file handles, error or no
infile.tryPerform(\close); outfile.tryPerform(\close);
// now we check if there was an error or not, for the return code
if(error.notNil) {
error.reportError;
"Unhandled exception".postln;
-1.exit;
} {
0.exit // success
};
};
More information about the Linux-audio-user
mailing list