headerless_test.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. ** Copyright (C) 1999-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "sfconfig.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include <inttypes.h>
  24. #if HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #include <sndfile.h>
  28. #include "utils.h"
  29. #define BUFFER_SIZE (2000)
  30. static void old_test (void) ;
  31. static void headerless_test (const char * filename, int format, int expected) ;
  32. int
  33. main (void)
  34. {
  35. old_test () ;
  36. headerless_test ("raw.vox", SF_FORMAT_VOX_ADPCM, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM) ;
  37. headerless_test ("raw.gsm", SF_FORMAT_GSM610, SF_FORMAT_RAW | SF_FORMAT_GSM610) ;
  38. headerless_test ("raw.snd", SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ;
  39. headerless_test ("raw.au" , SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ;
  40. return 0 ;
  41. } /* main */
  42. static void
  43. headerless_test (const char * filename, int format, int expected)
  44. { static short buffer [BUFFER_SIZE] ;
  45. SNDFILE *file ;
  46. SF_INFO sfinfo ;
  47. int k ;
  48. format &= SF_FORMAT_SUBMASK ;
  49. print_test_name (__func__, filename) ;
  50. for (k = 0 ; k < BUFFER_SIZE ; k++)
  51. buffer [k] = k ;
  52. sfinfo.samplerate = 8000 ;
  53. sfinfo.frames = 0 ;
  54. sfinfo.channels = 1 ;
  55. sfinfo.format = SF_FORMAT_RAW | format ;
  56. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  57. if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
  58. { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ;
  59. fflush (stdout) ;
  60. puts (sf_strerror (file)) ;
  61. exit (1) ;
  62. } ;
  63. sf_close (file) ;
  64. memset (buffer, 0, sizeof (buffer)) ;
  65. /* We should be able to detect these so clear sfinfo. */
  66. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  67. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  68. if (sfinfo.format != expected)
  69. { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, expected, sfinfo.format) ;
  70. exit (1) ;
  71. } ;
  72. if (sfinfo.frames < BUFFER_SIZE)
  73. { printf ("Line %d: Incorrect number of.frames in file. (%d => %" PRId64 ")\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
  74. exit (1) ;
  75. } ;
  76. if (sfinfo.channels != 1)
  77. { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
  78. exit (1) ;
  79. } ;
  80. check_log_buffer_or_die (file, __LINE__) ;
  81. sf_close (file) ;
  82. printf ("ok\n") ;
  83. unlink (filename) ;
  84. } /* headerless_test */
  85. static void
  86. old_test (void)
  87. { static short buffer [BUFFER_SIZE] ;
  88. SNDFILE *file ;
  89. SF_INFO sfinfo ;
  90. int k, filetype ;
  91. const char *filename = "headerless.wav" ;
  92. print_test_name (__func__, "") ;
  93. for (k = 0 ; k < BUFFER_SIZE ; k++)
  94. buffer [k] = k ;
  95. filetype = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
  96. sfinfo.samplerate = 32000 ;
  97. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  98. sfinfo.channels = 1 ;
  99. sfinfo.format = filetype ;
  100. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  101. if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
  102. { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ;
  103. fflush (stdout) ;
  104. puts (sf_strerror (file)) ;
  105. exit (1) ;
  106. } ;
  107. sf_close (file) ;
  108. memset (buffer, 0, sizeof (buffer)) ;
  109. /* Read as RAW but get the bit width and endian-ness correct. */
  110. sfinfo.format = filetype = SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
  111. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  112. if (sfinfo.format != filetype)
  113. { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
  114. exit (1) ;
  115. } ;
  116. if (sfinfo.frames < BUFFER_SIZE)
  117. { printf ("Line %d: Incorrect number of.frames in file. (%d => %" PRId64 ")\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
  118. exit (1) ;
  119. } ;
  120. if (sfinfo.channels != 1)
  121. { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
  122. exit (1) ;
  123. } ;
  124. check_log_buffer_or_die (file, __LINE__) ;
  125. if ((k = sf_read_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
  126. { printf ("Line %d: short read (%d).\n", __LINE__, k) ;
  127. exit (1) ;
  128. } ;
  129. for (k = 0 ; k < BUFFER_SIZE - 22 ; k++)
  130. if (buffer [k + 22] != k)
  131. { printf ("Line %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__, k, k, buffer [k]) ;
  132. exit (1) ;
  133. } ;
  134. sf_close (file) ;
  135. printf ("ok\n") ;
  136. unlink (filename) ;
  137. } /* old_test */