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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
/*************************************************************************/
/* */
/* Centre for Speech Technology Research */
/* University of Edinburgh, UK */
/* Copyright (c) 1996,1997 */
/* All Rights Reserved. */
/* */
/* Permission is hereby granted, free of charge, to use and distribute */
/* this software and its documentation without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of this work, and to */
/* permit persons to whom this work is furnished to do so, subject to */
/* the following conditions: */
/* 1. The code must retain the above copyright notice, this list of */
/* conditions and the following disclaimer. */
/* 2. Any modifications must be clearly marked as such. */
/* 3. Original authors' names are not deleted. */
/* 4. The authors' names are not used to endorse or promote products */
/* derived from this software without specific prior written */
/* permission. */
/* */
/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
/* THIS SOFTWARE. */
/* */
/*************************************************************************/
/* Author : Alan W Black */
/* Date : September 1996 */
/*-----------------------------------------------------------------------*/
/* */
/* An audio file spooler, like lpd. Reads in commands about files to */
/* to play, and queues them until any previous requests are finished. */
/* This allows the synthesizer to get on with synthesizing the next */
/* utterance. */
/* */
/* Actually this doesn't use anything in Festival, only the speech_tools */
/*=======================================================================*/
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <csignal>
using namespace std;
#include "EST.h"
#include "EST_unix.h"
#ifdef NO_SPOOLER
int main(int argc, char **argv)
{
printf("Audio spooler not supported\n");
return 0;
}
#else
class Command {
private:
EST_String p_file;
int p_rate;
public:
Command(const EST_String &f, int rate) { p_file=f; p_rate=rate; }
int rate(void) const { return p_rate; }
const EST_String &file(void) const { return p_file; }
};
class CQueue_Item {
public:
Command *c;
CQueue_Item *next;
CQueue_Item(Command *com) { c=com; next=0; }
~CQueue_Item() { delete c; if (next != 0) delete next; }
};
class CQueue {
private:
CQueue_Item *head;
CQueue_Item *tail;
public:
CQueue() { head = tail = 0; }
~CQueue() { delete head; }
void push(Command *c);
Command *pop(void);
void display(void) const;
int length(void) const;
void clear(void);
};
static void auspl_main(int argc, char **argv);
static void check_new_input(void);
static char *read_a_line(void);
static void process_command(char *line);
static void check_new_output(void);
static int execute_command(Command *c);
static void load_play_file(Command *c);
static int sp_terminate(void);
static void tidy_up(void);
void CQueue::push(Command *c)
{
// Put this item on tail
CQueue_Item *n = new CQueue_Item(c);
if (head == 0)
{ // first one
head = n;
tail = n;
}
else
{
tail->next = n;
tail = n;
}
}
Command *CQueue::pop(void)
{
// Pop top from the queue
if (head == 0)
return 0;
else
{
Command *c = head->c;
CQueue_Item *h;
h = head;
h->c = 0;
head = head->next;
h->next = 0;
delete h;
return c;
}
}
void CQueue::display(void) const
{
CQueue_Item *t;
int i;
cerr << "Command_queue: " << length() << endl;
for (i=0,t=head; t != 0; t=t->next,i++)
cerr << " " << i << ": " << t->c->file() << endl;
}
int CQueue::length(void) const
{
// length of queue
CQueue_Item *t;
int i;
for (i=0,t=head; t != 0; t=t->next)
i++;
return i;
}
void CQueue::clear(void)
{
// Remove all memebers in the queue
CQueue_Item *t;
// Somebody has to do it ...
for (t=head; t != 0; t=t->next)
unlink(t->c->file());
delete head;
head = 0;
tail = 0;
}
static int no_more_input = FALSE;
static CQueue command_queue;
static int child_pid = 0;
static EST_String current_file;
static EST_Option play_wave_options;
static int maxqueue = 5;
static int pending_close = FALSE;
static int kids = 0;
int main(int argc, char **argv)
{
auspl_main(argc,argv);
return 0;
}
static void auspl_main(int argc, char **argv)
{
EST_Option al;
EST_StrList files;
parse_command_line(argc, argv,
EST_String("Usage: audio spooler \n")+
"auspl <options> <file0> <file1> ...\n"+
"--method <string> audio play method\n"+
"--command <string> Unix command to play file, used when\n"+
" method is audio_command\n"+
"--maxqueue <int> {5} Maximum number of files in queue\n",
files, al);
if (al.present("--method"))
play_wave_options.add_item("-p",al.val("--method"));
if (al.present("--command"))
play_wave_options.add_item("-command",al.val("--command"));
play_wave_options.add_item("-quality","HIGH");
if (al.present("--maxqueue"))
maxqueue = al.ival("--maxqueue");
while (!sp_terminate())
{
check_new_input();
check_new_output();
}
tidy_up();
}
static int sp_terminate(void)
{
// I'm never very sure of all the conditions necessary to terminate
if (no_more_input && (command_queue.length() == 0))
return TRUE;
else
return FALSE;
}
static void tidy_up(void)
{
// should wait for any remaining children if I've been
// requested to.
int pid;
int statusp;
if (pending_close == TRUE)
{
while (kids > 0)
{
pid = waitpid(0,&statusp,0);
kids--;
}
fprintf(stdout,"OK\n"); // give an acknowledgement
fflush(stdout);
}
return;
}
static void check_new_input(void)
{
// Do a select on stdin to find out if there is any new
// commands to process
fd_set inset;
fd_set outset;
fd_set exset;
struct timeval t;
int sv;
t.tv_sec = 0;
t.tv_usec = 1000; // 0.1 seconds
FD_ZERO(&inset);
FD_ZERO(&outset);
FD_ZERO(&exset);
if ((command_queue.length() >= maxqueue) ||
no_more_input)
{
// wait a bit for the queue to go down a bit
// not we're selecting on no fds at all, just for the delay
sv = select(0,&inset,&outset,&exset,&t);
return;
}
FD_SET(0,&inset);
sv = select(1,&inset,&outset,&exset,&t);
if (sv == 1)
process_command(read_a_line());
else if (sv == -1)
no_more_input = TRUE;
}
static int getc_unbuffered(int fd)
{
// An attempted to get rid of the buffering
char c;
int n;
n = read(fd,&c,1);
if (n == 0)
return EOF;
else
return c;
}
static char *read_a_line(void)
{
// read upto \n on stdin -- wonder if I should read instead
int maxsize = 1024;
char *line = walloc(char,maxsize+2);
int i,c;
for (i=0;
(((c=getc_unbuffered(0)) != '\n') &&
(c != EOF));
i++)
{
if (i == maxsize)
{
char *nline = walloc(char,maxsize*2);
memcpy(nline,line,maxsize);
maxsize = maxsize*2;
wfree(line);
line = nline;
}
line[i] = c;
}
line[i] = '\n';
line[i+1] = '\0';
if (c == EOF)
no_more_input = TRUE;
if (strncmp(line,"close",5) != 0)
{
fprintf(stdout,"OK\n"); // give an acknowledgement
fflush(stdout);
}
return line;
}
static void process_command(char *line)
{
// Process command, some are immediate
EST_TokenStream ts;
ts.open_string(line);
EST_String comm = ts.get().string();
if ((comm == "quit") || (comm == ""))
{
no_more_input = TRUE;
}
else if (comm == "play")
{
EST_String file = ts.get().string();
int rate = atoi(ts.get().string());
Command *c = new Command(file,rate);
command_queue.push(c);
}
else if (comm == "method")
{
play_wave_options.add_item("-p",ts.get().string());
}
else if (comm == "command")
{
play_wave_options.add_item("-command",ts.get_upto_eoln().string());
}
else if (comm == "rate")
{
play_wave_options.add_item("-rate",ts.get().string());
}
else if (comm == "otype")
{
play_wave_options.add_item("-otype",ts.get().string());
}
else if (comm == "device")
{
play_wave_options.add_item("-audiodevice",ts.get().string());
}
else if (comm == "close")
{
pending_close = TRUE;
no_more_input = TRUE;
}
else if (comm == "shutup")
{
// clear queue and kill and child currently playing
command_queue.clear();
if (child_pid != 0)
{
kill(child_pid,SIGKILL);
unlink(current_file);
}
}
else if (comm == "query")
command_queue.display();
else if (comm != "")
{
cerr << "audsp: unknown command \"" << comm << "\"\n";
}
ts.close();
wfree(line);
}
static void check_new_output(void)
{
// If we are not waiting on any children lauch next command
int pid;
int statusp;
if (kids > 0)
{
pid = waitpid(0,&statusp,WNOHANG);
if (pid != 0)
{
kids--;
child_pid = 0;
}
}
else if (command_queue.length() != 0)
{
Command *c = command_queue.pop();
if (execute_command(c) == 0)
kids++;
delete c;
}
// else do nothing
}
static int execute_command(Command *c)
{
// Execute the command as a child process
int pid;
current_file = c->file();
if ((pid=fork()) == 0)
{ // child process
load_play_file(c);
_exit(0); // don't close any files on exit
return 0; // can't get here
}
else if (pid > 0)
{ // parent process
child_pid = pid;
return 0;
}
else
{
cerr << "auspd: fork failed, \"" << c->file() << "\"\n";
return -1;
}
}
static void load_play_file(Command *c)
{
// Load in wave file and play it
EST_Wave w;
w.load(c->file());
play_wave(w,play_wave_options);
unlink(c->file()); // delete it afterwards
}
#endif
|