No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

3364 líneas
99KB

  1. #include "mus-config.h"
  2. #if (defined(__GNUC__)) && (!(defined(__cplusplus)))
  3. #define _GNU_SOURCE
  4. /* this is needed to get the vasprintf declaration */
  5. #endif
  6. #if USE_SND
  7. #include "snd.h"
  8. #endif
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <fcntl.h>
  12. #include <limits.h>
  13. #include <errno.h>
  14. #include <stdlib.h>
  15. #ifndef _MSC_VER
  16. #include <unistd.h>
  17. #else
  18. #include <io.h>
  19. #pragma warning(disable: 4244)
  20. #endif
  21. #include <string.h>
  22. #include <stdarg.h>
  23. #ifdef _MSC_VER
  24. #include <direct.h>
  25. #endif
  26. #include <sys/stat.h>
  27. #include "_sndlib.h"
  28. #define HAVE_BYTESWAP_H __linux__
  29. #define mus_byte_to_sample(n) (((mus_float_t)(n) / (mus_float_t)(1 << 7)))
  30. #define mus_short_to_sample(n) (((mus_float_t)(n) / (mus_float_t)(1 << 15)))
  31. #define mus_int_to_sample(n) (((mus_float_t)(n) / (mus_float_t)(1 << 23)))
  32. #define mus_int24_to_sample(n) (((mus_float_t)(n) / (mus_float_t)(1 << 23)))
  33. #define mus_sample_to_int(n) ((int)((n) * (1 << 23)))
  34. #define mus_sample_to_int24(n) ((int)((n) * (1 << 23)))
  35. #define mus_sample_to_short(n) ((short)((n) * (1 << 15)))
  36. #define mus_sample_to_byte(n) ((char)((n) * (1 << 7)))
  37. #if defined(__x86_64__) || defined(__i386__)
  38. #define bint24_to_sample(n) ((mus_float_t)(big_endian_int(jchar) >> 8) / (mus_float_t)(1 << 23))
  39. #define int24_to_sample(n) ((mus_float_t)(little_endian_int(jchar) >> 8) / (mus_float_t)(1 << 23))
  40. #else
  41. #define bint24_to_sample(n) ((mus_float_t)(mus_char_to_bint(jchar) >> 8) / (mus_float_t)(1 << 23))
  42. #define int24_to_sample(n) ((mus_float_t)(mus_char_to_lint(jchar) >> 8) / (mus_float_t)(1 << 23))
  43. #endif
  44. static mus_long_t mus_maximum_malloc = MUS_MAX_MALLOC_DEFAULT;
  45. mus_long_t mus_max_malloc(void)
  46. {
  47. return(mus_maximum_malloc);
  48. }
  49. mus_long_t mus_set_max_malloc(mus_long_t new_max)
  50. {
  51. mus_maximum_malloc = new_max;
  52. return(new_max);
  53. }
  54. static mus_long_t mus_maximum_table_size = MUS_MAX_TABLE_SIZE_DEFAULT;
  55. mus_long_t mus_max_table_size(void)
  56. {
  57. return(mus_maximum_table_size);
  58. }
  59. mus_long_t mus_set_max_table_size(mus_long_t new_max)
  60. {
  61. mus_maximum_table_size = new_max;
  62. return(new_max);
  63. }
  64. void mus_bint_to_char(unsigned char *j, int x)
  65. {
  66. unsigned char *ox = (unsigned char *)&x;
  67. #if MUS_LITTLE_ENDIAN
  68. j[0] = ox[3]; j[1] = ox[2]; j[2] = ox[1]; j[3] = ox[0];
  69. #else
  70. memcpy((void *)j, (void *)ox, 4);
  71. #endif
  72. }
  73. int mus_char_to_bint(const unsigned char *inp)
  74. {
  75. int o;
  76. unsigned char *outp = (unsigned char *)&o;
  77. #if MUS_LITTLE_ENDIAN
  78. outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];
  79. #else
  80. memcpy((void *)outp, (void *)inp, 4);
  81. #endif
  82. return(o);
  83. }
  84. void mus_lint_to_char(unsigned char *j, int x)
  85. {
  86. unsigned char *ox = (unsigned char *)&x;
  87. #if (!MUS_LITTLE_ENDIAN)
  88. j[0] = ox[3]; j[1] = ox[2]; j[2] = ox[1]; j[3] = ox[0];
  89. #else
  90. memcpy((void *)j, (void *)ox, 4);
  91. #endif
  92. }
  93. int mus_char_to_lint(const unsigned char *inp)
  94. {
  95. int o;
  96. unsigned char *outp = (unsigned char *)&o;
  97. #if (!MUS_LITTLE_ENDIAN)
  98. outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];
  99. #else
  100. memcpy((void *)outp, (void *)inp, 4);
  101. #endif
  102. return(o);
  103. }
  104. void mus_blong_to_char(unsigned char *j, mus_long_t x)
  105. {
  106. unsigned char *ox = (unsigned char *)&x;
  107. #if (!MUS_LITTLE_ENDIAN)
  108. memcpy((void *)j, (void *)ox, 8);
  109. #else
  110. j[0] = ox[7]; j[1] = ox[6]; j[2] = ox[5]; j[3] = ox[4]; j[4] = ox[3]; j[5] = ox[2]; j[6] = ox[1]; j[7] = ox[0];
  111. #endif
  112. }
  113. mus_long_t mus_char_to_blong(const unsigned char *inp)
  114. {
  115. mus_long_t o;
  116. unsigned char *outp = (unsigned char *)&o;
  117. #if MUS_LITTLE_ENDIAN
  118. outp[0] = inp[7]; outp[1] = inp[6]; outp[2] = inp[5]; outp[3] = inp[4]; outp[4] = inp[3]; outp[5] = inp[2]; outp[6] = inp[1]; outp[7] = inp[0];
  119. #else
  120. memcpy((void *)outp, (void *)inp, 8);
  121. #endif
  122. return(o);
  123. }
  124. void mus_llong_to_char(unsigned char *j, mus_long_t x)
  125. {
  126. unsigned char *ox = (unsigned char *)&x;
  127. #if (MUS_LITTLE_ENDIAN)
  128. memcpy((void *)j, (void *)ox, 8);
  129. #else
  130. j[0] = ox[7]; j[1] = ox[6]; j[2] = ox[5]; j[3] = ox[4]; j[4] = ox[3]; j[5] = ox[2]; j[6] = ox[1]; j[7] = ox[0];
  131. #endif
  132. }
  133. mus_long_t mus_char_to_llong(const unsigned char *inp)
  134. {
  135. mus_long_t o;
  136. unsigned char *outp = (unsigned char *)&o;
  137. #if (!MUS_LITTLE_ENDIAN)
  138. outp[0] = inp[7]; outp[1] = inp[6]; outp[2] = inp[5]; outp[3] = inp[4]; outp[4] = inp[3]; outp[5] = inp[2]; outp[6] = inp[1]; outp[7] = inp[0];
  139. #else
  140. memcpy((void *)outp, (void *)inp, 8);
  141. #endif
  142. return(o);
  143. }
  144. void mus_bfloat_to_char(unsigned char *j, float x)
  145. {
  146. unsigned char *ox = (unsigned char *)&x;
  147. #if MUS_LITTLE_ENDIAN
  148. j[0] = ox[3]; j[1] = ox[2]; j[2] = ox[1]; j[3] = ox[0];
  149. #else
  150. memcpy((void *)j, (void *)ox, 4);
  151. #endif
  152. }
  153. float mus_char_to_bfloat(const unsigned char *inp)
  154. {
  155. float o;
  156. unsigned char *outp = (unsigned char *)&o;
  157. #if MUS_LITTLE_ENDIAN
  158. outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];
  159. #else
  160. memcpy((void *)outp, (void *)inp, 4);
  161. #endif
  162. return(o);
  163. }
  164. void mus_lfloat_to_char(unsigned char *j, float x)
  165. {
  166. unsigned char *ox = (unsigned char *)&x;
  167. #if (!MUS_LITTLE_ENDIAN)
  168. j[0] = ox[3]; j[1] = ox[2]; j[2] = ox[1]; j[3] = ox[0];
  169. #else
  170. memcpy((void *)j, (void *)ox, 4);
  171. #endif
  172. }
  173. float mus_char_to_lfloat(const unsigned char *inp)
  174. {
  175. float o;
  176. unsigned char *outp = (unsigned char *)&o;
  177. #if (!MUS_LITTLE_ENDIAN)
  178. outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];
  179. #else
  180. memcpy((void *)outp, (void *)inp, 4);
  181. #endif
  182. return(o);
  183. }
  184. void mus_bshort_to_char(unsigned char *j, short x)
  185. {
  186. unsigned char *ox = (unsigned char *)&x;
  187. #if MUS_LITTLE_ENDIAN
  188. j[0] = ox[1]; j[1] = ox[0];
  189. #else
  190. memcpy((void *)j, (void *)ox, 2); /* I wonder if this is faster */
  191. #endif
  192. }
  193. short mus_char_to_bshort(const unsigned char *inp)
  194. {
  195. short o;
  196. unsigned char *outp = (unsigned char *)&o;
  197. #if MUS_LITTLE_ENDIAN
  198. outp[0] = inp[1]; outp[1] = inp[0];
  199. #else
  200. memcpy((void *)outp, (void *)inp, 2);
  201. #endif
  202. return(o);
  203. }
  204. void mus_lshort_to_char(unsigned char *j, short x)
  205. {
  206. unsigned char *ox = (unsigned char *)&x;
  207. #if (!MUS_LITTLE_ENDIAN)
  208. j[0] = ox[1]; j[1] = ox[0];
  209. #else
  210. memcpy((void *)j, (void *)ox, 2);
  211. #endif
  212. }
  213. short mus_char_to_lshort(const unsigned char *inp)
  214. {
  215. short o;
  216. unsigned char *outp = (unsigned char *)&o;
  217. #if (!MUS_LITTLE_ENDIAN)
  218. outp[0] = inp[1]; outp[1] = inp[0];
  219. #else
  220. memcpy((void *)outp, (void *)inp, 2);
  221. #endif
  222. return(o);
  223. }
  224. #if ((MUS_LITTLE_ENDIAN) && (!HAVE_BYTESWAP_H)) || ((!MUS_LITTLE_ENDIAN) && (HAVE_SUN))
  225. static void mus_ubshort_to_char(unsigned char *j, unsigned short x)
  226. {
  227. unsigned char *ox = (unsigned char *)&x;
  228. #if MUS_LITTLE_ENDIAN
  229. j[0] = ox[1]; j[1] = ox[0];
  230. #else
  231. memcpy((void *)j, (void *)ox, 2);
  232. #endif
  233. }
  234. #endif
  235. unsigned short mus_char_to_ubshort(const unsigned char *inp)
  236. {
  237. unsigned short o;
  238. unsigned char *outp = (unsigned char *)&o;
  239. #if MUS_LITTLE_ENDIAN
  240. outp[0] = inp[1]; outp[1] = inp[0];
  241. #else
  242. memcpy((void *)outp, (void *)inp, 2);
  243. #endif
  244. return(o);
  245. }
  246. #if (!MUS_LITTLE_ENDIAN) && (!HAVE_BYTESWAP_H)
  247. static void mus_ulshort_to_char(unsigned char *j, unsigned short x)
  248. {
  249. unsigned char *ox = (unsigned char *)&x;
  250. #if (!MUS_LITTLE_ENDIAN)
  251. j[0] = ox[1]; j[1] = ox[0];
  252. #else
  253. memcpy((void *)j, (void *)ox, 2);
  254. #endif
  255. }
  256. #endif
  257. unsigned short mus_char_to_ulshort(const unsigned char *inp)
  258. {
  259. unsigned short o;
  260. unsigned char *outp = (unsigned char *)&o;
  261. #if (!MUS_LITTLE_ENDIAN)
  262. outp[0] = inp[1]; outp[1] = inp[0];
  263. #else
  264. memcpy((void *)outp, (void *)inp, 2);
  265. #endif
  266. return(o);
  267. }
  268. void mus_bdouble_to_char(unsigned char *j, double x)
  269. {
  270. unsigned char *ox = (unsigned char *)&x;
  271. #if (MUS_LITTLE_ENDIAN)
  272. j[0] = ox[7]; j[1] = ox[6]; j[2] = ox[5]; j[3] = ox[4]; j[4] = ox[3]; j[5] = ox[2]; j[6] = ox[1]; j[7] = ox[0];
  273. #else
  274. memcpy((void *)j, (void *)ox, 8);
  275. #endif
  276. }
  277. double mus_char_to_ldouble(const unsigned char *inp)
  278. {
  279. double o;
  280. unsigned char *outp = (unsigned char *)&o;
  281. #if (MUS_LITTLE_ENDIAN)
  282. memcpy((void *)outp, (void *)inp, 8);
  283. #else
  284. outp[0] = inp[7]; outp[1] = inp[6]; outp[2] = inp[5]; outp[3] = inp[4]; outp[4] = inp[3]; outp[5] = inp[2]; outp[6] = inp[1]; outp[7] = inp[0];
  285. #endif
  286. return(o);
  287. }
  288. #if (!MUS_LITTLE_ENDIAN)
  289. static void mus_ldouble_to_char(unsigned char *j, double x)
  290. {
  291. unsigned char *ox = (unsigned char *)&x;
  292. #if (MUS_LITTLE_ENDIAN)
  293. memcpy((void *)j, (void *)ox, 8);
  294. #else
  295. j[0] = ox[7]; j[1] = ox[6]; j[2] = ox[5]; j[3] = ox[4]; j[4] = ox[3]; j[5] = ox[2]; j[6] = ox[1]; j[7] = ox[0];
  296. #endif
  297. }
  298. #endif
  299. double mus_char_to_bdouble(const unsigned char *inp)
  300. {
  301. double o;
  302. unsigned char *outp = (unsigned char *)&o;
  303. #if (MUS_LITTLE_ENDIAN)
  304. outp[0] = inp[7]; outp[1] = inp[6]; outp[2] = inp[5]; outp[3] = inp[4]; outp[4] = inp[3]; outp[5] = inp[2]; outp[6] = inp[1]; outp[7] = inp[0];
  305. #else
  306. memcpy((void *)outp, (void *)inp, 8);
  307. #endif
  308. return(o);
  309. }
  310. int mus_char_to_uninterpreted_int(const unsigned char *inp)
  311. {
  312. int o;
  313. unsigned char *outp = (unsigned char *)&o;
  314. memcpy((void *)outp, (void *)inp, 4);
  315. return(o);
  316. }
  317. unsigned int mus_char_to_ubint(const unsigned char *inp)
  318. {
  319. unsigned int o;
  320. unsigned char *outp = (unsigned char *)&o;
  321. #if MUS_LITTLE_ENDIAN
  322. outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];
  323. #else
  324. memcpy((void *)outp, (void *)inp, 4);
  325. #endif
  326. return(o);
  327. }
  328. unsigned int mus_char_to_ulint(const unsigned char *inp)
  329. {
  330. unsigned int o;
  331. unsigned char *outp = (unsigned char *)&o;
  332. #if (!MUS_LITTLE_ENDIAN)
  333. outp[0] = inp[3]; outp[1] = inp[2]; outp[2] = inp[1]; outp[3] = inp[0];
  334. #else
  335. memcpy((void *)outp, (void *)inp, 4);
  336. #endif
  337. return(o);
  338. }
  339. #if HAVE_BYTESWAP_H
  340. #include <byteswap.h>
  341. #endif
  342. #if MUS_LITTLE_ENDIAN
  343. #if HAVE_BYTESWAP_H
  344. #define big_endian_short(n) ((short)(bswap_16((*((unsigned short *)n)))))
  345. #define big_endian_int(n) ((int)(bswap_32((*((unsigned int *)n)))))
  346. #define big_endian_unsigned_short(n) ((unsigned short)(bswap_16((*((unsigned short *)n)))))
  347. #else
  348. #define big_endian_short(n) (mus_char_to_bshort(n))
  349. #define big_endian_int(n) (mus_char_to_bint(n))
  350. #define big_endian_unsigned_short(n) (mus_char_to_ubshort(n))
  351. #endif
  352. #if __GNUC__ && HAVE_BYTESWAP_H
  353. /* these work, but newer versions of gcc complain about strict aliasing rules
  354. * #define big_endian_float(n) ({unsigned int x; x = bswap_32((*((unsigned int *)n))); (*((float *)&x));})
  355. * #define big_endian_double(n) ({unsigned long long int x; x = bswap_64((*((unsigned long long int *)n))); (*((double *)&x));})
  356. */
  357. typedef struct {union {unsigned int i; float f;} u;} uiflt;
  358. typedef struct {union {unsigned long long int l; double d;} u;} uidbl;
  359. #define big_endian_float(n) ({uiflt x; x.u.i = bswap_32((*((unsigned int *)n))); x.u.f;})
  360. #define big_endian_double(n) ({uidbl x; x.u.l = bswap_64((*((unsigned long long int *)n))); x.u.d;})
  361. #else
  362. #define big_endian_float(n) (mus_char_to_bfloat(n))
  363. #define big_endian_double(n) (mus_char_to_bdouble(n))
  364. #endif
  365. #define little_endian_short(n) (*((short *)n))
  366. #define little_endian_int(n) (*((int *)n))
  367. #define little_endian_float(n) (*((float *)n))
  368. #define little_endian_double(n) (*((double *)n))
  369. #define little_endian_unsigned_short(n) (*((unsigned short *)n))
  370. #if HAVE_BYTESWAP_H
  371. #define set_big_endian_short(n, x) (*((short *)n)) = ((short)(bswap_16(x)))
  372. #define set_big_endian_int(n, x) (*((int *)n)) = ((int)(bswap_32(x)))
  373. #define set_big_endian_unsigned_short(n, x) (*((unsigned short *)n)) = ((unsigned short)(bswap_16(x)))
  374. #else
  375. #define set_big_endian_short(n, x) mus_bshort_to_char(n, x)
  376. #define set_big_endian_int(n, x) mus_bint_to_char(n, x)
  377. #define set_big_endian_unsigned_short(n, x) mus_ubshort_to_char(n, x)
  378. #endif
  379. #define set_big_endian_float(n, x) mus_bfloat_to_char(n, x)
  380. #define set_big_endian_double(n, x) mus_bdouble_to_char(n, x)
  381. #define set_little_endian_short(n, x) (*((short *)n)) = x
  382. #define set_little_endian_int(n, x) (*((int *)n)) = x
  383. #define set_little_endian_float(n, x) (*((float *)n)) = x
  384. #define set_little_endian_double(n, x) (*((double *)n)) = x
  385. #define set_little_endian_unsigned_short(n, x) (*((unsigned short *)n)) = x
  386. #else
  387. #if (!HAVE_SUN)
  388. #define big_endian_short(n) (*((short *)n))
  389. #define big_endian_int(n) (*((int *)n))
  390. #define big_endian_float(n) (*((float *)n))
  391. #define big_endian_double(n) (*((double *)n))
  392. #define big_endian_unsigned_short(n) (*((unsigned short *)n))
  393. #define set_big_endian_short(n, x) (*((short *)n)) = x
  394. #define set_big_endian_int(n, x) (*((int *)n)) = x
  395. #define set_big_endian_float(n, x) (*((float *)n)) = x
  396. #define set_big_endian_double(n, x) (*((double *)n)) = x
  397. #define set_big_endian_unsigned_short(n, x) (*((unsigned short *)n)) = x
  398. #else
  399. #define big_endian_short(n) (mus_char_to_bshort(n))
  400. #define big_endian_int(n) (mus_char_to_bint(n))
  401. #define big_endian_float(n) (mus_char_to_bfloat(n))
  402. #define big_endian_double(n) (mus_char_to_bdouble(n))
  403. #define big_endian_unsigned_short(n) (mus_char_to_ubshort(n))
  404. #define set_big_endian_short(n, x) mus_bshort_to_char(n, x)
  405. #define set_big_endian_int(n, x) mus_bint_to_char(n, x)
  406. #define set_big_endian_float(n, x) mus_bfloat_to_char(n, x)
  407. #define set_big_endian_double(n, x) mus_bdouble_to_char(n, x)
  408. #define set_big_endian_unsigned_short(n, x) mus_ubshort_to_char(n, x)
  409. #endif
  410. #if HAVE_BYTESWAP_H
  411. #define little_endian_short(n) ((short)(bswap_16((*((unsigned short *)n)))))
  412. #define little_endian_int(n) ((int)(bswap_32((*((unsigned int *)n)))))
  413. #define little_endian_unsigned_short(n) ((unsigned short)(bswap_16((*((unsigned short *)n)))))
  414. #else
  415. #define little_endian_short(n) (mus_char_to_lshort(n))
  416. #define little_endian_int(n) (mus_char_to_lint(n))
  417. #define little_endian_unsigned_short(n) (mus_char_to_ulshort(n))
  418. #endif
  419. #if __GNUC__ && HAVE_BYTESWAP_H
  420. #define little_endian_float(n) ({unsigned int x; x = bswap_32((*((unsigned int *)n))); (*((float *)&x));})
  421. #define little_endian_double(n) ({unsigned long long int x; x = bswap_64((*((unsigned long long int *)n))); (*((double *)&x));})
  422. #else
  423. #define little_endian_float(n) (mus_char_to_lfloat(n))
  424. #define little_endian_double(n) (mus_char_to_ldouble(n))
  425. #endif
  426. #if HAVE_BYTESWAP_H
  427. #define set_little_endian_short(n, x) (*((short *)n)) = ((short)(bswap_16(x)))
  428. #define set_little_endian_int(n, x) (*((int *)n)) = ((int)(bswap_32(x)))
  429. #define set_little_endian_unsigned_short(n, x) (*((unsigned short *)n)) = ((unsigned short)(bswap_16(x)))
  430. #else
  431. #define set_little_endian_short(n, x) mus_lshort_to_char(n, x)
  432. #define set_little_endian_int(n, x) mus_lint_to_char(n, x)
  433. #define set_little_endian_unsigned_short(n, x) mus_ulshort_to_char(n, x)
  434. #endif
  435. #define set_little_endian_float(n, x) mus_lfloat_to_char(n, x)
  436. #define set_little_endian_double(n, x) mus_ldouble_to_char(n, x)
  437. #endif
  438. /* ---------------- file descriptors ---------------- */
  439. static bool clipping_default = false;
  440. bool mus_clipping(void) {return(clipping_default);}
  441. bool mus_set_clipping(bool new_value) {clipping_default = new_value; return(new_value);}
  442. typedef struct {
  443. char *name;
  444. mus_sample_t sample_type;
  445. mus_header_t header_type;
  446. int bytes_per_sample, chans;
  447. bool clipping;
  448. mus_long_t data_location;
  449. bool saved;
  450. mus_long_t framples;
  451. mus_float_t **saved_data;
  452. void *next;
  453. } io_fd;
  454. static io_fd *io_fd_free_list = NULL;
  455. static io_fd *io_fd_alloc(void)
  456. {
  457. if (io_fd_free_list)
  458. {
  459. io_fd *p;
  460. p = io_fd_free_list;
  461. io_fd_free_list = (io_fd *)(p->next);
  462. return(p);
  463. }
  464. return((io_fd *)malloc(sizeof(io_fd)));
  465. }
  466. static void io_fd_free(io_fd *p)
  467. {
  468. p->next = (void *)io_fd_free_list;
  469. io_fd_free_list = p;
  470. }
  471. static int io_fd_size = 0;
  472. static io_fd **io_fds = NULL;
  473. #define IO_FD_ALLOC_SIZE 8
  474. int mus_file_open_descriptors(int tfd, const char *name, mus_sample_t samp_type, int size /* datum size */, mus_long_t location, int chans, mus_header_t type)
  475. {
  476. int err = MUS_NO_ERROR;
  477. if (io_fd_size == 0)
  478. {
  479. io_fd_size = tfd + IO_FD_ALLOC_SIZE;
  480. io_fds = (io_fd **)calloc(io_fd_size, sizeof(io_fd *));
  481. }
  482. if (io_fds)
  483. {
  484. if (io_fd_size <= tfd)
  485. {
  486. int i, lim;
  487. lim = io_fd_size;
  488. io_fd_size = tfd + IO_FD_ALLOC_SIZE;
  489. io_fds = (io_fd **)realloc(io_fds, io_fd_size * sizeof(io_fd *));
  490. for (i = lim; i < io_fd_size; i++) io_fds[i] = NULL;
  491. }
  492. if (io_fds[tfd] == NULL)
  493. io_fds[tfd] = io_fd_alloc();
  494. if (io_fds[tfd])
  495. {
  496. io_fd *fd;
  497. fd = io_fds[tfd];
  498. fd->framples = 0;
  499. fd->sample_type = samp_type;
  500. fd->bytes_per_sample = size;
  501. fd->data_location = location;
  502. fd->clipping = clipping_default;
  503. fd->header_type = type;
  504. fd->chans = chans;
  505. fd->saved = false;
  506. fd->saved_data = NULL;
  507. if (name)
  508. {
  509. int len;
  510. len = strlen(name);
  511. fd->name = (char *)malloc((len + 1) * sizeof(char));
  512. strcpy(fd->name, name);
  513. fd->name[len] = 0;
  514. }
  515. else fd->name = NULL;
  516. }
  517. else err = MUS_MEMORY_ALLOCATION_FAILED;
  518. }
  519. else err = MUS_MEMORY_ALLOCATION_FAILED;
  520. return(err);
  521. }
  522. void scan_io_fds_for_saved_data(mus_float_t **data);
  523. void scan_io_fds_for_saved_data(mus_float_t **data)
  524. {
  525. if ((io_fds) &&
  526. (io_fd_size > 0))
  527. {
  528. int i;
  529. for (i = 0; i < io_fd_size; i++)
  530. {
  531. io_fd *fd;
  532. fd = io_fds[i];
  533. if ((fd) &&
  534. (fd->saved) &&
  535. (fd->saved_data == data))
  536. {
  537. fd->saved = false;
  538. fd->saved_data = NULL;
  539. }
  540. }
  541. }
  542. }
  543. bool mus_file_clipping(int tfd)
  544. {
  545. io_fd *fd;
  546. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(false);
  547. fd = io_fds[tfd];
  548. return(fd->clipping);
  549. }
  550. int mus_file_set_clipping(int tfd, bool clipped)
  551. {
  552. io_fd *fd;
  553. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED);
  554. fd = io_fds[tfd];
  555. fd->clipping = clipped;
  556. return(MUS_NO_ERROR);
  557. }
  558. int mus_file_set_header_type(int tfd, mus_header_t type)
  559. {
  560. io_fd *fd;
  561. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED);
  562. fd = io_fds[tfd];
  563. fd->header_type = type;
  564. return(MUS_NO_ERROR);
  565. }
  566. mus_header_t mus_file_header_type(int tfd)
  567. {
  568. io_fd *fd;
  569. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(MUS_UNKNOWN_HEADER);
  570. fd = io_fds[tfd];
  571. return(fd->header_type);
  572. }
  573. char *mus_file_fd_name(int tfd)
  574. {
  575. io_fd *fd;
  576. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(NULL);
  577. fd = io_fds[tfd];
  578. return(fd->name);
  579. }
  580. int mus_file_set_chans(int tfd, int chans)
  581. {
  582. io_fd *fd;
  583. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL)) return(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED);
  584. fd = io_fds[tfd];
  585. fd->chans = chans;
  586. return(MUS_NO_ERROR);
  587. }
  588. void mus_file_save_data(int tfd, mus_long_t framples, mus_float_t **data)
  589. {
  590. io_fd *fd;
  591. fd = io_fds[tfd];
  592. if (!fd->saved)
  593. {
  594. /* fprintf(stderr, "mus_file_save_data %d\n", tfd); */
  595. fd->saved = true;
  596. fd->framples = framples;
  597. fd->saved_data = data;
  598. }
  599. }
  600. /* ---------------- open, creat, close ---------------- */
  601. int mus_file_open_read(const char *arg)
  602. {
  603. int fd;
  604. #if (defined(_MSC_VER) || __CYGWIN__)
  605. fd = OPEN(arg, O_RDONLY | O_BINARY, 0);
  606. #else
  607. fd = OPEN(arg, O_RDONLY, 0);
  608. #endif
  609. return(fd);
  610. }
  611. bool mus_file_probe(const char *arg)
  612. {
  613. #ifndef _MSC_VER
  614. if (!arg) return(false);
  615. return(access(arg, F_OK) == 0);
  616. #else
  617. int fd;
  618. #ifdef O_NONBLOCK
  619. fd = OPEN(arg, O_RDONLY, O_NONBLOCK);
  620. #else
  621. fd = OPEN(arg, O_RDONLY, 0);
  622. #endif
  623. if (fd == -1) return(false);
  624. CLOSE(fd, arg);
  625. return(true);
  626. #endif
  627. }
  628. int mus_file_open_write(const char *arg)
  629. {
  630. int fd;
  631. #if (defined(_MSC_VER) || __CYGWIN__)
  632. if ((fd = OPEN(arg, O_RDWR | O_BINARY, 0)) == -1)
  633. fd = CREAT(arg, S_IREAD | S_IWRITE); /* 0x0100 | 0x0080 */
  634. #else
  635. if ((fd = OPEN(arg, O_RDWR, 0)) == -1)
  636. fd = CREAT(arg, 0666); /* equivalent to the new open(arg, O_RDWR | O_CREAT | O_TRUNC, 0666) */
  637. #endif
  638. else lseek(fd, 0L, SEEK_END);
  639. return(fd);
  640. }
  641. int mus_file_create(const char *arg)
  642. {
  643. #if (defined(_MSC_VER ) || __CYGWIN__)
  644. return(CREAT(arg, S_IREAD | S_IWRITE));
  645. #else
  646. return(CREAT(arg, 0666));
  647. #endif
  648. }
  649. int mus_file_reopen_write(const char *arg)
  650. {
  651. int fd;
  652. #if (defined(_MSC_VER) || __CYGWIN__)
  653. fd = OPEN(arg, O_RDWR | O_BINARY, 0);
  654. #else
  655. fd = OPEN(arg, O_RDWR, 0);
  656. #endif
  657. return(fd);
  658. }
  659. int mus_file_close(int fd)
  660. {
  661. io_fd *fdp;
  662. #if (!USE_SND)
  663. int close_result = 0;
  664. #endif
  665. if ((io_fds == NULL) || (fd >= io_fd_size) || (fd < 0) || (io_fds[fd] == NULL)) return(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED);
  666. fdp = io_fds[fd];
  667. #if USE_SND
  668. CLOSE(fd, fdp->name);
  669. #else
  670. close_result = close(fd);
  671. #endif
  672. if (fdp->name) {free(fdp->name); fdp->name = NULL;}
  673. io_fd_free(fdp);
  674. io_fds[fd] = NULL;
  675. #if (!USE_SND)
  676. if (close_result < 0)
  677. return(MUS_CANT_CLOSE_FILE);
  678. #endif
  679. return(MUS_NO_ERROR);
  680. }
  681. /* ---------------- seek ---------------- */
  682. mus_long_t mus_file_seek_frample(int tfd, mus_long_t frample)
  683. {
  684. io_fd *fd;
  685. if (io_fds == NULL)
  686. return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED, "mus_file_seek_frample: no file descriptors!"));
  687. if (tfd >= io_fd_size)
  688. return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED,
  689. "mus_file_seek_frample: file descriptors not realloc'd? (tfd: %d, io_fd_size: %d)", tfd, io_fd_size));
  690. if ((tfd < 0) ||
  691. (io_fds[tfd] == NULL))
  692. return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED, "mus_file_seek_frample: file descriptor = %d?", tfd));
  693. fd = io_fds[tfd];
  694. if (fd->sample_type == MUS_UNKNOWN_SAMPLE)
  695. return(mus_error(MUS_NOT_A_SOUND_FILE, "mus_file_seek_frample: invalid sample type for %s", fd->name));
  696. return(lseek(tfd, fd->data_location + (fd->chans * frample * fd->bytes_per_sample), SEEK_SET));
  697. }
  698. /* ---------------- mulaw/alaw conversions ----------------
  699. *
  700. * x : input signal with max value 32767
  701. * mu : compression parameter (mu = 255 used for telephony)
  702. * y = (32767/log(1+mu))*log(1+mu*abs(x)/32767)*sign(x); -- this isn't right -- typo?
  703. */
  704. /* from sox g711.c */
  705. #define QUANT_MASK (0xf) /* Quantization field mask. */
  706. #define SEG_SHIFT (4) /* Left shift for segment number. */
  707. static short seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
  708. static int search(int val, short *table, int size)
  709. {
  710. int i;
  711. for (i = 0; i < size; i++) {if (val <= *table++) return(i);}
  712. return(size);
  713. }
  714. static unsigned char to_alaw(int pcm_val)
  715. {
  716. int mask, seg;
  717. if (pcm_val >= 0) mask = 0xD5; else {mask = 0x55; pcm_val = -pcm_val - 8;}
  718. seg = search(pcm_val, seg_end, 8);
  719. if (seg >= 8) return(0x7F ^ mask);
  720. else
  721. {
  722. unsigned char aval;
  723. aval = seg << SEG_SHIFT;
  724. if (seg < 2) aval |= (pcm_val >> 4) & QUANT_MASK; else aval |= (pcm_val >> (seg + 3)) & QUANT_MASK;
  725. return(aval ^ mask);
  726. }
  727. }
  728. #define A_(a) mus_short_to_sample(a)
  729. static const mus_float_t mus_alaw[256] = {
  730. A_(-5504), A_(-5248), A_(-6016), A_(-5760), A_(-4480), A_(-4224), A_(-4992), A_(-4736), A_(-7552), A_(-7296),
  731. A_(-8064), A_(-7808), A_(-6528), A_(-6272), A_(-7040), A_(-6784), A_(-2752), A_(-2624), A_(-3008), A_(-2880),
  732. A_(-2240), A_(-2112), A_(-2496), A_(-2368), A_(-3776), A_(-3648), A_(-4032), A_(-3904), A_(-3264), A_(-3136),
  733. A_(-3520), A_(-3392), A_(-22016), A_(-20992), A_(-24064), A_(-23040), A_(-17920), A_(-16896), A_(-19968), A_(-18944),
  734. A_(-30208), A_(-29184), A_(-32256), A_(-31232), A_(-26112), A_(-25088), A_(-28160), A_(-27136), A_(-11008), A_(-10496),
  735. A_(-12032), A_(-11520), A_(-8960), A_(-8448), A_(-9984), A_(-9472), A_(-15104), A_(-14592), A_(-16128), A_(-15616),
  736. A_(-13056), A_(-12544), A_(-14080), A_(-13568), A_(-344), A_(-328), A_(-376), A_(-360), A_(-280), A_(-264), A_(-312),
  737. A_(-296), A_(-472), A_(-456), A_(-504), A_(-488), A_(-408), A_(-392), A_(-440), A_(-424), A_(-88), A_(-72), A_(-120),
  738. A_(-104), A_(-24), A_(-8), A_(-56), A_(-40), A_(-216), A_(-200), A_(-248), A_(-232), A_(-152), A_(-136), A_(-184),
  739. A_(-168), A_(-1376), A_(-1312), A_(-1504), A_(-1440), A_(-1120), A_(-1056), A_(-1248), A_(-1184), A_(-1888), A_(-1824),
  740. A_(-2016), A_(-1952), A_(-1632), A_(-1568), A_(-1760), A_(-1696), A_(-688), A_(-656), A_(-752), A_(-720), A_(-560),
  741. A_(-528), A_(-624), A_(-592), A_(-944), A_(-912), A_(-1008), A_(-976), A_(-816), A_(-784), A_(-880), A_(-848), A_(5504),
  742. A_(5248), A_(6016), A_(5760), A_(4480), A_(4224), A_(4992), A_(4736), A_(7552), A_(7296), A_(8064), A_(7808), A_(6528),
  743. A_(6272), A_(7040), A_(6784), A_(2752), A_(2624), A_(3008), A_(2880), A_(2240), A_(2112), A_(2496), A_(2368), A_(3776),
  744. A_(3648), A_(4032), A_(3904), A_(3264), A_(3136), A_(3520), A_(3392), A_(22016), A_(20992), A_(24064), A_(23040), A_(17920),
  745. A_(16896), A_(19968), A_(18944), A_(30208), A_(29184), A_(32256), A_(31232), A_(26112), A_(25088), A_(28160), A_(27136),
  746. A_(11008), A_(10496), A_(12032), A_(11520), A_(8960), A_(8448), A_(9984), A_(9472), A_(15104), A_(14592), A_(16128),
  747. A_(15616), A_(13056), A_(12544), A_(14080), A_(13568), A_(344), A_(328), A_(376), A_(360), A_(280), A_(264), A_(312),
  748. A_(296), A_(472), A_(456), A_(504), A_(488), A_(408), A_(392), A_(440), A_(424), A_(88), A_(72), A_(120), A_(104),
  749. A_(24), A_(8), A_(56), A_(40), A_(216), A_(200), A_(248), A_(232), A_(152), A_(136), A_(184), A_(168), A_(1376),
  750. A_(1312), A_(1504), A_(1440), A_(1120), A_(1056), A_(1248), A_(1184), A_(1888), A_(1824), A_(2016), A_(1952), A_(1632),
  751. A_(1568), A_(1760), A_(1696), A_(688), A_(656), A_(752), A_(720), A_(560), A_(528), A_(624), A_(592), A_(944), A_(912),
  752. A_(1008), A_(976), A_(816), A_(784), A_(880), A_(848)
  753. };
  754. #define BIAS (0x84) /* Bias for linear code. */
  755. static unsigned char to_mulaw(int pcm_val)
  756. {
  757. int mask;
  758. int seg;
  759. if (pcm_val < 0) {pcm_val = BIAS - pcm_val; mask = 0x7F;} else {pcm_val += BIAS; mask = 0xFF;}
  760. seg = search(pcm_val, seg_end, 8);
  761. if (seg >= 8) return(0x7F ^ mask);
  762. else
  763. {
  764. unsigned char uval;
  765. uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF);
  766. return(uval ^ mask);
  767. }
  768. }
  769. #define MU_(a) mus_short_to_sample(a)
  770. static const mus_float_t mus_mulaw[256] = {
  771. MU_(-32124), MU_(-31100), MU_(-30076), MU_(-29052), MU_(-28028), MU_(-27004), MU_(-25980), MU_(-24956), MU_(-23932), MU_(-22908),
  772. MU_(-21884), MU_(-20860), MU_(-19836), MU_(-18812), MU_(-17788), MU_(-16764), MU_(-15996), MU_(-15484), MU_(-14972), MU_(-14460),
  773. MU_(-13948), MU_(-13436), MU_(-12924), MU_(-12412), MU_(-11900), MU_(-11388), MU_(-10876), MU_(-10364), MU_(-9852), MU_(-9340),
  774. MU_(-8828), MU_(-8316), MU_(-7932), MU_(-7676), MU_(-7420), MU_(-7164), MU_(-6908), MU_(-6652), MU_(-6396), MU_(-6140), MU_(-5884),
  775. MU_(-5628), MU_(-5372), MU_(-5116), MU_(-4860), MU_(-4604), MU_(-4348), MU_(-4092), MU_(-3900), MU_(-3772), MU_(-3644), MU_(-3516),
  776. MU_(-3388), MU_(-3260), MU_(-3132), MU_(-3004), MU_(-2876), MU_(-2748), MU_(-2620), MU_(-2492), MU_(-2364), MU_(-2236), MU_(-2108),
  777. MU_(-1980), MU_(-1884), MU_(-1820), MU_(-1756), MU_(-1692), MU_(-1628), MU_(-1564), MU_(-1500), MU_(-1436), MU_(-1372), MU_(-1308),
  778. MU_(-1244), MU_(-1180), MU_(-1116), MU_(-1052), MU_(-988), MU_(-924), MU_(-876), MU_(-844), MU_(-812), MU_(-780), MU_(-748),
  779. MU_(-716), MU_(-684), MU_(-652), MU_(-620), MU_(-588), MU_(-556), MU_(-524), MU_(-492), MU_(-460), MU_(-428), MU_(-396),
  780. MU_(-372), MU_(-356), MU_(-340), MU_(-324), MU_(-308), MU_(-292), MU_(-276), MU_(-260), MU_(-244), MU_(-228), MU_(-212),
  781. MU_(-196), MU_(-180), MU_(-164), MU_(-148), MU_(-132), MU_(-120), MU_(-112), MU_(-104), MU_(-96), MU_(-88), MU_(-80), MU_(-72),
  782. MU_(-64), MU_(-56), MU_(-48), MU_(-40), MU_(-32), MU_(-24), MU_(-16), MU_(-8), MU_(0), MU_(32124), MU_(31100), MU_(30076),
  783. MU_(29052), MU_(28028), MU_(27004), MU_(25980), MU_(24956), MU_(23932), MU_(22908), MU_(21884), MU_(20860), MU_(19836),
  784. MU_(18812), MU_(17788), MU_(16764), MU_(15996), MU_(15484), MU_(14972), MU_(14460), MU_(13948), MU_(13436), MU_(12924),
  785. MU_(12412), MU_(11900), MU_(11388), MU_(10876), MU_(10364), MU_(9852), MU_(9340), MU_(8828), MU_(8316), MU_(7932), MU_(7676),
  786. MU_(7420), MU_(7164), MU_(6908), MU_(6652), MU_(6396), MU_(6140), MU_(5884), MU_(5628), MU_(5372), MU_(5116), MU_(4860),
  787. MU_(4604), MU_(4348), MU_(4092), MU_(3900), MU_(3772), MU_(3644), MU_(3516), MU_(3388), MU_(3260), MU_(3132), MU_(3004),
  788. MU_(2876), MU_(2748), MU_(2620), MU_(2492), MU_(2364), MU_(2236), MU_(2108), MU_(1980), MU_(1884), MU_(1820), MU_(1756),
  789. MU_(1692), MU_(1628), MU_(1564), MU_(1500), MU_(1436), MU_(1372), MU_(1308), MU_(1244), MU_(1180), MU_(1116), MU_(1052),
  790. MU_(988), MU_(924), MU_(876), MU_(844), MU_(812), MU_(780), MU_(748), MU_(716), MU_(684), MU_(652), MU_(620), MU_(588),
  791. MU_(556), MU_(524), MU_(492), MU_(460), MU_(428), MU_(396), MU_(372), MU_(356), MU_(340), MU_(324), MU_(308), MU_(292),
  792. MU_(276), MU_(260), MU_(244), MU_(228), MU_(212), MU_(196), MU_(180), MU_(164), MU_(148), MU_(132), MU_(120), MU_(112),
  793. MU_(104), MU_(96), MU_(88), MU_(80), MU_(72), MU_(64), MU_(56), MU_(48), MU_(40), MU_(32), MU_(24), MU_(16), MU_(8), MU_(0)
  794. };
  795. #define B_(a) mus_byte_to_sample(a)
  796. static const mus_float_t mus_byte[256] = {
  797. B_(0), B_(1), B_(2), B_(3), B_(4), B_(5), B_(6), B_(7), B_(8), B_(9), B_(10), B_(11), B_(12), B_(13), B_(14), B_(15), B_(16),
  798. B_(17), B_(18), B_(19), B_(20), B_(21), B_(22), B_(23), B_(24), B_(25), B_(26), B_(27), B_(28), B_(29), B_(30), B_(31), B_(32),
  799. B_(33), B_(34), B_(35), B_(36), B_(37), B_(38), B_(39), B_(40), B_(41), B_(42), B_(43), B_(44), B_(45), B_(46), B_(47), B_(48),
  800. B_(49), B_(50), B_(51), B_(52), B_(53), B_(54), B_(55), B_(56), B_(57), B_(58), B_(59), B_(60), B_(61), B_(62), B_(63), B_(64),
  801. B_(65), B_(66), B_(67), B_(68), B_(69), B_(70), B_(71), B_(72), B_(73), B_(74), B_(75), B_(76), B_(77), B_(78), B_(79), B_(80),
  802. B_(81), B_(82), B_(83), B_(84), B_(85), B_(86), B_(87), B_(88), B_(89), B_(90), B_(91), B_(92), B_(93), B_(94), B_(95), B_(96),
  803. B_(97), B_(98), B_(99), B_(100), B_(101), B_(102), B_(103), B_(104), B_(105), B_(106), B_(107), B_(108), B_(109), B_(110), B_(111),
  804. B_(112), B_(113), B_(114), B_(115), B_(116), B_(117), B_(118), B_(119), B_(120), B_(121), B_(122), B_(123), B_(124), B_(125),
  805. B_(126), B_(127), B_(-128), B_(-127), B_(-126), B_(-125), B_(-124), B_(-123), B_(-122), B_(-121), B_(-120), B_(-119), B_(-118),
  806. B_(-117), B_(-116), B_(-115), B_(-114), B_(-113), B_(-112), B_(-111), B_(-110), B_(-109), B_(-108), B_(-107), B_(-106), B_(-105),
  807. B_(-104), B_(-103), B_(-102), B_(-101), B_(-100), B_(-99), B_(-98), B_(-97), B_(-96), B_(-95), B_(-94), B_(-93), B_(-92), B_(-91),
  808. B_(-90), B_(-89), B_(-88), B_(-87), B_(-86), B_(-85), B_(-84), B_(-83), B_(-82), B_(-81), B_(-80), B_(-79), B_(-78), B_(-77),
  809. B_(-76), B_(-75), B_(-74), B_(-73), B_(-72), B_(-71), B_(-70), B_(-69), B_(-68), B_(-67), B_(-66), B_(-65), B_(-64), B_(-63),
  810. B_(-62), B_(-61), B_(-60), B_(-59), B_(-58), B_(-57), B_(-56), B_(-55), B_(-54), B_(-53), B_(-52), B_(-51), B_(-50), B_(-49),
  811. B_(-48), B_(-47), B_(-46), B_(-45), B_(-44), B_(-43), B_(-42), B_(-41), B_(-40), B_(-39), B_(-38), B_(-37), B_(-36), B_(-35),
  812. B_(-34), B_(-33), B_(-32), B_(-31), B_(-30), B_(-29), B_(-28), B_(-27), B_(-26), B_(-25), B_(-24), B_(-23), B_(-22), B_(-21),
  813. B_(-20), B_(-19), B_(-18), B_(-17), B_(-16), B_(-15), B_(-14), B_(-13), B_(-12), B_(-11), B_(-10), B_(-9), B_(-8), B_(-7),
  814. B_(-6), B_(-5), B_(-4), B_(-3), B_(-2), B_(-1)
  815. };
  816. #define UB_(a) mus_byte_to_sample(a)
  817. static const mus_float_t mus_ubyte[256] = {
  818. UB_(-128), UB_(-127), UB_(-126), UB_(-125), UB_(-124), UB_(-123), UB_(-122), UB_(-121), UB_(-120), UB_(-119), UB_(-118), UB_(-117),
  819. UB_(-116), UB_(-115), UB_(-114), UB_(-113), UB_(-112), UB_(-111), UB_(-110), UB_(-109), UB_(-108), UB_(-107), UB_(-106), UB_(-105),
  820. UB_(-104), UB_(-103), UB_(-102), UB_(-101), UB_(-100), UB_(-99), UB_(-98), UB_(-97), UB_(-96), UB_(-95), UB_(-94), UB_(-93), UB_(-92),
  821. UB_(-91), UB_(-90), UB_(-89), UB_(-88), UB_(-87), UB_(-86), UB_(-85), UB_(-84), UB_(-83), UB_(-82), UB_(-81), UB_(-80), UB_(-79),
  822. UB_(-78), UB_(-77), UB_(-76), UB_(-75), UB_(-74), UB_(-73), UB_(-72), UB_(-71), UB_(-70), UB_(-69), UB_(-68), UB_(-67), UB_(-66),
  823. UB_(-65), UB_(-64), UB_(-63), UB_(-62), UB_(-61), UB_(-60), UB_(-59), UB_(-58), UB_(-57), UB_(-56), UB_(-55), UB_(-54), UB_(-53),
  824. UB_(-52), UB_(-51), UB_(-50), UB_(-49), UB_(-48), UB_(-47), UB_(-46), UB_(-45), UB_(-44), UB_(-43), UB_(-42), UB_(-41), UB_(-40),
  825. UB_(-39), UB_(-38), UB_(-37), UB_(-36), UB_(-35), UB_(-34), UB_(-33), UB_(-32), UB_(-31), UB_(-30), UB_(-29), UB_(-28), UB_(-27),
  826. UB_(-26), UB_(-25), UB_(-24), UB_(-23), UB_(-22), UB_(-21), UB_(-20), UB_(-19), UB_(-18), UB_(-17), UB_(-16), UB_(-15), UB_(-14),
  827. UB_(-13), UB_(-12), UB_(-11), UB_(-10), UB_(-9), UB_(-8), UB_(-7), UB_(-6), UB_(-5), UB_(-4), UB_(-3), UB_(-2), UB_(-1), UB_(0),
  828. UB_(1), UB_(2), UB_(3), UB_(4), UB_(5), UB_(6), UB_(7), UB_(8), UB_(9), UB_(10), UB_(11), UB_(12), UB_(13), UB_(14), UB_(15),
  829. UB_(16), UB_(17), UB_(18), UB_(19), UB_(20), UB_(21), UB_(22), UB_(23), UB_(24), UB_(25), UB_(26), UB_(27), UB_(28), UB_(29),
  830. UB_(30), UB_(31), UB_(32), UB_(33), UB_(34), UB_(35), UB_(36), UB_(37), UB_(38), UB_(39), UB_(40), UB_(41), UB_(42), UB_(43),
  831. UB_(44), UB_(45), UB_(46), UB_(47), UB_(48), UB_(49), UB_(50), UB_(51), UB_(52), UB_(53), UB_(54), UB_(55), UB_(56), UB_(57),
  832. UB_(58), UB_(59), UB_(60), UB_(61), UB_(62), UB_(63), UB_(64), UB_(65), UB_(66), UB_(67), UB_(68), UB_(69), UB_(70), UB_(71),
  833. UB_(72), UB_(73), UB_(74), UB_(75), UB_(76), UB_(77), UB_(78), UB_(79), UB_(80), UB_(81), UB_(82), UB_(83), UB_(84), UB_(85),
  834. UB_(86), UB_(87), UB_(88), UB_(89), UB_(90), UB_(91), UB_(92), UB_(93), UB_(94), UB_(95), UB_(96), UB_(97), UB_(98), UB_(99),
  835. UB_(100), UB_(101), UB_(102), UB_(103), UB_(104), UB_(105), UB_(106), UB_(107), UB_(108), UB_(109), UB_(110), UB_(111), UB_(112),
  836. UB_(113), UB_(114), UB_(115), UB_(116), UB_(117), UB_(118), UB_(119), UB_(120), UB_(121), UB_(122), UB_(123), UB_(124), UB_(125),
  837. UB_(126), UB_(127)
  838. };
  839. /* ---------------- read ---------------- */
  840. #define BUFLIM (64 * 1024)
  841. #define UBYTE_ZERO 128
  842. #define USHORT_ZERO 32768
  843. #define MUS_SAMPLE_UNSCALED(n) ((n) / 32768.0)
  844. /* see note in _sndlib.h" values are "unscaled" from the DAC's point of view */
  845. static mus_float_t *swapped_shorts = NULL;
  846. static void initialize_swapped_shorts(void)
  847. {
  848. int i;
  849. swapped_shorts = (mus_float_t *)malloc(65536 * sizeof(mus_float_t));
  850. for (i = 0; i < 65536; i++)
  851. {
  852. signed short x;
  853. x = (signed short)(((i >> 8) & 0xff) | ((i & 0xff) << 8));
  854. swapped_shorts[i] = mus_short_to_sample(x);
  855. }
  856. }
  857. static mus_long_t mus_read_any_1(int tfd, mus_long_t beg, int chans, mus_long_t nints, mus_float_t **bufs, mus_float_t **cm, char *inbuf)
  858. {
  859. /* beg no longer means buffer-relative start point (that is assumed to be 0): changed 18-Dec-13
  860. * beg is now the starting frample number in the file data (first frample = 0)
  861. * so old form
  862. * mus_read_any_1(f, 0, ...)
  863. * is now
  864. * mus_read_any_1(f, frample, ...)
  865. */
  866. mus_sample_t samp_type;
  867. int siz, siz_chans;
  868. mus_long_t bytes, lim, leftover, total_read, k, loc, buflim;
  869. unsigned char *jchar;
  870. static char *ur_charbuf = NULL;
  871. char *charbuf = NULL;
  872. mus_float_t *buffer;
  873. if (nints <= 0) return(0);
  874. if (!inbuf)
  875. {
  876. io_fd *fd;
  877. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL))
  878. return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED, "mus_read: no file descriptors!"));
  879. fd = io_fds[tfd];
  880. if (fd->sample_type == MUS_UNKNOWN_SAMPLE)
  881. return(mus_error(MUS_FILE_CLOSED, "mus_read: invalid sample type for %s", fd->name));
  882. samp_type = fd->sample_type;
  883. siz = fd->bytes_per_sample;
  884. if ((samp_type == MUS_OUT_SAMPLE_TYPE) &&
  885. (chans == 1))
  886. /* (beg == 0)) */
  887. {
  888. ssize_t total;
  889. bytes = nints * siz;
  890. total = read(tfd, (char *)(bufs[0]), bytes);
  891. if (total != bytes)
  892. {
  893. if (total <= 0)
  894. memset((void *)(bufs[0]), 0, bytes);
  895. else
  896. {
  897. int i;
  898. for (i = total / siz; i < nints; i++)
  899. bufs[0][i] = 0.0;
  900. }
  901. }
  902. return(total / siz);
  903. }
  904. if (fd->saved)
  905. {
  906. /* fprintf(stderr, "mus_read_any_1 %d use saved data\n", tfd); */
  907. lim = nints;
  908. if (lim > fd->framples)
  909. lim = fd->framples;
  910. bytes = lim * sizeof(mus_float_t);
  911. if (beg < 0) beg = 0;
  912. if ((chans == 1) &&
  913. ((!cm) || (cm[0])))
  914. {
  915. buffer = (mus_float_t *)(bufs[0]);
  916. if (buffer)
  917. {
  918. memcpy((void *)buffer, (void *)(fd->saved_data[0] + beg), bytes);
  919. if (lim < nints)
  920. memset((void *)(buffer + lim), 0, (nints - lim) * sizeof(mus_float_t));
  921. }
  922. }
  923. else
  924. {
  925. for (k = 0; k < chans; k++)
  926. if ((cm == NULL) || (cm[k]))
  927. {
  928. buffer = (mus_float_t *)(bufs[k]);
  929. if (buffer)
  930. {
  931. memcpy((void *)buffer, (void *)(fd->saved_data[k] + beg), bytes);
  932. if (lim < nints)
  933. memset((void *)(buffer + lim), 0, (nints - lim) * sizeof(mus_float_t));
  934. }
  935. }
  936. }
  937. return(lim);
  938. }
  939. if (ur_charbuf == NULL)
  940. ur_charbuf = (char *)malloc(BUFLIM * sizeof(char));
  941. charbuf = ur_charbuf;
  942. }
  943. else
  944. {
  945. charbuf = inbuf;
  946. siz = mus_bytes_per_sample((mus_sample_t)tfd);
  947. samp_type = (mus_sample_t)tfd;
  948. }
  949. siz_chans = siz * chans;
  950. leftover = (nints * siz_chans);
  951. k = (BUFLIM) % siz_chans;
  952. if (k != 0) /* for example, 3 channel output of 1-byte (mulaw) samples will need a mod 3 buffer */
  953. buflim = (BUFLIM) - k;
  954. else buflim = BUFLIM;
  955. total_read = 0;
  956. loc = 0;
  957. #if MUS_LITTLE_ENDIAN
  958. if ((samp_type == MUS_BSHORT) && (!swapped_shorts))
  959. initialize_swapped_shorts();
  960. #else
  961. if ((samp_type == MUS_LSHORT) && (!swapped_shorts))
  962. initialize_swapped_shorts();
  963. #endif
  964. while (leftover > 0)
  965. {
  966. mus_long_t oldloc, loclim;
  967. mus_float_t *bufnow, *bufend, *bufend4;
  968. bytes = leftover;
  969. if (bytes > buflim)
  970. {
  971. leftover = (bytes - buflim);
  972. bytes = buflim;
  973. }
  974. else leftover = 0;
  975. if (!inbuf)
  976. {
  977. ssize_t total;
  978. total = read(tfd, charbuf, bytes);
  979. if (total <= 0)
  980. {
  981. /* zero out trailing section (some callers don't check the returned value) -- this added 9-May-99 */
  982. if (loc < nints)
  983. for (k = 0; k < chans; k++)
  984. if ((cm == NULL) || (cm[k]))
  985. {
  986. mus_float_t *p;
  987. p = bufs[k];
  988. memset((void *)(p + loc), 0, (nints - loc) * sizeof(mus_float_t));
  989. }
  990. return(total_read);
  991. }
  992. lim = (int) (total / siz_chans); /* this divide must be exact (hence the buflim calc above) */
  993. }
  994. else
  995. {
  996. lim = nints; /* framples in this case */
  997. leftover = 0;
  998. }
  999. total_read += lim;
  1000. oldloc = loc;
  1001. if ((chans == 1) &&
  1002. ((!cm) || (cm[0])))
  1003. {
  1004. buffer = (mus_float_t *)(bufs[0]);
  1005. if (buffer)
  1006. {
  1007. loc = oldloc;
  1008. loclim = loc + lim;
  1009. bufnow = (mus_float_t *)(buffer + loc);
  1010. bufend = (mus_float_t *)(buffer + loclim - 1);
  1011. bufend4 = (mus_float_t *)(bufend - 4);
  1012. jchar = (unsigned char *)charbuf;
  1013. switch (samp_type)
  1014. {
  1015. case MUS_BSHORT:
  1016. #if MUS_LITTLE_ENDIAN
  1017. while (bufnow <= bufend4)
  1018. {
  1019. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1020. jchar += 2;
  1021. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1022. jchar += 2;
  1023. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1024. jchar += 2;
  1025. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1026. jchar += 2;
  1027. }
  1028. for (; bufnow <= bufend; jchar += 2) /* unsigned short here as charbuf loc is slower */
  1029. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))]; /* bswap16 is much slower here because of the subsequent short->double conversion */
  1030. #else
  1031. for (; bufnow <= bufend; jchar += 2)
  1032. (*bufnow++) = mus_short_to_sample(big_endian_short(jchar));
  1033. #endif
  1034. break;
  1035. case MUS_LSHORT:
  1036. #if (!MUS_LITTLE_ENDIAN)
  1037. for (; bufnow <= bufend; jchar += 2)
  1038. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1039. #else
  1040. while (bufnow <= bufend4)
  1041. {
  1042. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1043. jchar += 2;
  1044. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1045. jchar += 2;
  1046. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1047. jchar += 2;
  1048. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1049. jchar += 2;
  1050. }
  1051. for (; bufnow <= bufend; jchar += 2)
  1052. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1053. #endif
  1054. break;
  1055. case MUS_BINT:
  1056. while (bufnow <= bufend4)
  1057. {
  1058. (*bufnow++) = mus_int_to_sample(big_endian_int(jchar));
  1059. jchar += 4;
  1060. (*bufnow++) = mus_int_to_sample(big_endian_int(jchar));
  1061. jchar += 4;
  1062. (*bufnow++) = mus_int_to_sample(big_endian_int(jchar));
  1063. jchar += 4;
  1064. (*bufnow++) = mus_int_to_sample(big_endian_int(jchar));
  1065. jchar += 4;
  1066. }
  1067. for (; bufnow <= bufend; jchar += 4)
  1068. (*bufnow++) = mus_int_to_sample(big_endian_int(jchar));
  1069. break;
  1070. case MUS_LINT:
  1071. while (bufnow <= bufend4)
  1072. {
  1073. (*bufnow++) = mus_int_to_sample(little_endian_int(jchar));
  1074. jchar += 4;
  1075. (*bufnow++) = mus_int_to_sample(little_endian_int(jchar));
  1076. jchar += 4;
  1077. (*bufnow++) = mus_int_to_sample(little_endian_int(jchar));
  1078. jchar += 4;
  1079. (*bufnow++) = mus_int_to_sample(little_endian_int(jchar));
  1080. jchar += 4;
  1081. }
  1082. for (; bufnow <= bufend; jchar += 4)
  1083. (*bufnow++) = mus_int_to_sample(little_endian_int(jchar));
  1084. break;
  1085. case MUS_BINTN:
  1086. for (; bufnow <= bufend; jchar += 4)
  1087. (*bufnow++) = mus_int_to_sample((big_endian_int(jchar) >> 8));
  1088. break;
  1089. case MUS_LINTN:
  1090. for (; bufnow <= bufend; jchar += 4)
  1091. (*bufnow++) = mus_int_to_sample((little_endian_int(jchar) >> 8));
  1092. break;
  1093. case MUS_MULAW:
  1094. while (bufnow <= bufend4)
  1095. {
  1096. (*bufnow++) = mus_mulaw[*jchar++];
  1097. (*bufnow++) = mus_mulaw[*jchar++];
  1098. (*bufnow++) = mus_mulaw[*jchar++];
  1099. (*bufnow++) = mus_mulaw[*jchar++];
  1100. }
  1101. for (; bufnow <= bufend; jchar++)
  1102. (*bufnow++) = mus_mulaw[*jchar];
  1103. break;
  1104. case MUS_ALAW:
  1105. while (bufnow <= bufend4)
  1106. {
  1107. (*bufnow++) = mus_alaw[*jchar++];
  1108. (*bufnow++) = mus_alaw[*jchar++];
  1109. (*bufnow++) = mus_alaw[*jchar++];
  1110. (*bufnow++) = mus_alaw[*jchar++];
  1111. }
  1112. for (; bufnow <= bufend; jchar++)
  1113. (*bufnow++) = mus_alaw[*jchar];
  1114. break;
  1115. case MUS_BYTE:
  1116. while (bufnow <= bufend4)
  1117. {
  1118. (*bufnow++) = mus_byte[(unsigned char)(*jchar++)];
  1119. (*bufnow++) = mus_byte[(unsigned char)(*jchar++)];
  1120. (*bufnow++) = mus_byte[(unsigned char)(*jchar++)];
  1121. (*bufnow++) = mus_byte[(unsigned char)(*jchar++)];
  1122. }
  1123. for (; bufnow <= bufend; jchar++)
  1124. (*bufnow++) = mus_byte[(unsigned char)(*jchar)];
  1125. break;
  1126. case MUS_UBYTE:
  1127. while (bufnow <= bufend4)
  1128. {
  1129. (*bufnow++) = mus_ubyte[(unsigned char)(*jchar++)];
  1130. (*bufnow++) = mus_ubyte[(unsigned char)(*jchar++)];
  1131. (*bufnow++) = mus_ubyte[(unsigned char)(*jchar++)];
  1132. (*bufnow++) = mus_ubyte[(unsigned char)(*jchar++)];
  1133. }
  1134. for (; bufnow <= bufend; jchar++)
  1135. (*bufnow++) = mus_ubyte[(unsigned char)(*jchar)];
  1136. break;
  1137. case MUS_BFLOAT:
  1138. while (bufnow <= bufend4)
  1139. {
  1140. (*bufnow++) = (mus_float_t)(big_endian_float(jchar));
  1141. jchar += 4;
  1142. (*bufnow++) = (mus_float_t)(big_endian_float(jchar));
  1143. jchar += 4;
  1144. (*bufnow++) = (mus_float_t)(big_endian_float(jchar));
  1145. jchar += 4;
  1146. (*bufnow++) = (mus_float_t)(big_endian_float(jchar));
  1147. jchar += 4;
  1148. }
  1149. for (; bufnow <= bufend; jchar += 4)
  1150. (*bufnow++) = (mus_float_t)(big_endian_float(jchar));
  1151. break;
  1152. case MUS_BFLOAT_UNSCALED:
  1153. for (; bufnow <= bufend; jchar += 4)
  1154. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(big_endian_float(jchar)));
  1155. break;
  1156. case MUS_BDOUBLE:
  1157. while (bufnow <= bufend4)
  1158. {
  1159. (*bufnow++) = (mus_float_t)(big_endian_double(jchar));
  1160. jchar += 8;
  1161. (*bufnow++) = (mus_float_t)(big_endian_double(jchar));
  1162. jchar += 8;
  1163. (*bufnow++) = (mus_float_t)(big_endian_double(jchar));
  1164. jchar += 8;
  1165. (*bufnow++) = (mus_float_t)(big_endian_double(jchar));
  1166. jchar += 8;
  1167. }
  1168. for (; bufnow <= bufend; jchar += 8)
  1169. (*bufnow++) = (mus_float_t)(big_endian_double(jchar));
  1170. break;
  1171. case MUS_BDOUBLE_UNSCALED:
  1172. for (; bufnow <= bufend; jchar += 8)
  1173. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(big_endian_double(jchar)));
  1174. break;
  1175. case MUS_LFLOAT:
  1176. while (bufnow <= bufend4)
  1177. {
  1178. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1179. jchar += 4;
  1180. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1181. jchar += 4;
  1182. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1183. jchar += 4;
  1184. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1185. jchar += 4;
  1186. }
  1187. for (; bufnow <= bufend; jchar += 4)
  1188. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1189. break;
  1190. case MUS_LFLOAT_UNSCALED:
  1191. for (; bufnow <= bufend; jchar += 4)
  1192. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(little_endian_float(jchar)));
  1193. break;
  1194. case MUS_LDOUBLE:
  1195. for (; bufnow <= bufend; jchar += 8)
  1196. (*bufnow++) = (mus_float_t)(little_endian_double(jchar));
  1197. break;
  1198. case MUS_LDOUBLE_UNSCALED:
  1199. for (; bufnow <= bufend; jchar += 8)
  1200. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(little_endian_double(jchar)));
  1201. break;
  1202. case MUS_UBSHORT:
  1203. while (bufnow <= bufend4)
  1204. {
  1205. (*bufnow++) = mus_short_to_sample((int)(big_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1206. jchar += 2;
  1207. (*bufnow++) = mus_short_to_sample((int)(big_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1208. jchar += 2;
  1209. (*bufnow++) = mus_short_to_sample((int)(big_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1210. jchar += 2;
  1211. (*bufnow++) = mus_short_to_sample((int)(big_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1212. jchar += 2;
  1213. }
  1214. for (; bufnow <= bufend; jchar += 2)
  1215. (*bufnow++) = mus_short_to_sample((int)(big_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1216. break;
  1217. case MUS_ULSHORT:
  1218. while (bufnow <= bufend4)
  1219. {
  1220. (*bufnow++) = mus_short_to_sample((int)(little_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1221. jchar += 2;
  1222. (*bufnow++) = mus_short_to_sample((int)(little_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1223. jchar += 2;
  1224. (*bufnow++) = mus_short_to_sample((int)(little_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1225. jchar += 2;
  1226. (*bufnow++) = mus_short_to_sample((int)(little_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1227. jchar += 2;
  1228. }
  1229. for (; bufnow <= bufend; jchar += 2)
  1230. (*bufnow++) = mus_short_to_sample((int)(little_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1231. break;
  1232. case MUS_B24INT:
  1233. while (bufnow <= bufend4)
  1234. {
  1235. (*bufnow++) = bint24_to_sample(jchar);
  1236. jchar += 3;
  1237. (*bufnow++) = bint24_to_sample(jchar);
  1238. jchar += 3;
  1239. (*bufnow++) = bint24_to_sample(jchar);
  1240. jchar += 3;
  1241. (*bufnow++) = bint24_to_sample(jchar);
  1242. jchar += 3;
  1243. }
  1244. for (; bufnow <= bufend; jchar += 3)
  1245. (*bufnow++) = bint24_to_sample(jchar);
  1246. break;
  1247. case MUS_L24INT:
  1248. {
  1249. int val;
  1250. val = (jchar[2] << 16) + (jchar[1] << 8) + jchar[0];
  1251. if (val >= (1 << 23)) val -= (1 << 24);
  1252. (*bufnow++) = mus_int24_to_sample(val);
  1253. jchar += 2;
  1254. }
  1255. while (bufnow <= bufend4)
  1256. {
  1257. (*bufnow++) = int24_to_sample(jchar);
  1258. jchar += 3;
  1259. (*bufnow++) = int24_to_sample(jchar);
  1260. jchar += 3;
  1261. (*bufnow++) = int24_to_sample(jchar);
  1262. jchar += 3;
  1263. (*bufnow++) = int24_to_sample(jchar);
  1264. jchar += 3;
  1265. }
  1266. for (; bufnow <= bufend; jchar += 3)
  1267. (*bufnow++) = int24_to_sample(jchar);
  1268. break;
  1269. default: break;
  1270. }
  1271. loc = loclim;
  1272. }
  1273. }
  1274. else
  1275. {
  1276. for (k = 0; k < chans; k++)
  1277. {
  1278. if ((cm == NULL) || (cm[k]))
  1279. {
  1280. buffer = (mus_float_t *)(bufs[k]);
  1281. if (buffer)
  1282. {
  1283. loc = oldloc;
  1284. loclim = loc + lim;
  1285. bufnow = (mus_float_t *)(buffer + loc);
  1286. bufend = (mus_float_t *)(buffer + loclim - 1);
  1287. bufend4 = (mus_float_t *)(bufend - 4);
  1288. jchar = (unsigned char *)charbuf;
  1289. jchar += (k * siz);
  1290. switch (samp_type)
  1291. {
  1292. case MUS_BSHORT:
  1293. #if MUS_LITTLE_ENDIAN
  1294. while (bufnow <= bufend4)
  1295. {
  1296. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1297. jchar += siz_chans;
  1298. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1299. jchar += siz_chans;
  1300. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1301. jchar += siz_chans;
  1302. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1303. jchar += siz_chans;
  1304. }
  1305. for (; bufnow <= bufend; jchar += siz_chans)
  1306. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1307. #else
  1308. for (; bufnow <= bufend; jchar += siz_chans)
  1309. (*bufnow++) = mus_short_to_sample(big_endian_short(jchar));
  1310. #endif
  1311. break;
  1312. case MUS_LSHORT:
  1313. #if (!MUS_LITTLE_ENDIAN)
  1314. for (; bufnow <= bufend; jchar += siz_chans)
  1315. (*bufnow++) = swapped_shorts[(*((unsigned short *)jchar))];
  1316. #else
  1317. while (bufnow <= bufend4)
  1318. {
  1319. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1320. jchar += siz_chans;
  1321. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1322. jchar += siz_chans;
  1323. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1324. jchar += siz_chans;
  1325. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1326. jchar += siz_chans;
  1327. }
  1328. for (; bufnow <= bufend; jchar += siz_chans)
  1329. (*bufnow++) = mus_short_to_sample(little_endian_short(jchar));
  1330. #endif
  1331. break;
  1332. case MUS_BINT:
  1333. for (; bufnow <= bufend; jchar += siz_chans)
  1334. (*bufnow++) = mus_int_to_sample(big_endian_int(jchar));
  1335. break;
  1336. case MUS_LINT:
  1337. for (; bufnow <= bufend; jchar += siz_chans)
  1338. (*bufnow++) = mus_int_to_sample(little_endian_int(jchar));
  1339. break;
  1340. case MUS_BINTN:
  1341. for (; bufnow <= bufend; jchar += siz_chans)
  1342. (*bufnow++) = mus_int_to_sample((big_endian_int(jchar) >> 8));
  1343. break;
  1344. case MUS_LINTN:
  1345. for (; bufnow <= bufend; jchar += siz_chans)
  1346. (*bufnow++) = mus_int_to_sample((little_endian_int(jchar) >> 8));
  1347. break;
  1348. case MUS_MULAW:
  1349. for (; bufnow <= bufend; jchar += siz_chans)
  1350. (*bufnow++) = mus_mulaw[*jchar];
  1351. break;
  1352. case MUS_ALAW:
  1353. for (; bufnow <= bufend; jchar += siz_chans)
  1354. (*bufnow++) = mus_alaw[*jchar];
  1355. break;
  1356. case MUS_BYTE:
  1357. for (; bufnow <= bufend; jchar += siz_chans)
  1358. (*bufnow++) = mus_byte[(unsigned char)(*jchar)];
  1359. break;
  1360. case MUS_UBYTE:
  1361. for (; bufnow <= bufend; jchar += siz_chans)
  1362. (*bufnow++) = mus_ubyte[(unsigned char)(*jchar)];
  1363. break;
  1364. case MUS_BFLOAT:
  1365. for (; bufnow <= bufend; jchar += siz_chans)
  1366. (*bufnow++) = (mus_float_t)(big_endian_float(jchar));
  1367. break;
  1368. case MUS_BFLOAT_UNSCALED:
  1369. for (; bufnow <= bufend; jchar += siz_chans)
  1370. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(big_endian_float(jchar)));
  1371. break;
  1372. case MUS_BDOUBLE:
  1373. for (; bufnow <= bufend; jchar += siz_chans)
  1374. (*bufnow++) = (mus_float_t)(big_endian_double(jchar));
  1375. break;
  1376. case MUS_BDOUBLE_UNSCALED:
  1377. for (; bufnow <= bufend; jchar += siz_chans)
  1378. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(big_endian_double(jchar)));
  1379. break;
  1380. case MUS_LFLOAT:
  1381. while (bufnow <= bufend4)
  1382. {
  1383. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1384. jchar += siz_chans;
  1385. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1386. jchar += siz_chans;
  1387. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1388. jchar += siz_chans;
  1389. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1390. jchar += siz_chans;
  1391. }
  1392. for (; bufnow <= bufend; jchar += siz_chans)
  1393. (*bufnow++) = (mus_float_t)(little_endian_float(jchar));
  1394. break;
  1395. case MUS_LFLOAT_UNSCALED:
  1396. for (; bufnow <= bufend; jchar += siz_chans)
  1397. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(little_endian_float(jchar)));
  1398. break;
  1399. case MUS_LDOUBLE:
  1400. while (bufnow <= bufend4)
  1401. {
  1402. (*bufnow++) = (mus_float_t)(little_endian_double(jchar));
  1403. jchar += siz_chans;
  1404. (*bufnow++) = (mus_float_t)(little_endian_double(jchar));
  1405. jchar += siz_chans;
  1406. (*bufnow++) = (mus_float_t)(little_endian_double(jchar));
  1407. jchar += siz_chans;
  1408. (*bufnow++) = (mus_float_t)(little_endian_double(jchar));
  1409. jchar += siz_chans;
  1410. }
  1411. for (; bufnow <= bufend; jchar += siz_chans)
  1412. (*bufnow++) = (mus_float_t)(little_endian_double(jchar));
  1413. break;
  1414. case MUS_LDOUBLE_UNSCALED:
  1415. for (; bufnow <= bufend; jchar += siz_chans)
  1416. (*bufnow++) = (mus_float_t)(MUS_SAMPLE_UNSCALED(little_endian_double(jchar)));
  1417. break;
  1418. case MUS_UBSHORT:
  1419. for (; bufnow <= bufend; jchar += siz_chans)
  1420. (*bufnow++) = mus_short_to_sample((int)(big_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1421. break;
  1422. case MUS_ULSHORT:
  1423. for (; bufnow <= bufend; jchar += siz_chans)
  1424. (*bufnow++) = mus_short_to_sample((int)(little_endian_unsigned_short(jchar)) - USHORT_ZERO);
  1425. break;
  1426. case MUS_B24INT:
  1427. for (; bufnow <= bufend; jchar += siz_chans)
  1428. (*bufnow++) = bint24_to_sample(jchar);
  1429. break;
  1430. case MUS_L24INT:
  1431. {
  1432. /*align for little_endian_int as above */
  1433. int val;
  1434. val = (jchar[2] << 16) + (jchar[1] << 8) + jchar[0];
  1435. if (val >= (1 << 23)) val -= (1 << 24);
  1436. (*bufnow++) = mus_int24_to_sample(val);
  1437. jchar += siz_chans - 1;
  1438. }
  1439. for (; bufnow <= bufend; jchar += siz_chans)
  1440. (*bufnow++) = int24_to_sample(jchar);
  1441. break;
  1442. default: break;
  1443. }
  1444. loc = loclim;
  1445. }
  1446. }
  1447. }
  1448. }
  1449. }
  1450. return(total_read);
  1451. }
  1452. mus_long_t mus_file_read_any(int tfd, mus_long_t beg, int chans, mus_long_t nints, mus_float_t **bufs, mus_float_t **cm)
  1453. {
  1454. return(mus_read_any_1(tfd, beg, chans, nints, bufs, cm, NULL));
  1455. }
  1456. mus_long_t mus_file_read_file(int tfd, mus_long_t beg, int chans, mus_long_t nints, mus_float_t **bufs)
  1457. {
  1458. /* not currently used anywhere */
  1459. return(mus_read_any_1(tfd, beg, chans, nints, bufs, NULL, NULL));
  1460. }
  1461. mus_long_t mus_file_read_buffer(int charbuf_sample_type, mus_long_t beg, int chans, mus_long_t nints, mus_float_t **bufs, char *charbuf)
  1462. {
  1463. return(mus_read_any_1(charbuf_sample_type, beg, chans, nints, bufs, NULL, charbuf));
  1464. }
  1465. /* the next two were changed 19-Dec-13 (sndlib.h version 23.1)
  1466. * "end" is now actually "dur" -- the number of samples to read, not the end point in the buffer
  1467. * "beg" is the frample number to start at in the data, not the buffer location
  1468. * so old form
  1469. * mus_file_read(f, 0, 99...)
  1470. * is now
  1471. * mus_file_read(f, frample, 100...)
  1472. */
  1473. mus_long_t mus_file_read(int tfd, mus_long_t beg, mus_long_t num, int chans, mus_float_t **bufs)
  1474. {
  1475. mus_long_t rtn;
  1476. rtn = mus_read_any_1(tfd, beg, chans, num, bufs, NULL, NULL);
  1477. if (rtn == MUS_ERROR) return(MUS_ERROR);
  1478. if (rtn < num)
  1479. {
  1480. mus_long_t k;
  1481. /* this zeroing can be fooled if the file is chunked and has trailing, non-data chunks */
  1482. for (k = 0; k < chans; k++)
  1483. {
  1484. mus_float_t *buffer;
  1485. buffer = bufs[k];
  1486. /* this happens routinely in mus_outa + initial write (reads ahead in effect) */
  1487. /* fprintf(stderr, "clear from %lld for %lld\n", rtn, num-rtn); */
  1488. memset((void *)(buffer + rtn), 0, (num - rtn) * sizeof(mus_float_t));
  1489. }
  1490. }
  1491. return(num);
  1492. }
  1493. mus_long_t mus_file_read_chans(int tfd, mus_long_t beg, mus_long_t num, int chans, mus_float_t **bufs, mus_float_t **cm)
  1494. {
  1495. /* an optimization of mus_file_read -- just reads the desired channels */
  1496. mus_long_t rtn, k;
  1497. rtn = mus_read_any_1(tfd, beg, chans, num, bufs, cm, NULL);
  1498. if (rtn == MUS_ERROR) return(MUS_ERROR);
  1499. if (rtn < num)
  1500. for (k = 0; k < chans; k++)
  1501. if ((cm == NULL) || (cm[k]))
  1502. {
  1503. mus_float_t *buffer;
  1504. buffer = bufs[k];
  1505. memset((void *)(buffer + rtn), 0, (num - rtn) * sizeof(mus_float_t));
  1506. }
  1507. return(num);
  1508. }
  1509. /* ---------------- write ---------------- */
  1510. static int checked_write(int tfd, char *buf, mus_long_t chars)
  1511. {
  1512. long long int bytes;
  1513. bytes = (long long int)write(tfd, buf, chars);
  1514. if (bytes != chars)
  1515. {
  1516. io_fd *fd;
  1517. if ((io_fds == NULL) || (tfd >= io_fd_size) || (tfd < 0) || (io_fds[tfd] == NULL))
  1518. return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED, "mus_write: no file descriptors!"));
  1519. fd = io_fds[tfd];
  1520. if (fd->sample_type == MUS_UNKNOWN_SAMPLE)
  1521. return(mus_error(MUS_FILE_CLOSED,
  1522. "attempt to write closed file %s",
  1523. fd->name));
  1524. else
  1525. return(mus_error(MUS_WRITE_ERROR,
  1526. "mus_write: write error for %s%s%s: only %lld of %lld" " bytes written",
  1527. fd->name, (errno) ? ": " : "", (errno) ? STRERROR(errno) : "",
  1528. bytes, chars));
  1529. }
  1530. return(MUS_NO_ERROR);
  1531. }
  1532. static mus_clip_handler_t *mus_clip_handler = NULL;
  1533. static bool (*clip_checker)(void) = NULL;
  1534. mus_clip_handler_t *mus_clip_set_handler(mus_clip_handler_t *new_clip_handler)
  1535. {
  1536. mus_clip_handler_t *old_handler;
  1537. old_handler = mus_clip_handler;
  1538. mus_clip_handler = new_clip_handler;
  1539. return(old_handler);
  1540. }
  1541. mus_clip_handler_t *mus_clip_set_handler_and_checker(mus_clip_handler_t *new_clip_handler, bool (*checker)(void))
  1542. {
  1543. mus_clip_handler_t *old_handler;
  1544. old_handler = mus_clip_handler;
  1545. mus_clip_handler = new_clip_handler;
  1546. clip_checker = checker;
  1547. return(old_handler);
  1548. }
  1549. static int mus_write_1(int tfd, mus_long_t beg, mus_long_t end, int chans, mus_float_t **bufs, char *inbuf, bool clipped)
  1550. {
  1551. int err, siz, siz_chans, val;
  1552. mus_sample_t sample_type;
  1553. mus_long_t bytes, k, lim, leftover, loc, buflim;
  1554. bool clipping = false;
  1555. unsigned char *jchar;
  1556. static char *ur_charbuf = NULL;
  1557. char *charbuf = NULL;
  1558. mus_float_t *buffer;
  1559. if (chans <= 0) return(MUS_NO_ERROR); /* ?? */
  1560. if (!inbuf)
  1561. {
  1562. io_fd *fd;
  1563. if ((io_fds == NULL) ||
  1564. (tfd >= io_fd_size) ||
  1565. (tfd < 0) ||
  1566. (io_fds[tfd] == NULL))
  1567. return(mus_error(MUS_FILE_DESCRIPTORS_NOT_INITIALIZED, "mus_write: no file descriptors!"));
  1568. fd = io_fds[tfd];
  1569. if (fd->sample_type == MUS_UNKNOWN_SAMPLE)
  1570. return(mus_error(MUS_FILE_CLOSED, "mus_write: invalid sample type for %s", fd->name));
  1571. siz = fd->bytes_per_sample;
  1572. sample_type = fd->sample_type;
  1573. clipping = fd->clipping;
  1574. if ((sample_type == MUS_OUT_SAMPLE_TYPE) &&
  1575. (chans == 1) &&
  1576. (!clipping) &&
  1577. (beg == 0))
  1578. {
  1579. bytes = (end + 1) * siz;
  1580. return(checked_write(tfd, (char *)(bufs[0]), bytes));
  1581. }
  1582. }
  1583. else
  1584. {
  1585. siz = mus_bytes_per_sample((mus_sample_t)tfd);
  1586. sample_type = (mus_sample_t)tfd; /* in this case, tfd is the sample type (see mus_file_write_buffer below) -- this should be changed! */
  1587. clipping = clipped;
  1588. }
  1589. lim = (end - beg + 1);
  1590. siz_chans = siz * chans;
  1591. leftover = lim * siz_chans;
  1592. k = (BUFLIM) % siz_chans;
  1593. if (k != 0)
  1594. buflim = (BUFLIM) - k;
  1595. else buflim = BUFLIM;
  1596. loc = beg;
  1597. if (inbuf)
  1598. charbuf = inbuf;
  1599. while (leftover > 0)
  1600. {
  1601. int oldloc;
  1602. bytes = leftover;
  1603. if (bytes > buflim)
  1604. {
  1605. leftover = (bytes - buflim);
  1606. bytes = buflim;
  1607. }
  1608. else leftover = 0;
  1609. lim = (int)(bytes / siz_chans); /* see note above */
  1610. oldloc = loc;
  1611. for (k = 0; k < chans; k++)
  1612. {
  1613. mus_long_t loclim;
  1614. mus_float_t *bufnow, *bufend, *bufend4;
  1615. if (bufs[k] == NULL) continue;
  1616. loc = oldloc;
  1617. buffer = (mus_float_t *)(bufs[k]);
  1618. if (clipping)
  1619. {
  1620. int clipend;
  1621. mus_float_t sample;
  1622. clipend = oldloc + lim;
  1623. bufnow = (mus_float_t *)(buffer + oldloc);
  1624. bufend = (mus_float_t *)(buffer + clipend - 1);
  1625. bufend4 = (mus_float_t *)(bufend - 4);
  1626. if (clip_checker) clip_checker();
  1627. if (mus_clip_handler)
  1628. {
  1629. for (; bufnow <= bufend; bufnow++)
  1630. {
  1631. sample = (*bufnow);
  1632. if ((sample >= 1.0) || (sample < -1.0)) (*bufnow) = (*mus_clip_handler)(sample);
  1633. }
  1634. }
  1635. else
  1636. {
  1637. while (bufnow <= bufend4)
  1638. {
  1639. sample = (*bufnow);
  1640. if (sample >= 1.0) (*bufnow) = 0.99999; else if (sample < -1.0) (*bufnow) = -1.0;
  1641. bufnow++;
  1642. sample = (*bufnow);
  1643. if (sample >= 1.0) (*bufnow) = 0.99999; else if (sample < -1.0) (*bufnow) = -1.0;
  1644. bufnow++;
  1645. sample = (*bufnow);
  1646. if (sample >= 1.0) (*bufnow) = 0.99999; else if (sample < -1.0) (*bufnow) = -1.0;
  1647. bufnow++;
  1648. sample = (*bufnow);
  1649. if (sample >= 1.0) (*bufnow) = 0.99999; else if (sample < -1.0) (*bufnow) = -1.0;
  1650. bufnow++;
  1651. }
  1652. for (; bufnow <= bufend; bufnow++)
  1653. {
  1654. sample = (*bufnow);
  1655. if (sample >= 1.0) (*bufnow) = 0.99999; else if (sample < -1.0) (*bufnow) = -1.0;
  1656. }
  1657. }
  1658. }
  1659. if ((sample_type == MUS_OUT_SAMPLE_TYPE) &&
  1660. (chans == 1) &&
  1661. (beg == 0) &&
  1662. (!inbuf)) /* "tfd" can be sample type */
  1663. {
  1664. bytes = (end + 1) * siz;
  1665. return(checked_write(tfd, (char *)(bufs[0]), bytes));
  1666. }
  1667. loclim = loc + lim;
  1668. if (!charbuf)
  1669. {
  1670. if (ur_charbuf == NULL)
  1671. ur_charbuf = (char *)malloc(BUFLIM * sizeof(char));
  1672. /*
  1673. ur_charbuf = (char *)calloc(BUFLIM, sizeof(char));
  1674. else memset((void *)ur_charbuf, 0, BUFLIM);
  1675. */
  1676. charbuf = ur_charbuf;
  1677. }
  1678. bufnow = (mus_float_t *)(buffer + loc);
  1679. bufend = (mus_float_t *)(buffer + loclim - 1);
  1680. bufend4 = (mus_float_t *)(bufend - 4);
  1681. jchar = (unsigned char *)charbuf; /* if to_buffer we should add the loop offset here, or never loop */
  1682. jchar += (k * siz);
  1683. switch (sample_type)
  1684. {
  1685. case MUS_BSHORT:
  1686. while (bufnow <= bufend4)
  1687. {
  1688. set_big_endian_short(jchar, mus_sample_to_short(*bufnow++));
  1689. jchar += siz_chans;
  1690. set_big_endian_short(jchar, mus_sample_to_short(*bufnow++));
  1691. jchar += siz_chans;
  1692. set_big_endian_short(jchar, mus_sample_to_short(*bufnow++));
  1693. jchar += siz_chans;
  1694. set_big_endian_short(jchar, mus_sample_to_short(*bufnow++));
  1695. jchar += siz_chans;
  1696. }
  1697. for (; bufnow <= bufend; jchar += siz_chans)
  1698. set_big_endian_short(jchar, mus_sample_to_short(*bufnow++));
  1699. break;
  1700. case MUS_LSHORT:
  1701. for (; bufnow <= bufend; jchar += siz_chans)
  1702. set_little_endian_short(jchar, mus_sample_to_short(*bufnow++));
  1703. break;
  1704. case MUS_BINT:
  1705. for (; bufnow <= bufend; jchar += siz_chans)
  1706. set_big_endian_int(jchar, mus_sample_to_int(*bufnow++));
  1707. break;
  1708. case MUS_LINT:
  1709. for (; bufnow <= bufend; jchar += siz_chans)
  1710. set_little_endian_int(jchar, mus_sample_to_int(*bufnow++));
  1711. break;
  1712. case MUS_BINTN:
  1713. for (; bufnow <= bufend; jchar += siz_chans)
  1714. set_big_endian_int(jchar, mus_sample_to_int(*bufnow++) << 8);
  1715. break;
  1716. case MUS_LINTN:
  1717. for (; bufnow <= bufend; jchar += siz_chans)
  1718. set_little_endian_int(jchar, mus_sample_to_int(*bufnow++) << 8);
  1719. break;
  1720. case MUS_MULAW:
  1721. for (; bufnow <= bufend; jchar += siz_chans)
  1722. (*jchar) = to_mulaw(mus_sample_to_short(*bufnow++));
  1723. break;
  1724. case MUS_ALAW:
  1725. for (; bufnow <= bufend; jchar += siz_chans)
  1726. (*jchar) = to_alaw(mus_sample_to_short(*bufnow++));
  1727. break;
  1728. case MUS_BYTE:
  1729. for (; bufnow <= bufend; jchar += siz_chans)
  1730. (*((signed char *)jchar)) = mus_sample_to_byte(*bufnow++);
  1731. break;
  1732. case MUS_UBYTE:
  1733. for (; bufnow <= bufend; jchar += siz_chans)
  1734. (*jchar) = mus_sample_to_byte(*bufnow++) + UBYTE_ZERO;
  1735. break;
  1736. case MUS_BFLOAT:
  1737. for (; bufnow <= bufend; jchar += siz_chans)
  1738. set_big_endian_float(jchar, *bufnow++);
  1739. break;
  1740. case MUS_LFLOAT:
  1741. while (bufnow <= bufend4)
  1742. {
  1743. set_little_endian_float(jchar, *bufnow++);
  1744. jchar += siz_chans;
  1745. set_little_endian_float(jchar, *bufnow++);
  1746. jchar += siz_chans;
  1747. set_little_endian_float(jchar, *bufnow++);
  1748. jchar += siz_chans;
  1749. set_little_endian_float(jchar, *bufnow++);
  1750. jchar += siz_chans;
  1751. }
  1752. for (; bufnow <= bufend; jchar += siz_chans)
  1753. set_little_endian_float(jchar, *bufnow++);
  1754. break;
  1755. case MUS_BDOUBLE:
  1756. for (; bufnow <= bufend; jchar += siz_chans)
  1757. set_big_endian_double(jchar, *bufnow++);
  1758. break;
  1759. case MUS_LDOUBLE:
  1760. while (bufnow <= bufend4)
  1761. {
  1762. set_little_endian_double(jchar, *bufnow++);
  1763. jchar += siz_chans;
  1764. set_little_endian_double(jchar, *bufnow++);
  1765. jchar += siz_chans;
  1766. set_little_endian_double(jchar, *bufnow++);
  1767. jchar += siz_chans;
  1768. set_little_endian_double(jchar, *bufnow++);
  1769. jchar += siz_chans;
  1770. }
  1771. for (; bufnow <= bufend; jchar += siz_chans)
  1772. set_little_endian_double(jchar, *bufnow++);
  1773. break;
  1774. case MUS_BFLOAT_UNSCALED:
  1775. for (; bufnow <= bufend; jchar += siz_chans)
  1776. set_big_endian_float(jchar, 32768.0 * (*bufnow++));
  1777. break;
  1778. case MUS_LFLOAT_UNSCALED:
  1779. for (; bufnow <= bufend; jchar += siz_chans)
  1780. set_little_endian_float(jchar, 32768.0 * (*bufnow++));
  1781. break;
  1782. case MUS_BDOUBLE_UNSCALED:
  1783. for (; bufnow <= bufend; jchar += siz_chans)
  1784. set_big_endian_double(jchar, 32768.0 * (*bufnow++));
  1785. break;
  1786. case MUS_LDOUBLE_UNSCALED:
  1787. for (; bufnow <= bufend; jchar += siz_chans)
  1788. set_little_endian_double(jchar, 32768.0 * (*bufnow++));
  1789. break;
  1790. case MUS_UBSHORT:
  1791. for (; bufnow <= bufend; jchar += siz_chans)
  1792. set_big_endian_unsigned_short(jchar, (unsigned short)(mus_sample_to_short(*bufnow++) + USHORT_ZERO));
  1793. break;
  1794. case MUS_ULSHORT:
  1795. for (; bufnow <= bufend; jchar += siz_chans)
  1796. set_little_endian_unsigned_short(jchar, (unsigned short)(mus_sample_to_short(*bufnow++) + USHORT_ZERO));
  1797. break;
  1798. case MUS_B24INT:
  1799. {
  1800. int c3;
  1801. mus_long_t bk;
  1802. bk = (k * 3);
  1803. c3 = chans * 3;
  1804. for (; bufnow <= bufend; bk += c3)
  1805. {
  1806. val = mus_sample_to_int24(*bufnow++);
  1807. charbuf[bk] = (val >> 16);
  1808. charbuf[bk + 1] = (val >> 8);
  1809. charbuf[bk + 2] = (val & 0xFF);
  1810. }
  1811. }
  1812. break;
  1813. case MUS_L24INT:
  1814. {
  1815. int c3;
  1816. mus_long_t bk;
  1817. bk = (k * 3);
  1818. c3 = chans * 3;
  1819. for (; bufnow <= bufend; bk += c3)
  1820. {
  1821. val = mus_sample_to_int24(*bufnow++);
  1822. charbuf[bk + 2] = (val >> 16);
  1823. charbuf[bk + 1] = (val >> 8);
  1824. charbuf[bk] = (val & 0xFF);
  1825. }
  1826. }
  1827. break;
  1828. default: break;
  1829. }
  1830. loc = loclim;
  1831. }
  1832. if (!inbuf)
  1833. {
  1834. err = checked_write(tfd, charbuf, bytes);
  1835. if (err == MUS_ERROR)
  1836. return(MUS_ERROR);
  1837. }
  1838. }
  1839. return(MUS_NO_ERROR);
  1840. }
  1841. int mus_file_write(int tfd, mus_long_t beg, mus_long_t end, int chans, mus_float_t **bufs)
  1842. {
  1843. return(mus_write_1(tfd, beg, end, chans, bufs, NULL, false)); /* if inbuf is NULL (as here), clipped is ignored, so the "false" could be anything */
  1844. }
  1845. int mus_file_write_file(int tfd, mus_long_t beg, mus_long_t end, int chans, mus_float_t **bufs)
  1846. {
  1847. return(mus_write_1(tfd, beg, end, chans, bufs, NULL, false));
  1848. }
  1849. int mus_file_write_buffer(int charbuf_sample_type, mus_long_t beg, mus_long_t end, int chans, mus_float_t **bufs, char *charbuf, bool clipped)
  1850. {
  1851. return(mus_write_1(charbuf_sample_type, beg, end, chans, bufs, charbuf, clipped));
  1852. }
  1853. /* for CLM */
  1854. void mus_reset_io_c(void)
  1855. {
  1856. io_fd_size = 0;
  1857. io_fds = NULL;
  1858. clipping_default = false;
  1859. mus_clip_set_handler(NULL);
  1860. }
  1861. #if !(defined(_MSC_VER) && (!(defined(__CYGWIN__))))
  1862. static int sndlib_strlen(const char *str)
  1863. {
  1864. /* strlen(NULL) -> seg fault! */
  1865. if ((str) && (*str)) return(strlen(str));
  1866. return(0);
  1867. }
  1868. #endif
  1869. static char *saved_cwd = NULL;
  1870. char *mus_getcwd(void)
  1871. {
  1872. int i, path_max = 0;
  1873. char *pwd = NULL;
  1874. if (saved_cwd) return(saved_cwd);
  1875. #ifndef _MSC_VER
  1876. path_max = pathconf("/", _PC_PATH_MAX);
  1877. #endif
  1878. if (path_max < 1024)
  1879. {
  1880. #if defined(PATH_MAX)
  1881. path_max = PATH_MAX;
  1882. #endif
  1883. if (path_max < 1024)
  1884. path_max = 1024;
  1885. }
  1886. for (i = path_max;; i *= 2)
  1887. {
  1888. char *res;
  1889. if (pwd) free(pwd);
  1890. pwd = (char *)calloc(i, sizeof(char));
  1891. #if (defined(_MSC_VER) || __CYGWIN__)
  1892. res = _getcwd(pwd, i);
  1893. #else
  1894. res = getcwd(pwd, i);
  1895. #endif
  1896. if (res) break; /* NULL is returned if failure, but what about success? should I check errno=ERANGE? */
  1897. }
  1898. saved_cwd = pwd;
  1899. return(pwd);
  1900. }
  1901. #if HAVE_EXTENSION_LANGUAGE
  1902. #include "sndlib2xen.h"
  1903. /* for g_mus_sound_path */
  1904. #endif
  1905. char *mus_expand_filename(const char *filename)
  1906. {
  1907. /* fill out under-specified library pathnames etc */
  1908. #if defined(_MSC_VER) && (!(defined(__CYGWIN__)))
  1909. return(mus_strdup(filename));
  1910. #else
  1911. char *file_name_buf;
  1912. char *tok, *orig;
  1913. int i, j, len, orig_len;
  1914. /* realpath does not speed this up */
  1915. if ((filename) && (*filename))
  1916. len = strlen(filename);
  1917. else return(NULL);
  1918. if ((len == 1) && (filename[0] == '.'))
  1919. {
  1920. orig = mus_getcwd();
  1921. orig_len = sndlib_strlen(orig);
  1922. file_name_buf = (char *)malloc((orig_len + 4) * sizeof(char));
  1923. strcpy(file_name_buf, orig);
  1924. file_name_buf[orig_len] = '/';
  1925. file_name_buf[orig_len + 1] = '\0';
  1926. /* fprintf(stderr, "%d: %s -> %s\n", __LINE__, filename, file_name_buf); */
  1927. return(file_name_buf);
  1928. }
  1929. tok = (char *)strchr(filename, (int)'/');
  1930. if (!tok)
  1931. {
  1932. orig = mus_getcwd();
  1933. orig_len = sndlib_strlen(orig);
  1934. file_name_buf = (char *)malloc((len + orig_len + 4) * sizeof(char));
  1935. strcpy(file_name_buf, orig);
  1936. file_name_buf[orig_len] = '/';
  1937. strncpy((char *)(file_name_buf + orig_len + 1), filename, len + 1);
  1938. /* fprintf(stderr, "%d: %s -> %s\n", __LINE__, filename, file_name_buf); */
  1939. #if HAVE_EXTENSION_LANGUAGE
  1940. if (!mus_file_probe(file_name_buf))
  1941. {
  1942. Xen p;
  1943. for (p = g_mus_sound_path(); Xen_is_pair(p); p = Xen_cdr(p))
  1944. {
  1945. char *nfile;
  1946. const char *path;
  1947. int path_len;
  1948. path = Xen_string_to_C_string(Xen_car(p));
  1949. path_len = sndlib_strlen(path);
  1950. nfile = (char *)malloc((len + path_len + 4) * sizeof(char));
  1951. strcpy(nfile, path);
  1952. if (nfile[path_len - 1] != '/')
  1953. {
  1954. nfile[path_len] = '/';
  1955. strncpy((char *)(nfile + path_len + 1), filename, len + 1);
  1956. }
  1957. else strncpy((char *)(nfile + path_len), filename, len + 1);
  1958. /* fprintf(stderr, "%d: %s -> %s\n", __LINE__, filename, nfile); */
  1959. if (mus_file_probe(nfile))
  1960. {
  1961. free(file_name_buf);
  1962. return(nfile);
  1963. }
  1964. free(nfile);
  1965. }
  1966. }
  1967. #endif
  1968. return(file_name_buf);
  1969. }
  1970. orig = mus_strdup(filename);
  1971. tok = orig;
  1972. /* get rid of "//" */
  1973. for (i = 0, j = 0; i < len - 1; i++)
  1974. {
  1975. if ((tok[i] == '/') &&
  1976. (tok[i + 1] == '/'))
  1977. j = i + 1;
  1978. }
  1979. if (j > 0)
  1980. {
  1981. for (i = 0; j < len; i++, j++)
  1982. tok[i] = tok[j];
  1983. tok[i] ='\0';
  1984. }
  1985. /* get rid of "~/" at start */
  1986. if (tok[0] != '/')
  1987. {
  1988. char *home = NULL;
  1989. if ((tok[0] == '~') && (home = getenv("HOME")))
  1990. {
  1991. file_name_buf = (char *)malloc((len + sndlib_strlen(home) + 8) * sizeof(char));
  1992. strcpy(file_name_buf, home);
  1993. strcat(file_name_buf, ++tok);
  1994. }
  1995. else
  1996. {
  1997. char *pwd;
  1998. pwd = mus_getcwd();
  1999. file_name_buf = (char *)malloc((len + sndlib_strlen(pwd) + 8) * sizeof(char));
  2000. strcpy(file_name_buf, pwd);
  2001. strcat(file_name_buf, "/");
  2002. if (tok[0])
  2003. strcat(file_name_buf, tok);
  2004. }
  2005. }
  2006. else
  2007. {
  2008. file_name_buf = (char *)malloc((len + 8) * sizeof(char));
  2009. strcpy(file_name_buf, tok);
  2010. }
  2011. /* get rid of "/../" and "/./" also "/." at end */
  2012. {
  2013. int slash_at = -1;
  2014. bool found_one = true;
  2015. while (found_one)
  2016. {
  2017. found_one = false;
  2018. len = strlen(file_name_buf);
  2019. for (i = 0; i < len - 3; i++)
  2020. if (file_name_buf[i] == '/')
  2021. {
  2022. if ((file_name_buf[i + 1] == '.') &&
  2023. (file_name_buf[i + 2] == '.') &&
  2024. (file_name_buf[i + 3] == '/'))
  2025. {
  2026. i += 4;
  2027. for (j = slash_at + 1; i < len; i++, j++)
  2028. file_name_buf[j] = file_name_buf[i];
  2029. file_name_buf[j] = '\0';
  2030. found_one = true;
  2031. break;
  2032. }
  2033. else
  2034. {
  2035. if ((file_name_buf[i + 1] == '.') &&
  2036. (file_name_buf[i + 2] == '/'))
  2037. {
  2038. for (j = i + 3, i = i + 1; j < len; i++, j++)
  2039. file_name_buf[i] = file_name_buf[j];
  2040. file_name_buf[i] = '\0';
  2041. found_one = true;
  2042. }
  2043. else slash_at = i;
  2044. }
  2045. }
  2046. }
  2047. len = strlen(file_name_buf);
  2048. if ((len > 1) &&
  2049. (file_name_buf[len - 1] == '.') &&
  2050. (file_name_buf[len - 2] == '/'))
  2051. file_name_buf[len - 1] = '\0';
  2052. }
  2053. free(orig);
  2054. return(file_name_buf);
  2055. #endif
  2056. }
  2057. char *mus_format(const char *format, ...)
  2058. {
  2059. /* caller should free result */
  2060. #ifndef _MSC_VER
  2061. va_list ap;
  2062. int bytes;
  2063. char *result = NULL;
  2064. va_start(ap, format);
  2065. bytes = vasprintf(&result, format, ap);
  2066. va_end(ap);
  2067. if (bytes == -1)
  2068. return(NULL);
  2069. return(result);
  2070. #else
  2071. #define MUS_FORMAT_BUFFER_SIZE 256
  2072. char *buf = NULL;
  2073. int needed_bytes = 0;
  2074. va_list ap;
  2075. buf = (char *)calloc(MUS_FORMAT_BUFFER_SIZE, sizeof(char));
  2076. va_start(ap, format);
  2077. needed_bytes = vsnprintf(buf, MUS_FORMAT_BUFFER_SIZE, format, ap);
  2078. va_end(ap);
  2079. if (needed_bytes >= MUS_FORMAT_BUFFER_SIZE) /* "=" here because we need room for the trailing 0 */
  2080. {
  2081. free(buf);
  2082. buf = (char *)calloc(needed_bytes + 1, sizeof(char));
  2083. va_start(ap, format);
  2084. vsnprintf(buf, needed_bytes + 1, format, ap);
  2085. va_end(ap);
  2086. }
  2087. return(buf);
  2088. #endif
  2089. }
  2090. mus_float_t mus_fclamp(mus_float_t lo, mus_float_t val, mus_float_t hi)
  2091. {
  2092. if (val > hi)
  2093. return(hi);
  2094. else
  2095. if (val < lo)
  2096. return(lo);
  2097. else return(val);
  2098. }
  2099. int mus_iclamp(int lo, int val, int hi)
  2100. {
  2101. if (val > hi)
  2102. return(hi);
  2103. else
  2104. if (val < lo)
  2105. return(lo);
  2106. else return(val);
  2107. }
  2108. mus_long_t mus_oclamp(mus_long_t lo, mus_long_t val, mus_long_t hi)
  2109. {
  2110. if (val > hi)
  2111. return(hi);
  2112. else
  2113. if (val < lo)
  2114. return(lo);
  2115. else return(val);
  2116. }
  2117. /* raw sample data peak value (without per-sample conversion) */
  2118. /* ---------------- short ---------------- */
  2119. #define SHORT_BYTES 2
  2120. static void min_max_shorts(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2121. {
  2122. short cur_min, cur_max, tmp;
  2123. short *sbuf;
  2124. int i, len, len2;
  2125. sbuf = (short *)data;
  2126. len = bytes / SHORT_BYTES;
  2127. cur_min = sbuf[chan];
  2128. cur_max = cur_min;
  2129. len2 = len - 2 * chans;
  2130. i = chan; /* we read the first sample above, but we're reading by twos below */
  2131. while (i <= len2)
  2132. {
  2133. tmp = sbuf[i];
  2134. if (tmp < cur_min) cur_min = tmp; else if (tmp > cur_max) cur_max = tmp;
  2135. i += chans;
  2136. tmp = sbuf[i];
  2137. if (tmp < cur_min) cur_min = tmp; else if (tmp > cur_max) cur_max = tmp;
  2138. i += chans;
  2139. }
  2140. if (i < len)
  2141. {
  2142. tmp = sbuf[i];
  2143. if (tmp < cur_min) cur_min = tmp; else if (tmp > cur_max) cur_max = tmp;
  2144. }
  2145. (*min_samp) = (mus_float_t)cur_min / (mus_float_t)(1 << 15);
  2146. (*max_samp) = (mus_float_t)cur_max / (mus_float_t)(1 << 15);
  2147. }
  2148. static void min_max_switch_shorts(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2149. {
  2150. short cur_min, cur_max;
  2151. /* frample based */
  2152. unsigned char *samp, *eod, *eod2;
  2153. int bytes_per_frample;
  2154. bytes_per_frample = chans * SHORT_BYTES;
  2155. eod = (unsigned char *)(data + bytes);
  2156. eod2 = (unsigned char *)(eod - 2 * bytes_per_frample);
  2157. #if MUS_LITTLE_ENDIAN
  2158. cur_min = big_endian_short((unsigned char *)(data + (chan * SHORT_BYTES)));
  2159. #else
  2160. cur_min = little_endian_short((unsigned char *)(data + (chan * SHORT_BYTES)));
  2161. #endif
  2162. cur_max = cur_min;
  2163. samp = (unsigned char *)(data + (chan * SHORT_BYTES));
  2164. while (samp <= eod2)
  2165. {
  2166. short val;
  2167. #if MUS_LITTLE_ENDIAN
  2168. val = big_endian_short(samp);
  2169. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2170. samp += bytes_per_frample;
  2171. val = big_endian_short(samp);
  2172. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2173. samp += bytes_per_frample;
  2174. #else
  2175. val = little_endian_short(samp);
  2176. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2177. samp += bytes_per_frample;
  2178. val = little_endian_short(samp);
  2179. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2180. samp += bytes_per_frample;
  2181. #endif
  2182. }
  2183. if (samp < eod)
  2184. {
  2185. short val;
  2186. #if MUS_LITTLE_ENDIAN
  2187. val = big_endian_short(samp);
  2188. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2189. #else
  2190. val = little_endian_short(samp);
  2191. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2192. #endif
  2193. }
  2194. (*min_samp) = (mus_float_t)cur_min / (mus_float_t)(1 << 15);
  2195. (*max_samp) = (mus_float_t)cur_max / (mus_float_t)(1 << 15);
  2196. }
  2197. /* ---------------- unsigned short ---------------- */
  2198. static void min_max_ushorts(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2199. {
  2200. unsigned short cur_min, cur_max;
  2201. unsigned short *sbuf;
  2202. int i, len, len2;
  2203. sbuf = (unsigned short *)data;
  2204. len = bytes / SHORT_BYTES;
  2205. len2 = len - 2 * chans;
  2206. cur_min = sbuf[chan];
  2207. cur_max = cur_min;
  2208. i = chan;
  2209. while (i <= len2)
  2210. {
  2211. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2212. i += chans;
  2213. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2214. i += chans;
  2215. }
  2216. if (i < len)
  2217. {
  2218. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2219. }
  2220. (*min_samp) = (mus_float_t)(cur_min - USHORT_ZERO) / (mus_float_t)(1 << 15);
  2221. (*max_samp) = (mus_float_t)(cur_max - USHORT_ZERO) / (mus_float_t)(1 << 15);
  2222. }
  2223. static void min_max_switch_ushorts(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2224. {
  2225. unsigned short cur_min, cur_max;
  2226. /* frample based */
  2227. unsigned char *samp, *eod, *eod2;
  2228. int bytes_per_frample;
  2229. bytes_per_frample = chans * SHORT_BYTES;
  2230. eod = (unsigned char *)(data + bytes);
  2231. eod2 = (unsigned char *)(eod - 2 * bytes_per_frample);
  2232. #if MUS_LITTLE_ENDIAN
  2233. cur_min = big_endian_unsigned_short((unsigned char *)(data + (chan * SHORT_BYTES)));
  2234. #else
  2235. cur_min = little_endian_unsigned_short((unsigned char *)(data + (chan * SHORT_BYTES)));
  2236. #endif
  2237. cur_max = cur_min;
  2238. samp = (unsigned char *)(data + (chan * SHORT_BYTES));
  2239. while (samp <= eod2)
  2240. {
  2241. unsigned short val;
  2242. #if MUS_LITTLE_ENDIAN
  2243. val = big_endian_unsigned_short(samp);
  2244. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2245. samp += bytes_per_frample;
  2246. val = big_endian_unsigned_short(samp);
  2247. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2248. samp += bytes_per_frample;
  2249. #else
  2250. val = little_endian_unsigned_short(samp);
  2251. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2252. samp += bytes_per_frample;
  2253. val = little_endian_unsigned_short(samp);
  2254. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2255. samp += bytes_per_frample;
  2256. #endif
  2257. }
  2258. if (samp < eod)
  2259. {
  2260. unsigned short val;
  2261. #if MUS_LITTLE_ENDIAN
  2262. val = big_endian_unsigned_short(samp);
  2263. #else
  2264. val = little_endian_unsigned_short(samp);
  2265. #endif
  2266. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2267. }
  2268. (*min_samp) = (mus_float_t)(cur_min - USHORT_ZERO) / (mus_float_t)(1 << 15);
  2269. (*max_samp) = (mus_float_t)(cur_max - USHORT_ZERO) / (mus_float_t)(1 << 15);
  2270. }
  2271. /* ---------------- int ---------------- */
  2272. #define INT_BYTES 4
  2273. static void min_max_ints(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp, bool standard)
  2274. {
  2275. int cur_min, cur_max;
  2276. int *sbuf;
  2277. int i, len, len2;
  2278. sbuf = (int *)data;
  2279. len = bytes / INT_BYTES;
  2280. len2 = len - 2 *chans;
  2281. cur_min = sbuf[chan];
  2282. cur_max = cur_min;
  2283. i = chan;
  2284. while (i <= len2)
  2285. {
  2286. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2287. i += chans;
  2288. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2289. i += chans;
  2290. }
  2291. if (i < len)
  2292. {
  2293. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2294. }
  2295. (*min_samp) = (mus_float_t)cur_min / (mus_float_t)(1 << 23);
  2296. (*max_samp) = (mus_float_t)cur_max / (mus_float_t)(1 << 23);
  2297. if (!standard)
  2298. {
  2299. (*min_samp) = (mus_float_t)(*min_samp) / (mus_float_t)(1 << 8);
  2300. (*max_samp) = (mus_float_t)(*max_samp) / (mus_float_t)(1 << 8);
  2301. }
  2302. }
  2303. /* (with-sound (:sample-type mus-lintn :statistics #t) (fm-violin 0 1 440 .1)) */
  2304. static void min_max_switch_ints(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp, bool standard)
  2305. {
  2306. int cur_min, cur_max;
  2307. /* frample based */
  2308. unsigned char *samp, *eod, *eod2;
  2309. int bytes_per_frample;
  2310. bytes_per_frample = chans * INT_BYTES;
  2311. eod = (unsigned char *)(data + bytes);
  2312. eod2 = (unsigned char *)(eod - 2 * bytes_per_frample);
  2313. #if MUS_LITTLE_ENDIAN
  2314. cur_min = big_endian_int((unsigned char *)(data + (chan * INT_BYTES)));
  2315. #else
  2316. cur_min = little_endian_int((unsigned char *)(data + (chan * INT_BYTES)));
  2317. #endif
  2318. cur_max = cur_min;
  2319. samp = (unsigned char *)(data + (chan * INT_BYTES));
  2320. while (samp <= eod2)
  2321. {
  2322. int val;
  2323. #if MUS_LITTLE_ENDIAN
  2324. val = big_endian_int(samp);
  2325. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2326. samp += bytes_per_frample;
  2327. val = big_endian_int(samp);
  2328. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2329. samp += bytes_per_frample;
  2330. #else
  2331. val = little_endian_int(samp);
  2332. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2333. samp += bytes_per_frample;
  2334. val = little_endian_int(samp);
  2335. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2336. samp += bytes_per_frample;
  2337. #endif
  2338. }
  2339. if (samp < eod)
  2340. {
  2341. int val;
  2342. #if MUS_LITTLE_ENDIAN
  2343. val = big_endian_int(samp);
  2344. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2345. #else
  2346. val = little_endian_int(samp);
  2347. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2348. #endif
  2349. }
  2350. (*min_samp) = (mus_float_t)cur_min / (mus_float_t)(1 << 23);
  2351. (*max_samp) = (mus_float_t)cur_max / (mus_float_t)(1 << 23);
  2352. if (!standard)
  2353. {
  2354. (*min_samp) = (mus_float_t)(*min_samp) / (mus_float_t)(1 << 8);
  2355. (*max_samp) = (mus_float_t)(*max_samp) / (mus_float_t)(1 << 8);
  2356. }
  2357. }
  2358. /* ---------------- float ---------------- */
  2359. #define FLOAT_BYTES 4
  2360. static void min_max_floats(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp, bool unscaled)
  2361. {
  2362. float cur_min, cur_max;
  2363. float *sbuf;
  2364. int i, len, len2;
  2365. sbuf = (float *)data;
  2366. len = bytes / FLOAT_BYTES;
  2367. len2 = len - 2 *chans;
  2368. cur_min = sbuf[chan];
  2369. cur_max = cur_min;
  2370. i = chan;
  2371. while (i < len2)
  2372. {
  2373. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2374. i += chans;
  2375. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2376. i += chans;
  2377. }
  2378. while (i < len)
  2379. {
  2380. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2381. i += chans;
  2382. }
  2383. if (unscaled)
  2384. {
  2385. (*min_samp) = cur_min / 32768.0;
  2386. (*max_samp) = cur_max / 32768.0;
  2387. }
  2388. else
  2389. {
  2390. (*min_samp) = cur_min;
  2391. (*max_samp) = cur_max;
  2392. }
  2393. }
  2394. static void min_max_switch_floats(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp, bool unscaled)
  2395. {
  2396. float cur_min, cur_max;
  2397. /* frample based */
  2398. unsigned char *samp, *eod, *eod2;
  2399. int bytes_per_frample;
  2400. bytes_per_frample = chans * FLOAT_BYTES;
  2401. eod = (unsigned char *)(data + bytes);
  2402. eod2 = (unsigned char *)(eod - 2 * bytes_per_frample);
  2403. #if MUS_LITTLE_ENDIAN
  2404. cur_min = big_endian_float((unsigned char *)(data + (chan * FLOAT_BYTES)));
  2405. #else
  2406. cur_min = little_endian_float((unsigned char *)(data + (chan * FLOAT_BYTES)));
  2407. #endif
  2408. cur_max = cur_min;
  2409. samp = (unsigned char *)(data + (chan * FLOAT_BYTES));
  2410. while (samp <= eod2)
  2411. {
  2412. float val;
  2413. #if MUS_LITTLE_ENDIAN
  2414. val = big_endian_float(samp);
  2415. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2416. samp += bytes_per_frample;
  2417. val = big_endian_float(samp);
  2418. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2419. samp += bytes_per_frample;
  2420. #else
  2421. val = little_endian_float(samp);
  2422. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2423. samp += bytes_per_frample;
  2424. val = little_endian_float(samp);
  2425. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2426. samp += bytes_per_frample;
  2427. #endif
  2428. }
  2429. if (samp < eod)
  2430. {
  2431. float val;
  2432. #if MUS_LITTLE_ENDIAN
  2433. val = big_endian_float(samp);
  2434. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2435. #else
  2436. val = little_endian_float(samp);
  2437. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2438. #endif
  2439. }
  2440. if (unscaled)
  2441. {
  2442. (*min_samp) = cur_min / 32768.0;
  2443. (*max_samp) = cur_max / 32768.0;
  2444. }
  2445. else
  2446. {
  2447. (*min_samp) = cur_min;
  2448. (*max_samp) = cur_max;
  2449. }
  2450. }
  2451. /* ---------------- double ---------------- */
  2452. #define DOUBLE_BYTES 8
  2453. static void min_max_doubles(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp, bool unscaled)
  2454. {
  2455. double cur_min, cur_max;
  2456. double *sbuf;
  2457. int i, len, len2;
  2458. sbuf = (double *)data;
  2459. len = bytes / DOUBLE_BYTES;
  2460. len2 = len - 2 * chans;
  2461. cur_min = sbuf[chan];
  2462. cur_max = cur_min;
  2463. i = chan;
  2464. while (i <= len2)
  2465. {
  2466. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2467. i += chans;
  2468. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2469. i += chans;
  2470. }
  2471. if (i < len)
  2472. {
  2473. if (sbuf[i] < cur_min) cur_min = sbuf[i]; else if (sbuf[i] > cur_max) cur_max = sbuf[i];
  2474. }
  2475. if (unscaled)
  2476. {
  2477. (*min_samp) = cur_min / 32768.0;
  2478. (*max_samp) = cur_max / 32768.0;
  2479. }
  2480. else
  2481. {
  2482. (*min_samp) = cur_min;
  2483. (*max_samp) = cur_max;
  2484. }
  2485. }
  2486. static void min_max_switch_doubles(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp, bool unscaled)
  2487. {
  2488. double cur_min, cur_max;
  2489. /* frample based */
  2490. unsigned char *samp, *eod, *eod2;
  2491. int bytes_per_frample;
  2492. bytes_per_frample = chans * DOUBLE_BYTES;
  2493. eod = (unsigned char *)(data + bytes);
  2494. eod2 = (unsigned char *)(eod - 2 * bytes_per_frample);
  2495. #if MUS_LITTLE_ENDIAN
  2496. cur_min = big_endian_double((unsigned char *)(data + (chan * DOUBLE_BYTES)));
  2497. #else
  2498. cur_min = little_endian_double((unsigned char *)(data + (chan * DOUBLE_BYTES)));
  2499. #endif
  2500. cur_max = cur_min;
  2501. samp = (unsigned char *)(data + (chan * DOUBLE_BYTES));
  2502. while (samp <= eod2)
  2503. {
  2504. double val;
  2505. #if MUS_LITTLE_ENDIAN
  2506. val = big_endian_double(samp);
  2507. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2508. samp += bytes_per_frample;
  2509. val = big_endian_double(samp);
  2510. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2511. samp += bytes_per_frample;
  2512. #else
  2513. val = little_endian_double(samp);
  2514. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2515. samp += bytes_per_frample;
  2516. val = little_endian_double(samp);
  2517. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2518. samp += bytes_per_frample;
  2519. #endif
  2520. }
  2521. if (samp < eod)
  2522. {
  2523. double val;
  2524. #if MUS_LITTLE_ENDIAN
  2525. val = big_endian_double(samp);
  2526. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2527. #else
  2528. val = little_endian_double(samp);
  2529. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2530. #endif
  2531. }
  2532. if (unscaled)
  2533. {
  2534. (*min_samp) = cur_min / 32768.0;
  2535. (*max_samp) = cur_max / 32768.0;
  2536. }
  2537. else
  2538. {
  2539. (*min_samp) = cur_min;
  2540. (*max_samp) = cur_max;
  2541. }
  2542. }
  2543. /* ---------------- 3-byte samples ---------------- */
  2544. #define THREE_BYTES 3
  2545. static int big_three(unsigned char *data, int loc)
  2546. {
  2547. int val;
  2548. val = (data[loc + 0] << 16) + (data[loc + 1] << 8) + data[loc + 2];
  2549. if (val >= (1 << 23)) val -= (1 << 24);
  2550. return(val);
  2551. }
  2552. static int little_three(unsigned char *data, int loc)
  2553. {
  2554. int val;
  2555. val = (data[loc + 2] << 16) + (data[loc + 1] << 8) + data[loc + 0];
  2556. if (val >= (1 << 23)) val -= (1 << 24);
  2557. return(val);
  2558. }
  2559. static void min_max_24s(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp, bool big_endian)
  2560. {
  2561. int cur_min, cur_max;
  2562. int i, k, bytes_per_frample, len, len2, offset;
  2563. bytes_per_frample = chans * THREE_BYTES;
  2564. len = bytes / bytes_per_frample;
  2565. len2 = len - 2 * bytes_per_frample;
  2566. offset = chan * THREE_BYTES;
  2567. k = offset;
  2568. if (big_endian)
  2569. {
  2570. cur_min = big_three(data, k);
  2571. cur_max = cur_min;
  2572. i = 0;
  2573. /* k += bytes_per_frample; */
  2574. while (i <= len2)
  2575. {
  2576. int val;
  2577. val = big_three(data, k);
  2578. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2579. i++;
  2580. k += bytes_per_frample;
  2581. val = big_three(data, k);
  2582. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2583. i++;
  2584. k += bytes_per_frample;
  2585. }
  2586. while (i < len)
  2587. {
  2588. int val;
  2589. val = big_three(data, k);
  2590. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2591. i++;
  2592. k += bytes_per_frample;
  2593. }
  2594. }
  2595. else
  2596. {
  2597. cur_min = little_three(data, k);
  2598. cur_max = cur_min;
  2599. i = 0;
  2600. /* k += bytes_per_frample; */
  2601. while (i <= len2)
  2602. {
  2603. int val;
  2604. val = little_three(data, k);
  2605. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2606. i++;
  2607. k += bytes_per_frample;
  2608. val = little_three(data, k);
  2609. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2610. i++;
  2611. k += bytes_per_frample;
  2612. }
  2613. while (i < len)
  2614. {
  2615. int val;
  2616. val = little_three(data, k);
  2617. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2618. i++;
  2619. k += bytes_per_frample;
  2620. }
  2621. }
  2622. (*min_samp) = (mus_float_t)cur_min / (mus_float_t)(1 << 23);
  2623. (*max_samp) = (mus_float_t)cur_max / (mus_float_t)(1 << 23);
  2624. }
  2625. /* ---------------- mulaw, alaw, byte ---------------- */
  2626. static void min_max_mulaw(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2627. {
  2628. mus_float_t cur_min, cur_max;
  2629. int i, b2;
  2630. cur_min = mus_mulaw[(int)data[chan]];
  2631. cur_max = cur_min;
  2632. i = chan;
  2633. b2 = bytes - 2 * chans;
  2634. while (i <= b2)
  2635. {
  2636. mus_float_t val;
  2637. val = mus_mulaw[(int)data[i]];
  2638. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2639. i += chans;
  2640. val = mus_mulaw[(int)data[i]];
  2641. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2642. i += chans;
  2643. }
  2644. if (i < bytes)
  2645. {
  2646. mus_float_t val;
  2647. val = mus_mulaw[(int)data[i]];
  2648. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2649. }
  2650. (*min_samp) = cur_min;
  2651. (*max_samp) = cur_max;
  2652. }
  2653. static void min_max_alaw(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2654. {
  2655. mus_float_t cur_min, cur_max;
  2656. int i, b2;
  2657. cur_min = mus_alaw[(int)data[chan]];
  2658. cur_max = cur_min;
  2659. i = chan;
  2660. b2 = bytes - 2 * chans;
  2661. while (i <= b2)
  2662. {
  2663. mus_float_t val;
  2664. val = mus_alaw[(int)data[i]];
  2665. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2666. i += chans;
  2667. val = mus_alaw[(int)data[i]];
  2668. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2669. i += chans;
  2670. }
  2671. if (i < bytes)
  2672. {
  2673. mus_float_t val;
  2674. val = mus_alaw[(int)data[i]];
  2675. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2676. }
  2677. (*min_samp) = cur_min;
  2678. (*max_samp) = cur_max;
  2679. }
  2680. static void min_max_bytes(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2681. {
  2682. signed char cur_min, cur_max;
  2683. int i, b2;
  2684. cur_min = (signed char)(data[chan]);
  2685. cur_max = cur_min;
  2686. i = chan;
  2687. b2 = bytes - 2 * chans;
  2688. while (i <= b2)
  2689. {
  2690. signed char val;
  2691. val = (signed char)(data[i]);
  2692. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2693. i += chans;
  2694. val = (signed char)(data[i]);
  2695. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2696. i += chans;
  2697. }
  2698. if (i < bytes)
  2699. {
  2700. signed char val;
  2701. val = (signed char)(data[i]);
  2702. if (val < cur_min) cur_min = val; else if (val > cur_max) cur_max = val;
  2703. }
  2704. (*min_samp) = (mus_float_t)cur_min / (mus_float_t)(1 << 7);
  2705. (*max_samp) = (mus_float_t)cur_max / (mus_float_t)(1 << 7);
  2706. }
  2707. static void min_max_ubytes(unsigned char *data, int bytes, int chan, int chans, mus_float_t *min_samp, mus_float_t *max_samp)
  2708. {
  2709. unsigned char cur_min, cur_max;
  2710. int i, b2;
  2711. cur_min = data[chan];
  2712. cur_max = cur_min;
  2713. i = chan;
  2714. b2 = bytes - 2 * chans;
  2715. while (i <= b2)
  2716. {
  2717. if (data[i] < cur_min) cur_min = data[i]; else if (data[i] > cur_max) cur_max = data[i];
  2718. i += chans;
  2719. if (data[i] < cur_min) cur_min = data[i]; else if (data[i] > cur_max) cur_max = data[i];
  2720. i += chans;
  2721. }
  2722. if (i < bytes)
  2723. {
  2724. if (data[i] < cur_min) cur_min = data[i]; else if (data[i] > cur_max) cur_max = data[i];
  2725. }
  2726. (*min_samp) = (mus_float_t)(cur_min - UBYTE_ZERO) / (mus_float_t)(1 << 7);
  2727. (*max_samp) = (mus_float_t)(cur_max - UBYTE_ZERO) / (mus_float_t)(1 << 7);
  2728. }
  2729. int mus_samples_bounds(unsigned char *data, int bytes, int chan, int chans, mus_sample_t samp_type, mus_float_t *min_samp, mus_float_t *max_samp)
  2730. {
  2731. switch (samp_type)
  2732. {
  2733. case MUS_MULAW:
  2734. min_max_mulaw(data, bytes, chan, chans, min_samp, max_samp);
  2735. break;
  2736. case MUS_ALAW:
  2737. min_max_alaw(data, bytes, chan, chans, min_samp, max_samp);
  2738. break;
  2739. case MUS_BYTE:
  2740. min_max_bytes(data, bytes, chan, chans, min_samp, max_samp);
  2741. break;
  2742. case MUS_UBYTE:
  2743. min_max_ubytes(data, bytes, chan, chans, min_samp, max_samp);
  2744. break;
  2745. case MUS_L24INT:
  2746. min_max_24s(data, bytes, chan, chans, min_samp, max_samp, false);
  2747. break;
  2748. case MUS_B24INT:
  2749. min_max_24s(data, bytes, chan, chans, min_samp, max_samp, true);
  2750. break;
  2751. #if MUS_LITTLE_ENDIAN
  2752. case MUS_LSHORT:
  2753. min_max_shorts(data, bytes, chan, chans, min_samp, max_samp);
  2754. break;
  2755. case MUS_BSHORT:
  2756. min_max_switch_shorts(data, bytes, chan, chans, min_samp, max_samp);
  2757. break;
  2758. case MUS_ULSHORT:
  2759. min_max_ushorts(data, bytes, chan, chans, min_samp, max_samp);
  2760. break;
  2761. case MUS_UBSHORT:
  2762. min_max_switch_ushorts(data, bytes, chan, chans, min_samp, max_samp);
  2763. break;
  2764. case MUS_LINT:
  2765. case MUS_LINTN:
  2766. min_max_ints(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_LINT);
  2767. break;
  2768. case MUS_BINT:
  2769. case MUS_BINTN:
  2770. min_max_switch_ints(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_BINT);
  2771. break;
  2772. case MUS_LFLOAT:
  2773. case MUS_LFLOAT_UNSCALED:
  2774. min_max_floats(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_LFLOAT_UNSCALED);
  2775. break;
  2776. case MUS_BFLOAT:
  2777. case MUS_BFLOAT_UNSCALED:
  2778. min_max_switch_floats(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_BFLOAT_UNSCALED);
  2779. break;
  2780. case MUS_LDOUBLE:
  2781. case MUS_LDOUBLE_UNSCALED:
  2782. min_max_doubles(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_LDOUBLE_UNSCALED);
  2783. break;
  2784. case MUS_BDOUBLE:
  2785. case MUS_BDOUBLE_UNSCALED:
  2786. min_max_switch_doubles(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_BDOUBLE_UNSCALED);
  2787. break;
  2788. #else /* big endian */
  2789. case MUS_LSHORT:
  2790. min_max_switch_shorts(data, bytes, chan, chans, min_samp, max_samp);
  2791. break;
  2792. case MUS_BSHORT:
  2793. min_max_shorts(data, bytes, chan, chans, min_samp, max_samp);
  2794. break;
  2795. case MUS_ULSHORT:
  2796. min_max_switch_ushorts(data, bytes, chan, chans, min_samp, max_samp);
  2797. break;
  2798. case MUS_UBSHORT:
  2799. min_max_ushorts(data, bytes, chan, chans, min_samp, max_samp);
  2800. break;
  2801. case MUS_LINT:
  2802. case MUS_LINTN:
  2803. min_max_switch_ints(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_LINT);
  2804. break;
  2805. case MUS_BINT:
  2806. case MUS_BINTN:
  2807. min_max_ints(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_BINT);
  2808. break;
  2809. case MUS_LFLOAT:
  2810. case MUS_LFLOAT_UNSCALED:
  2811. min_max_switch_floats(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_LFLOAT_UNSCALED);
  2812. break;
  2813. case MUS_BFLOAT:
  2814. case MUS_BFLOAT_UNSCALED:
  2815. min_max_floats(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_BFLOAT_UNSCALED);
  2816. break;
  2817. case MUS_LDOUBLE:
  2818. case MUS_LDOUBLE_UNSCALED:
  2819. min_max_switch_doubles(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_LDOUBLE_UNSCALED);
  2820. break;
  2821. case MUS_BDOUBLE:
  2822. case MUS_BDOUBLE_UNSCALED:
  2823. min_max_doubles(data, bytes, chan, chans, min_samp, max_samp, samp_type == MUS_BDOUBLE_UNSCALED);
  2824. break;
  2825. #endif
  2826. default:
  2827. return(MUS_ERROR);
  2828. break;
  2829. }
  2830. return(MUS_NO_ERROR);
  2831. }
  2832. char *mus_strdup(const char *str)
  2833. {
  2834. char *newstr = NULL;
  2835. int len;
  2836. if ((!str) || (!(*str))) return(NULL);
  2837. len = strlen(str);
  2838. newstr = (char *)malloc(len + 1);
  2839. strcpy(newstr, str);
  2840. newstr[len] = '\0';
  2841. return(newstr);
  2842. }
  2843. int mus_strlen(const char *str)
  2844. {
  2845. /* strlen(NULL) -> seg fault! */
  2846. if ((str) && (*str)) return(strlen(str));
  2847. return(0);
  2848. }
  2849. bool mus_strcmp(const char *s1, const char *s2)
  2850. {
  2851. if ((!s1) || (!s2)) return(s1 == s2);
  2852. while (true)
  2853. {
  2854. unsigned char c1, c2;
  2855. c1 = (unsigned char) *s1++;
  2856. c2 = (unsigned char) *s2++;
  2857. if (c1 != c2) return(false);
  2858. if (c1 == '\0') break;
  2859. }
  2860. return(true);
  2861. #if 0
  2862. return((str1 == str2) ||
  2863. ((str1) && (str2) &&
  2864. (strcmp(str1, str2) == 0)));
  2865. #endif
  2866. }
  2867. char *mus_strcat(char *errmsg, const char *str, int *size)
  2868. {
  2869. int new_len, err_size;
  2870. new_len = (mus_strlen(str) + mus_strlen(errmsg));
  2871. err_size = size[0];
  2872. if (new_len >= err_size)
  2873. {
  2874. if ((err_size * 2) > new_len)
  2875. err_size = err_size * 2;
  2876. else err_size = new_len * 2;
  2877. errmsg = (char *)realloc(errmsg, err_size * sizeof(char));
  2878. size[0] = err_size;
  2879. }
  2880. strcat(errmsg, str);
  2881. return(errmsg);
  2882. }