Zitat:
void input_sound(unsigned int sample_rate, const char *ifname) {
/************************************************** *********************
* Audio-Device abfragen
************************************************** *********************/
int sndparam;
int fd;
/* verdoppelt für 2-Kanal */
union {
short s[8192 * CHANNELS];
unsigned char b[8192 * CHANNELS];
} b;
float fbuf[CHANNELS][16384];
unsigned int fbuf_cnt = 0;
int i;
short *sp;
unsigned char *bp;
int fmt = 0;
/* gfgf ==> */
time_t tp;
int rec_n, n;
void *rec_p;
/* <== */
if ((fd = open(ifname ? ifname : "/dev/dsp", O_RDONLY)) < 0) {
perror("open");
exit(10);
}
sndparam = AFMT_S16_LE; /* we want 16 bits/sample signed */
/* little endian; works only on little endian systems! */
if (ioctl(fd, SNDCTL_DSP_SETFMT, &sndparam) == -1) {
perror("ioctl: SNDCTL_DSP_SETFMT");
exit(10);
}
if (sndparam != AFMT_S16_LE) {
fmt = 1;
sndparam = AFMT_U8;
if (ioctl(fd, SNDCTL_DSP_SETFMT, &sndparam) == -1) {
perror("ioctl: SNDCTL_DSP_SETFMT");
exit(10);
}
if (sndparam != AFMT_U8) {
perror("ioctl: SNDCTL_DSP_SETFMT");
exit(10);
}
}
sndparam = 1; /* 2 Kanäle */
if (ioctl(fd, SNDCTL_DSP_STEREO, &sndparam) == -1) {
perror("ioctl: SNDCTL_DSP_STEREO");
exit(10);
}
if (sndparam != 1) {
/* Monovariante? */
fprintf(stderr, "soundif: Error, cannot set the channel "
"number to 2\n");
exit(10);
}
sndparam = sample_rate;
if (ioctl(fd, SNDCTL_DSP_SPEED, &sndparam) == -1) {
// perror("ioctl: SNDCTL_DSP_SPEED");exit(10);
}
if ((10 * abs(sndparam - sample_rate)) > sample_rate) {
// perror("ioctl: SNDCTL_DSP_SPEED");exit(10);
}
if (sndparam != sample_rate) {
fprintf(stderr, "Warning: Sampling rate is %u, "
"requested %u\n", sndparam, sample_rate);
}
#if 0
sndparam = 4;
if (ioctl(fd, SOUND_PCM_SUBDIVIDE, &sndparam) == -1) {
perror("ioctl: SOUND_PCM_SUBDIVIDE");
}
if (sndparam != 4) {
perror("ioctl: SOUND_PCM_SUBDIVIDE");
}
#endif
for (;;) {
channel = 0;
if (fmt) {
i = read(fd, bp = b.b, sizeof(b.b));
if (i < 0 && errno != EAGAIN) {
perror("read");
exit(4);
}
if (!i)
break;
if (i > 0) {
if (rec.stat) {
time(&tp);
if (tp > rec.start + rec.time)
record_stop();
else {
verbprintf(3, "Aufzeichung Kanal %c %i sec", _channel[channel], tp - rec.start);
sprintf(rec.in_args, "-u -b -r %d -c 2 -t raw", sample_rate);
rec_n = i;
rec_p = (void *) bp;
while (rec_n) {
n = write(rec.fd, rec_p, rec_n);
rec_n -= n;
rec_p += rec_n;
}
}
}
for (; i >= sizeof(b.b[0]); i -= sizeof(b.b[0] * 2), bp++) {
fbuf[0][fbuf_cnt] = ((int)(*bp++) - 0x80) * (1.0 / 128.0);
fbuf[1][fbuf_cnt++] = ((int)(*bp) - 0x80) * (1.0 / 128.0);
}
if (i)
fprintf(stderr, "warning: noninteger number of samples read\n");
if (fbuf_cnt > overlap) {
process_buffer(fbuf[0], fbuf_cnt - overlap);
memmove(fbuf[0], fbuf[0] + fbuf_cnt - overlap, overlap * sizeof(fbuf[0][0]));
channel++;
process_buffer(fbuf[1], fbuf_cnt - overlap);
memmove(fbuf[1], fbuf[1] + fbuf_cnt - overlap, overlap * sizeof(fbuf[1][0]));
fbuf_cnt = overlap;
}
}
}
else {
#ifdef ZLOG
char outprint[ITEMLEN];
#endif
i = read(fd, sp = b.s, sizeof(b.s));
if (i < 0 && errno != EAGAIN) {
/* Schliessen des Sounddevices und neustarten */
close(fd);
return;
}
#ifdef ZLOG
update_file("newbuf", LOG);
#endif
if (!i) break;
if (i > 0) {
if (rec.stat) {
time(&tp);
if (tp > rec.start + rec.time)
record_stop();
else {
verbprintf(3, "Aufzeichung Kanal %c %i sec", _channel[channel], tp - rec.start);
sprintf(rec.in_args, "-e single-integer -2 -r %d -c 2 -t raw", sample_rate);
rec_n = i;
rec_p = (void *) sp;
while(rec_n) {
n = write(rec.fd, rec_p, rec_n);
rec_n -= n;
rec_p += rec_n;
}
}
}
/* <== */
for (; i >= sizeof(b.s[0]); i -= sizeof(b.s[0]) * 2, sp++) {
fbuf[0][fbuf_cnt] = (*sp++) * (1.0 / 32768.0);
fbuf[1][fbuf_cnt++] = (*sp) * (1.0 / 32768.0);
#ifdef ZLOG
memset(outprint, 0, sizeof(outprint));
sprintf(outprint, "fbuf[%d]\t\t[li]%8.5f\t\t[re]%8.5f", fbuf_cnt-1, fbuf[0][fbuf_cnt-1], fbuf[1][fbuf_cnt-1]);
update_file(outprint, LOG);
#endif
}
if (i)
fprintf(stderr, "warning: noninteger number of samples read\n");
if (fbuf_cnt > overlap) {
process_buffer(fbuf[0], fbuf_cnt - overlap);
memmove(fbuf[0], fbuf[0] + fbuf_cnt - overlap, overlap * sizeof(fbuf[0][0]));
channel++;
process_buffer(fbuf[1], fbuf_cnt - overlap);
memmove(fbuf[1], fbuf[1] + fbuf_cnt - overlap, overlap * sizeof(fbuf[1][0]));
fbuf_cnt = overlap;
}
}
}
}
close(fd);
}
#endif /* SUN_AUDIO */
/* ------------------------------------------------------------------- */
void record_start (short *code) {
/************************************************** *********************
* Audio-Aufzeichung starten
************************************************** *********************/
char cmdline[LINELEN], buf[5];
struct tm *today;
int i = 0;
time(&tp);
if(!rec.stat) {
memmove(buf, code, 5);
today = localtime(&tp);
strftime(cmdline, 17, "%Y-%m-%d_%H%M", today);
sprintf(rec.file, "%s/%s_", rec.path, cmdline);
while(i < 5)
sprintf(rec.file, "%s%i", rec.file, code[i++]);
if((rec.fd = open(rec.file, O_WRONLY | O_CREAT | O_TRUNC, 0664)) < 0) {
fprintf(stderr, "Cannot open %s\n", rec.file);
}
rec.chan = _channel[channel] + ' ';
rec.stat = 1;
verbprintf(2, "Aufzeichung Kanal %c", _channel[channel]);
}
else
verbprintf(2, "Aufzeichung Kanal %c verlängert", _channel[channel]);
rec.start = tp;
} /* record_start
---------------------------------------------------------------------- */
void record_stop (void) {
/************************************************** *********************
* Audio-Aufzeichung abschließen
************************************************** *********************/
char cmdline[256];
close(rec.fd);
rec.stat = 0;
sprintf(cmdline, "%s -v%f %s %s %s %s%s -%c", rec.cmd, (float)rec.volume / 100, rec.in_args, rec.file,
rec.out_args, rec.file, rec.suff, rec.chan);
system(cmdline);
sprintf(cmdline, "rm %s", rec.file);
system(cmdline);
verbprintf(2, "Aufzeichung Kanal %c beendet", _channel[channel]);
} /* record_stop
Original hier: