| 1 |
/* |
| 2 |
** |
| 3 |
** bplay/shmbuf.c (C) David Monro 1996 |
| 4 |
** |
| 5 |
** Copyright under the GPL - see the file COPYING in this directory |
| 6 |
** |
| 7 |
** Adapted by J.A. Bezemer for use with GramoFile - July, August 1998 |
| 8 |
** |
| 9 |
** Patch to compile with egcs from Daniel Kobras, applied by J.A. Bezemer |
| 10 |
** - October, 1998 |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <unistd.h> |
| 16 |
#include <errno.h> |
| 17 |
#include <signal.h> |
| 18 |
#include <sys/types.h> |
| 19 |
#include <sys/ipc.h> |
| 20 |
#include <sys/sem.h> |
| 21 |
#include <sys/shm.h> |
| 22 |
#include <sys/wait.h> |
| 23 |
|
| 24 |
#include "../errorwindow.h" |
| 25 |
#include "../clrscr.h" |
| 26 |
#include "../playwav.h" |
| 27 |
#include "../secshms.h" |
| 28 |
#include "../reclp_main.h" |
| 29 |
#include "../boxes.h" |
| 30 |
#include "../buttons.h" |
| 31 |
#include <string.h> |
| 32 |
#ifndef OLD_CURSES |
| 33 |
#include <ncurses.h> |
| 34 |
#else |
| 35 |
#include <curses.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifndef SEMMSL |
| 39 |
#ifdef __FreeBSD__ |
| 40 |
/* |
| 41 |
* you may want to adjust this to whats configured into your kernel, |
| 42 |
* 30 is just the current default. (see <sys/sem.h>) -nox |
| 43 |
*/ |
| 44 |
#define SEMMSL 30 |
| 45 |
#else |
| 46 |
#define SEMMSL 32 |
| 47 |
#endif |
| 48 |
#endif |
| 49 |
|
| 50 |
#ifdef _SEM_SEMUN_UNDEFINED |
| 51 |
union semun /* This has gone out of standard */ |
| 52 |
{ /* libc headers as of glibc2.1, */ |
| 53 |
int val; /* we need to define it ourselves.*/ |
| 54 |
struct semid_ds *buf; |
| 55 |
unsigned short int *array; |
| 56 |
struct seminfo *__buf; |
| 57 |
}; |
| 58 |
#endif |
| 59 |
|
| 60 |
extern void finish_curses(int sig); |
| 61 |
|
| 62 |
/* The size of the big array */ |
| 63 |
/* (currently 256K - nearly 1.5 sec at max rate) */ |
| 64 |
#define BIGBUFFSIZE 0x040000 |
| 65 |
|
| 66 |
/* Types */ |
| 67 |
typedef struct blockinf_t |
| 68 |
{ |
| 69 |
int count; /* How many bytes in this buffer */ |
| 70 |
int last; /* Should we terminate after this buffer? */ |
| 71 |
int setit; /* Should we re-set the audio parameters to be the ones here? */ |
| 72 |
int speed; |
| 73 |
int bits; |
| 74 |
int stereo; |
| 75 |
} blockinf_t; |
| 76 |
|
| 77 |
/* Statics - mostly shared memory etc */ |
| 78 |
static int shmid, shmid2, *disksemid, *sndsemid; |
| 79 |
static char *bigbuff; |
| 80 |
static char **buffarr; |
| 81 |
static int numbuffs, numsemblks; |
| 82 |
static blockinf_t *buffinf; |
| 83 |
|
| 84 |
/* prototypes */ |
| 85 |
void cleanupsems(void); |
| 86 |
static void sighandler(int i); |
| 87 |
|
| 88 |
/* Extern globals */ |
| 89 |
extern int abuf_size; |
| 90 |
extern int audio; |
| 91 |
extern char *progname; |
| 92 |
|
| 93 |
|
| 94 |
/* extern prototypes */ |
| 95 |
extern void ErrDie(char *err); |
| 96 |
extern void snd_parm(int speed, int bits, int stereo); |
| 97 |
extern void sync_audio(void); |
| 98 |
|
| 99 |
void init_shm(void) |
| 100 |
{ |
| 101 |
int i; |
| 102 |
|
| 103 |
/* Create, attach and mark for death the big buffer */ |
| 104 |
shmid = shmget(IPC_PRIVATE, BIGBUFFSIZE, |
| 105 |
IPC_EXCL | IPC_CREAT | 0600); |
| 106 |
if (shmid == -1) |
| 107 |
ErrDie("shmget"); |
| 108 |
bigbuff = shmat(shmid, IPC_RMID, SHM_RND); |
| 109 |
if (bigbuff == (char*)-1) |
| 110 |
{ |
| 111 |
perror("shmat"); |
| 112 |
if(shmctl(shmid, IPC_RMID, NULL)) |
| 113 |
perror("shmctl"); |
| 114 |
exit(-1); |
| 115 |
} |
| 116 |
if(shmctl(shmid, IPC_RMID, NULL)) |
| 117 |
ErrDie("shmctl"); |
| 118 |
|
| 119 |
/* Create an array of pointers. Point them at equally spaced |
| 120 |
** chunks in the main buffer, to give lots of smaller buffers |
| 121 |
*/ |
| 122 |
numbuffs = BIGBUFFSIZE/abuf_size; |
| 123 |
buffarr = (char**)malloc(numbuffs*sizeof(char*)); |
| 124 |
for (i=0; i<numbuffs; i++) |
| 125 |
buffarr[i] = bigbuff + i * abuf_size; |
| 126 |
|
| 127 |
/* Create a small amount of shared memory to hold the info |
| 128 |
** for each buffer. |
| 129 |
*/ |
| 130 |
shmid2 = shmget(IPC_PRIVATE, numbuffs*sizeof(blockinf_t), |
| 131 |
IPC_EXCL | IPC_CREAT | 0600); |
| 132 |
if (shmid2 == -1) |
| 133 |
ErrDie("shmget"); |
| 134 |
buffinf = (blockinf_t*)shmat(shmid2, IPC_RMID, SHM_RND); |
| 135 |
if (buffinf == (blockinf_t*)((char*)-1)) |
| 136 |
{ |
| 137 |
perror("shmat"); |
| 138 |
if(shmctl(shmid2, IPC_RMID, NULL)) |
| 139 |
perror("shmctl"); |
| 140 |
exit(-1); |
| 141 |
} |
| 142 |
if(shmctl(shmid2, IPC_RMID, NULL)) |
| 143 |
ErrDie("shmctl"); |
| 144 |
|
| 145 |
#if USEBUFFLOCK |
| 146 |
/* Ok, go root to lock the buffers down */ |
| 147 |
if(setreuid(geteuid(), getuid()) == -1) |
| 148 |
{ |
| 149 |
#ifndef LP2CD |
| 150 |
fprintf(stderr, "%s: setreuid: %s: continuing anyway\n", |
| 151 |
progname, strerror(errno)); |
| 152 |
fprintf(stderr, "real uid = %d, effective uid = %d\n", |
| 153 |
getuid(), geteuid()); |
| 154 |
#endif |
| 155 |
} |
| 156 |
|
| 157 |
if(shmctl(shmid, SHM_LOCK, NULL) || shmctl(shmid2, SHM_LOCK, NULL)) |
| 158 |
#ifndef LP2CD |
| 159 |
fprintf(stderr, |
| 160 |
"%s: shmctl: %s: continuing with unlocked buffers\n", |
| 161 |
progname, strerror(errno)) |
| 162 |
#endif |
| 163 |
; |
| 164 |
|
| 165 |
if(setreuid(geteuid(), getuid()) == -1) |
| 166 |
{ |
| 167 |
#ifndef LP2CD |
| 168 |
fprintf(stderr, "%s: setreuid: %s: continuing anyway\n", |
| 169 |
progname, strerror(errno)); |
| 170 |
fprintf(stderr, "real uid = %d, effective uid = %d\n", |
| 171 |
getuid(), geteuid()); |
| 172 |
#endif |
| 173 |
} |
| 174 |
|
| 175 |
#endif |
| 176 |
/* Set up the appropriate number of semaphore blocks */ |
| 177 |
numsemblks = numbuffs/SEMMSL; |
| 178 |
if((numsemblks * SEMMSL) < numbuffs) |
| 179 |
numsemblks++; |
| 180 |
/* Malloc arrays of semaphore ids (ints) for the semaphores */ |
| 181 |
if ((disksemid = (int*)malloc(sizeof(int)*numsemblks)) == NULL) |
| 182 |
ErrDie("malloc"); |
| 183 |
if ((sndsemid = (int*)malloc(sizeof(int)*numsemblks)) == NULL) |
| 184 |
ErrDie("malloc"); |
| 185 |
/* Create the semaphores */ |
| 186 |
for (i=0;i<numsemblks;i++) |
| 187 |
{ |
| 188 |
if ((disksemid[i] = semget(IPC_PRIVATE, SEMMSL, |
| 189 |
IPC_EXCL | IPC_CREAT | 0600)) == -1) |
| 190 |
ErrDie("semget"); |
| 191 |
if ((sndsemid[i] = semget(IPC_PRIVATE, SEMMSL, |
| 192 |
IPC_EXCL | IPC_CREAT | 0600)) == -1) |
| 193 |
ErrDie("semget"); |
| 194 |
} |
| 195 |
/* Catch some signals, so we clean up semaphores */ |
| 196 |
signal(SIGINT, sighandler); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
/* Does an up on the appropriate semaphore */ |
| 201 |
void up(int *semblk, int xsemnum) |
| 202 |
{ |
| 203 |
struct sembuf sbuf; |
| 204 |
|
| 205 |
sbuf.sem_num = xsemnum%SEMMSL; |
| 206 |
sbuf.sem_op = 1; |
| 207 |
sbuf.sem_flg = 0; |
| 208 |
|
| 209 |
if (semop(semblk[xsemnum/SEMMSL], &sbuf, 1) == -1) |
| 210 |
perror("semop"); |
| 211 |
} |
| 212 |
|
| 213 |
/* Does a down on the appropriate semaphore */ |
| 214 |
void down(int *semblk, int xsemnum) |
| 215 |
{ |
| 216 |
struct sembuf sbuf; |
| 217 |
|
| 218 |
sbuf.sem_num = xsemnum%SEMMSL; |
| 219 |
sbuf.sem_op = -1; |
| 220 |
sbuf.sem_flg = 0; |
| 221 |
|
| 222 |
if (semop(semblk[xsemnum/SEMMSL], &sbuf, 1) == -1) |
| 223 |
perror("semop"); |
| 224 |
} |
| 225 |
|
| 226 |
/* The recording function */ |
| 227 |
void shmrec(int outfd, long totalcount, int terminate) |
| 228 |
{ |
| 229 |
pid_t pid; |
| 230 |
int i; |
| 231 |
button_t ok_button; |
| 232 |
char timestring[100]; |
| 233 |
|
| 234 |
nodelay(stdscr, TRUE); |
| 235 |
|
| 236 |
sync(); |
| 237 |
|
| 238 |
pid = fork(); |
| 239 |
if (pid == 0) |
| 240 |
{ |
| 241 |
int cbuff = 0; |
| 242 |
long totalrd = 0; /* we need it here too... */ |
| 243 |
|
| 244 |
#ifdef VUMETER |
| 245 |
signed short *ssptr; |
| 246 |
signed short leftvalue, rightvalue, maxleft, maxright; |
| 247 |
long samplesabove50pct = 0; |
| 248 |
long samplesabove90pct = 0; |
| 249 |
long samplesabove99pct = 0; |
| 250 |
long samplestooloud = 0; |
| 251 |
#endif |
| 252 |
|
| 253 |
/* Uncatch the signals */ |
| 254 |
signal(SIGINT, SIG_DFL); |
| 255 |
|
| 256 |
/* Child process writes the disk */ |
| 257 |
while(1) |
| 258 |
{ |
| 259 |
long count, numwr, trgt; |
| 260 |
char *tmpptr; |
| 261 |
|
| 262 |
/* Grab the buffer. Blocks till it is OK to do so. */ |
| 263 |
down(disksemid, cbuff); |
| 264 |
/* Spit it out */ |
| 265 |
tmpptr = buffarr[cbuff]; |
| 266 |
#ifdef VUMETER |
| 267 |
ssptr = (signed short *) tmpptr; |
| 268 |
#endif |
| 269 |
numwr = 0; |
| 270 |
trgt = buffinf[cbuff].count; |
| 271 |
while ( (numwr < trgt) && |
| 272 |
((count = write(outfd, tmpptr, trgt - numwr)) > 0) ) |
| 273 |
{ |
| 274 |
numwr += count; |
| 275 |
tmpptr += count; |
| 276 |
totalrd += count; |
| 277 |
} |
| 278 |
|
| 279 |
#ifdef VUMETER |
| 280 |
maxleft = 0; |
| 281 |
maxright = 0; |
| 282 |
trgt = buffinf[cbuff].count; |
| 283 |
|
| 284 |
for (numwr = 0; numwr < trgt; numwr += 4 ) |
| 285 |
{ |
| 286 |
leftvalue=abs(*ssptr); |
| 287 |
if (leftvalue > maxleft) maxleft = leftvalue; |
| 288 |
ssptr ++; |
| 289 |
|
| 290 |
rightvalue=abs(*ssptr); |
| 291 |
if (rightvalue > maxright) maxright = rightvalue; |
| 292 |
ssptr ++; |
| 293 |
|
| 294 |
if (leftvalue > 16383 || rightvalue > 16383) |
| 295 |
samplesabove50pct ++; |
| 296 |
if (leftvalue > 29490 || rightvalue > 29490) |
| 297 |
samplesabove90pct ++; |
| 298 |
if (leftvalue > 32439 || rightvalue > 32439) |
| 299 |
samplesabove99pct ++; |
| 300 |
if (leftvalue > 32764 || rightvalue > 32764) |
| 301 |
samplestooloud ++; |
| 302 |
} |
| 303 |
move(ERROR_WINDOW_Y + 2,ERROR_WINDOW_X +1); |
| 304 |
addstr("L: ="); |
| 305 |
leftvalue = maxleft / (32768/(ERROR_WINDOW_W-6)); |
| 306 |
for (numwr = 0; numwr < leftvalue; numwr ++) |
| 307 |
addch(numwr >= ERROR_WINDOW_W-8 ? '#' : '='); |
| 308 |
for (; numwr < ERROR_WINDOW_W-6; numwr ++) |
| 309 |
addch(' '); |
| 310 |
|
| 311 |
move(ERROR_WINDOW_Y + 3,ERROR_WINDOW_X +1); |
| 312 |
addstr("R: ="); |
| 313 |
rightvalue = maxright / (32768/(ERROR_WINDOW_W-6)); |
| 314 |
for (numwr = 0; numwr < rightvalue; numwr ++) |
| 315 |
addch(numwr >= ERROR_WINDOW_W-8 ? '#' : '='); |
| 316 |
for (; numwr < ERROR_WINDOW_W-6; numwr ++) |
| 317 |
addch(' '); |
| 318 |
|
| 319 |
move(0,79); |
| 320 |
refresh(); |
| 321 |
#endif |
| 322 |
|
| 323 |
/* Mark the buffer as clean */ |
| 324 |
up(sndsemid, cbuff); |
| 325 |
|
| 326 |
/* If the block was marked as the last one, stop */ |
| 327 |
if (buffinf[cbuff].last) |
| 328 |
break; |
| 329 |
/* Advance the pointer */ |
| 330 |
cbuff++; |
| 331 |
cbuff%=numbuffs; |
| 332 |
} |
| 333 |
/* Tidy up and exit, we are being waited for */ |
| 334 |
close(outfd); |
| 335 |
|
| 336 |
#ifdef VUMETER |
| 337 |
/* Display some informative data. This is really weird: we |
| 338 |
display it here (in the child), then exit() and wait for a key |
| 339 |
in the _parent_ process. But it's the only simple way to get it |
| 340 |
working */ |
| 341 |
clearscreen(RECLP_HEADERTEXT); |
| 342 |
|
| 343 |
printw("\n\n"); |
| 344 |
printw(" Recording information:\n\n\n"); |
| 345 |
fsec2hmsf ( (double) totalrd / (4 * 44100) , timestring); |
| 346 |
printw(" Recorded time : %s\n", timestring); |
| 347 |
printw(" Recorded samples : %11ld\n", totalrd / 4); |
| 348 |
printw(" Recorded bytes : %11ld (excl. header)\n", totalrd); |
| 349 |
printw("\n"); |
| 350 |
printw(" Samples above 50%% of max. volume : %9ld (%5.1f%%)\n", |
| 351 |
samplesabove50pct, samplesabove50pct * 100. / (totalrd/4)); |
| 352 |
printw(" Samples above 90%% of max. volume : %9ld (%5.1f%%)\n", |
| 353 |
samplesabove90pct, samplesabove90pct * 100. / (totalrd/4)); |
| 354 |
printw(" Samples above 99%% of max. volume : %9ld (%5.1f%%)\n", |
| 355 |
samplesabove99pct, samplesabove99pct * 100. / (totalrd/4)); |
| 356 |
printw(" Really too loud (clipped) samples : %9ld (%5.1f%%)\n", |
| 357 |
samplestooloud, samplestooloud * 100. / (totalrd/4)); |
| 358 |
|
| 359 |
#if 0 |
| 360 |
/* The computation of the avg volume is not simple. One approach |
| 361 |
is totalvolume+=abs(sampleleft)+abs(sampleright) for each |
| 362 |
sample, but if totalvolume gets too big, nothing is added any |
| 363 |
more (lack of precision). If anyone has a better (working) |
| 364 |
idea, please tell me! */ |
| 365 |
printw("\n"); |
| 366 |
printw(" Average volume : %7.1f (%5.1f%% of max.)\n", |
| 367 |
totalvolume / (totalrd/2), |
| 368 |
(totalvolume / (totalrd/2) * 100) / 32768); |
| 369 |
/* (totalrd/2)=((totalrd/4)*2) */ |
| 370 |
#endif /* 0 */ |
| 371 |
|
| 372 |
ok_button.text = " OK "; |
| 373 |
ok_button.y = 20; |
| 374 |
ok_button.x = 71; |
| 375 |
ok_button.selected = TRUE; |
| 376 |
|
| 377 |
button_display (&ok_button); |
| 378 |
mybox (ok_button.y - 1, ok_button.x - 1, |
| 379 |
3, strlen (ok_button.text) + 2); |
| 380 |
move (0, 79); |
| 381 |
refresh(); |
| 382 |
#endif |
| 383 |
|
| 384 |
exit(0); |
| 385 |
} |
| 386 |
else |
| 387 |
{ |
| 388 |
/* Parent reads audio */ |
| 389 |
int cbuff = 0; |
| 390 |
long totalrd = 0; |
| 391 |
|
| 392 |
int stoprecording=0; |
| 393 |
int in_ch; |
| 394 |
|
| 395 |
while (totalrd < totalcount && !stoprecording) |
| 396 |
{ |
| 397 |
long trgt, count, numrd; |
| 398 |
char *tmpptr; |
| 399 |
trgt = totalcount - totalrd; |
| 400 |
if (trgt > abuf_size) |
| 401 |
trgt = abuf_size; |
| 402 |
/* Get the buffer. Blocks until OK to do so */ |
| 403 |
down(sndsemid, cbuff); |
| 404 |
/* Read a block of data */ |
| 405 |
numrd = 0; |
| 406 |
tmpptr = buffarr[cbuff]; |
| 407 |
while( (numrd < trgt) && |
| 408 |
((count = read(audio, tmpptr, trgt - numrd)) > 0) ) |
| 409 |
{ |
| 410 |
numrd += count; |
| 411 |
tmpptr += count; |
| 412 |
} |
| 413 |
/* Update the count for this block */ |
| 414 |
buffinf[cbuff].count = numrd; |
| 415 |
/* Mark the buffer dirty */ |
| 416 |
up(disksemid, cbuff); |
| 417 |
/* Update the amount done */ |
| 418 |
totalrd += numrd; |
| 419 |
/* Tell the reader to stop if needed */ |
| 420 |
|
| 421 |
in_ch=getch(); |
| 422 |
#ifdef DEBUG |
| 423 |
printw(" %d",cbuff); |
| 424 |
#endif |
| 425 |
if (in_ch==KEY_ENTER || in_ch==13 || in_ch==27) |
| 426 |
stoprecording=1; |
| 427 |
if ( ((totalrd >= totalcount) && terminate) || stoprecording) |
| 428 |
buffinf[cbuff].last = 1; |
| 429 |
/* Update the counter */ |
| 430 |
cbuff++; |
| 431 |
cbuff%=numbuffs; |
| 432 |
} |
| 433 |
/* Tidy up and wait for the child */ |
| 434 |
close(audio); |
| 435 |
|
| 436 |
/* XXX fix the occasional deadlock in the following wait() -nox */ |
| 437 |
for (i = 0; i < numbuffs; i++) |
| 438 |
up(disksemid, i); |
| 439 |
|
| 440 |
wait(NULL); |
| 441 |
|
| 442 |
/* Free all the semaphores */ |
| 443 |
cleanupsems(); |
| 444 |
|
| 445 |
#ifdef VUMETER |
| 446 |
nodelay(stdscr, TRUE); |
| 447 |
|
| 448 |
/* child has displayed informative data */ |
| 449 |
do |
| 450 |
i = getch (); |
| 451 |
while (i != 13 && i != KEY_ENTER && i != 27); |
| 452 |
#endif |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
void diskread(int infd, long totalplay, long skipped, char hd_buf[20], |
| 457 |
int terminate, int speed, int bits, int stereo) |
| 458 |
{ |
| 459 |
|
| 460 |
int count, i, limited = 0; |
| 461 |
char *tmppt; |
| 462 |
long numread, totalread = 0; |
| 463 |
int first = 1; |
| 464 |
|
| 465 |
static int triggered = 0; /* Have we let the writer go? */ |
| 466 |
static int cbuff = 0; /* Which buffer */ |
| 467 |
|
| 468 |
char tempstring[50]; |
| 469 |
int in_ch; |
| 470 |
|
| 471 |
if (totalplay) limited = 1; |
| 472 |
if (totalplay == -1) |
| 473 |
{ |
| 474 |
totalplay = 0; |
| 475 |
limited = 1; |
| 476 |
} |
| 477 |
|
| 478 |
clearscreen(PLAYWAV_HEADERTEXT); |
| 479 |
error_window_display("Playing...", " Stop "); |
| 480 |
nodelay(stdscr, TRUE); |
| 481 |
refresh(); |
| 482 |
|
| 483 |
while (1) |
| 484 |
{ |
| 485 |
int trgt; |
| 486 |
|
| 487 |
/* Wait for a clean buffer */ |
| 488 |
down(disksemid, cbuff); |
| 489 |
/* Read from the input */ |
| 490 |
numread = 0; |
| 491 |
trgt = abuf_size; |
| 492 |
if (limited && (totalread + trgt > totalplay)) |
| 493 |
trgt = totalplay - totalread; |
| 494 |
tmppt = buffarr[cbuff]; |
| 495 |
if(first && trgt) |
| 496 |
{ |
| 497 |
buffinf[cbuff].setit = 1; |
| 498 |
buffinf[cbuff].speed = speed; |
| 499 |
buffinf[cbuff].bits = bits; |
| 500 |
buffinf[cbuff].stereo = stereo; |
| 501 |
if(hd_buf) |
| 502 |
{ |
| 503 |
memcpy(tmppt, hd_buf, 20); |
| 504 |
tmppt += 20; numread = 20; |
| 505 |
} |
| 506 |
first = 0; |
| 507 |
} |
| 508 |
while ( (numread < trgt) && |
| 509 |
((count = read(infd, tmppt, trgt - numread)) != 0) ) |
| 510 |
{ |
| 511 |
tmppt += count; numread += count; |
| 512 |
} |
| 513 |
#ifdef DEBUG |
| 514 |
fprintf(stderr, "in:%d, %d\n", cbuff, numread); |
| 515 |
#endif |
| 516 |
/* Update the count for this block */ |
| 517 |
buffinf[cbuff].count = numread; |
| 518 |
totalread += numread; |
| 519 |
|
| 520 |
in_ch = getch(); |
| 521 |
/* Was it our last block? */ |
| 522 |
if (numread < abuf_size ) |
| 523 |
{ |
| 524 |
break; |
| 525 |
} |
| 526 |
if ( in_ch == 27 || in_ch == KEY_ENTER || in_ch == 13 ) |
| 527 |
{ |
| 528 |
mvprintw(ERROR_WINDOW_Y+2, ERROR_WINDOW_X+1, |
| 529 |
"Time: "); |
| 530 |
move(0,79); |
| 531 |
refresh(); |
| 532 |
break; |
| 533 |
} |
| 534 |
|
| 535 |
if(triggered) |
| 536 |
{ |
| 537 |
up(sndsemid, cbuff); |
| 538 |
|
| 539 |
fsec2hmsf( (skipped + totalread - BIGBUFFSIZE - 65536.) / |
| 540 |
(speed * (bits/8) * (stereo+1)), tempstring); |
| 541 |
|
| 542 |
mvprintw(ERROR_WINDOW_Y+2, ERROR_WINDOW_X+1, |
| 543 |
"Time: %s", tempstring); |
| 544 |
move(0,79); |
| 545 |
refresh(); |
| 546 |
|
| 547 |
/* fprintf(stderr,"\nbyte(1): %ld", skipped + totalread - BIGBUFFSIZE - 65536); |
| 548 |
*/ } |
| 549 |
else |
| 550 |
if(cbuff == numbuffs-1) |
| 551 |
{ |
| 552 |
#ifdef DEBUG |
| 553 |
fprintf(stderr, "Triggering (in loop)\n"); |
| 554 |
#endif |
| 555 |
for(i = 0; i < numbuffs; i++) |
| 556 |
up(sndsemid,i); |
| 557 |
triggered = 1; |
| 558 |
} |
| 559 |
/* Update counter */ |
| 560 |
cbuff++; |
| 561 |
cbuff %= numbuffs; |
| 562 |
} |
| 563 |
/* Finish off this set of buffers */ |
| 564 |
if(terminate) |
| 565 |
{ |
| 566 |
buffinf[cbuff].last = 1; |
| 567 |
if(!triggered) |
| 568 |
#ifdef DEBUG |
| 569 |
fprintf(stderr, "Triggering (after loop, partial)\n"); |
| 570 |
#endif |
| 571 |
/* If it wasn't triggered, we haven't filled past cbuff */ |
| 572 |
for(i = 0; i < cbuff; i++) |
| 573 |
up(sndsemid, i); |
| 574 |
up(sndsemid, cbuff); |
| 575 |
} |
| 576 |
else if((!triggered) && (cbuff == numbuffs-1)) |
| 577 |
{ |
| 578 |
#ifdef DEBUG |
| 579 |
fprintf(stderr, "Triggering (after loop, full)\n"); |
| 580 |
#endif |
| 581 |
for(i = 0; i < numbuffs; i++) |
| 582 |
up(sndsemid,i); |
| 583 |
triggered = 1; |
| 584 |
} |
| 585 |
else if(triggered) |
| 586 |
up(sndsemid,cbuff); |
| 587 |
cbuff++; |
| 588 |
cbuff %= numbuffs; |
| 589 |
} |
| 590 |
|
| 591 |
volatile void audiowrite(void) |
| 592 |
{ |
| 593 |
int cbuff = 0, count, numwr, trgt; |
| 594 |
char *tmpptr; |
| 595 |
|
| 596 |
/* Uncatch the signals, so we don't clean up twice */ |
| 597 |
signal(SIGINT, SIG_DFL); |
| 598 |
|
| 599 |
/* Child process writes the audio */ |
| 600 |
while(1) |
| 601 |
{ |
| 602 |
/* Wait for dirty buffer */ |
| 603 |
down(sndsemid, cbuff); |
| 604 |
/* Spit to the audio device */ |
| 605 |
if(buffinf[cbuff].setit) |
| 606 |
{ |
| 607 |
snd_parm(buffinf[cbuff].speed, buffinf[cbuff].bits, |
| 608 |
buffinf[cbuff].stereo); |
| 609 |
buffinf[cbuff].setit = 0; |
| 610 |
} |
| 611 |
trgt = buffinf[cbuff].count; |
| 612 |
numwr = 0; |
| 613 |
tmpptr = buffarr[cbuff]; |
| 614 |
while ( (numwr < trgt) && |
| 615 |
((count = write(audio, tmpptr, trgt - numwr)) > 0) ) |
| 616 |
{ |
| 617 |
if (count == -1) |
| 618 |
ErrDie("write"); |
| 619 |
numwr += count; |
| 620 |
tmpptr += count; |
| 621 |
} |
| 622 |
#ifdef DEBUG |
| 623 |
fprintf(stderr, "out:%d, %d\n", cbuff, numwr); |
| 624 |
#endif |
| 625 |
/* Was it the last buffer? */ |
| 626 |
if (buffinf[cbuff].last) |
| 627 |
{ |
| 628 |
up(disksemid, cbuff); /* Not really needed */ |
| 629 |
break; |
| 630 |
} |
| 631 |
/* Mark as clean */ |
| 632 |
up(disksemid, cbuff); |
| 633 |
/* Update counter */ |
| 634 |
cbuff++; |
| 635 |
cbuff %= numbuffs; |
| 636 |
} |
| 637 |
/* Tidy up and be reaped */ |
| 638 |
sync_audio(); |
| 639 |
close(audio); |
| 640 |
exit(0); |
| 641 |
} |
| 642 |
|
| 643 |
void initsems(int disks, int snds) |
| 644 |
{ |
| 645 |
int i,j; |
| 646 |
|
| 647 |
for (i=0;i<numsemblks;i++) |
| 648 |
for (j=0; j<SEMMSL;j++) |
| 649 |
{ |
| 650 |
if(semctl(disksemid[i], j, SETVAL, (union semun) disks) == -1) |
| 651 |
ErrDie("semctl"); |
| 652 |
if(semctl(sndsemid[i], j, SETVAL, (union semun) snds) == -1) |
| 653 |
ErrDie("semctl"); |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
void cleanupsems(void) |
| 658 |
{ |
| 659 |
int i; |
| 660 |
|
| 661 |
for (i = 0; i < numsemblks; i++) |
| 662 |
{ |
| 663 |
semctl(disksemid[i], 0, IPC_RMID, (union semun) 0); |
| 664 |
semctl(sndsemid[i], 0, IPC_RMID, (union semun) 0); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
static void sighandler(int i) |
| 669 |
{ |
| 670 |
#ifndef LP2CD |
| 671 |
fprintf(stderr, "signal %d received, cleaning up.\n", i); |
| 672 |
#endif |
| 673 |
cleanupsems(); |
| 674 |
finish_curses(1); |
| 675 |
exit(1); |
| 676 |
} |