File: misc.c

package info (click to toggle)
siggen 2.3.10-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,032 kB
  • sloc: ansic: 5,545; makefile: 188; sh: 79
file content (149 lines) | stat: -rw-r--r-- 3,347 bytes parent folder | download | duplicates (4)
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
/* misc.c
 * Miscellaneous Functions 
 * Jim Jackson     Dec 96
 */

/*
 * Copyright (C) 1997-2008 Jim Jackson                    jj@franjam.org.uk
 * 
 *  This program 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.
 * 
 *  This program 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 this program - see the file COPYING; if not, write to 
 *  the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 *  MA 02139, USA.
 */

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include "config.h"
/*
 * delay(us)  wait us microsecs using select. Effectively
 *            causes current process to stop and a reschedule take place
 *            as well as doing the delay.
 */

delay(us)
int us;
{
        struct timeval tv;

        tv.tv_sec = 0;
        tv.tv_usec = us;
        (void)select( 1, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv );
}

/***ERR_RPT***/
/* err_rpt(err,msg)
 */

err_rpt(err,msg)
short int err;
char *msg;

 { 
   extern char *sys;

   if (err) fprintf(stderr,"[%s] %s : %s\n",sys,strerror(err),msg);
   return(err);
 }

/***HCF***/
/* hcf(x,y)   find highest common factor of x and y
 */

hcf(x,y)
unsigned x,y;

{
   register unsigned a,b,r;
   
   if (x>y) {
      a=x; b=y; 
   } else {
      a=y; b=x; 
   }
   for ( ; r=a%b ; a=b, b=r) { }
   return(b);
}

/***PARSE***/
/* parse(s,aa,sep) splits s up into parameters and stores ptrs
 *                   to each prm in ptr array aa (upto MAX_ARGS)
 *                   params are space or tab or 'sep' seperated, and may be
 *                   enclosed in single or double quotes e.g. 'this is 1 prm'
 *
 *   returns number of parameters parsed
 */

parse(s,aa,sep)
char *s,**aa,sep;

{ 
  char *p,*q,c;
  int i;

#define EOL ((*s=='\0') || (*s=='\n'))
#define EOP ((*s==' ') || (*s=='\t') || (*s==sep))
#define QUOTE ((*s=='\'') || (*s=='"'))
#define MAX_ARGS 50

   for ( i=1; ; i++) { 
      for ( ; EOP; s++) {}    /* skip leading  separators */
      *aa++=p=s;
      if (EOL) return((--i)?i:1);
      if (i==MAX_ARGS) {
	 return(--i);
      }
      while (!(EOL || EOP)) {
	 if (QUOTE) {
            for (s++; !(EOL || QUOTE); ) *p++=*s++;
            if (QUOTE) s++;
	 } else *p++=*s++;
      }
      if (EOL) {*p='\0'; return(i);}
      *p='\0';
      ++s;
   }
}

#undef EOL
#undef EOP

/***mstosmaples***/
/*
 * mstosamples(ms,sr)  convert ms millisecs into number of samples
 *                     given that the sample rate is sr samples/sec
 */

mstosamples(ms,sr)
int ms;
int sr;

{
  int d;
  int m;

  m=INT_MAX/(4*sr);  /* we need to make sure we don't overflow INT size
			including when no. of samples may get upped for stereo
			16 bit into a byte count for getting buffers etc
 		     */
  for ( d=1000; d>0 && ms>m; d/=10 ) { ms/=10; }
  if (d)
    return((sr*ms+d/2)/d);
  else 
    return(0);
}