sampleio.c
1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "vadpcm.h"
#define MAXSIZE 2048
static void
fmtchan(int size, char *p, int *data, int stride)
{
int i, c;
for(i=0; i<size; i++) {
c = *data++;
if(c < -0x7fff)
c = -0x7fff;
if(c > 0x7fff)
c = 0x7fff;
p[0] = c>>8;
p[1] = c;
p += stride;
}
}
void
writeout(FILE *outfd, int size, int *l_out, int *r_out, int chans)
{
static char obuf[MAXSIZE<<1];
int i;
switch (chans){
case 2:
fmtchan(size, obuf, l_out, 2*chans);
fmtchan(size, obuf+2, r_out, 2*chans);
if (outfd!=NULL){
if ((i=fwrite(obuf, 1, 4*size, outfd)) != 4*size) {
fprintf(stderr, "write error %d\n",i);
exit(1);
}
}
break;
case 1: /* For mono only use the left channel buffer */
fmtchan(size, obuf, l_out, 2);
if (outfd!=NULL){
if ((i=fwrite(obuf, 1, 2*size, outfd)) != 2*size) {
fprintf(stderr, "write error %d\n",i);
exit(1);
}
}
break;
default:
fprintf(stderr,"Error in number of channels\n");
exit(1);
}
}