Codebase list rplay / 4f3f5aa6-6e93-4bb8-b89a-5d8b0223cc8f/main rplayd / misc.c
4f3f5aa6-6e93-4bb8-b89a-5d8b0223cc8f/main

Tree @4f3f5aa6-6e93-4bb8-b89a-5d8b0223cc8f/main (Download .tar.gz)

misc.c @4f3f5aa6-6e93-4bb8-b89a-5d8b0223cc8f/mainraw · history · blame

  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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/* $Id: misc.c,v 1.5 1999/03/10 07:58:03 boyns Exp $ */

/*
 * Copyright (C) 1993-99 Mark R. Boyns <boyns@doit.org>
 *
 * This file is part of rplay.
 *
 * rplay is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * rplay is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with rplay; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */



#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "rplayd.h"
#include <string.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef ultrix
#include <fcntl.h>
#else
#include <sys/fcntl.h>
#endif
#include <sys/param.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <math.h>
#include "sound.h"
#include "timer.h"

#ifdef __STDC__
char *
sys_err_str(int error)
#else
char *
sys_err_str(error)
    int error;
#endif
{
#ifdef HAVE_STRERROR
    return (char *) strerror(error);
#else
    extern char *sys_errlist[];
    return sys_errlist[error];
#endif
}

#ifdef __STDC__
int
udp_socket(int port)
#else
int
udp_socket(port)
    int port;
#endif
{
    int fd;
    struct sockaddr_in s;

    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd < 0)
    {
	report(REPORT_ERROR, "socket: %s\n", sys_err_str(errno));
	done(1);
    }

    s.sin_family = AF_INET;
    s.sin_port = htons(port);
    s.sin_addr.s_addr = INADDR_ANY;

    if (bind(fd, (struct sockaddr *) &s, sizeof(s)) < 0)
    {
	report(REPORT_ERROR, "bind: %s\n", sys_err_str(errno));
	done(1);
    }

    return fd;
}

#ifdef __STDC__
int
tcp_socket(int port)
#else
int
tcp_socket(port)
    int port;
#endif
{
    int fd;
    struct sockaddr_in s;
    int on = 1;

    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd < 0)
    {
	report(REPORT_ERROR, "socket: %s\n", sys_err_str(errno));
	done(1);
    }

    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0)
    {
	report(REPORT_ERROR, "setsockopt: SO_REUSEADDR: %s\n", sys_err_str(errno));
	done(1);
    }

    s.sin_family = AF_INET;
    s.sin_port = htons(port);
    s.sin_addr.s_addr = INADDR_ANY;

    if (bind(fd, (struct sockaddr *) &s, sizeof(s)) < 0)
    {
	report(REPORT_ERROR, "bind: %s\n", sys_err_str(errno));
	done(1);
    }

    if (listen(fd, 5) < 0)
    {
	report(REPORT_ERROR, "listen: %s\n", sys_err_str(errno));
	done(1);
    }

    return fd;
}

/*
 * make a file descriptor non-blocking
 */
#ifdef __STDC__
void
fd_nonblock(int fd)
#else
void
fd_nonblock(fd)
    int fd;
#endif
{
    int flags;

    flags = fcntl(fd, F_GETFL, 0);
    if (flags < 0)
    {
	report(REPORT_ERROR, "fd_nonblock: F_GETFL fcntl: %s\n", sys_err_str(errno));
	done(1);
    }
#ifdef linux
    flags |= O_NONBLOCK;
#else
    flags |= FNDELAY;
#endif
    if (fcntl(fd, F_SETFL, flags) < 0)
    {
	report(REPORT_ERROR, "fd_nonblock: F_SETFL fcntl: %s\n", sys_err_str(errno));
	done(1);
    }
}

/*
 * make a file descriptor blocking
 */
#ifdef __STDC__
void
fd_block(int fd)
#else
void
fd_block(fd)
    int fd;
#endif
{
    int flags;

    flags = fcntl(fd, F_GETFL, 0);
    if (flags < 0)
    {
	report(REPORT_ERROR, "fd_block: F_GETFL fcntl: %s\n", sys_err_str(errno));
	done(1);
    }
#ifdef linux
    flags &= ~O_NONBLOCK;
#else
    flags &= ~FNDELAY;
#endif
    if (fcntl(fd, F_SETFL, flags) < 0)
    {
	report(REPORT_ERROR, "fd_block: F_SETFL fcntl: %s\n", sys_err_str(errno));
	done(1);
    }
}

#ifdef __STDC__
int
modified(char *filename, time_t since)
#else
int
modified(filename, since)
    char *filename;
    time_t since;
#endif
{
    struct stat st;

    if (stat(filename, &st) < 0)
    {
	report(REPORT_ERROR, "%s: %s\n", filename, sys_err_str(errno));
	return 0;
    }

    return ((time_t) st.st_mtime > since) ? 1 : 0;
}

#ifdef __STDC__
char *
time2string(time_t t)
#else
char *
time2string(t)
    time_t t;
#endif
{
    static char buf[64];
    int days, hours, mins, secs;

    days = t / (60 * 60 * 24);
    t %= (60 * 60 * 24);
    hours = t / (60 * 60);
    t %= (60 * 60);
    mins = t / 60;
    t %= 60;
    secs = t;

    buf[0] = '\0';

    if (days > 0)
    {
	SNPRINTF(SIZE(buf, sizeof(buf)), "%d+", days);
    }

    SNPRINTF(SIZE(buf + strlen(buf), sizeof(buf) - strlen(buf)), "%02d:%02d:%02d", hours, mins, secs);

    return buf;
}


#ifdef __STDC__
char *
audio_format_to_string(int format)
#else
char *
audio_format_to_string(format)
    int format;
#endif
{
    switch (format)
    {
    case RPLAY_FORMAT_ULAW:
	return "ulaw";

    case RPLAY_FORMAT_LINEAR_8:
	return "linear-8";

    case RPLAY_FORMAT_ULINEAR_8:
	return "ulinear-8";

    case RPLAY_FORMAT_LINEAR_16:
	return "linear-16";

    case RPLAY_FORMAT_ULINEAR_16:
	return "ulinear-16";

    case RPLAY_FORMAT_G721:
	return "g721";

    case RPLAY_FORMAT_G723_3:
	return "g723-3";

    case RPLAY_FORMAT_G723_5:
	return "g723-5";

    case RPLAY_FORMAT_GSM:
	return "gsm";

    default:
	return "unknown";
    }
}

#ifdef __STDC__
int
string_to_audio_format(char *string)
#else
int
string_to_audio_format(string)
    char *string;
#endif
{
    if (strcmp(string, "ulaw") == 0
	|| strcmp(string, "u_law") == 0
	|| strcmp(string, "u-law") == 0)
    {
	return RPLAY_FORMAT_ULAW;
    }
    else if (strcmp(string, "linear-16") == 0
	     || strcmp(string, "linear_16") == 0
	     || strcmp(string, "linear16") == 0)
    {
	return RPLAY_FORMAT_LINEAR_16;
    }
    else if (strcmp(string, "ulinear-16") == 0
	     || strcmp(string, "ulinear_16") == 0
	     || strcmp(string, "ulinear16") == 0)
    {
	return RPLAY_FORMAT_ULINEAR_16;
    }
    else if (strcmp(string, "linear-8") == 0
	     || strcmp(string, "linear_8") == 0
	     || strcmp(string, "linear8") == 0)
    {
	return RPLAY_FORMAT_LINEAR_8;
    }
    else if (strcmp(string, "ulinear-8") == 0
	     || strcmp(string, "ulinear_8") == 0
	     || strcmp(string, "ulinear8") == 0)
    {
	return RPLAY_FORMAT_ULINEAR_8;
    }
    else if (strcmp(string, "g721") == 0)
    {
	return RPLAY_FORMAT_G721;
    }
    else if (strcmp(string, "g723-3") == 0
	     || strcmp(string, "g723_3") == 0)
    {
	return RPLAY_FORMAT_G723_3;
    }
    else if (strcmp(string, "g723-5") == 0
	     || strcmp(string, "g723_5") == 0)
    {
	return RPLAY_FORMAT_G723_5;
    }
    else if (strcmp(string, "gsm") == 0
	     || strcmp(string, "GSM") == 0)
    {
	return RPLAY_FORMAT_GSM;
    }
    else
    {
	return RPLAY_FORMAT_NONE;
    }
}

#ifdef __STDC__
char *
byte_order_to_string(int byte_order)
#else
char *
byte_order_to_string(byte_order)
    int byte_order;
#endif
{
    switch (byte_order)
    {
    case RPLAY_BIG_ENDIAN:
	return "big-endian";

    case RPLAY_LITTLE_ENDIAN:
	return "little-endian";

    default:
	return "unknown";
    }
}

#ifdef __STDC__
int
string_to_byte_order(char *string)
#else
int
string_to_byte_order(string)
    char *string;
#endif
{
    if (strcmp(string, "big-endian") == 0
	|| strcmp(string, "big") == 0)
    {
	return RPLAY_BIG_ENDIAN;
    }
    else if (strcmp(string, "little-endian") == 0
	     || strcmp(string, "little") == 0)
    {
	return RPLAY_LITTLE_ENDIAN;
    }
    else
    {
	return 0;
    }
}

#ifdef __STDC__
char *
storage_to_string(int storage)
#else
char *
storage_to_string(storage)
    int storage;
#endif
{
    switch (storage)
    {
    case SOUND_STORAGE_NONE:
	return "none";

    case SOUND_STORAGE_DISK:
	return "disk";

    case SOUND_STORAGE_MEMORY:
	return "memory";

    default:
	return "unknown";
    }
}

#ifdef __STDC__
int
string_to_storage(char *string)
#else
int
string_to_storage(string)
    char *string;
#endif
{
    if (strcmp(string, "none") == 0)
    {
	return SOUND_STORAGE_NONE;
    }
    else if (strcmp(string, "disk") == 0)
    {
	return SOUND_STORAGE_DISK;
    }
    else if (strcmp(string, "memory") == 0)
    {
	return SOUND_STORAGE_MEMORY;
    }
    else
    {
	return SOUND_STORAGE_NULL;
    }
}

#ifdef __STDC__
char *
input_to_string(int input)
#else
char *
input_to_string(input)
    int input;
#endif
{
    switch (input)
    {
    case SOUND_FILE:
	return "file";

    case SOUND_FLOW:
	return "flow";

#ifdef HAVE_CDROM
    case SOUND_CDROM:
	return "cdrom";
#endif /* HAVE_CDROM */

    case SOUND_VIRTUAL:
	return "virtual";

    default:
	return "unknown";
    }
}

#ifdef __STDC__
int
string_to_input(char *string)
#else
int
string_to_input(string)
    char *string;
#endif
{
    if (strcmp(string, "file") == 0)
    {
	return SOUND_FILE;
    }
    else if (strcmp(string, "flow") == 0)
    {
	return SOUND_FLOW;
    }
    else
    {
	return 0;
    }
}

#ifdef __STDC__
char *
audio_port_to_string(int port)
#else
char *
audio_port_to_string(port)
    int port;
#endif
{
    static char string[128];
    int n;

    string[0] = '\0';
    if (BIT(port, RPLAY_AUDIO_PORT_NONE))
    {
	strncat(string, "none,", sizeof(string) - strlen(string));
    }
    if (BIT(port, RPLAY_AUDIO_PORT_SPEAKER))
    {
	strncat(string, "speaker,", sizeof(string) - strlen(string));
    }
    if (BIT(port, RPLAY_AUDIO_PORT_HEADPHONE))
    {
	strncat(string, "headphone,", sizeof(string) - strlen(string));
    }
    if (BIT(port, RPLAY_AUDIO_PORT_LINEOUT))
    {
	strncat(string, "lineout,", sizeof(string) - strlen(string));
    }
    string[strlen(string) - 1] = '\0';

    return string;
}

unsigned short
little_short(p)
    char *p;
{
    return (((unsigned long) (((unsigned char *) p)[1])) << 8) |
	((unsigned long) (((unsigned char *) p)[0]));
}

unsigned short
big_short(p)
    char *p;
{
    return (((unsigned long) (((unsigned char *) p)[0])) << 8) |
	((unsigned long) (((unsigned char *) p)[1]));
}

unsigned long
little_long(p)
    char *p;
{
    return (((unsigned long) (((unsigned char *) p)[3])) << 24) |
	(((unsigned long) (((unsigned char *) p)[2])) << 16) |
	(((unsigned long) (((unsigned char *) p)[1])) << 8) |
	((unsigned long) (((unsigned char *) p)[0]));
}

unsigned long
big_long(p)
    char *p;
{
    return (((unsigned long) (((unsigned char *) p)[0])) << 24) |
	(((unsigned long) (((unsigned char *) p)[1])) << 16) |
	(((unsigned long) (((unsigned char *) p)[2])) << 8) |
	((unsigned long) (((unsigned char *) p)[3]));
}

/*
 * C O N V E R T   F R O M   I E E E   E X T E N D E D  
 */

/* 
 * Copyright (C) 1988-1991 Apple Computer, Inc.
 * All rights reserved.
 *
 * Machine-independent I/O routines for IEEE floating-point numbers.
 *
 * NaN's and infinities are converted to HUGE_VAL or HUGE, which
 * happens to be infinity on IEEE machines.  Unfortunately, it is
 * impossible to preserve NaN's in a machine-independent way.
 * Infinities are, however, preserved on IEEE machines.
 *
 * These routines have been tested on the following machines:
 *    Apple Macintosh, MPW 3.1 C compiler
 *    Apple Macintosh, THINK C compiler
 *    Silicon Graphics IRIS, MIPS compiler
 *    Cray X/MP and Y/MP
 *    Digital Equipment VAX
 *
 *
 * Implemented by Malcolm Slaney and Ken Turkowski.
 *
 * Malcolm Slaney contributions during 1988-1990 include big- and little-
 * endian file I/O, conversion to and from Motorola's extended 80-bit
 * floating-point format, and conversions to and from IEEE single-
 * precision floating-point format.
 *
 * In 1991, Ken Turkowski implemented the conversions to and from
 * IEEE double-precision format, added more precision to the extended
 * conversions, and accommodated conversions involving +/- infinity,
 * NaN's, and denormalized numbers.
 */

#ifndef HUGE_VAL
#define HUGE_VAL HUGE
#endif /*HUGE_VAL */

#define UnsignedToFloat(u)         (((double)((long)(u - 2147483647L - 1))) + 2147483648.0)

/****************************************************************
 * Extended precision IEEE floating-point conversion routine.
 ****************************************************************/

double
ConvertFromIeeeExtended(bytes)
    unsigned char *bytes;	/* LCN */
{
    double f;
    int expon;
    unsigned long hiMant, loMant;

    expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
    hiMant = ((unsigned long) (bytes[2] & 0xFF) << 24)
	| ((unsigned long) (bytes[3] & 0xFF) << 16)
	| ((unsigned long) (bytes[4] & 0xFF) << 8)
	| ((unsigned long) (bytes[5] & 0xFF));
    loMant = ((unsigned long) (bytes[6] & 0xFF) << 24)
	| ((unsigned long) (bytes[7] & 0xFF) << 16)
	| ((unsigned long) (bytes[8] & 0xFF) << 8)
	| ((unsigned long) (bytes[9] & 0xFF));

    if (expon == 0 && hiMant == 0 && loMant == 0)
    {
	f = 0;
    }
    else
    {
	if (expon == 0x7FFF)
	{			/* Infinity or NaN */
	    f = HUGE_VAL;
	}
	else
	{
	    expon -= 16383;
	    f = ldexp(UnsignedToFloat(hiMant), expon -= 31);
	    f += ldexp(UnsignedToFloat(loMant), expon -= 32);
	}
    }

    if (bytes[0] & 0x80)
	return -f;
    else
	return f;
}