generate.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. ** Copyright (C) 2002-2011 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 <math.h>
  36. #include <sndfile.h>
  37. #define BUFFER_LEN 4096
  38. static void encode_file (const char *infilename, const char *outfilename, int filetype) ;
  39. int
  40. main (int argc, char **argv)
  41. {
  42. if (argc != 2)
  43. { puts ("\nEncode a single input file into a number of different output ") ;
  44. puts ("encodings. These output encodings can then be moved to another ") ;
  45. puts ("OS for testing.\n") ;
  46. puts (" Usage : generate <filename>\n") ;
  47. exit (1) ;
  48. } ;
  49. /* A couple of standard WAV files. Make sure Win32 plays these. */
  50. encode_file (argv [1], "pcmu8.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
  51. encode_file (argv [1], "pcm16.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
  52. encode_file (argv [1], "imaadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
  53. encode_file (argv [1], "msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
  54. encode_file (argv [1], "gsm610.wav" , SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
  55. /* Soundforge W64. */
  56. encode_file (argv [1], "pcmu8.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_U8) ;
  57. encode_file (argv [1], "pcm16.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
  58. encode_file (argv [1], "imaadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM) ;
  59. encode_file (argv [1], "msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM) ;
  60. encode_file (argv [1], "gsm610.w64" , SF_FORMAT_W64 | SF_FORMAT_GSM610) ;
  61. return 0 ;
  62. } /* main */
  63. /*============================================================================================
  64. ** Helper functions and macros.
  65. */
  66. #define PUT_DOTS(k) \
  67. { while (k--) \
  68. putchar ('.') ; \
  69. putchar (' ') ; \
  70. }
  71. /*========================================================================================
  72. */
  73. static void
  74. encode_file (const char *infilename, const char *outfilename, int filetype)
  75. { static float buffer [BUFFER_LEN] ;
  76. SNDFILE *infile, *outfile ;
  77. SF_INFO sfinfo ;
  78. int k, readcount ;
  79. printf (" %s -> %s ", infilename, outfilename) ;
  80. fflush (stdout) ;
  81. k = 16 - strlen (outfilename) ;
  82. PUT_DOTS (k) ;
  83. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  84. if (! (infile = sf_open (infilename, SFM_READ, &sfinfo)))
  85. { printf ("Error : could not open file : %s\n", infilename) ;
  86. puts (sf_strerror (NULL)) ;
  87. exit (1) ;
  88. }
  89. sfinfo.format = filetype ;
  90. if (! sf_format_check (&sfinfo))
  91. { sf_close (infile) ;
  92. printf ("Invalid encoding\n") ;
  93. return ;
  94. } ;
  95. if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfo)))
  96. { printf ("Error : could not open file : %s\n", outfilename) ;
  97. puts (sf_strerror (NULL)) ;
  98. exit (1) ;
  99. } ;
  100. while ((readcount = (int) sf_read_float (infile, buffer, BUFFER_LEN)) > 0)
  101. sf_write_float (outfile, buffer, readcount) ;
  102. sf_close (infile) ;
  103. sf_close (outfile) ;
  104. printf ("ok\n") ;
  105. return ;
  106. } /* encode_file */