-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbag.cpp
177 lines (153 loc) · 4.2 KB
/
bag.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
bag -- A quick hack to buffer audio data between TiMidity and an
external player program.
This program is in the Public Domain. It is distributed without
much hope that it will be useful, and WITHOUT ANY WARRANTY;
definitely without any implied warranty of MERCHANTABILITY or
FITNESS FOR ANY PURPOSE.
bag may enable you to play complex files at higher output rates,
but it's a bit of a pain to use.
Here's a usage example for Linux. splay is a program that (with
this invocation) plays audio data from stdin. Your flag
requirements may vary.
timidity -id -Or1lsS -o- -s22 -p48 filenames |
bag -b 2M -t 1M -c 4k |
splay -b16 -s22 -S
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
static int bytes(char *s)
{
int i;
i=atoi(s);
while (*s>='0' && *s<='9')
s++;
switch(*s)
{
case 'm':
case 'M':
i *= 1024;
case 'k':
case 'K':
i *= 1024;
}
return i;
}
int main(int argc, char **argv)
{
int bs=1024*1024, rs=4*1024, ws=4*1024, cs=4*1024, thresh=64*1024;
int c, junk=0, verbosity=0, flush_and_go=0;
char *bag, *head, *tail;
extern char *optarg; /* for Sun's gcc */
fd_set readfds, writefds, brokefds;
struct timeval tmo;
while ((c=getopt(argc, argv, "b:t:r:w:c:hv"))>0)
switch(c)
{
case 'b': bs=bytes(optarg); break;
case 't': thresh=bytes(optarg); break;
case 'r': rs=bytes(optarg); break;
case 'w': ws=bytes(optarg); break;
case 'c': rs=ws=bytes(optarg); break;
case 'v': verbosity++; break;
case '?':
case 'h':
printf("\nBag usage:\n\n"
" timidity -id -Or[format options] -o- [options and filenames] |\n"
" bag [bag options] | audio_player [player options]\n\n"
"Bag options:\n\n"
" -b size Set buffer size (default %d)\n"
" -r size Set read chunk size (default %d)\n"
" -w size Set write chunk size (default %d)\n"
" -c size Set read/write chunk size\n"
" -t size Set start threshold for writing (default %d)\n"
" -v Verbose mode\n"
" -h This help screen\n\n"
,bs,rs,ws,thresh);
return 0;
}
if (ws>rs) cs=ws;
else cs=rs;
if (bs < ws+rs)
{
bs=ws+rs;
fprintf(stderr, "bag: Buffer size increased to %d bytes\n", bs);
}
if (thresh > (bs-rs))
{
thresh=bs-rs;
fprintf(stderr, "bag: Threshold lowered to %d bytes\n", bs);
}
head=tail=bag=malloc(bs);
junk=0;
while (!flush_and_go)
{
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&brokefds);
if (junk<(bs-rs))
FD_SET(STDIN_FILENO, &readfds);
if (junk>ws && junk>thresh)
{
FD_SET(STDOUT_FILENO, &writefds);
thresh=0;
}
FD_SET(STDIN_FILENO, &brokefds);
FD_SET(STDOUT_FILENO, &brokefds);
tmo.tv_sec=1;
tmo.tv_usec=0;
#ifdef HPUX
if ((c=select(FD_SETSIZE,(int *) &readfds,(int *) &writefds, (int *) &brokefds, &tmo))<0)
#else
if ((c=select(FD_SETSIZE, &readfds, &writefds, &brokefds, &tmo))<0)
#endif
perror("\nbag: select");
if (verbosity)
fprintf(stderr, "\rbag: %8d bytes", junk);
if (FD_ISSET(STDOUT_FILENO, &brokefds))
{
fprintf(stderr, "\nbag: Elvis has left the building\n");
return 1;
}
if (FD_ISSET(STDIN_FILENO, &readfds))
{
if ((c=read(STDIN_FILENO, head,
(head+rs >= bag+bs) ? (bag+bs-head) : rs))<=0)
{
if (c==0 || errno!=EAGAIN)
flush_and_go=1;
}
else
{
junk+=c;
head+=c;
if (head>=bag+bs)
head-=bs;
}
}
else if (FD_ISSET(STDIN_FILENO, &brokefds))
flush_and_go=1;
if (flush_and_go || FD_ISSET(STDOUT_FILENO, &writefds))
do
{
c=(tail+ws >= bag+bs) ? (bag+bs-tail) : ws;
if (c>junk) c=junk;
if (flush_and_go && verbosity)
fprintf(stderr, "\rbag: Flushing, %8d bytes", junk);
c=write(STDOUT_FILENO, tail, c);
junk -= c;
tail += c;
if (tail>=bag+bs)
tail-=bs;
} while (flush_and_go && junk);
}
if (verbosity)
fprintf(stderr, "\nbag: All done\n");
return 0;
}