combine_waves_main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*************************************************************************/
  2. /* */
  3. /* Language Technologies Institute */
  4. /* Carnegie Mellon University */
  5. /* Copyright (c) 2003 */
  6. /* All Rights Reserved. */
  7. /* */
  8. /* Permission is hereby granted, free of charge, to use and distribute */
  9. /* this software and its documentation without restriction, including */
  10. /* without limitation the rights to use, copy, modify, merge, publish, */
  11. /* distribute, sublicense, and/or sell copies of this work, and to */
  12. /* permit persons to whom this work is furnished to do so, subject to */
  13. /* the following conditions: */
  14. /* 1. The code must retain the above copyright notice, this list of */
  15. /* conditions and the following disclaimer. */
  16. /* 2. Any modifications must be clearly marked as such. */
  17. /* 3. Original authors' names are not deleted. */
  18. /* 4. The authors' names are not used to endorse or promote products */
  19. /* derived from this software without specific prior written */
  20. /* permission. */
  21. /* */
  22. /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
  23. /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
  24. /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
  25. /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
  26. /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
  27. /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
  28. /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
  29. /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
  30. /* THIS SOFTWARE. */
  31. /* */
  32. /*************************************************************************/
  33. /* Author: Alan W Black (awb@cs.cmu.edu) */
  34. /* Date: November 2003 */
  35. /*************************************************************************/
  36. /* */
  37. /* Combine multichannel waveforms into a single waveform file */
  38. /*************************************************************************/
  39. #include <stdio.h>
  40. #include "cst_wave.h"
  41. #include "cst_tokenstream.h"
  42. #include "cst_args.h"
  43. float decode_time(const char *ctime)
  44. {
  45. /* return number of seconds from time */
  46. return 0.0;
  47. }
  48. cst_val *get_wavelist(const char *wavelistfile)
  49. {
  50. cst_val *l = 0;
  51. cst_tokenstream *ts;
  52. const char *token;
  53. int i=0;
  54. ts = ts_open(wavelistfile);
  55. if (!ts)
  56. {
  57. fprintf(stderr,"combine_waves: can't open \"%s\"\n",wavelistfile);
  58. return 0;
  59. }
  60. while ((token=ts_get(ts)) != 0)
  61. {
  62. l = cons_val(string_val(token),l);
  63. i++;
  64. }
  65. if (i%2 != 0)
  66. {
  67. fprintf(stderr,"combine_waves: doesn't have matched pairs \"%s\"\n",wavelistfile);
  68. delete_val(l);
  69. l = 0;
  70. }
  71. ts_close(ts);
  72. return val_reverse(l);
  73. }
  74. int main(int argc, char **argv)
  75. {
  76. cst_wave *nw, *all;
  77. cst_val *files;
  78. const cst_val *w;
  79. cst_val *wavelist;
  80. cst_features *args;
  81. int i,j;
  82. float ntime;
  83. int stime;
  84. const char *nwfile;
  85. args = new_features();
  86. files =
  87. cst_args(argv,argc,
  88. "usage: combine_waves OPTIONS\n"
  89. "Combine waves into single waveform\n"
  90. "-o <string> Output waveform\n"
  91. "-f <int> Input sample rate (for raw input)\n"
  92. "-itype <string> Input type, raw or headered\n"
  93. "-wavelist <string> File containing times and wave filenames\n",
  94. args);
  95. wavelist = get_wavelist(get_param_string(args,"-wavelist","-"));
  96. if (wavelist == 0)
  97. return -1;
  98. all = new_wave();
  99. for (w = wavelist; w; w = val_cdr(w))
  100. {
  101. ntime = decode_time(val_string(val_car(w)));
  102. nwfile = val_string(val_car(val_cdr(w)));
  103. nw = new_wave();
  104. if (cst_wave_load_riff(nw,nwfile) != CST_OK_FORMAT)
  105. {
  106. fprintf(stderr,
  107. "combine_waves: can't read file or wrong format \"%s\"\n",
  108. nwfile);
  109. continue;
  110. }
  111. stime = ntime * nw->sample_rate;
  112. cst_wave_resize(all,stime+nw->num_samples,1);
  113. for (i=0,j=stime; i<nw->num_samples; i++,j++)
  114. {
  115. /* this will cause overflows */
  116. all->samples[j] += nw->samples[i];
  117. }
  118. delete_wave(nw);
  119. }
  120. cst_wave_save_riff(all,get_param_string(args,"-o","-"));
  121. return 0;
  122. }