bin2ascii_main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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: December 2003 */
  35. /*************************************************************************/
  36. /* */
  37. /* Convert binary to ascii files (for tracks) */
  38. /*************************************************************************/
  39. #include <stdio.h>
  40. #include "cst_wave.h"
  41. #include "cst_tokenstream.h"
  42. #include "cst_args.h"
  43. int main(int argc, char **argv)
  44. {
  45. cst_val *files;
  46. const cst_val *f;
  47. cst_features *args;
  48. int i,n;
  49. const char *datatype;
  50. FILE *fd;
  51. int channels;
  52. int d_int;
  53. float d_flt;
  54. double d_dbl;
  55. int swap = FALSE;
  56. args = new_features();
  57. files =
  58. cst_args(argv,argc,
  59. "usage: bin2ascii OPTIONS\n"
  60. "Convert a binary file to ascii\n"
  61. "-type <string> int, float or double\n"
  62. "-swap Swap data\n"
  63. "-c <int> Number of channels\n",
  64. args);
  65. channels = get_param_int(args,"-c",12);
  66. if (feat_present(args,"-swap"))
  67. swap = TRUE;
  68. datatype = get_param_string(args,"-type","int");
  69. for (f=files; f; f=val_cdr(f))
  70. {
  71. fd = fopen(val_string(val_car(f)),"rb");
  72. n = 0;
  73. while (! feof(fd))
  74. {
  75. for (i=0; i<channels; i++)
  76. {
  77. if (cst_streq(datatype,"int"))
  78. {
  79. n = fread(&d_int,4,1,fd);
  80. if (n != 1) break;
  81. if (swap) d_int = SWAPINT(d_int);
  82. printf("%d ",d_int);
  83. }
  84. else if (cst_streq(datatype,"float"))
  85. {
  86. n = fread(&d_flt,4,1,fd);
  87. if (n != 1) break;
  88. if (swap) swapfloat(&d_flt);
  89. printf("%f ",d_flt);
  90. }
  91. else if (cst_streq(datatype,"double"))
  92. {
  93. n = fread(&d_dbl,8,1,fd);
  94. if (n != 1) break;
  95. if (swap) swapdouble(&d_dbl);
  96. printf("%g ",d_dbl);
  97. }
  98. else
  99. {
  100. fprintf(stderr,"bin2ascii: unknown data type \"%s\"",datatype);
  101. return -1;
  102. }
  103. }
  104. if (n==0 && i == 0)
  105. break;
  106. printf("\n");
  107. }
  108. }
  109. return 0;
  110. }