You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1514 lines
37KB

  1. #include "mus-config.h"
  2. #if USE_SND
  3. #include "snd.h"
  4. #endif
  5. #include <math.h>
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <stddef.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <time.h>
  13. #include <stdarg.h>
  14. #ifndef _MSC_VER
  15. #include <unistd.h>
  16. #else
  17. #include <io.h>
  18. #pragma warning(disable: 4244)
  19. #endif
  20. #include <string.h>
  21. #include "_sndlib.h"
  22. #include "sndlib-strings.h"
  23. static mus_error_handler_t *mus_error_handler = NULL;
  24. mus_error_handler_t *mus_error_set_handler(mus_error_handler_t *new_error_handler)
  25. {
  26. mus_error_handler_t *old_handler;
  27. old_handler = mus_error_handler;
  28. mus_error_handler = new_error_handler;
  29. return(old_handler);
  30. }
  31. static char *mus_error_buffer = NULL;
  32. static int mus_error_buffer_size = 1024;
  33. int mus_error(int error, const char *format, ...)
  34. {
  35. int bytes_needed = 0;
  36. va_list ap;
  37. if (format == NULL)
  38. return(MUS_ERROR); /* else bus error in Mac OSX */
  39. if (mus_error_buffer == NULL)
  40. mus_error_buffer = (char *)calloc(mus_error_buffer_size, sizeof(char));
  41. va_start(ap, format);
  42. /* can't use vasprintf here or below because the error handler may jump anywhere,
  43. * leaving unfreed memory behind.
  44. */
  45. bytes_needed = vsnprintf(mus_error_buffer, mus_error_buffer_size, format, ap);
  46. va_end(ap);
  47. if (bytes_needed >= mus_error_buffer_size)
  48. {
  49. mus_error_buffer_size = bytes_needed * 2;
  50. free(mus_error_buffer);
  51. mus_error_buffer = (char *)calloc(mus_error_buffer_size, sizeof(char));
  52. va_start(ap, format);
  53. vsnprintf(mus_error_buffer, mus_error_buffer_size, format, ap);
  54. va_end(ap);
  55. }
  56. if (mus_error_handler)
  57. (*mus_error_handler)(error, mus_error_buffer);
  58. else
  59. {
  60. fprintf(stderr, "%s", mus_error_buffer);
  61. fputc('\n', stderr);
  62. }
  63. return(MUS_ERROR);
  64. }
  65. static mus_print_handler_t *mus_print_handler = NULL;
  66. mus_print_handler_t *mus_print_set_handler(mus_print_handler_t *new_print_handler)
  67. {
  68. mus_print_handler_t *old_handler;
  69. old_handler = mus_print_handler;
  70. mus_print_handler = new_print_handler;
  71. return(old_handler);
  72. }
  73. void mus_print(const char *format, ...)
  74. {
  75. va_list ap;
  76. if (mus_print_handler)
  77. {
  78. int bytes_needed = 0;
  79. if (mus_error_buffer == NULL)
  80. mus_error_buffer = (char *)calloc(mus_error_buffer_size, sizeof(char));
  81. va_start(ap, format);
  82. bytes_needed = vsnprintf(mus_error_buffer, mus_error_buffer_size, format, ap);
  83. va_end(ap);
  84. if (bytes_needed >= mus_error_buffer_size)
  85. {
  86. mus_error_buffer_size = bytes_needed * 2;
  87. free(mus_error_buffer);
  88. mus_error_buffer = (char *)calloc(mus_error_buffer_size, sizeof(char));
  89. va_start(ap, format);
  90. vsnprintf(mus_error_buffer, mus_error_buffer_size, format, ap);
  91. va_end(ap);
  92. }
  93. (*mus_print_handler)(mus_error_buffer);
  94. }
  95. else
  96. {
  97. va_start(ap, format);
  98. vfprintf(stdout, format, ap);
  99. va_end(ap);
  100. }
  101. }
  102. static const char *mus_error_names[MUS_NUM_ERRORS] = {
  103. "no error", "no frequency method", "no phase method", "null gen arg to method", "no length method",
  104. "no describe method", "no data method", "no scaler method",
  105. "memory allocation failed",
  106. "can't open file", "no sample input", "no sample output",
  107. "no such channel", "no file name provided", "no location method", "no channel method",
  108. "no such fft window", "unknown sample type", "header read failed",
  109. "unknown header type", "file descriptors not initialized", "not a sound file", "file closed", "write error",
  110. "header write failed", "can't open temp file", "interrupted", "bad envelope",
  111. "audio channels not available", "audio srate not available", "audio sample type not available",
  112. "no audio input available", "audio configuration not available",
  113. "audio write error", "audio size not available", "audio device not available",
  114. "can't close audio", "can't open audio", "audio read error",
  115. "can't write audio", "can't read audio", "no audio read permission",
  116. "can't close file", "arg out of range",
  117. "no channels method", "no hop method", "no width method", "no file-name method", "no ramp method", "no run method",
  118. "no increment method", "no offset method",
  119. "no xcoeff method", "no ycoeff method", "no xcoeffs method", "no ycoeffs method", "no reset", "bad size", "can't convert",
  120. "read error",
  121. "no feedforward method", "no feedback method", "no interp-type method", "no position method", "no order method", "no copy method",
  122. "can't translate"
  123. };
  124. const char *mus_error_type_to_string(int err)
  125. {
  126. if ((err >= 0) &&
  127. (err < MUS_NUM_ERRORS))
  128. return(mus_error_names[err]);
  129. return("unknown mus error");
  130. }
  131. static void default_mus_error(int ignore, char *msg)
  132. {
  133. /* default error handler simply prints the error message */
  134. fprintf(stderr, "%s", msg);
  135. }
  136. static time_t local_file_write_date(const char *filename)
  137. {
  138. struct stat statbuf;
  139. int err;
  140. err = stat(filename, &statbuf);
  141. if (err < 0) return((time_t)0);
  142. return((time_t)(statbuf.st_mtime));
  143. }
  144. /* -------- sound file table -------- */
  145. typedef struct sound_file {
  146. char *file_name; /* full path -- everything is keyed to this name */
  147. int table_pos, file_name_length, table_index;
  148. mus_long_t *aux_comment_start, *aux_comment_end;
  149. int *loop_modes, *loop_starts, *loop_ends;
  150. int markers, base_detune, base_note;
  151. int *marker_ids, *marker_positions;
  152. mus_long_t samples, true_file_length;
  153. mus_long_t data_location;
  154. int srate, chans, original_sound_samp_type, datum_size;
  155. mus_header_t header_type;
  156. mus_sample_t sample_type;
  157. mus_long_t comment_start, comment_end;
  158. int type_specifier, bits_per_sample, block_align, fact_samples;
  159. time_t write_date;
  160. mus_float_t *maxamps;
  161. mus_long_t *maxtimes;
  162. int maxamps_size; /* we can't depend on sf->chans here because the user could set chans to some bogus value */
  163. mus_float_t **saved_data;
  164. struct sound_file *next;
  165. } sound_file;
  166. static int *sound_table_sizes = NULL;
  167. static sound_file ***sound_tables = NULL;
  168. #define NUM_SOUND_TABLES 64
  169. /* it's not enough to hash on the file name length -- they're nearly all the same length!
  170. * I think I'll try taking the last few chars instead (the first 15-20 chars are
  171. * also always the same: the tmp directory, eg: /home/bil/zap/tmp/snd_16687_632.snd).
  172. */
  173. static int sound_file_hash_index(const char *name, int len)
  174. {
  175. unsigned char *s;
  176. if (!name) return(0);
  177. if (len < 8) return(len);
  178. s = (unsigned char *)(name + len - 8);
  179. return((s[0] + s[1] + s[2] + s[3]) % NUM_SOUND_TABLES);
  180. }
  181. void scan_io_fds_for_saved_data(mus_float_t **data);
  182. static sound_file *sf_free_list = NULL;
  183. static void free_sound_file(sound_file *sf)
  184. {
  185. if (sf)
  186. {
  187. sound_tables[sf->table_index][sf->table_pos] = NULL;
  188. if (sf->aux_comment_start) free(sf->aux_comment_start);
  189. if (sf->aux_comment_end) free(sf->aux_comment_end);
  190. if (sf->file_name) free(sf->file_name);
  191. if (sf->loop_modes) free(sf->loop_modes);
  192. if (sf->loop_starts) free(sf->loop_starts);
  193. if (sf->loop_ends) free(sf->loop_ends);
  194. if (sf->marker_ids) free(sf->marker_ids);
  195. if (sf->marker_positions) free(sf->marker_positions);
  196. if (sf->maxamps) free(sf->maxamps);
  197. if (sf->maxtimes) free(sf->maxtimes);
  198. sf->maxamps_size = 0;
  199. if (sf->saved_data)
  200. {
  201. int i;
  202. scan_io_fds_for_saved_data(sf->saved_data);
  203. for (i = 0; i < sf->chans; i++)
  204. if (sf->saved_data[i])
  205. free(sf->saved_data[i]);
  206. free(sf->saved_data);
  207. sf->saved_data = NULL;
  208. }
  209. /* free(sf); */
  210. sf->next = sf_free_list;
  211. sf_free_list = sf;
  212. }
  213. }
  214. static sound_file *add_to_sound_table(const char *name)
  215. {
  216. int i, len, pos = -1, index, sound_table_size;
  217. sound_file **sound_table;
  218. sound_file *sf;
  219. len = strlen(name);
  220. index = sound_file_hash_index(name, len);
  221. sound_table = sound_tables[index];
  222. sound_table_size = sound_table_sizes[index];
  223. for (i = 0; i < sound_table_size; i++)
  224. if (sound_table[i] == NULL)
  225. {
  226. pos = i;
  227. break;
  228. }
  229. if (pos == -1)
  230. {
  231. pos = sound_table_size;
  232. sound_table_size += 16;
  233. if (sound_table == NULL)
  234. {
  235. sound_table = (sound_file **)calloc(sound_table_size, sizeof(sound_file *));
  236. }
  237. else
  238. {
  239. sound_table = (sound_file **)realloc(sound_table, sound_table_size * sizeof(sound_file *));
  240. for (i = pos; i < sound_table_size; i++) sound_table[i] = NULL;
  241. }
  242. sound_tables[index] = sound_table;
  243. sound_table_sizes[index] = sound_table_size;
  244. }
  245. if (sf_free_list)
  246. {
  247. sf = sf_free_list;
  248. sf_free_list = sf->next;
  249. memset((void *)sf, 0, sizeof(sound_file));
  250. }
  251. else sf = (sound_file *)calloc(1, sizeof(sound_file));
  252. sound_table[pos] = sf;
  253. sf->table_pos = pos;
  254. sf->table_index = index;
  255. sf->file_name = (char *)malloc((len + 1) * sizeof(char));
  256. strcpy(sf->file_name, name);
  257. sf->file_name[len] = 0;
  258. sf->file_name_length = len;
  259. sf->saved_data = NULL;
  260. return(sf);
  261. }
  262. int mus_sound_prune(void)
  263. {
  264. int j, pruned = 0;
  265. for (j = 0; j < NUM_SOUND_TABLES; j++)
  266. {
  267. int i, sound_table_size;
  268. sound_file **sound_table;
  269. sound_table = sound_tables[j];
  270. sound_table_size = sound_table_sizes[j];
  271. for (i = 0; i < sound_table_size; i++)
  272. if ((sound_table[i]) &&
  273. (!(mus_file_probe(sound_table[i]->file_name))))
  274. {
  275. free_sound_file(sound_table[i]);
  276. sound_table[i] = NULL;
  277. pruned++;
  278. }
  279. }
  280. return(pruned);
  281. }
  282. int mus_sound_forget(const char *name)
  283. {
  284. /* apparently here we want to forget either name or the expanded or contracted forms of name -- as many as we find! */
  285. int i, len, len2, short_len = 0;
  286. bool free_name = false;
  287. char *short_name = NULL;
  288. sound_file **sound_table;
  289. int sound_table_size, index;
  290. char c;
  291. if (name == NULL) return(MUS_ERROR);
  292. len = strlen(name);
  293. if (len > 6)
  294. len2 = len - 6;
  295. else len2 = len / 2;
  296. c = name[len2];
  297. if (name[0] == '/')
  298. {
  299. for (i = 0; i < len; i++)
  300. if (name[i] == '/')
  301. short_name = (char *)(name + i + 1);
  302. }
  303. else
  304. {
  305. short_name = mus_expand_filename(name);
  306. free_name = true;
  307. }
  308. if (short_name)
  309. short_len = strlen(short_name);
  310. index = sound_file_hash_index(name, len);
  311. sound_table = sound_tables[index];
  312. sound_table_size = sound_table_sizes[index];
  313. for (i = 0; i < sound_table_size; i++)
  314. if ((sound_table[i]) &&
  315. (sound_table[i]->file_name_length == len) &&
  316. (sound_table[i]->file_name[len2] == c) &&
  317. (mus_strcmp(name, sound_table[i]->file_name)))
  318. {
  319. free_sound_file(sound_table[i]);
  320. sound_table[i] = NULL;
  321. }
  322. if (short_name)
  323. {
  324. if (short_len > 6)
  325. len2 = short_len - 6;
  326. else len2 = short_len / 2;
  327. c = short_name[len2];
  328. index = sound_file_hash_index(short_name, short_len);
  329. sound_table = sound_tables[index];
  330. sound_table_size = sound_table_sizes[index];
  331. for (i = 0; i < sound_table_size; i++)
  332. if ((sound_table[i]) &&
  333. (sound_table[i]->file_name_length == short_len) &&
  334. (sound_table[i]->file_name[len2] == c) &&
  335. (mus_strcmp(short_name, sound_table[i]->file_name)))
  336. {
  337. free_sound_file(sound_table[i]);
  338. sound_table[i] = NULL;
  339. }
  340. }
  341. if (free_name) free(short_name);
  342. return(MUS_NO_ERROR);
  343. }
  344. static sound_file *check_write_date(const char *name, sound_file *sf)
  345. {
  346. if (sf)
  347. {
  348. time_t date;
  349. date = local_file_write_date(name);
  350. if (date == sf->write_date)
  351. return(sf);
  352. if ((sf->header_type == MUS_RAW) && (mus_header_no_header(name)))
  353. {
  354. int chan;
  355. mus_long_t data_size;
  356. /* sound has changed since we last read it, but it has no header, so
  357. * the only sensible thing to check is the new length (i.e. caller
  358. * has set other fields by hand)
  359. */
  360. sf->write_date = date;
  361. chan = mus_file_open_read(name);
  362. data_size = lseek(chan, 0L, SEEK_END);
  363. sf->true_file_length = data_size;
  364. sf->samples = mus_bytes_to_samples(sf->sample_type, data_size);
  365. CLOSE(chan, name);
  366. return(sf);
  367. }
  368. /* otherwise our data base is out-of-date, so clear it out */
  369. free_sound_file(sf);
  370. }
  371. return(NULL);
  372. }
  373. static sound_file *find_sound_file(const char *name)
  374. {
  375. /* assume name != NULL */
  376. int i, len, len2;
  377. sound_file **sound_table;
  378. int sound_table_size, index;
  379. char c;
  380. len = strlen(name);
  381. if (len > 6)
  382. len2 = len - 6; /* the names probably all start with '/' and end with ".snd", so try to find a changing character... */
  383. else len2 = len / 2;
  384. c = name[len2];
  385. index = sound_file_hash_index(name, len);
  386. sound_table = sound_tables[index];
  387. sound_table_size = sound_table_sizes[index];
  388. for (i = 0; i < sound_table_size; i++)
  389. {
  390. sound_file *sf;
  391. sf = sound_table[i];
  392. if ((sf) &&
  393. (sf->file_name_length == len) &&
  394. (c == sf->file_name[len2]) &&
  395. (strcmp(name, sf->file_name) == 0))
  396. return(check_write_date(name, sf));
  397. }
  398. return(NULL);
  399. }
  400. static void display_sound_file_entry(FILE *fp, const char *name, sound_file *sf)
  401. {
  402. #define TIME_BUFFER_SIZE 64
  403. time_t date;
  404. char timestr[TIME_BUFFER_SIZE];
  405. date = sf->write_date;
  406. if (date != 0)
  407. strftime(timestr, TIME_BUFFER_SIZE, "%a %d-%b-%Y %H:%M:%S", localtime(&date));
  408. else snprintf(timestr, TIME_BUFFER_SIZE, "(date cleared)");
  409. fprintf(fp, " %s: %s, chans: %d, srate: %d, header: %s, data: %s, samps: %lld",
  410. name,
  411. timestr,
  412. sf->chans,
  413. sf->srate,
  414. mus_header_type_name(sf->header_type),
  415. mus_sample_type_name(sf->sample_type),
  416. sf->samples);
  417. if (sf->loop_modes)
  418. {
  419. if (sf->loop_modes[0] != 0)
  420. fprintf(fp, ", loop mode %d: %d to %d", sf->loop_modes[0], sf->loop_starts[0], sf->loop_ends[0]);
  421. if (sf->loop_modes[1] != 0)
  422. fprintf(fp, ", loop mode %d: %d to %d, ", sf->loop_modes[1], sf->loop_starts[1], sf->loop_ends[1]);
  423. fprintf(fp, ", base: %d, detune: %d", sf->base_note, sf->base_detune);
  424. }
  425. if (sf->maxamps)
  426. {
  427. int lim;
  428. lim = sf->maxamps_size;
  429. if (lim > 0)
  430. {
  431. int i;
  432. if (lim > 64)
  433. lim = 64;
  434. fprintf(fp, ", maxamp:");
  435. for (i = 0; i < lim; i++)
  436. {
  437. if (i > 1) fprintf(fp, ", ");
  438. fprintf(fp, " %.3f at %.3f ",
  439. sf->maxamps[i],
  440. (sf->srate > 0) ? (float)((double)(sf->maxtimes[i]) / (double)(sf->srate)) : (float)(sf->maxtimes[i]));
  441. }
  442. }
  443. }
  444. if (mus_file_probe(name))
  445. {
  446. char *comment;
  447. comment = mus_sound_comment(name);
  448. if (comment)
  449. {
  450. fprintf(fp, "\n comment: %s", comment);
  451. free(comment);
  452. }
  453. }
  454. else fprintf(fp, " [defunct]");
  455. fprintf(fp, "\n");
  456. }
  457. void mus_sound_report_cache(FILE *fp)
  458. {
  459. sound_file *sf;
  460. int entries = 0;
  461. int i, j;
  462. sound_file **sound_table;
  463. fprintf(fp, "sound table:");
  464. for (j = 0; j < NUM_SOUND_TABLES; j++)
  465. {
  466. int sound_table_size;
  467. sound_table = sound_tables[j];
  468. sound_table_size = sound_table_sizes[j];
  469. for (i = 0; i < sound_table_size; i++)
  470. {
  471. sf = sound_table[i];
  472. if (sf)
  473. {
  474. if (entries == 0) fprintf(fp, "\n");
  475. display_sound_file_entry(fp, sf->file_name, sf);
  476. entries++;
  477. }
  478. }
  479. }
  480. if (entries > 0)
  481. fprintf(fp, "\nentries: %d\n", entries);
  482. else fprintf(fp, " empty");
  483. fflush(fp);
  484. }
  485. static sound_file *fill_sf_record(const char *name, sound_file *sf)
  486. {
  487. int i;
  488. sf->data_location = mus_header_data_location();
  489. sf->samples = mus_header_samples();
  490. sf->sample_type = mus_header_sample_type();
  491. sf->srate = mus_header_srate();
  492. /* if (sf->srate < 0) sf->srate = 0; */
  493. sf->chans = mus_header_chans();
  494. /* if (sf->chans < 0) sf->chans = 0; */
  495. sf->datum_size = mus_bytes_per_sample(sf->sample_type);
  496. sf->header_type = mus_header_type();
  497. sf->original_sound_samp_type = mus_header_original_sample_type();
  498. sf->true_file_length = mus_header_true_length();
  499. sf->comment_start = mus_header_comment_start();
  500. sf->comment_end = mus_header_comment_end();
  501. if (((sf->header_type == MUS_AIFC) ||
  502. (sf->header_type == MUS_AIFF) ||
  503. (sf->header_type == MUS_RF64) ||
  504. (sf->header_type == MUS_RIFF)) &&
  505. (mus_header_aux_comment_start(0) != 0))
  506. {
  507. sf->aux_comment_start = (mus_long_t *)calloc(4, sizeof(mus_long_t));
  508. sf->aux_comment_end = (mus_long_t *)calloc(4, sizeof(mus_long_t));
  509. for (i = 0; i < 4; i++)
  510. {
  511. sf->aux_comment_start[i] = mus_header_aux_comment_start(i);
  512. sf->aux_comment_end[i] = mus_header_aux_comment_end(i);
  513. }
  514. }
  515. sf->type_specifier = mus_header_type_specifier();
  516. sf->bits_per_sample = mus_header_bits_per_sample();
  517. sf->fact_samples = mus_header_fact_samples();
  518. sf->block_align = mus_header_block_align();
  519. sf->write_date = local_file_write_date(name);
  520. if ((sf->header_type == MUS_AIFF) || (sf->header_type == MUS_AIFC))
  521. {
  522. int *marker_ids, *marker_positions;
  523. sf->markers = mus_header_mark_info(&marker_ids, &marker_positions);
  524. if (sf->markers > 0)
  525. {
  526. sf->marker_ids = (int *)malloc(sf->markers * sizeof(int));
  527. sf->marker_positions = (int *)malloc(sf->markers * sizeof(int));
  528. memcpy((void *)(sf->marker_ids), (void *)marker_ids, sizeof(int) * sf->markers);
  529. memcpy((void *)(sf->marker_positions), (void *)marker_positions, sizeof(int) * sf->markers);
  530. }
  531. }
  532. if (mus_header_loop_mode(0) > 0)
  533. {
  534. sf->loop_modes = (int *)calloc(2, sizeof(int));
  535. sf->loop_starts = (int *)calloc(2, sizeof(int));
  536. sf->loop_ends = (int *)calloc(2, sizeof(int));
  537. for (i = 0; i < 2; i++)
  538. {
  539. sf->loop_modes[i] = mus_header_loop_mode(i);
  540. if ((sf->header_type == MUS_AIFF) ||
  541. (sf->header_type == MUS_AIFC))
  542. {
  543. sf->loop_starts[i] = mus_header_mark_position(mus_header_loop_start(i));
  544. sf->loop_ends[i] = mus_header_mark_position(mus_header_loop_end(i));
  545. }
  546. else
  547. {
  548. sf->loop_starts[i] = mus_header_loop_start(i);
  549. sf->loop_ends[i] = mus_header_loop_end(i);
  550. }
  551. }
  552. sf->base_detune = mus_header_base_detune();
  553. sf->base_note = mus_header_base_note();
  554. }
  555. return(sf);
  556. }
  557. static sound_file *read_sound_file_header(const char *name) /* 2 calls on this: mus_sound_open_input and get_sf */
  558. {
  559. int result;
  560. mus_sound_initialize();
  561. result = mus_header_read(name);
  562. /* this portion won't trigger mus_error */
  563. if (result != MUS_ERROR)
  564. return(fill_sf_record(name, add_to_sound_table(name))); /* only call on fill_sf_record and add_to_sound_table */
  565. return(NULL);
  566. }
  567. static sound_file *get_sf(const char *arg)
  568. {
  569. sound_file *sf = NULL;
  570. if (arg == NULL) return(NULL);
  571. sf = find_sound_file(arg);
  572. return((sf) ? sf : read_sound_file_header(arg));
  573. }
  574. mus_long_t mus_sound_samples(const char *arg)
  575. {
  576. sound_file *sf;
  577. sf = get_sf(arg);
  578. return((sf) ? sf->samples : (mus_long_t)MUS_ERROR);
  579. }
  580. mus_long_t mus_sound_framples(const char *arg)
  581. {
  582. sound_file *sf;
  583. sf = get_sf(arg);
  584. return((sf) ? ((sf->chans > 0) ? (sf->samples / sf->chans) : 0) : (mus_long_t)MUS_ERROR);
  585. }
  586. int mus_sound_datum_size(const char *arg)
  587. {
  588. sound_file *sf;
  589. sf = get_sf(arg);
  590. return((sf) ? sf->datum_size : MUS_ERROR);
  591. }
  592. mus_long_t mus_sound_data_location(const char *arg)
  593. {
  594. sound_file *sf;
  595. sf = get_sf(arg);
  596. return((sf) ? sf->data_location : MUS_ERROR);
  597. }
  598. int mus_sound_chans(const char *arg)
  599. {
  600. sound_file *sf;
  601. sf = get_sf(arg);
  602. return((sf) ? sf->chans : MUS_ERROR);
  603. }
  604. int mus_sound_srate(const char *arg)
  605. {
  606. sound_file *sf;
  607. sf = get_sf(arg);
  608. return((sf) ? sf->srate : MUS_ERROR);
  609. }
  610. mus_header_t mus_sound_header_type(const char *arg)
  611. {
  612. sound_file *sf;
  613. sf = get_sf(arg);
  614. return((sf) ? sf->header_type : MUS_UNKNOWN_HEADER);
  615. }
  616. mus_sample_t mus_sound_sample_type(const char *arg)
  617. {
  618. sound_file *sf;
  619. sf = get_sf(arg);
  620. return((sf) ? sf->sample_type : MUS_UNKNOWN_SAMPLE);
  621. }
  622. int mus_sound_original_sample_type(const char *arg)
  623. {
  624. sound_file *sf;
  625. sf = get_sf(arg);
  626. return((sf) ? sf->original_sound_samp_type : MUS_ERROR);
  627. }
  628. mus_long_t mus_sound_comment_start(const char *arg)
  629. {
  630. sound_file *sf;
  631. sf = get_sf(arg);
  632. return((sf) ? sf->comment_start : (mus_long_t)MUS_ERROR);
  633. }
  634. mus_long_t mus_sound_comment_end(const char *arg)
  635. {
  636. sound_file *sf;
  637. sf = get_sf(arg);
  638. return((sf) ? sf->comment_end : (mus_long_t)MUS_ERROR);
  639. }
  640. mus_long_t mus_sound_length(const char *arg)
  641. {
  642. sound_file *sf;
  643. sf = get_sf(arg);
  644. return((sf) ? sf->true_file_length : (mus_long_t)MUS_ERROR);
  645. }
  646. int mus_sound_fact_samples(const char *arg)
  647. {
  648. sound_file *sf;
  649. sf = get_sf(arg);
  650. return((sf) ? sf->fact_samples : MUS_ERROR);
  651. }
  652. time_t mus_sound_write_date(const char *arg)
  653. {
  654. sound_file *sf;
  655. sf = get_sf(arg);
  656. return((sf) ? sf->write_date : (time_t)MUS_ERROR);
  657. }
  658. int mus_sound_type_specifier(const char *arg)
  659. {
  660. sound_file *sf;
  661. sf = get_sf(arg);
  662. return((sf) ? sf->type_specifier : MUS_ERROR);
  663. }
  664. int mus_sound_block_align(const char *arg)
  665. {
  666. sound_file *sf;
  667. sf = get_sf(arg);
  668. return((sf) ? sf->block_align : MUS_ERROR);
  669. }
  670. int mus_sound_bits_per_sample(const char *arg)
  671. {
  672. sound_file *sf;
  673. sf = get_sf(arg);
  674. return((sf) ? sf->bits_per_sample : MUS_ERROR);
  675. }
  676. float mus_sound_duration(const char *arg)
  677. {
  678. float val = -1.0;
  679. sound_file *sf;
  680. sf = get_sf(arg);
  681. if (sf)
  682. {
  683. if ((sf->chans > 0) && (sf->srate > 0))
  684. val = (float)((double)(sf->samples) / ((float)(sf->chans) * (float)(sf->srate)));
  685. else val = 0.0;
  686. }
  687. return(val);
  688. }
  689. mus_float_t **mus_sound_saved_data(const char *arg)
  690. {
  691. /* slightly tricky -- we don't want to trigger a sound_file table entry here!
  692. */
  693. sound_file *sf;
  694. if (arg == NULL) return(NULL);
  695. sf = find_sound_file(arg); /* not get_sf which will make an entry in the table */
  696. return((sf) ? sf->saved_data : NULL);
  697. }
  698. void mus_sound_set_saved_data(const char *arg, mus_float_t **data)
  699. {
  700. sound_file *sf;
  701. sf = get_sf(arg);
  702. if (sf)
  703. sf->saved_data = data;
  704. }
  705. int *mus_sound_loop_info(const char *arg)
  706. {
  707. sound_file *sf;
  708. sf = get_sf(arg);
  709. if ((sf) && (sf->loop_modes))
  710. {
  711. int *info;
  712. info = (int *)calloc(MUS_LOOP_INFO_SIZE, sizeof(int));
  713. if (sf->loop_modes[0] != 0)
  714. {
  715. info[0] = sf->loop_starts[0];
  716. info[1] = sf->loop_ends[0];
  717. info[6] = sf->loop_modes[0];
  718. }
  719. if (sf->loop_modes[1] != 0)
  720. {
  721. info[2] = sf->loop_starts[1];
  722. info[3] = sf->loop_ends[1];
  723. info[7] = sf->loop_modes[1];
  724. }
  725. info[4] = sf->base_note;
  726. info[5] = sf->base_detune;
  727. return(info);
  728. }
  729. return(NULL);
  730. }
  731. void mus_sound_set_loop_info(const char *arg, int *loop)
  732. {
  733. sound_file *sf;
  734. sf = get_sf(arg);
  735. if (sf)
  736. {
  737. if (sf->loop_modes == NULL)
  738. {
  739. sf->loop_modes = (int *)calloc(2, sizeof(int));
  740. sf->loop_starts = (int *)calloc(2, sizeof(int));
  741. sf->loop_ends = (int *)calloc(2, sizeof(int));
  742. }
  743. sf->loop_modes[0] = loop[6];
  744. if (loop[6] != 0)
  745. {
  746. sf->loop_starts[0] = loop[0];
  747. sf->loop_ends[0] = loop[1];
  748. }
  749. else
  750. {
  751. sf->loop_starts[0] = 0;
  752. sf->loop_ends[0] = 0;
  753. }
  754. sf->loop_modes[1] = loop[7];
  755. if (loop[7] != 0)
  756. {
  757. sf->loop_starts[1] = loop[2];
  758. sf->loop_ends[1] = loop[3];
  759. }
  760. else
  761. {
  762. sf->loop_starts[1] = 0;
  763. sf->loop_ends[1] = 0;
  764. }
  765. sf->base_note = loop[4];
  766. sf->base_detune = loop[5];
  767. }
  768. }
  769. int mus_sound_mark_info(const char *arg, int **mark_ids, int **mark_positions)
  770. {
  771. sound_file *sf;
  772. int result = 0;
  773. sf = get_sf(arg);
  774. if (sf)
  775. {
  776. (*mark_ids) = sf->marker_ids;
  777. (*mark_positions) = sf->marker_positions;
  778. result = sf->markers;
  779. }
  780. return(result);
  781. }
  782. char *mus_sound_comment(const char *name)
  783. {
  784. char *sc = NULL;
  785. sound_file *sf = NULL;
  786. sf = get_sf(name);
  787. if (sf)
  788. {
  789. mus_long_t start, end;
  790. start = sf->comment_start;
  791. end = sf->comment_end;
  792. if (end == 0)
  793. {
  794. if (sf->aux_comment_start)
  795. {
  796. if ((sf->header_type == MUS_RIFF) ||
  797. (sf->header_type == MUS_RF64))
  798. sc = mus_header_riff_aux_comment(name,
  799. sf->aux_comment_start,
  800. sf->aux_comment_end);
  801. if ((sf->header_type == MUS_AIFF) ||
  802. (sf->header_type == MUS_AIFC))
  803. sc = mus_header_aiff_aux_comment(name,
  804. sf->aux_comment_start,
  805. sf->aux_comment_end);
  806. }
  807. }
  808. else
  809. {
  810. if (end <= sf->true_file_length)
  811. {
  812. int len;
  813. len = end - start + 1;
  814. if (len > 0)
  815. {
  816. /* open and get the comment */
  817. ssize_t bytes;
  818. int fd;
  819. fd = mus_file_open_read(name);
  820. if (fd == -1) return(NULL);
  821. lseek(fd, start, SEEK_SET);
  822. sc = (char *)calloc(len + 1, sizeof(char));
  823. bytes = read(fd, sc, len);
  824. CLOSE(fd, name);
  825. if (((sf->header_type == MUS_AIFF) ||
  826. (sf->header_type == MUS_AIFC)) &&
  827. (sf->aux_comment_start) &&
  828. (bytes != 0))
  829. {
  830. char *auxcom;
  831. auxcom = mus_header_aiff_aux_comment(name,
  832. sf->aux_comment_start,
  833. sf->aux_comment_end);
  834. if (auxcom)
  835. {
  836. size_t full_len;
  837. full_len = strlen(auxcom) + strlen(sc) + 2;
  838. sc = (char *)realloc(sc, full_len * sizeof(char));
  839. strcat(sc, "\n");
  840. strcat(sc, auxcom);
  841. }
  842. }
  843. }
  844. }
  845. }
  846. }
  847. return(sc);
  848. }
  849. int mus_sound_open_input(const char *arg)
  850. {
  851. int fd = -1;
  852. if (!(mus_file_probe(arg)))
  853. mus_error(MUS_CANT_OPEN_FILE, "mus_sound_open_input: can't open %s: %s", arg, STRERROR(errno));
  854. else
  855. {
  856. sound_file *sf = NULL;
  857. mus_sound_initialize();
  858. sf = get_sf(arg);
  859. if (sf)
  860. {
  861. fd = mus_file_open_read(arg);
  862. if (fd == -1)
  863. mus_error(MUS_CANT_OPEN_FILE, "mus_sound_open_input: can't open %s: %s", arg, STRERROR(errno));
  864. else
  865. {
  866. mus_file_open_descriptors(fd, arg, sf->sample_type, sf->datum_size, sf->data_location, sf->chans, sf->header_type);
  867. lseek(fd, sf->data_location, SEEK_SET);
  868. if (sf->saved_data)
  869. mus_file_save_data(fd, sf->samples / sf->chans, sf->saved_data);
  870. }
  871. }
  872. }
  873. return(fd);
  874. }
  875. int mus_sound_open_output(const char *arg, int srate, int chans, mus_sample_t sample_type, mus_header_t header_type, const char *comment)
  876. {
  877. int fd = MUS_ERROR, err;
  878. mus_sound_initialize();
  879. mus_sound_forget(arg);
  880. err = mus_write_header(arg, header_type, srate, chans, 0, sample_type, comment);
  881. if (err != MUS_ERROR)
  882. {
  883. fd = mus_file_open_write(arg);
  884. if (fd != -1)
  885. mus_file_open_descriptors(fd,
  886. arg,
  887. sample_type,
  888. mus_bytes_per_sample(sample_type),
  889. mus_header_data_location(),
  890. chans,
  891. header_type);
  892. }
  893. return(fd);
  894. }
  895. int mus_sound_reopen_output(const char *arg, int chans, mus_sample_t samp_type, mus_header_t type, mus_long_t data_loc)
  896. {
  897. int fd;
  898. mus_sound_initialize();
  899. fd = mus_file_reopen_write(arg);
  900. if (fd != -1)
  901. mus_file_open_descriptors(fd, arg, samp_type, mus_bytes_per_sample(samp_type), data_loc, chans, type);
  902. return(fd);
  903. }
  904. int mus_sound_close_input(int fd)
  905. {
  906. return(mus_file_close(fd)); /* this closes the clm file descriptors */
  907. }
  908. int mus_sound_close_output(int fd, mus_long_t bytes_of_data)
  909. {
  910. char *name;
  911. name = mus_file_fd_name(fd);
  912. if (name)
  913. {
  914. int err;
  915. mus_header_t old_type;
  916. char *fname;
  917. fname = mus_strdup(name);
  918. old_type = mus_file_header_type(fd);
  919. err = mus_file_close(fd); /* this frees the original fd->name, so we copied above */
  920. /* fd is NULL now */
  921. mus_sound_forget(fname);
  922. mus_header_change_data_size(fname, old_type, bytes_of_data);
  923. free(fname);
  924. return(err);
  925. }
  926. return(MUS_ERROR);
  927. }
  928. typedef enum {SF_CHANS, SF_SRATE, SF_TYPE, SF_SAMP_TYPE, SF_LOCATION, SF_SIZE} sf_field_t;
  929. static int mus_sound_set_field(const char *arg, sf_field_t field, int val)
  930. {
  931. sound_file *sf;
  932. int result = MUS_NO_ERROR;
  933. sf = get_sf(arg);
  934. if (sf)
  935. {
  936. switch (field)
  937. {
  938. case SF_CHANS:
  939. sf->chans = val;
  940. break;
  941. case SF_SRATE:
  942. sf->srate = val;
  943. break;
  944. case SF_TYPE:
  945. sf->header_type = (mus_header_t)val;
  946. break;
  947. case SF_SAMP_TYPE:
  948. sf->sample_type = (mus_sample_t)val;
  949. sf->datum_size = mus_bytes_per_sample(sf->sample_type);
  950. break;
  951. default:
  952. result = MUS_ERROR;
  953. break;
  954. }
  955. }
  956. else result = MUS_ERROR;
  957. return(result);
  958. }
  959. static int mus_sound_set_mus_long_t_field(const char *arg, sf_field_t field, mus_long_t val)
  960. {
  961. sound_file *sf;
  962. int result = MUS_NO_ERROR;
  963. sf = get_sf(arg);
  964. if (sf)
  965. {
  966. switch (field)
  967. {
  968. case SF_SIZE:
  969. sf->samples = val;
  970. break;
  971. case SF_LOCATION:
  972. sf->data_location = val;
  973. break;
  974. default:
  975. result = MUS_ERROR;
  976. break;
  977. }
  978. }
  979. else result = MUS_ERROR;
  980. return(result);
  981. }
  982. int mus_sound_set_chans(const char *arg, int val) {return(mus_sound_set_field(arg, SF_CHANS, val));}
  983. int mus_sound_set_srate(const char *arg, int val) {return(mus_sound_set_field(arg, SF_SRATE, val));}
  984. mus_header_t mus_sound_set_header_type(const char *arg, mus_header_t val) {return((mus_header_t)mus_sound_set_field(arg, SF_TYPE, val));}
  985. mus_sample_t mus_sound_set_sample_type(const char *arg, mus_sample_t val) {return((mus_sample_t)mus_sound_set_field(arg, SF_SAMP_TYPE, val));}
  986. int mus_sound_set_data_location(const char *arg, mus_long_t val) {return(mus_sound_set_mus_long_t_field(arg, SF_LOCATION, val));}
  987. int mus_sound_set_samples(const char *arg, mus_long_t val) {return(mus_sound_set_mus_long_t_field(arg, SF_SIZE, val));}
  988. int mus_sound_override_header(const char *arg, int srate, int chans, mus_sample_t samp_type, mus_header_t type, mus_long_t location, mus_long_t size)
  989. {
  990. sound_file *sf;
  991. int result = MUS_NO_ERROR;
  992. /* perhaps once a header has been over-ridden, we should not reset the relevant fields upon re-read? */
  993. sf = get_sf(arg);
  994. if (sf)
  995. {
  996. if (location != -1) sf->data_location = location;
  997. if (size != -1) sf->samples = size;
  998. if (samp_type != MUS_UNKNOWN_SAMPLE)
  999. {
  1000. sf->sample_type = samp_type;
  1001. sf->datum_size = mus_bytes_per_sample(samp_type);
  1002. }
  1003. if (srate != -1) sf->srate = srate;
  1004. if (chans != -1) sf->chans = chans;
  1005. if (type != MUS_UNKNOWN_HEADER) sf->header_type = (mus_header_t)type;
  1006. }
  1007. else result = MUS_ERROR;
  1008. return(result);
  1009. }
  1010. bool mus_sound_maxamp_exists(const char *ifile)
  1011. {
  1012. sound_file *sf;
  1013. sf = get_sf(ifile);
  1014. if ((sf) && (sf->maxtimes))
  1015. {
  1016. int i;
  1017. for (i = 0; i < sf->maxamps_size; i++)
  1018. if (sf->maxtimes[i] == -1)
  1019. return(false);
  1020. return(true);
  1021. }
  1022. return(false);
  1023. }
  1024. mus_long_t mus_sound_maxamps(const char *ifile, int chans, mus_float_t *vals, mus_long_t *times)
  1025. {
  1026. mus_long_t framples;
  1027. int ichans, chn;
  1028. sound_file *sf;
  1029. sf = get_sf(ifile);
  1030. if (sf->chans <= 0)
  1031. return(MUS_ERROR);
  1032. if ((sf) && (sf->maxamps))
  1033. {
  1034. if (chans > sf->maxamps_size)
  1035. ichans = sf->maxamps_size;
  1036. else ichans = chans;
  1037. for (chn = 0; chn < ichans; chn++)
  1038. {
  1039. times[chn] = sf->maxtimes[chn];
  1040. vals[chn] = sf->maxamps[chn];
  1041. }
  1042. framples = sf->samples / sf->chans;
  1043. return(framples);
  1044. }
  1045. {
  1046. int j, bufnum, ifd;
  1047. mus_long_t n, curframples;
  1048. mus_float_t *buffer, *samp;
  1049. mus_long_t *time;
  1050. mus_float_t **ibufs;
  1051. ifd = mus_sound_open_input(ifile);
  1052. if (ifd == MUS_ERROR) return(MUS_ERROR);
  1053. ichans = mus_sound_chans(ifile);
  1054. framples = mus_sound_framples(ifile);
  1055. if (framples == 0)
  1056. {
  1057. mus_sound_close_input(ifd);
  1058. return(0);
  1059. }
  1060. mus_file_seek_frample(ifd, 0);
  1061. ibufs = (mus_float_t **)calloc(ichans, sizeof(mus_float_t *));
  1062. bufnum = 8192;
  1063. for (j = 0; j < ichans; j++)
  1064. ibufs[j] = (mus_float_t *)calloc(bufnum, sizeof(mus_float_t));
  1065. time = (mus_long_t *)calloc(ichans, sizeof(mus_long_t));
  1066. samp = (mus_float_t *)calloc(ichans, sizeof(mus_float_t));
  1067. for (n = 0; n < framples; n += bufnum)
  1068. {
  1069. if ((n + bufnum) < framples)
  1070. curframples = bufnum;
  1071. else curframples = (framples - n);
  1072. mus_file_read(ifd, n, curframples, ichans, ibufs);
  1073. for (chn = 0; chn < ichans; chn++)
  1074. {
  1075. int i;
  1076. buffer = (mus_float_t *)(ibufs[chn]);
  1077. for (i = 0; i < curframples; i++)
  1078. {
  1079. mus_float_t abs_samp;
  1080. abs_samp = fabs(buffer[i]);
  1081. if (abs_samp > samp[chn])
  1082. {
  1083. time[chn] = i + n;
  1084. samp[chn] = abs_samp;
  1085. }
  1086. }
  1087. }
  1088. }
  1089. mus_sound_close_input(ifd);
  1090. /* fprintf(stderr, "set in mus_sound_maxamps\n"); */
  1091. mus_sound_set_maxamps(ifile, ichans, samp, time); /* save the complete set */
  1092. if (ichans > chans) ichans = chans;
  1093. for (chn = 0; chn < ichans; chn++)
  1094. {
  1095. times[chn] = time[chn];
  1096. vals[chn] = samp[chn];
  1097. }
  1098. free(time);
  1099. free(samp);
  1100. for (j = 0; j < ichans; j++) free(ibufs[j]);
  1101. free(ibufs);
  1102. return(framples);
  1103. }
  1104. }
  1105. int mus_sound_set_maxamps(const char *ifile, int chans, mus_float_t *vals, mus_long_t *times)
  1106. {
  1107. sound_file *sf;
  1108. int result = MUS_NO_ERROR;
  1109. /* fprintf(stderr, "set %s maxamps: %d %.3f %lld\n", ifile, chans, vals[0], times[0]); */
  1110. sf = get_sf(ifile);
  1111. if (sf)
  1112. {
  1113. int i, ichans = 0;
  1114. if (sf->maxamps)
  1115. {
  1116. if (chans > sf->maxamps_size)
  1117. ichans = sf->maxamps_size;
  1118. else ichans = chans;
  1119. for (i = 0; i < ichans; i++)
  1120. {
  1121. sf->maxtimes[i] = times[i];
  1122. sf->maxamps[i] = vals[i];
  1123. }
  1124. }
  1125. else
  1126. {
  1127. ichans = sf->chans;
  1128. if (sf->maxamps == NULL)
  1129. {
  1130. /* here we need to use the max, since the caller may be confused */
  1131. int max_chans;
  1132. max_chans = ichans;
  1133. if (max_chans < chans)
  1134. max_chans = chans;
  1135. sf->maxamps = (mus_float_t *)calloc(max_chans, sizeof(mus_float_t));
  1136. sf->maxtimes = (mus_long_t *)calloc(max_chans, sizeof(mus_long_t));
  1137. sf->maxamps_size = max_chans;
  1138. }
  1139. if (ichans > sf->maxamps_size)
  1140. ichans = sf->maxamps_size;
  1141. if (ichans > chans)
  1142. ichans = chans;
  1143. for (i = 0; i < ichans; i++)
  1144. {
  1145. sf->maxtimes[i] = times[i];
  1146. sf->maxamps[i] = vals[i];
  1147. }
  1148. }
  1149. }
  1150. else result = MUS_ERROR;
  1151. return(result);
  1152. }
  1153. bool mus_sound_channel_maxamp_exists(const char *file, int chan)
  1154. {
  1155. sound_file *sf;
  1156. sf = get_sf(file);
  1157. return((sf) &&
  1158. (sf->maxtimes) &&
  1159. (sf->maxamps_size > chan) &&
  1160. (sf->maxtimes[chan] != -1));
  1161. }
  1162. mus_float_t mus_sound_channel_maxamp(const char *file, int chan, mus_long_t *pos)
  1163. {
  1164. sound_file *sf;
  1165. sf = get_sf(file);
  1166. if ((chan < sf->maxamps_size) &&
  1167. (sf->maxtimes))
  1168. {
  1169. (*pos) = sf->maxtimes[chan];
  1170. return(sf->maxamps[chan]);
  1171. }
  1172. return(-1.0);
  1173. }
  1174. void mus_sound_channel_set_maxamp(const char *file, int chan, mus_float_t mx, mus_long_t pos)
  1175. {
  1176. sound_file *sf;
  1177. /* fprintf(stderr, "set %s maxamp: %.3f %lld\n", file, mx, pos); */
  1178. sf = get_sf(file);
  1179. if ((sf) &&
  1180. (sf->chans > chan))
  1181. {
  1182. if (!(sf->maxamps))
  1183. {
  1184. int i;
  1185. sf->maxamps = (mus_float_t *)malloc(sf->chans * sizeof(mus_float_t));
  1186. sf->maxtimes = (mus_long_t *)malloc(sf->chans * sizeof(mus_long_t));
  1187. sf->maxamps_size = sf->chans;
  1188. for (i = 0; i < sf->chans; i++)
  1189. {
  1190. sf->maxamps[i] = -1.0;
  1191. sf->maxtimes[i] = -1;
  1192. }
  1193. }
  1194. sf->maxamps[chan] = mx;
  1195. sf->maxtimes[chan] = pos;
  1196. }
  1197. }
  1198. mus_long_t mus_file_to_array(const char *filename, int chan, mus_long_t start, mus_long_t samples, mus_float_t *array)
  1199. {
  1200. int ifd, chans;
  1201. mus_long_t total_read;
  1202. mus_float_t **bufs;
  1203. ifd = mus_sound_open_input(filename);
  1204. if (ifd == MUS_ERROR) return(MUS_ERROR);
  1205. chans = mus_sound_chans(filename);
  1206. if ((chan >= chans) ||
  1207. (chan < 0))
  1208. {
  1209. mus_sound_close_input(ifd);
  1210. return(mus_error(MUS_NO_SUCH_CHANNEL, "mus_file_to_array can't read %s channel %d (file has %d chans)", filename, chan, chans));
  1211. }
  1212. bufs = (mus_float_t **)calloc(chans, sizeof(mus_float_t *));
  1213. bufs[chan] = array;
  1214. mus_file_seek_frample(ifd, start);
  1215. total_read = mus_file_read_any(ifd, start, chans, samples, bufs, bufs);
  1216. mus_sound_close_input(ifd);
  1217. free(bufs);
  1218. return(total_read);
  1219. }
  1220. const char *mus_array_to_file_with_error(const char *filename, mus_float_t *ddata, mus_long_t len, int srate, int channels)
  1221. {
  1222. /* put ddata into a sound file, taking byte order into account */
  1223. /* assume ddata is interleaved already if more than one channel */
  1224. int fd, err = MUS_NO_ERROR;
  1225. mus_long_t oloc;
  1226. mus_sound_forget(filename);
  1227. err = mus_write_header(filename, MUS_NEXT, srate, channels, len * channels, MUS_OUT_SAMPLE_TYPE, "array->file");
  1228. if (err != MUS_NO_ERROR)
  1229. return("mus_array_to_file can't create output file");
  1230. oloc = mus_header_data_location();
  1231. fd = mus_file_reopen_write(filename);
  1232. lseek(fd, oloc, SEEK_SET);
  1233. err = mus_file_open_descriptors(fd, filename,
  1234. MUS_OUT_SAMPLE_TYPE,
  1235. mus_bytes_per_sample(MUS_OUT_SAMPLE_TYPE),
  1236. oloc, channels, MUS_NEXT);
  1237. if (err != MUS_ERROR)
  1238. {
  1239. mus_float_t *bufs[1];
  1240. bufs[0] = ddata;
  1241. err = mus_file_write(fd, 0, len - 1, 1, bufs); /* 1 = chans?? */
  1242. }
  1243. mus_file_close(fd);
  1244. if (err == MUS_ERROR)
  1245. return("mus_array_to_file write error");
  1246. return(NULL);
  1247. }
  1248. int mus_array_to_file(const char *filename, mus_float_t *ddata, mus_long_t len, int srate, int channels)
  1249. {
  1250. const char *errmsg;
  1251. errmsg = mus_array_to_file_with_error(filename, ddata, len, srate, channels);
  1252. if (errmsg)
  1253. return(mus_error(MUS_CANT_OPEN_FILE, "%s", errmsg));
  1254. return(MUS_NO_ERROR);
  1255. }
  1256. mus_long_t mus_file_to_float_array(const char *filename, int chan, mus_long_t start, mus_long_t samples, mus_float_t *array)
  1257. {
  1258. return(mus_file_to_array(filename, chan, start, samples, array));
  1259. }
  1260. int mus_float_array_to_file(const char *filename, mus_float_t *ddata, mus_long_t len, int srate, int channels)
  1261. {
  1262. const char *errmsg;
  1263. errmsg = mus_array_to_file_with_error(filename, ddata, len, srate, channels);
  1264. if (errmsg)
  1265. return(mus_error(MUS_CANT_OPEN_FILE, "%s", errmsg));
  1266. return(MUS_NO_ERROR);
  1267. }
  1268. int mus_sound_initialize(void)
  1269. {
  1270. static bool sndlib_initialized = false;
  1271. if (!sndlib_initialized)
  1272. {
  1273. int err = MUS_NO_ERROR;
  1274. sndlib_initialized = true;
  1275. mus_error_handler = default_mus_error;
  1276. err = mus_header_initialize();
  1277. if (err == MUS_NO_ERROR)
  1278. err = mus_audio_initialize();
  1279. sound_tables = (sound_file ***)calloc(NUM_SOUND_TABLES, sizeof(sound_file **));
  1280. sound_table_sizes = (int *)calloc(NUM_SOUND_TABLES, sizeof(int));
  1281. return(err);
  1282. }
  1283. return(MUS_NO_ERROR);
  1284. }