sndfile-to-text.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. ** Copyright (C) 2008-2016 Erik de Castro Lopo <[email protected]>
  3. **
  4. ** All rights reserved.
  5. **
  6. ** Redistribution and use in source and binary forms, with or without
  7. ** modification, are permitted provided that the following conditions are
  8. ** met:
  9. **
  10. ** * Redistributions of source code must retain the above copyright
  11. ** notice, this list of conditions and the following disclaimer.
  12. ** * Redistributions in binary form must reproduce the above copyright
  13. ** notice, this list of conditions and the following disclaimer in
  14. ** the documentation and/or other materials provided with the
  15. ** distribution.
  16. ** * Neither the author nor the names of any contributors may be used
  17. ** to endorse or promote products derived from this software without
  18. ** specific prior written permission.
  19. **
  20. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30. ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <ctype.h>
  36. #include <float.h>
  37. #include <sndfile.h>
  38. #define BLOCK_SIZE 4096
  39. #ifdef DBL_DECIMAL_DIG
  40. #define OP_DBL_Digs (DBL_DECIMAL_DIG)
  41. #else
  42. #ifdef DECIMAL_DIG
  43. #define OP_DBL_Digs (DECIMAL_DIG)
  44. #else
  45. #define OP_DBL_Digs (DBL_DIG + 3)
  46. #endif
  47. #endif
  48. static void
  49. print_usage (char *progname)
  50. { printf ("\nUsage : %s [--full-precision] <input file> <output file>\n", progname) ;
  51. puts ("\n"
  52. " Where the output file will contain a line for each frame\n"
  53. " and a column for each channel.\n"
  54. ) ;
  55. } /* print_usage */
  56. static int
  57. convert_to_text (SNDFILE * infile, FILE * outfile, int channels, int full_precision)
  58. { float *buf ;
  59. sf_count_t frames ;
  60. int k, m, readcount ;
  61. buf = malloc (BLOCK_SIZE * sizeof (float)) ;
  62. if (buf == NULL)
  63. { printf ("Error : Out of memory.\n\n") ;
  64. return 1 ;
  65. } ;
  66. frames = BLOCK_SIZE / channels ;
  67. while ((readcount = (int) sf_readf_float (infile, buf, frames)) > 0)
  68. { for (k = 0 ; k < readcount ; k++)
  69. { for (m = 0 ; m < channels ; m++)
  70. if (full_precision)
  71. fprintf (outfile, " %.*e", OP_DBL_Digs - 1, buf [k * channels + m]) ;
  72. else
  73. fprintf (outfile, " % 12.10f", buf [k * channels + m]) ;
  74. fprintf (outfile, "\n") ;
  75. } ;
  76. } ;
  77. free (buf) ;
  78. return 0 ;
  79. } /* convert_to_text */
  80. int
  81. main (int argc, char * argv [])
  82. { char *progname, *infilename, *outfilename ;
  83. SNDFILE *infile = NULL ;
  84. FILE *outfile = NULL ;
  85. SF_INFO sfinfo ;
  86. int full_precision = 0 ;
  87. int ret = 1 ;
  88. progname = strrchr (argv [0], '/') ;
  89. progname = progname ? progname + 1 : argv [0] ;
  90. switch (argc)
  91. { case 4 :
  92. if (!strcmp ("--full-precision", argv [3]))
  93. { print_usage (progname) ;
  94. goto cleanup ;
  95. } ;
  96. full_precision = 1 ;
  97. argv++ ;
  98. case 3 :
  99. break ;
  100. default:
  101. print_usage (progname) ;
  102. goto cleanup ;
  103. } ;
  104. infilename = argv [1] ;
  105. outfilename = argv [2] ;
  106. if (strcmp (infilename, outfilename) == 0)
  107. { printf ("Error : Input and output filenames are the same.\n\n") ;
  108. print_usage (progname) ;
  109. goto cleanup ;
  110. } ;
  111. if (infilename [0] == '-')
  112. { printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
  113. print_usage (progname) ;
  114. goto cleanup ;
  115. } ;
  116. if (outfilename [0] == '-')
  117. { printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
  118. print_usage (progname) ;
  119. goto cleanup ;
  120. } ;
  121. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  122. if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL)
  123. { printf ("Not able to open input file %s.\n", infilename) ;
  124. puts (sf_strerror (NULL)) ;
  125. goto cleanup ;
  126. } ;
  127. /* Open the output file. */
  128. if ((outfile = fopen (outfilename, "w")) == NULL)
  129. { printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
  130. goto cleanup ;
  131. } ;
  132. fprintf (outfile, "# Converted from file %s.\n", infilename) ;
  133. fprintf (outfile, "# Channels %d, Sample rate %d\n", sfinfo.channels, sfinfo.samplerate) ;
  134. ret = convert_to_text (infile, outfile, sfinfo.channels, full_precision) ;
  135. cleanup :
  136. sf_close (infile) ;
  137. if (outfile != NULL)
  138. fclose (outfile) ;
  139. return ret ;
  140. } /* main */