home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Sound / SoX / Source / misc.c < prev    next >
C/C++ Source or Header  |  1999-07-18  |  4KB  |  299 lines

  1. /*
  2.  * July 5, 1991
  3.  * Copyright 1991 Lance Norskog And Sundry Contributors
  4.  * This source code is freely redistributable and may be used for
  5.  * any purpose.  This copyright notice must be maintained. 
  6.  * Lance Norskog And Sundry Contributors are not responsible for 
  7.  * the consequences of using this software.
  8.  */
  9.  
  10. /*
  11.  * Sound Tools miscellaneous stuff.
  12.  */
  13.  
  14. #include "st.h"
  15. #include "version.h"
  16. #include "patchlvl.h"
  17. #include <stdio.h>
  18. #include <time.h>
  19.  
  20. EXPORT char *sizes[] = {
  21.     "NONSENSE!",
  22.     "bytes",
  23.     "shorts",
  24.     "NONSENSE",
  25.     "longs",
  26.     "32-bit floats",
  27.     "64-bit floats",
  28.     "IEEE floats"
  29. };
  30.  
  31. EXPORT char *styles[] = {
  32.     "NONSENSE!",
  33.     "unsigned",
  34.     "signed (2's complement)",
  35.     "u-law",
  36.     "a-law",
  37.     "adpcm",
  38.     "gsm"
  39. };
  40.  
  41. char readerr[] = "Premature EOF while reading sample file.";
  42. char writerr[] = "Error writing sample file.  You are probably out of disk space.";
  43.  
  44. /* Utilities */
  45.  
  46. /* Read and write words and longs in "machine format".  Swap if indicated. */
  47.  
  48. /* Read short. */
  49. unsigned short
  50. rshort(ft)
  51. ft_t ft;
  52. {
  53.     unsigned short us;
  54.  
  55.     fread(&us, 2, 1, ft->fp);
  56.     if (ft->swap)
  57.         us = swapw(us);
  58.     return us;
  59. }
  60.  
  61. /* Write short. */
  62. unsigned short
  63. #if    defined(__STDC__)
  64. wshort(ft_t ft, unsigned short us)
  65. #else
  66. wshort(ft, us)
  67. ft_t ft;
  68. unsigned short us;
  69. #endif
  70. {
  71.     if (ft->swap)
  72.         us = swapw(us);
  73.     if (fwrite(&us, 2, 1, ft->fp) != 1)
  74.         fail(writerr);
  75.     return(0);
  76. }
  77.  
  78. /* Read long. */
  79. ULONG
  80. rlong(ft)
  81. ft_t ft;
  82. {
  83.     ULONG ul;
  84.  
  85.     fread(&ul, 4, 1, ft->fp);
  86.     if (ft->swap)
  87.         ul = swapl(ul);
  88.     return ul;
  89. }
  90.  
  91. /* Write long. */
  92. ULONG
  93. wlong(ft, ul)
  94. ft_t ft;
  95. ULONG ul;
  96. {
  97.     if (ft->swap)
  98.         ul = swapl(ul);
  99.     if (fwrite(&ul, 4, 1, ft->fp) != 1)
  100.         fail(writerr);
  101.     return(0);
  102. }
  103.  
  104. /* Read float. */
  105. float
  106. rfloat(ft)
  107. ft_t ft;
  108. {
  109.     float f;
  110.  
  111.     fread(&f, sizeof(float), 1, ft->fp);
  112.     if (ft->swap)
  113.         f = swapf(f);
  114.     return f;
  115. }
  116.  
  117. void
  118. wfloat(ft, f)
  119. ft_t ft;
  120. float f;
  121. {
  122.     float t = f;
  123.  
  124.     if (ft->swap)
  125.         t = swapf(t);
  126.     if (fwrite(&t, sizeof(float), 1, ft->fp) != 1)
  127.         fail(writerr);
  128. }
  129.  
  130. /* Read double. */
  131. double
  132. rdouble(ft)
  133. ft_t ft;
  134. {
  135.     double d;
  136.  
  137.     fread(&d, sizeof(double), 1, ft->fp);
  138.     if (ft->swap)
  139.         d = swapd(d);
  140.     return d;
  141. }
  142.  
  143. /* Write double. */
  144. void
  145. wdouble(ft, d)
  146. ft_t ft;
  147. double d;
  148. {
  149.     if (ft->swap)
  150.         d = swapd(d);
  151.     if (fwrite(&d, sizeof(double), 1, ft->fp) != 1)
  152.         fail(writerr);
  153. }
  154.  
  155. /* generic swap routine */
  156. static void
  157. swapb(l, f, n)
  158. char *l, *f;
  159. int n;
  160. {    register int i;
  161.  
  162.      for (i= 0; i< n; i++)
  163.     f[i]= l[n-i-1];
  164. }
  165.  
  166.  
  167. /* Byte swappers */
  168.  
  169. unsigned short
  170. #if    defined(__STDC__)
  171. swapw(unsigned short us)
  172. #else
  173. swapw(us)
  174. unsigned short us;
  175. #endif
  176. {
  177.     return ((us >> 8) | (us << 8)) & 0xffff;
  178. }
  179.  
  180. ULONG
  181. swapl(ul)
  182. ULONG ul;
  183. {
  184.     return (ul >> 24) | ((ul >> 8) & 0xff00) | ((ul << 8) & 0xff0000L) | (ul << 24);
  185. }
  186.  
  187. /* return swapped 32-bit float */
  188. float
  189. #if    defined(__STDC__)
  190. swapf(float uf)
  191. #else
  192. swapf(uf)
  193. float uf;
  194. #endif
  195. {
  196.     union {
  197.         ULONG l;
  198.         float f;
  199.     } u;
  200.  
  201.     u.f= uf;
  202.     u.l= (u.l>>24) | ((u.l>>8)&0xff00) | ((u.l<<8)&0xff0000L) | (u.l<<24);
  203.     return u.f;
  204. }
  205.  
  206. double
  207. swapd(df)
  208. double df;
  209. {
  210.     double sdf;
  211.     swapb(&df, &sdf, sizeof(double));
  212.     return (sdf);
  213. }
  214.  
  215.  
  216. /* dummy routines for do-nothing functions */
  217. void nothing() {}
  218. LONG nothing_success() {return(0);}
  219.  
  220. /* dummy drain routine for effects */
  221. void null_drain(effp, obuf, osamp)
  222. eff_t effp;
  223. LONG *obuf;
  224. LONG *osamp;
  225. {
  226.     *osamp = 0;
  227. }
  228.  
  229. /* here for linear interp.  might be useful for other things */
  230. LONG gcd(a, b) 
  231. LONG a, b;
  232. {
  233.     if (b == 0)
  234.         return a;
  235.     else
  236.         return gcd(b, a % b);
  237. }
  238.  
  239. LONG lcm(a, b)
  240. LONG a, b;
  241. {
  242.     return (a * b) / gcd(a, b);
  243. }
  244.  
  245. /* 
  246.  * Cribbed from Unix SVR3 programmer's manual 
  247.  */
  248.  
  249. static ULONG rand15_seed;
  250.  
  251. ULONG rand15() {
  252.     rand15_seed = (rand15_seed * 1103515245L) + 12345L;
  253.     return (ULONG) ((rand15_seed/65536L) % 32768L);
  254. }
  255.  
  256. void srand15(seed) 
  257. ULONG seed;
  258. {
  259.     rand15_seed = seed;
  260. }
  261.  
  262. void newrand15() {
  263.     time_t t;
  264.  
  265.     time(&t);
  266.     srand15(t);
  267. }
  268.  
  269. /* sine wave gen should be here, also */
  270.  
  271. char *
  272. version()
  273. {
  274.     static char versionstr[20];
  275.     
  276.     sprintf(versionstr, "Version %d.%d", VERSION, PATCHLEVEL);
  277.     return(versionstr);
  278. }
  279.  
  280.  
  281. #ifndef    HAVE_STRERROR
  282. /* strerror function */
  283. char *strerror(errcode)
  284. int errcode;
  285. {
  286.     static char  nomesg[30];
  287.     extern int sys_nerr;
  288.     extern char *sys_errlist[];
  289.  
  290.     if (errcode < sys_nerr)
  291.         return (sys_errlist[errcode]);
  292.     else
  293.     {
  294.         sprintf (nomesg, "Undocumented error %d", errcode);
  295.         return (nomesg);
  296.     }
  297. }
  298. #endif
  299.