ref: f5929fb0d242dbb338e5f6cae46e8f8a5a980aae
dir: /sample/sample.c/
#include <u.h> #include <libc.h> static void usage(void) { print("usage: %s [-b buf_frames] [-c channels]\n", argv0); exits("usage"); } void main(int argc, char **argv) { int bufsz, bufframes, framesz, chan, n, r, end, i; float f; u8int *b; bufframes = 2048; chan = 1; ARGBEGIN{ case 'b': bufframes = atoi(EARGF(usage())); break; case 'c': chan = atoi(EARGF(usage())); break; default: usage(); }ARGEND if (bufframes < 1) sysfatal("invalid buffer size: %d frames", bufframes); if (chan < 1) sysfatal("invalid number of channels: %d", chan); framesz = chan * 4; bufsz = bufframes * framesz; b = malloc(bufsz); n = 0; for (;;) { r = read(0, b+n, bufsz-n); if (r < 0) exits("read failed"); else if (r == 0) break; n += r; end = n & ~(framesz-1); if (end < 1) continue; for (; end > 0; end -= framesz) { for (i = 0; i < chan; i++) { f = *(float*)(b + end - framesz - i*4); if (f != 0) goto out; } } out: if (end == 0) break; if (write(1, b, end) != end) exits("write failed"); n -= end; memmove(b, b+end, n); } exits(nil); }