format_check_test.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. ** Copyright (C) 2011-2017 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. #if HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #else
  25. #include "sf_unistd.h"
  26. #endif
  27. #include "sndfile.h"
  28. #include "utils.h"
  29. static void format_error_test (void) ;
  30. static void format_combo_test (void) ;
  31. int
  32. main (void)
  33. {
  34. format_error_test () ;
  35. format_combo_test () ;
  36. return 0 ;
  37. } /* main */
  38. /*==============================================================================
  39. */
  40. static void
  41. format_error_test (void)
  42. { const char *filename = "format-error.wav" ;
  43. SNDFILE *file ;
  44. SF_INFO info ;
  45. print_test_name (__func__, NULL) ;
  46. memset (&info, 0, sizeof (info)) ;
  47. info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
  48. info.channels = 1 ;
  49. info.samplerate = 44100 ;
  50. info.format = SF_FORMAT_WAV ;
  51. file = sf_open (filename, SFM_WRITE, &info) ;
  52. exit_if_true (file != NULL, "\n\nLine %d : Format should not be valid.\n\n", __LINE__) ;
  53. exit_if_true (
  54. strstr (sf_strerror (NULL), "minor format") == NULL,
  55. "\n\nLine %d : Error string should reference bad 'minor format'.\n\n", __LINE__
  56. ) ;
  57. info.format = SF_FORMAT_PCM_16 ;
  58. file = sf_open (filename, SFM_WRITE, &info) ;
  59. exit_if_true (file != NULL, "\n\nLine %d : Format should not be valid.\n\n", __LINE__) ;
  60. exit_if_true (
  61. strstr (sf_strerror (NULL), "major format") == NULL,
  62. "\n\nLine %d : Error string should reference bad 'major format'.\n\n", __LINE__
  63. ) ;
  64. unlink (filename) ;
  65. puts ("ok") ;
  66. } /* format_error_test */
  67. static void
  68. format_combo_test (void)
  69. { int container_max, codec_max, cont, codec ;
  70. print_test_name (__func__, NULL) ;
  71. sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &container_max, sizeof (container_max)) ;
  72. sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &codec_max, sizeof (codec_max)) ;
  73. for (cont = 0 ; cont < container_max + 10 ; cont ++)
  74. { SF_FORMAT_INFO major_fmt_info ;
  75. memset (&major_fmt_info, 0, sizeof (major_fmt_info)) ;
  76. major_fmt_info.format = cont ;
  77. (void) sf_command (NULL, SFC_GET_FORMAT_MAJOR, &major_fmt_info, sizeof (major_fmt_info)) ;
  78. for (codec = 0 ; codec < codec_max + 10 ; codec ++)
  79. { SF_FORMAT_INFO subtype_fmt_info ;
  80. SNDFILE * sndfile ;
  81. SF_INFO info ;
  82. char filename [128] ;
  83. int subtype_is_valid, check_is_valid ;
  84. memset (&info, 0, sizeof (info)) ;
  85. memset (&subtype_fmt_info, 0, sizeof (subtype_fmt_info)) ;
  86. subtype_fmt_info.format = codec ;
  87. subtype_is_valid = sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &subtype_fmt_info, sizeof (subtype_fmt_info)) == 0 ;
  88. /* Opus only works with a fixed set of sample rates. */
  89. if (subtype_fmt_info.format == SF_FORMAT_OPUS)
  90. sf_info_setup (&info, major_fmt_info.format | subtype_fmt_info.format, 24000, 1) ;
  91. else
  92. sf_info_setup (&info, major_fmt_info.format | subtype_fmt_info.format, 22050, 1) ;
  93. check_is_valid = sf_format_check (&info) ;
  94. exit_if_true (
  95. NOT (subtype_is_valid) && check_is_valid,
  96. "\n\nLine %d : Subtype is not valid but checks ok.\n",
  97. __LINE__
  98. ) ;
  99. /* Only have decode, not encode support for MPEG Layer I and II */
  100. if (subtype_fmt_info.format == SF_FORMAT_MPEG_LAYER_I ||
  101. subtype_fmt_info.format == SF_FORMAT_MPEG_LAYER_II)
  102. continue ;
  103. /* MPEG Layer III in WAV is decode only currently */
  104. if (subtype_fmt_info.format == SF_FORMAT_MPEG_LAYER_III &&
  105. major_fmt_info.format == SF_FORMAT_WAV)
  106. continue ;
  107. snprintf (filename, sizeof (filename), "format-check.%s", major_fmt_info.extension) ;
  108. sndfile = sf_open (filename, SFM_WRITE, &info) ;
  109. sf_close (sndfile) ;
  110. unlink (filename) ;
  111. if (major_fmt_info.extension != NULL && strcmp (major_fmt_info.extension, "sd2") == 0)
  112. { snprintf (filename, sizeof (filename), "._format-check.%s", major_fmt_info.extension) ;
  113. unlink (filename) ;
  114. } ;
  115. exit_if_true (
  116. sndfile && NOT (check_is_valid),
  117. "\n\nError : Format was not valid but file opened correctly.\n"
  118. " Container : %s\n"
  119. " Codec : %s\n\n",
  120. major_fmt_info.name, subtype_fmt_info.name
  121. ) ;
  122. exit_if_true (
  123. NOT (sndfile) && check_is_valid,
  124. "\n\nError : Format was valid but file failed to open.\n"
  125. " Container : %s\n"
  126. " Codec : %s\n\n",
  127. major_fmt_info.name, subtype_fmt_info.name
  128. ) ;
  129. } ;
  130. } ;
  131. puts ("ok") ;
  132. } /* format_combo_test */