headerless_test.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. ** Copyright (C) 1999-2012 Erik de Castro Lopo <[email protected]>
  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. #else
  27. #include "sf_unistd.h"
  28. #endif
  29. #include <sndfile.h>
  30. #include "utils.h"
  31. #define BUFFER_SIZE (2000)
  32. static void old_test (void) ;
  33. static void headerless_test (const char * filename, int format, int expected) ;
  34. int
  35. main (void)
  36. {
  37. old_test () ;
  38. headerless_test ("headerless.vox", SF_FORMAT_VOX_ADPCM, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM) ;
  39. headerless_test ("headerless.gsm", SF_FORMAT_GSM610, SF_FORMAT_RAW | SF_FORMAT_GSM610) ;
  40. headerless_test ("headerless.snd", SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ;
  41. headerless_test ("headerless.au" , SF_FORMAT_ULAW, SF_FORMAT_RAW | SF_FORMAT_ULAW) ;
  42. return 0 ;
  43. } /* main */
  44. static void
  45. headerless_test (const char * filename, int format, int expected)
  46. { static short buffer [BUFFER_SIZE] ;
  47. SNDFILE *file ;
  48. SF_INFO sfinfo ;
  49. int k ;
  50. format &= SF_FORMAT_SUBMASK ;
  51. print_test_name (__func__, filename) ;
  52. for (k = 0 ; k < BUFFER_SIZE ; k++)
  53. buffer [k] = k ;
  54. sfinfo.samplerate = 8000 ;
  55. sfinfo.frames = 0 ;
  56. sfinfo.channels = 1 ;
  57. sfinfo.format = SF_FORMAT_RAW | format ;
  58. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  59. if ((k = (int) sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
  60. { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ;
  61. fflush (stdout) ;
  62. puts (sf_strerror (file)) ;
  63. exit (1) ;
  64. } ;
  65. sf_close (file) ;
  66. memset (buffer, 0, sizeof (buffer)) ;
  67. /* We should be able to detect these so clear sfinfo. */
  68. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  69. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  70. if (sfinfo.format != expected)
  71. { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, expected, sfinfo.format) ;
  72. exit (1) ;
  73. } ;
  74. if (sfinfo.frames < BUFFER_SIZE)
  75. { printf ("Line %d: Incorrect number of.frames in file. (%d => %" PRId64 ")\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
  76. exit (1) ;
  77. } ;
  78. if (sfinfo.channels != 1)
  79. { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
  80. exit (1) ;
  81. } ;
  82. check_log_buffer_or_die (file, __LINE__) ;
  83. sf_close (file) ;
  84. printf ("ok\n") ;
  85. unlink (filename) ;
  86. } /* headerless_test */
  87. static void
  88. old_test (void)
  89. { static short buffer [BUFFER_SIZE] ;
  90. SNDFILE *file ;
  91. SF_INFO sfinfo ;
  92. int k, filetype ;
  93. const char *filename = "headerless.wav" ;
  94. print_test_name (__func__, "") ;
  95. for (k = 0 ; k < BUFFER_SIZE ; k++)
  96. buffer [k] = k ;
  97. filetype = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
  98. sfinfo.samplerate = 32000 ;
  99. sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
  100. sfinfo.channels = 1 ;
  101. sfinfo.format = filetype ;
  102. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  103. if ((k = (int) sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
  104. { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ;
  105. fflush (stdout) ;
  106. puts (sf_strerror (file)) ;
  107. exit (1) ;
  108. } ;
  109. sf_close (file) ;
  110. memset (buffer, 0, sizeof (buffer)) ;
  111. /* Read as RAW but get the bit width and endian-ness correct. */
  112. sfinfo.format = filetype = SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
  113. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  114. if (sfinfo.format != filetype)
  115. { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
  116. exit (1) ;
  117. } ;
  118. if (sfinfo.frames < BUFFER_SIZE)
  119. { printf ("Line %d: Incorrect number of.frames in file. (%d => %" PRId64 ")\n", __LINE__, BUFFER_SIZE, sfinfo.frames) ;
  120. exit (1) ;
  121. } ;
  122. if (sfinfo.channels != 1)
  123. { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
  124. exit (1) ;
  125. } ;
  126. check_log_buffer_or_die (file, __LINE__) ;
  127. if ((k = (int) sf_read_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
  128. { printf ("Line %d: short read (%d).\n", __LINE__, k) ;
  129. exit (1) ;
  130. } ;
  131. for (k = 0 ; k < BUFFER_SIZE - 22 ; k++)
  132. if (buffer [k + 22] != k)
  133. { printf ("Line %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__, k, k, buffer [k]) ;
  134. exit (1) ;
  135. } ;
  136. sf_close (file) ;
  137. printf ("ok\n") ;
  138. unlink (filename) ;
  139. } /* old_test */