2
0

command_test.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. /*
  2. ** Copyright (C) 2001-2013 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 <stdint.h>
  22. #include <inttypes.h>
  23. #include <string.h>
  24. #include <time.h>
  25. #if HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #include <math.h>
  29. #include <sndfile.h>
  30. #include "utils.h"
  31. #define BUFFER_LEN (1 << 10)
  32. #define LOG_BUFFER_SIZE 1024
  33. static void float_norm_test (const char *filename) ;
  34. static void double_norm_test (const char *filename) ;
  35. static void format_tests (void) ;
  36. static void calc_peak_test (int filetype, const char *filename) ;
  37. static void truncate_test (const char *filename, int filetype) ;
  38. static void instrument_test (const char *filename, int filetype) ;
  39. static void channel_map_test (const char *filename, int filetype) ;
  40. static void current_sf_info_test (const char *filename) ;
  41. static void raw_needs_endswap_test (const char *filename, int filetype) ;
  42. static void broadcast_test (const char *filename, int filetype) ;
  43. static void broadcast_rdwr_test (const char *filename, int filetype) ;
  44. static void broadcast_coding_history_test (const char *filename) ;
  45. static void broadcast_coding_history_size (const char *filename) ;
  46. /* Cart Chunk tests */
  47. static void cart_test (const char *filename, int filetype) ;
  48. static void cart_rdwr_test (const char *filename, int filetype) ;
  49. /* Force the start of this buffer to be double aligned. Sparc-solaris will
  50. ** choke if its not.
  51. */
  52. static int int_data [BUFFER_LEN] ;
  53. static float float_data [BUFFER_LEN] ;
  54. static double double_data [BUFFER_LEN] ;
  55. int
  56. main (int argc, char *argv [])
  57. { int do_all = 0 ;
  58. int test_count = 0 ;
  59. if (argc != 2)
  60. { printf ("Usage : %s <test>\n", argv [0]) ;
  61. printf (" Where <test> is one of the following:\n") ;
  62. printf (" ver - test sf_command (SFC_GETLIB_VERSION)\n") ;
  63. printf (" norm - test floating point normalisation\n") ;
  64. printf (" format - test format string commands\n") ;
  65. printf (" peak - test peak calculation\n") ;
  66. printf (" trunc - test file truncation\n") ;
  67. printf (" inst - test set/get of SF_INSTRUMENT.\n") ;
  68. printf (" chanmap - test set/get of channel map data..\n") ;
  69. printf (" bext - test set/get of SF_BROADCAST_INFO.\n") ;
  70. printf (" bextch - test set/get of SF_BROADCAST_INFO coding_history.\n") ;
  71. printf (" cart - test set/get of SF_CART_INFO.\n") ;
  72. printf (" rawend - test SFC_RAW_NEEDS_ENDSWAP.\n") ;
  73. printf (" all - perform all tests\n") ;
  74. exit (1) ;
  75. } ;
  76. do_all = ! strcmp (argv [1], "all") ;
  77. if (do_all || strcmp (argv [1], "ver") == 0)
  78. { char buffer [128] ;
  79. print_test_name ("version_test", "(none)") ;
  80. buffer [0] = 0 ;
  81. sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ;
  82. if (strlen (buffer) < 1)
  83. { printf ("Line %d: could not retrieve lib version.\n", __LINE__) ;
  84. exit (1) ;
  85. } ;
  86. puts ("ok") ;
  87. test_count ++ ;
  88. } ;
  89. if (do_all || strcmp (argv [1], "norm") == 0)
  90. { /* Preliminary float/double normalisation tests. More testing
  91. ** is done in the program 'floating_point_test'.
  92. */
  93. float_norm_test ("float.wav") ;
  94. double_norm_test ("double.wav") ;
  95. test_count ++ ;
  96. } ;
  97. if (do_all || strcmp (argv [1], "peak") == 0)
  98. { calc_peak_test (SF_ENDIAN_BIG | SF_FORMAT_RAW, "be-peak.raw") ;
  99. calc_peak_test (SF_ENDIAN_LITTLE | SF_FORMAT_RAW, "le-peak.raw") ;
  100. test_count ++ ;
  101. } ;
  102. if (do_all || ! strcmp (argv [1], "format"))
  103. { format_tests () ;
  104. test_count ++ ;
  105. } ;
  106. if (do_all || strcmp (argv [1], "trunc") == 0)
  107. { truncate_test ("truncate.raw", SF_FORMAT_RAW | SF_FORMAT_PCM_32) ;
  108. truncate_test ("truncate.au" , SF_FORMAT_AU | SF_FORMAT_PCM_16) ;
  109. test_count ++ ;
  110. } ;
  111. if (do_all || strcmp (argv [1], "inst") == 0)
  112. { instrument_test ("instrument.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
  113. instrument_test ("instrument.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_24) ;
  114. /*-instrument_test ("instrument.xi", SF_FORMAT_XI | SF_FORMAT_DPCM_16) ;-*/
  115. test_count ++ ;
  116. } ;
  117. if (do_all || strcmp (argv [1], "current_sf_info") == 0)
  118. { current_sf_info_test ("current.wav") ;
  119. test_count ++ ;
  120. } ;
  121. if (do_all || strcmp (argv [1], "bext") == 0)
  122. { broadcast_test ("broadcast.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
  123. broadcast_rdwr_test ("broadcast.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
  124. broadcast_test ("broadcast.wavex", SF_FORMAT_WAVEX | SF_FORMAT_PCM_16) ;
  125. broadcast_rdwr_test ("broadcast.wavex", SF_FORMAT_WAVEX | SF_FORMAT_PCM_16) ;
  126. broadcast_test ("broadcast.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
  127. broadcast_rdwr_test ("broadcast.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
  128. test_count ++ ;
  129. } ;
  130. if (do_all || strcmp (argv [1], "cart") == 0)
  131. { cart_test ("cart.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
  132. cart_rdwr_test ("cart.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
  133. cart_test ("cart.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
  134. cart_rdwr_test ("cart.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
  135. test_count ++ ;
  136. } ;
  137. if (do_all || strcmp (argv [1], "bextch") == 0)
  138. { broadcast_coding_history_test ("coding_history.wav") ;
  139. broadcast_coding_history_size ("coding_hist_size.wav") ;
  140. test_count ++ ;
  141. } ;
  142. if (do_all || strcmp (argv [1], "chanmap") == 0)
  143. { channel_map_test ("chanmap.wavex", SF_FORMAT_WAVEX | SF_FORMAT_PCM_16) ;
  144. channel_map_test ("chanmap.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
  145. channel_map_test ("chanmap.aifc" , SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
  146. channel_map_test ("chanmap.caf" , SF_FORMAT_CAF | SF_FORMAT_PCM_16) ;
  147. test_count ++ ;
  148. } ;
  149. if (do_all || strcmp (argv [1], "rawend") == 0)
  150. { raw_needs_endswap_test ("raw_end.wav", SF_FORMAT_WAV) ;
  151. raw_needs_endswap_test ("raw_end.wavex", SF_FORMAT_WAVEX) ;
  152. raw_needs_endswap_test ("raw_end.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV) ;
  153. raw_needs_endswap_test ("raw_end.aiff", SF_FORMAT_AIFF) ;
  154. raw_needs_endswap_test ("raw_end.aiff_le", SF_ENDIAN_LITTLE | SF_FORMAT_AIFF) ;
  155. test_count ++ ;
  156. } ;
  157. if (test_count == 0)
  158. { printf ("Mono : ************************************\n") ;
  159. printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
  160. printf ("Mono : ************************************\n") ;
  161. return 1 ;
  162. } ;
  163. return 0 ;
  164. } /* main */
  165. /*============================================================================================
  166. ** Here are the test functions.
  167. */
  168. static void
  169. float_norm_test (const char *filename)
  170. { SNDFILE *file ;
  171. SF_INFO sfinfo ;
  172. unsigned int k ;
  173. print_test_name ("float_norm_test", filename) ;
  174. sfinfo.samplerate = 44100 ;
  175. sfinfo.format = (SF_FORMAT_RAW | SF_FORMAT_PCM_16) ;
  176. sfinfo.channels = 1 ;
  177. sfinfo.frames = BUFFER_LEN ;
  178. /* Create float_data with all values being less than 1.0. */
  179. for (k = 0 ; k < BUFFER_LEN / 2 ; k++)
  180. float_data [k] = (k + 5) / (2.0 * BUFFER_LEN) ;
  181. for (k = BUFFER_LEN / 2 ; k < BUFFER_LEN ; k++)
  182. float_data [k] = (k + 5) ;
  183. if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
  184. { printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;
  185. fflush (stdout) ;
  186. puts (sf_strerror (NULL)) ;
  187. exit (1) ;
  188. } ;
  189. /* Normalisation is on by default so no need to do anything here. */
  190. if ((k = sf_write_float (file, float_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
  191. { printf ("Line %d: sf_write_float failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
  192. exit (1) ;
  193. } ;
  194. /* Turn normalisation off. */
  195. sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
  196. if ((k = sf_write_float (file, float_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
  197. { printf ("Line %d: sf_write_float failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
  198. exit (1) ;
  199. } ;
  200. sf_close (file) ;
  201. /* sfinfo struct should still contain correct data. */
  202. if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
  203. { printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;
  204. fflush (stdout) ;
  205. puts (sf_strerror (NULL)) ;
  206. exit (1) ;
  207. } ;
  208. if (sfinfo.format != (SF_FORMAT_RAW | SF_FORMAT_PCM_16))
  209. { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (SF_FORMAT_RAW | SF_FORMAT_PCM_16), sfinfo.format) ;
  210. exit (1) ;
  211. } ;
  212. if (sfinfo.frames != BUFFER_LEN)
  213. { printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %" PRId64 ")\n", __LINE__, BUFFER_LEN, sfinfo.frames) ;
  214. exit (1) ;
  215. } ;
  216. if (sfinfo.channels != 1)
  217. { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
  218. exit (1) ;
  219. } ;
  220. /* Read float_data and check that it is normalised (ie default). */
  221. if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
  222. { printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
  223. exit (1) ;
  224. } ;
  225. for (k = 0 ; k < BUFFER_LEN ; k++)
  226. if (float_data [k] >= 1.0)
  227. { printf ("\n\nLine %d: float_data [%d] == %f which is greater than 1.0\n", __LINE__, k, float_data [k]) ;
  228. exit (1) ;
  229. } ;
  230. /* Seek to start of file, turn normalisation off, read float_data and check again. */
  231. sf_seek (file, 0, SEEK_SET) ;
  232. sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
  233. if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
  234. { printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
  235. exit (1) ;
  236. } ;
  237. for (k = 0 ; k < BUFFER_LEN ; k++)
  238. if (float_data [k] < 1.0)
  239. { printf ("\n\nLine %d: float_data [%d] == %f which is less than 1.0\n", __LINE__, k, float_data [k]) ;
  240. exit (1) ;
  241. } ;
  242. /* Seek to start of file, turn normalisation on, read float_data and do final check. */
  243. sf_seek (file, 0, SEEK_SET) ;
  244. sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_TRUE) ;
  245. if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
  246. { printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
  247. exit (1) ;
  248. } ;
  249. for (k = 0 ; k < BUFFER_LEN ; k++)
  250. if (float_data [k] > 1.0)
  251. { printf ("\n\nLine %d: float_data [%d] == %f which is greater than 1.0\n", __LINE__, k, float_data [k]) ;
  252. exit (1) ;
  253. } ;
  254. sf_close (file) ;
  255. unlink (filename) ;
  256. printf ("ok\n") ;
  257. } /* float_norm_test */
  258. static void
  259. double_norm_test (const char *filename)
  260. { SNDFILE *file ;
  261. SF_INFO sfinfo ;
  262. unsigned int k ;
  263. print_test_name ("double_norm_test", filename) ;
  264. sfinfo.samplerate = 44100 ;
  265. sfinfo.format = (SF_FORMAT_RAW | SF_FORMAT_PCM_16) ;
  266. sfinfo.channels = 1 ;
  267. sfinfo.frames = BUFFER_LEN ;
  268. /* Create double_data with all values being less than 1.0. */
  269. for (k = 0 ; k < BUFFER_LEN / 2 ; k++)
  270. double_data [k] = (k + 5) / (2.0 * BUFFER_LEN) ;
  271. for (k = BUFFER_LEN / 2 ; k < BUFFER_LEN ; k++)
  272. double_data [k] = (k + 5) ;
  273. if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
  274. { printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;
  275. fflush (stdout) ;
  276. puts (sf_strerror (NULL)) ;
  277. exit (1) ;
  278. } ;
  279. /* Normailsation is on by default so no need to do anything here. */
  280. /*-sf_command (file, "set-norm-double", "true", 0) ;-*/
  281. if ((k = sf_write_double (file, double_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
  282. { printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
  283. exit (1) ;
  284. } ;
  285. /* Turn normalisation off. */
  286. sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
  287. if ((k = sf_write_double (file, double_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
  288. { printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
  289. exit (1) ;
  290. } ;
  291. sf_close (file) ;
  292. if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
  293. { printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;
  294. fflush (stdout) ;
  295. puts (sf_strerror (NULL)) ;
  296. exit (1) ;
  297. } ;
  298. if (sfinfo.format != (SF_FORMAT_RAW | SF_FORMAT_PCM_16))
  299. { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (SF_FORMAT_RAW | SF_FORMAT_PCM_16), sfinfo.format) ;
  300. exit (1) ;
  301. } ;
  302. if (sfinfo.frames != BUFFER_LEN)
  303. { printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %" PRId64 ")\n", __LINE__, BUFFER_LEN, sfinfo.frames) ;
  304. exit (1) ;
  305. } ;
  306. if (sfinfo.channels != 1)
  307. { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
  308. exit (1) ;
  309. } ;
  310. /* Read double_data and check that it is normalised (ie default). */
  311. if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
  312. { printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
  313. exit (1) ;
  314. } ;
  315. for (k = 0 ; k < BUFFER_LEN ; k++)
  316. if (double_data [k] >= 1.0)
  317. { printf ("\n\nLine %d: double_data [%d] == %f which is greater than 1.0\n", __LINE__, k, double_data [k]) ;
  318. exit (1) ;
  319. } ;
  320. /* Seek to start of file, turn normalisation off, read double_data and check again. */
  321. sf_seek (file, 0, SEEK_SET) ;
  322. sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
  323. if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
  324. { printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
  325. exit (1) ;
  326. } ;
  327. for (k = 0 ; k < BUFFER_LEN ; k++)
  328. if (double_data [k] < 1.0)
  329. { printf ("\n\nLine %d: double_data [%d] == %f which is less than 1.0\n", __LINE__, k, double_data [k]) ;
  330. exit (1) ;
  331. } ;
  332. /* Seek to start of file, turn normalisation on, read double_data and do final check. */
  333. sf_seek (file, 0, SEEK_SET) ;
  334. sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_TRUE) ;
  335. if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
  336. { printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
  337. exit (1) ;
  338. } ;
  339. for (k = 0 ; k < BUFFER_LEN ; k++)
  340. if (double_data [k] > 1.0)
  341. { printf ("\n\nLine %d: double_data [%d] == %f which is greater than 1.0\n", __LINE__, k, double_data [k]) ;
  342. exit (1) ;
  343. } ;
  344. sf_close (file) ;
  345. unlink (filename) ;
  346. printf ("ok\n") ;
  347. } /* double_norm_test */
  348. static void
  349. format_tests (void)
  350. { SF_FORMAT_INFO format_info ;
  351. SF_INFO sfinfo ;
  352. const char *last_name ;
  353. int k, count ;
  354. print_test_name ("format_tests", "(null)") ;
  355. /* Clear out SF_INFO struct and set channels > 0. */
  356. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  357. sfinfo.channels = 1 ;
  358. /* First test simple formats. */
  359. sf_command (NULL, SFC_GET_SIMPLE_FORMAT_COUNT, &count, sizeof (int)) ;
  360. if (count < 0 || count > 30)
  361. { printf ("Line %d: Weird count.\n", __LINE__) ;
  362. exit (1) ;
  363. } ;
  364. format_info.format = 0 ;
  365. sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
  366. last_name = format_info.name ;
  367. for (k = 1 ; k < count ; k ++)
  368. { format_info.format = k ;
  369. sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
  370. if (strcmp (last_name, format_info.name) >= 0)
  371. { printf ("\n\nLine %d: format names out of sequence `%s' < `%s'.\n", __LINE__, last_name, format_info.name) ;
  372. exit (1) ;
  373. } ;
  374. sfinfo.format = format_info.format ;
  375. if (! sf_format_check (&sfinfo))
  376. { printf ("\n\nLine %d: sf_format_check failed.\n", __LINE__) ;
  377. printf (" Name : %s\n", format_info.name) ;
  378. printf (" Format : 0x%X\n", sfinfo.format) ;
  379. printf (" Channels : 0x%X\n", sfinfo.channels) ;
  380. printf (" Sample Rate : 0x%X\n", sfinfo.samplerate) ;
  381. exit (1) ;
  382. } ;
  383. last_name = format_info.name ;
  384. } ;
  385. format_info.format = 666 ;
  386. sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
  387. /* Now test major formats. */
  388. sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof (int)) ;
  389. if (count < 0 || count > 30)
  390. { printf ("Line %d: Weird count.\n", __LINE__) ;
  391. exit (1) ;
  392. } ;
  393. format_info.format = 0 ;
  394. sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
  395. last_name = format_info.name ;
  396. for (k = 1 ; k < count ; k ++)
  397. { format_info.format = k ;
  398. sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
  399. if (strcmp (last_name, format_info.name) >= 0)
  400. { printf ("\n\nLine %d: format names out of sequence (%d) `%s' < `%s'.\n", __LINE__, k, last_name, format_info.name) ;
  401. exit (1) ;
  402. } ;
  403. last_name = format_info.name ;
  404. } ;
  405. format_info.format = 666 ;
  406. sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
  407. /* Now test subtype formats. */
  408. sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &count, sizeof (int)) ;
  409. if (count < 0 || count > 30)
  410. { printf ("Line %d: Weird count.\n", __LINE__) ;
  411. exit (1) ;
  412. } ;
  413. format_info.format = 0 ;
  414. sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
  415. last_name = format_info.name ;
  416. for (k = 1 ; k < count ; k ++)
  417. { format_info.format = k ;
  418. sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
  419. } ;
  420. format_info.format = 666 ;
  421. sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
  422. printf ("ok\n") ;
  423. } /* format_tests */
  424. static void
  425. calc_peak_test (int filetype, const char *filename)
  426. { SNDFILE *file ;
  427. SF_INFO sfinfo ;
  428. int k, format ;
  429. double peak ;
  430. print_test_name ("calc_peak_test", filename) ;
  431. format = (filetype | SF_FORMAT_PCM_16) ;
  432. sfinfo.samplerate = 44100 ;
  433. sfinfo.format = format ;
  434. sfinfo.channels = 1 ;
  435. sfinfo.frames = BUFFER_LEN ;
  436. /* Create double_data with max value of 0.5. */
  437. for (k = 0 ; k < BUFFER_LEN ; k++)
  438. double_data [k] = (k + 1) / (2.0 * BUFFER_LEN) ;
  439. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  440. test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
  441. sf_close (file) ;
  442. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  443. if (sfinfo.format != format)
  444. { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  445. exit (1) ;
  446. } ;
  447. if (sfinfo.frames != BUFFER_LEN)
  448. { printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %" PRId64 ")\n", __LINE__, BUFFER_LEN, sfinfo.frames) ;
  449. exit (1) ;
  450. } ;
  451. if (sfinfo.channels != 1)
  452. { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
  453. exit (1) ;
  454. } ;
  455. sf_command (file, SFC_CALC_SIGNAL_MAX, &peak, sizeof (peak)) ;
  456. if (fabs (peak - (1 << 14)) > 1.0)
  457. { printf ("Line %d : Peak value should be %d (is %f).\n", __LINE__, (1 << 14), peak) ;
  458. exit (1) ;
  459. } ;
  460. sf_command (file, SFC_CALC_NORM_SIGNAL_MAX, &peak, sizeof (peak)) ;
  461. if (fabs (peak - 0.5) > 4e-5)
  462. { printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
  463. exit (1) ;
  464. } ;
  465. sf_close (file) ;
  466. format = (filetype | SF_FORMAT_FLOAT) ;
  467. sfinfo.samplerate = 44100 ;
  468. sfinfo.format = format ;
  469. sfinfo.channels = 1 ;
  470. sfinfo.frames = BUFFER_LEN ;
  471. /* Create double_data with max value of 0.5. */
  472. for (k = 0 ; k < BUFFER_LEN ; k++)
  473. double_data [k] = (k + 1) / (2.0 * BUFFER_LEN) ;
  474. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  475. test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
  476. sf_close (file) ;
  477. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  478. if (sfinfo.format != format)
  479. { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
  480. exit (1) ;
  481. } ;
  482. if (sfinfo.frames != BUFFER_LEN)
  483. { printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %" PRId64 ")\n", __LINE__, BUFFER_LEN, sfinfo.frames) ;
  484. exit (1) ;
  485. } ;
  486. if (sfinfo.channels != 1)
  487. { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
  488. exit (1) ;
  489. } ;
  490. sf_command (file, SFC_CALC_SIGNAL_MAX, &peak, sizeof (peak)) ;
  491. if (fabs (peak - 0.5) > 1e-5)
  492. { printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
  493. exit (1) ;
  494. } ;
  495. sf_command (file, SFC_CALC_NORM_SIGNAL_MAX, &peak, sizeof (peak)) ;
  496. if (fabs (peak - 0.5) > 1e-5)
  497. { printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
  498. exit (1) ;
  499. } ;
  500. sf_close (file) ;
  501. unlink (filename) ;
  502. printf ("ok\n") ;
  503. } /* calc_peak_test */
  504. static void
  505. truncate_test (const char *filename, int filetype)
  506. { SNDFILE *file ;
  507. SF_INFO sfinfo ;
  508. sf_count_t len ;
  509. print_test_name ("truncate_test", filename) ;
  510. sfinfo.samplerate = 11025 ;
  511. sfinfo.format = filetype ;
  512. sfinfo.channels = 2 ;
  513. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
  514. test_write_int_or_die (file, 0, int_data, BUFFER_LEN, __LINE__) ;
  515. len = 100 ;
  516. if (sf_command (file, SFC_FILE_TRUNCATE, &len, sizeof (len)))
  517. { printf ("Line %d: sf_command (SFC_FILE_TRUNCATE) returned error.\n", __LINE__) ;
  518. exit (1) ;
  519. } ;
  520. test_seek_or_die (file, 0, SEEK_CUR, len, 2, __LINE__) ;
  521. test_seek_or_die (file, 0, SEEK_END, len, 2, __LINE__) ;
  522. sf_close (file) ;
  523. unlink (filename) ;
  524. puts ("ok") ;
  525. } /* truncate_test */
  526. /*------------------------------------------------------------------------------
  527. */
  528. static void
  529. instrumet_rw_test (const char *filename)
  530. { SNDFILE *sndfile ;
  531. SF_INFO sfinfo ;
  532. SF_INSTRUMENT inst ;
  533. memset (&sfinfo, 0, sizeof (SF_INFO)) ;
  534. sndfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
  535. if (sf_command (sndfile, SFC_GET_INSTRUMENT, &inst, sizeof (inst)) == SF_TRUE)
  536. { inst.basenote = 22 ;
  537. if (sf_command (sndfile, SFC_SET_INSTRUMENT, &inst, sizeof (inst)) == SF_TRUE)
  538. printf ("Sucess: [%s] updated\n", filename) ;
  539. else
  540. printf ("Error: SFC_SET_INSTRUMENT on [%s] [%s]\n", filename, sf_strerror (sndfile)) ;
  541. }
  542. else
  543. printf ("Error: SFC_GET_INSTRUMENT on [%s] [%s]\n", filename, sf_strerror (sndfile)) ;
  544. if (sf_command (sndfile, SFC_UPDATE_HEADER_NOW, NULL, 0) != 0)
  545. printf ("Error: SFC_UPDATE_HEADER_NOW on [%s] [%s]\n", filename, sf_strerror (sndfile)) ;
  546. sf_write_sync (sndfile) ;
  547. sf_close (sndfile) ;
  548. return ;
  549. } /* instrumet_rw_test */
  550. static void
  551. instrument_test (const char *filename, int filetype)
  552. { static SF_INSTRUMENT write_inst =
  553. { 2, /* gain */
  554. 3, /* detune */
  555. 4, /* basenote */
  556. 5, 6, /* key low and high */
  557. 7, 8, /* velocity low and high */
  558. 2, /* loop_count */
  559. { { 801, 2, 3, 0 },
  560. { 801, 3, 4, 0 },
  561. }
  562. } ;
  563. SF_INSTRUMENT read_inst ;
  564. SNDFILE *file ;
  565. SF_INFO sfinfo ;
  566. print_test_name ("instrument_test", filename) ;
  567. sfinfo.samplerate = 11025 ;
  568. sfinfo.format = filetype ;
  569. sfinfo.channels = 1 ;
  570. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  571. if (sf_command (file, SFC_SET_INSTRUMENT, &write_inst, sizeof (write_inst)) == SF_FALSE)
  572. { printf ("\n\nLine %d : sf_command (SFC_SET_INSTRUMENT) failed.\n\n", __LINE__) ;
  573. exit (1) ;
  574. } ;
  575. test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
  576. sf_close (file) ;
  577. memset (&read_inst, 0, sizeof (read_inst)) ;
  578. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  579. if (sf_command (file, SFC_GET_INSTRUMENT, &read_inst, sizeof (read_inst)) == SF_FALSE)
  580. { printf ("\n\nLine %d : sf_command (SFC_GET_INSTRUMENT) failed.\n\n", __LINE__) ;
  581. exit (1) ;
  582. return ;
  583. } ;
  584. check_log_buffer_or_die (file, __LINE__) ;
  585. sf_close (file) ;
  586. if ((filetype & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV)
  587. { /*
  588. ** For all the fields that WAV doesn't support, modify the
  589. ** write_inst struct to hold the default value that the WAV
  590. ** module should hold.
  591. */
  592. write_inst.detune = 0 ;
  593. write_inst.key_lo = write_inst.velocity_lo = 0 ;
  594. write_inst.key_hi = write_inst.velocity_hi = 127 ;
  595. write_inst.gain = 1 ;
  596. } ;
  597. if ((filetype & SF_FORMAT_TYPEMASK) == SF_FORMAT_XI)
  598. { /*
  599. ** For all the fields that XI doesn't support, modify the
  600. ** write_inst struct to hold the default value that the XI
  601. ** module should hold.
  602. */
  603. write_inst.basenote = 0 ;
  604. write_inst.detune = 0 ;
  605. write_inst.key_lo = write_inst.velocity_lo = 0 ;
  606. write_inst.key_hi = write_inst.velocity_hi = 127 ;
  607. write_inst.gain = 1 ;
  608. } ;
  609. if (memcmp (&write_inst, &read_inst, sizeof (write_inst)) != 0)
  610. { printf ("\n\nLine %d : instrument comparison failed.\n\n", __LINE__) ;
  611. printf ("W Base Note : %u\n"
  612. " Detune : %u\n"
  613. " Low Note : %u\tHigh Note : %u\n"
  614. " Low Vel. : %u\tHigh Vel. : %u\n"
  615. " Gain : %d\tCount : %d\n"
  616. " mode : %d\n"
  617. " start : %d\tend : %d\tcount :%d\n"
  618. " mode : %d\n"
  619. " start : %d\tend : %d\tcount :%d\n\n",
  620. write_inst.basenote,
  621. write_inst.detune,
  622. write_inst.key_lo, write_inst.key_hi,
  623. write_inst.velocity_lo, write_inst.velocity_hi,
  624. write_inst.gain, write_inst.loop_count,
  625. write_inst.loops [0].mode, write_inst.loops [0].start,
  626. write_inst.loops [0].end, write_inst.loops [0].count,
  627. write_inst.loops [1].mode, write_inst.loops [1].start,
  628. write_inst.loops [1].end, write_inst.loops [1].count) ;
  629. printf ("R Base Note : %u\n"
  630. " Detune : %u\n"
  631. " Low Note : %u\tHigh Note : %u\n"
  632. " Low Vel. : %u\tHigh Vel. : %u\n"
  633. " Gain : %d\tCount : %d\n"
  634. " mode : %d\n"
  635. " start : %d\tend : %d\tcount :%d\n"
  636. " mode : %d\n"
  637. " start : %d\tend : %d\tcount :%d\n\n",
  638. read_inst.basenote,
  639. read_inst.detune,
  640. read_inst.key_lo, read_inst.key_hi,
  641. read_inst.velocity_lo, read_inst.velocity_hi,
  642. read_inst.gain, read_inst.loop_count,
  643. read_inst.loops [0].mode, read_inst.loops [0].start,
  644. read_inst.loops [0].end, read_inst.loops [0].count,
  645. read_inst.loops [1].mode, read_inst.loops [1].start,
  646. read_inst.loops [1].end, read_inst.loops [1].count) ;
  647. if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_XI)
  648. exit (1) ;
  649. } ;
  650. if (0) instrumet_rw_test (filename) ;
  651. unlink (filename) ;
  652. puts ("ok") ;
  653. } /* instrument_test */
  654. static void
  655. current_sf_info_test (const char *filename)
  656. { SNDFILE *outfile, *infile ;
  657. SF_INFO outinfo, ininfo ;
  658. print_test_name ("current_sf_info_test", filename) ;
  659. outinfo.samplerate = 44100 ;
  660. outinfo.format = (SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
  661. outinfo.channels = 1 ;
  662. outinfo.frames = 0 ;
  663. outfile = test_open_file_or_die (filename, SFM_WRITE, &outinfo, SF_TRUE, __LINE__) ;
  664. sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, 0) ;
  665. exit_if_true (outinfo.frames != 0,
  666. "\n\nLine %d : Initial sfinfo.frames is not zero.\n\n", __LINE__
  667. ) ;
  668. test_write_double_or_die (outfile, 0, double_data, BUFFER_LEN, __LINE__) ;
  669. sf_command (outfile, SFC_GET_CURRENT_SF_INFO, &outinfo, sizeof (outinfo)) ;
  670. exit_if_true (outinfo.frames != BUFFER_LEN,
  671. "\n\nLine %d : Initial sfinfo.frames (%" PRId64 ") should be %d.\n\n", __LINE__,
  672. outinfo.frames, BUFFER_LEN
  673. ) ;
  674. /* Read file making sure no channel map exists. */
  675. memset (&ininfo, 0, sizeof (ininfo)) ;
  676. infile = test_open_file_or_die (filename, SFM_READ, &ininfo, SF_TRUE, __LINE__) ;
  677. test_write_double_or_die (outfile, 0, double_data, BUFFER_LEN, __LINE__) ;
  678. sf_command (infile, SFC_GET_CURRENT_SF_INFO, &ininfo, sizeof (ininfo)) ;
  679. exit_if_true (ininfo.frames != BUFFER_LEN,
  680. "\n\nLine %d : Initial sfinfo.frames (%" PRId64 ") should be %d.\n\n", __LINE__,
  681. ininfo.frames, BUFFER_LEN
  682. ) ;
  683. sf_close (outfile) ;
  684. sf_close (infile) ;
  685. unlink (filename) ;
  686. puts ("ok") ;
  687. } /* current_sf_info_test */
  688. static void
  689. broadcast_test (const char *filename, int filetype)
  690. { static SF_BROADCAST_INFO bc_write, bc_read ;
  691. SNDFILE *file ;
  692. SF_INFO sfinfo ;
  693. int errors = 0 ;
  694. print_test_name ("broadcast_test", filename) ;
  695. sfinfo.samplerate = 11025 ;
  696. sfinfo.format = filetype ;
  697. sfinfo.channels = 1 ;
  698. memset (&bc_write, 0, sizeof (bc_write)) ;
  699. snprintf (bc_write.description, sizeof (bc_write.description), "Test description") ;
  700. snprintf (bc_write.originator, sizeof (bc_write.originator), "Test originator") ;
  701. snprintf (bc_write.originator_reference, sizeof (bc_write.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
  702. snprintf (bc_write.origination_date, sizeof (bc_write.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
  703. snprintf (bc_write.origination_time, sizeof (bc_write.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
  704. snprintf (bc_write.umid, sizeof (bc_write.umid), "Some umid") ;
  705. bc_write.coding_history_size = 0 ;
  706. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  707. if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
  708. { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
  709. exit (1) ;
  710. } ;
  711. test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
  712. sf_close (file) ;
  713. memset (&bc_read, 0, sizeof (bc_read)) ;
  714. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  715. if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
  716. { printf ("\n\nLine %d : sf_command (SFC_GET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
  717. exit (1) ;
  718. return ;
  719. } ;
  720. check_log_buffer_or_die (file, __LINE__) ;
  721. sf_close (file) ;
  722. if (bc_read.version != 1)
  723. { printf ("\n\nLine %d : Read bad version number %d.\n\n", __LINE__, bc_read.version) ;
  724. exit (1) ;
  725. return ;
  726. } ;
  727. bc_read.version = bc_write.version = 0 ;
  728. if (memcmp (bc_write.description, bc_read.description, sizeof (bc_write.description)) != 0)
  729. { printf ("\n\nLine %d : description mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, bc_write.description, bc_read.description) ;
  730. errors ++ ;
  731. } ;
  732. if (memcmp (bc_write.originator, bc_read.originator, sizeof (bc_write.originator)) != 0)
  733. { printf ("\n\nLine %d : originator mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, bc_write.originator, bc_read.originator) ;
  734. errors ++ ;
  735. } ;
  736. if (memcmp (bc_write.originator_reference, bc_read.originator_reference, sizeof (bc_write.originator_reference)) != 0)
  737. { printf ("\n\nLine %d : originator_reference mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, bc_write.originator_reference, bc_read.originator_reference) ;
  738. errors ++ ;
  739. } ;
  740. if (memcmp (bc_write.origination_date, bc_read.origination_date, sizeof (bc_write.origination_date)) != 0)
  741. { printf ("\n\nLine %d : origination_date mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, bc_write.origination_date, bc_read.origination_date) ;
  742. errors ++ ;
  743. } ;
  744. if (memcmp (bc_write.origination_time, bc_read.origination_time, sizeof (bc_write.origination_time)) != 0)
  745. { printf ("\n\nLine %d : origination_time mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, bc_write.origination_time, bc_read.origination_time) ;
  746. errors ++ ;
  747. } ;
  748. if (memcmp (bc_write.umid, bc_read.umid, sizeof (bc_write.umid)) != 0)
  749. { printf ("\n\nLine %d : umid mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, bc_write.umid, bc_read.umid) ;
  750. errors ++ ;
  751. } ;
  752. if (errors)
  753. exit (1) ;
  754. unlink (filename) ;
  755. puts ("ok") ;
  756. } /* broadcast_test */
  757. static void
  758. broadcast_rdwr_test (const char *filename, int filetype)
  759. { SF_BROADCAST_INFO binfo ;
  760. SNDFILE *file ;
  761. SF_INFO sfinfo ;
  762. sf_count_t frames ;
  763. print_test_name (__func__, filename) ;
  764. create_short_sndfile (filename, filetype, 2) ;
  765. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  766. memset (&binfo, 0, sizeof (binfo)) ;
  767. snprintf (binfo.description, sizeof (binfo.description), "Test description") ;
  768. snprintf (binfo.originator, sizeof (binfo.originator), "Test originator") ;
  769. snprintf (binfo.originator_reference, sizeof (binfo.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
  770. snprintf (binfo.origination_date, sizeof (binfo.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
  771. snprintf (binfo.origination_time, sizeof (binfo.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
  772. snprintf (binfo.umid, sizeof (binfo.umid), "Some umid") ;
  773. binfo.coding_history_size = 0 ;
  774. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
  775. frames = sfinfo.frames ;
  776. if (sf_command (file, SFC_SET_BROADCAST_INFO, &binfo, sizeof (binfo)) != SF_FALSE)
  777. { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) should have failed but didn't.\n\n", __LINE__) ;
  778. exit (1) ;
  779. } ;
  780. sf_close (file) ;
  781. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  782. sf_close (file) ;
  783. exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo.frames, frames) ;
  784. unlink (filename) ;
  785. puts ("ok") ;
  786. } /* broadcast_rdwr_test */
  787. static void
  788. check_coding_history_newlines (const char *filename)
  789. { static SF_BROADCAST_INFO bc_write, bc_read ;
  790. SNDFILE *file ;
  791. SF_INFO sfinfo ;
  792. unsigned k ;
  793. sfinfo.samplerate = 22050 ;
  794. sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
  795. sfinfo.channels = 1 ;
  796. memset (&bc_write, 0, sizeof (bc_write)) ;
  797. snprintf (bc_write.description, sizeof (bc_write.description), "Test description") ;
  798. snprintf (bc_write.originator, sizeof (bc_write.originator), "Test originator") ;
  799. snprintf (bc_write.originator_reference, sizeof (bc_write.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
  800. snprintf (bc_write.origination_date, sizeof (bc_write.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
  801. snprintf (bc_write.origination_time, sizeof (bc_write.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
  802. snprintf (bc_write.umid, sizeof (bc_write.umid), "Some umid") ;
  803. bc_write.coding_history_size = snprintf (bc_write.coding_history, sizeof (bc_write.coding_history), "This has\nUnix\nand\rMac OS9\rline endings.\nLast line") ; ;
  804. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  805. if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
  806. { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
  807. exit (1) ;
  808. } ;
  809. test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
  810. sf_close (file) ;
  811. memset (&bc_read, 0, sizeof (bc_read)) ;
  812. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  813. if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
  814. { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
  815. exit (1) ;
  816. } ;
  817. check_log_buffer_or_die (file, __LINE__) ;
  818. sf_close (file) ;
  819. if (bc_read.coding_history_size == 0)
  820. { printf ("\n\nLine %d : missing coding history.\n\n", __LINE__) ;
  821. exit (1) ;
  822. } ;
  823. if (strstr (bc_read.coding_history, "Last line") == NULL)
  824. { printf ("\n\nLine %d : coding history truncated.\n\n", __LINE__) ;
  825. exit (1) ;
  826. } ;
  827. for (k = 1 ; k < bc_read.coding_history_size ; k++)
  828. { if (bc_read.coding_history [k] == '\n' && bc_read.coding_history [k - 1] != '\r')
  829. { printf ("\n\nLine %d : '\\n' without '\\r' before.\n\n", __LINE__) ;
  830. exit (1) ;
  831. } ;
  832. if (bc_read.coding_history [k] == '\r' && bc_read.coding_history [k + 1] != '\n')
  833. { printf ("\n\nLine %d : '\\r' without '\\n' after.\n\n", __LINE__) ;
  834. exit (1) ;
  835. } ;
  836. if (bc_read.coding_history [k] == 0 && k < bc_read.coding_history_size - 1)
  837. { printf ("\n\nLine %d : '\\0' within coding history at index %d of %d.\n\n", __LINE__, k, bc_read.coding_history_size) ;
  838. exit (1) ;
  839. } ;
  840. } ;
  841. return ;
  842. } /* check_coding_history_newlines */
  843. static void
  844. broadcast_coding_history_test (const char *filename)
  845. { static SF_BROADCAST_INFO bc_write, bc_read ;
  846. SNDFILE *file ;
  847. SF_INFO sfinfo ;
  848. const char *default_history = "A=PCM,F=22050,W=16,M=mono" ;
  849. const char *supplied_history =
  850. "A=PCM,F=44100,W=24,M=mono,T=other\r\n"
  851. "A=PCM,F=22050,W=16,M=mono,T=yet_another\r\n" ;
  852. print_test_name ("broadcast_coding_history_test", filename) ;
  853. sfinfo.samplerate = 22050 ;
  854. sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
  855. sfinfo.channels = 1 ;
  856. memset (&bc_write, 0, sizeof (bc_write)) ;
  857. snprintf (bc_write.description, sizeof (bc_write.description), "Test description") ;
  858. snprintf (bc_write.originator, sizeof (bc_write.originator), "Test originator") ;
  859. snprintf (bc_write.originator_reference, sizeof (bc_write.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
  860. snprintf (bc_write.origination_date, sizeof (bc_write.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
  861. snprintf (bc_write.origination_time, sizeof (bc_write.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
  862. snprintf (bc_write.umid, sizeof (bc_write.umid), "Some umid") ;
  863. /* Coding history will be filled in by the library. */
  864. bc_write.coding_history_size = 0 ;
  865. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  866. if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
  867. { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
  868. exit (1) ;
  869. } ;
  870. test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
  871. sf_close (file) ;
  872. memset (&bc_read, 0, sizeof (bc_read)) ;
  873. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  874. if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
  875. { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
  876. exit (1) ;
  877. } ;
  878. check_log_buffer_or_die (file, __LINE__) ;
  879. sf_close (file) ;
  880. if (bc_read.coding_history_size == 0)
  881. { printf ("\n\nLine %d : missing coding history.\n\n", __LINE__) ;
  882. exit (1) ;
  883. } ;
  884. if (bc_read.coding_history_size < strlen (default_history) || memcmp (bc_read.coding_history, default_history, strlen (default_history)) != 0)
  885. { printf ("\n\n"
  886. "Line %d : unexpected coding history '%.*s',\n"
  887. " should be '%s'\n\n", __LINE__, bc_read.coding_history_size, bc_read.coding_history, default_history) ;
  888. exit (1) ;
  889. } ;
  890. bc_write.coding_history_size = strlen (supplied_history) ;
  891. bc_write.coding_history_size = snprintf (bc_write.coding_history, sizeof (bc_write.coding_history), "%s", supplied_history) ;
  892. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  893. if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
  894. { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
  895. exit (1) ;
  896. } ;
  897. test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
  898. sf_close (file) ;
  899. memset (&bc_read, 0, sizeof (bc_read)) ;
  900. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  901. if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
  902. { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
  903. exit (1) ;
  904. } ;
  905. check_log_buffer_or_die (file, __LINE__) ;
  906. sf_close (file) ;
  907. if (strstr (bc_read.coding_history, supplied_history) != bc_read.coding_history)
  908. { printf ("\n\nLine %d : unexpected coding history :\n"
  909. "----------------------------------------------------\n%s"
  910. "----------------------------------------------------\n"
  911. "should be this :\n"
  912. "----------------------------------------------------\n%s"
  913. "----------------------------------------------------\n"
  914. "with one more line at the end.\n\n",
  915. __LINE__, bc_read.coding_history, supplied_history) ;
  916. exit (1) ;
  917. } ;
  918. check_coding_history_newlines (filename) ;
  919. unlink (filename) ;
  920. puts ("ok") ;
  921. } /* broadcast_coding_history_test */
  922. /*==============================================================================
  923. */
  924. static void
  925. broadcast_coding_history_size (const char *filename)
  926. { /* SF_BROADCAST_INFO struct with coding_history field of 1024 bytes. */
  927. static SF_BROADCAST_INFO_VAR (1024) bc_write ;
  928. static SF_BROADCAST_INFO_VAR (1024) bc_read ;
  929. SNDFILE *file ;
  930. SF_INFO sfinfo ;
  931. int k ;
  932. print_test_name (__func__, filename) ;
  933. sfinfo.samplerate = 22050 ;
  934. sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
  935. sfinfo.channels = 1 ;
  936. memset (&bc_write, 0, sizeof (bc_write)) ;
  937. snprintf (bc_write.description, sizeof (bc_write.description), "Test description") ;
  938. snprintf (bc_write.originator, sizeof (bc_write.originator), "Test originator") ;
  939. snprintf (bc_write.originator_reference, sizeof (bc_write.originator_reference), "%08x-%08x", (unsigned int) time (NULL), (unsigned int) (~ time (NULL))) ;
  940. snprintf (bc_write.origination_date, sizeof (bc_write.origination_date), "%d/%02d/%02d", 2006, 3, 30) ;
  941. snprintf (bc_write.origination_time, sizeof (bc_write.origination_time), "%02d:%02d:%02d", 20, 27, 0) ;
  942. snprintf (bc_write.umid, sizeof (bc_write.umid), "Some umid") ;
  943. bc_write.coding_history_size = 0 ;
  944. for (k = 0 ; bc_write.coding_history_size < 512 ; k++)
  945. { snprintf (bc_write.coding_history + bc_write.coding_history_size,
  946. sizeof (bc_write.coding_history) - bc_write.coding_history_size, "line %4d\n", k) ;
  947. bc_write.coding_history_size = strlen (bc_write.coding_history) ;
  948. } ;
  949. exit_if_true (bc_write.coding_history_size < 512,
  950. "\n\nLine %d : bc_write.coding_history_size (%d) should be > 512.\n\n", __LINE__, bc_write.coding_history_size) ;
  951. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  952. if (sf_command (file, SFC_SET_BROADCAST_INFO, &bc_write, sizeof (bc_write)) == SF_FALSE)
  953. { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
  954. exit (1) ;
  955. } ;
  956. test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
  957. sf_close (file) ;
  958. memset (&bc_read, 0, sizeof (bc_read)) ;
  959. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  960. if (sf_command (file, SFC_GET_BROADCAST_INFO, &bc_read, sizeof (bc_read)) == SF_FALSE)
  961. { printf ("\n\nLine %d : sf_command (SFC_SET_BROADCAST_INFO) failed.\n\n", __LINE__) ;
  962. exit (1) ;
  963. } ;
  964. check_log_buffer_or_die (file, __LINE__) ;
  965. sf_close (file) ;
  966. exit_if_true (bc_read.coding_history_size < 512,
  967. "\n\nLine %d : unexpected coding history size %d (should be > 512).\n\n", __LINE__, bc_read.coding_history_size) ;
  968. exit_if_true (strstr (bc_read.coding_history, "libsndfile") == NULL,
  969. "\n\nLine %d : coding history incomplete (should contain 'libsndfile').\n\n", __LINE__) ;
  970. unlink (filename) ;
  971. puts ("ok") ;
  972. } /* broadcast_coding_history_size */
  973. /*==============================================================================
  974. */
  975. static void
  976. cart_test (const char *filename, int filetype)
  977. { static SF_CART_INFO ca_write, ca_read ;
  978. SNDFILE *file ;
  979. SF_INFO sfinfo ;
  980. int errors = 0 ;
  981. print_test_name ("cart_test", filename) ;
  982. sfinfo.samplerate = 11025 ;
  983. sfinfo.format = filetype ;
  984. sfinfo.channels = 1 ;
  985. memset (&ca_write, 0, sizeof (ca_write)) ;
  986. // example test data
  987. snprintf (ca_write.artist, sizeof (ca_write.artist), "Test artist") ;
  988. snprintf (ca_write.version, sizeof (ca_write.version), "Test version") ;
  989. snprintf (ca_write.cut_id, sizeof (ca_write.cut_id), "Test cut ID") ;
  990. snprintf (ca_write.client_id, sizeof (ca_write.client_id), "Test client ID") ;
  991. snprintf (ca_write.category, sizeof (ca_write.category), "Test category") ;
  992. snprintf (ca_write.classification, sizeof (ca_write.classification), "Test classification") ;
  993. snprintf (ca_write.out_cue, sizeof (ca_write.out_cue), "Test out cue") ;
  994. snprintf (ca_write.start_date, sizeof (ca_write.start_date), "%d/%02d/%02d", 2006, 3, 30) ;
  995. snprintf (ca_write.start_time, sizeof (ca_write.start_time), "%02d:%02d:%02d", 20, 27, 0) ;
  996. snprintf (ca_write.end_date, sizeof (ca_write.end_date), "%d/%02d/%02d", 2006, 3, 30) ;
  997. snprintf (ca_write.end_time, sizeof (ca_write.end_time), "%02d:%02d:%02d", 20, 27, 0) ;
  998. snprintf (ca_write.producer_app_id, sizeof (ca_write.producer_app_id), "Test producer app id") ;
  999. snprintf (ca_write.producer_app_version, sizeof (ca_write.producer_app_version), "Test producer app version") ;
  1000. snprintf (ca_write.user_def, sizeof (ca_write.user_def), "test user def test test") ;
  1001. ca_write.level_reference = 42 ;
  1002. snprintf (ca_write.url, sizeof (ca_write.url), "http://www.test.com/test_url") ;
  1003. snprintf (ca_write.tag_text, sizeof (ca_write.tag_text), "tag text test! \r\n") ; // must be terminated \r\n to be valid
  1004. ca_write.tag_text_size = strlen (ca_write.tag_text) ;
  1005. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  1006. if (sf_command (file, SFC_SET_CART_INFO, &ca_write, sizeof (ca_write)) == SF_FALSE)
  1007. exit (1) ;
  1008. test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
  1009. sf_close (file) ;
  1010. memset (&ca_read, 0, sizeof (ca_read)) ;
  1011. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  1012. if (sf_command (file, SFC_GET_CART_INFO, &ca_read, sizeof (ca_read)) == SF_FALSE)
  1013. { printf ("\n\nLine %d : sf_command (SFC_GET_CART_INFO) failed.\n\n", __LINE__) ;
  1014. exit (1) ;
  1015. return ;
  1016. } ;
  1017. check_log_buffer_or_die (file, __LINE__) ;
  1018. sf_close (file) ;
  1019. if (memcmp (ca_write.artist, ca_read.artist, sizeof (ca_write.artist)) != 0)
  1020. { printf ("\n\nLine %d : artist mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.artist, ca_read.artist) ;
  1021. errors ++ ;
  1022. } ;
  1023. if (memcmp (ca_write.version, ca_read.version, sizeof (ca_write.version)) != 0)
  1024. { printf ("\n\nLine %d : version mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.version, ca_read.version) ;
  1025. errors ++ ;
  1026. } ;
  1027. if (memcmp (ca_write.title, ca_read.title, sizeof (ca_write.title)) != 0)
  1028. { printf ("\n\nLine %d : title mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.title, ca_read.title) ;
  1029. errors ++ ;
  1030. } ;
  1031. if (memcmp (ca_write.cut_id, ca_read.cut_id, sizeof (ca_write.cut_id)) != 0)
  1032. { printf ("\n\nLine %d : cut_id mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.cut_id, ca_read.cut_id) ;
  1033. errors ++ ;
  1034. } ;
  1035. if (memcmp (ca_write.client_id, ca_read.client_id, sizeof (ca_write.client_id)) != 0)
  1036. { printf ("\n\nLine %d : client_id mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.client_id, ca_read.client_id) ;
  1037. errors ++ ;
  1038. } ;
  1039. if (memcmp (ca_write.category, ca_read.category, sizeof (ca_write.category)) != 0)
  1040. { printf ("\n\nLine %d : category mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.category, ca_read.category) ;
  1041. errors ++ ;
  1042. } ;
  1043. if (memcmp (ca_write.out_cue, ca_read.out_cue, sizeof (ca_write.out_cue)) != 0)
  1044. { printf ("\n\nLine %d : out_cue mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.out_cue, ca_read.out_cue) ;
  1045. errors ++ ;
  1046. } ;
  1047. if (memcmp (ca_write.start_date, ca_read.start_date, sizeof (ca_write.start_date)) != 0)
  1048. { printf ("\n\nLine %d : start_date mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.start_date, ca_read.start_date) ;
  1049. errors ++ ;
  1050. } ;
  1051. if (memcmp (ca_write.start_time, ca_read.start_time, sizeof (ca_write.start_time)) != 0)
  1052. { printf ("\n\nLine %d : start_time mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.start_time, ca_read.start_time) ;
  1053. errors ++ ;
  1054. } ;
  1055. if (memcmp (ca_write.end_date, ca_read.end_date, sizeof (ca_write.end_date)) != 0)
  1056. { printf ("\n\nLine %d : end_date mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.end_date, ca_read.end_date) ;
  1057. errors ++ ;
  1058. } ;
  1059. if (memcmp (ca_write.end_time, ca_read.end_time, sizeof (ca_write.end_time)) != 0)
  1060. { printf ("\n\nLine %d : end_time mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.end_time, ca_read.end_time) ;
  1061. errors ++ ;
  1062. } ;
  1063. if (memcmp (ca_write.producer_app_id, ca_read.producer_app_id, sizeof (ca_write.producer_app_id)) != 0)
  1064. { printf ("\n\nLine %d : producer_app_id mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.producer_app_id, ca_read.producer_app_id) ;
  1065. errors ++ ;
  1066. } ;
  1067. if (memcmp (ca_write.producer_app_version, ca_read.producer_app_version, sizeof (ca_write.producer_app_version)) != 0)
  1068. { printf ("\n\nLine %d : producer_app_version mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.producer_app_version, ca_read.producer_app_version) ;
  1069. errors ++ ;
  1070. } ;
  1071. if (memcmp (ca_write.user_def, ca_read.user_def, sizeof (ca_write.user_def)) != 0)
  1072. { printf ("\n\nLine %d : user_def mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.user_def, ca_read.user_def) ;
  1073. errors ++ ;
  1074. } ;
  1075. if (ca_write.level_reference != ca_read.level_reference)
  1076. { printf ("\n\nLine %d : level_reference mismatch :\n\twrite : '%d'\n\tread : '%d'\n\n", __LINE__, ca_write.level_reference, ca_read.level_reference) ;
  1077. errors ++ ;
  1078. } ;
  1079. // TODO: make this more helpful
  1080. if (memcmp (ca_write.post_timers, ca_read.post_timers, sizeof (ca_write.post_timers)) != 0)
  1081. { printf ("\n\nLine %d : post_timers mismatch :\n'\n\n", __LINE__) ;
  1082. errors ++ ;
  1083. } ;
  1084. if (memcmp (ca_write.url, ca_read.url, sizeof (ca_write.url)) != 0)
  1085. { printf ("\n\nLine %d : url mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.url, ca_read.url) ;
  1086. errors ++ ;
  1087. } ;
  1088. if (memcmp (ca_write.tag_text, ca_read.tag_text, (size_t) (ca_read.tag_text_size)) != 0)
  1089. { printf ("\n\nLine %d : tag_text mismatch :\n\twrite : '%s'\n\tread : '%s'\n\n", __LINE__, ca_write.tag_text, ca_read.tag_text) ;
  1090. errors ++ ;
  1091. } ;
  1092. if (errors)
  1093. exit (1) ;
  1094. unlink (filename) ;
  1095. puts ("ok") ;
  1096. } /* cart_test */
  1097. static void
  1098. cart_rdwr_test (const char *filename, int filetype)
  1099. { SF_CART_INFO cinfo ;
  1100. SNDFILE *file ;
  1101. SF_INFO sfinfo ;
  1102. sf_count_t frames ;
  1103. print_test_name (__func__, filename) ;
  1104. create_short_sndfile (filename, filetype, 2) ;
  1105. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1106. memset (&cinfo, 0, sizeof (cinfo)) ;
  1107. snprintf (cinfo.artist, sizeof (cinfo.artist), "Test artist") ;
  1108. snprintf (cinfo.version, sizeof (cinfo.version), "Test version") ;
  1109. snprintf (cinfo.cut_id, sizeof (cinfo.cut_id), "Test cut ID") ;
  1110. snprintf (cinfo.client_id, sizeof (cinfo.client_id), "Test client ID") ;
  1111. snprintf (cinfo.category, sizeof (cinfo.category), "Test category") ;
  1112. snprintf (cinfo.classification, sizeof (cinfo.classification), "Test classification") ;
  1113. snprintf (cinfo.out_cue, sizeof (cinfo.out_cue), "Test out cue") ;
  1114. snprintf (cinfo.start_date, sizeof (cinfo.start_date), "%d/%02d/%02d", 2006, 3, 30) ;
  1115. snprintf (cinfo.start_time, sizeof (cinfo.start_time), "%02d:%02d:%02d", 20, 27, 0) ;
  1116. snprintf (cinfo.end_date, sizeof (cinfo.end_date), "%d/%02d/%02d", 2006, 3, 30) ;
  1117. snprintf (cinfo.end_time, sizeof (cinfo.end_time), "%02d:%02d:%02d", 20, 27, 0) ;
  1118. snprintf (cinfo.producer_app_id, sizeof (cinfo.producer_app_id), "Test producer app id") ;
  1119. snprintf (cinfo.producer_app_version, sizeof (cinfo.producer_app_version), "Test producer app version") ;
  1120. snprintf (cinfo.user_def, sizeof (cinfo.user_def), "test user def test test") ;
  1121. cinfo.level_reference = 42 ;
  1122. snprintf (cinfo.url, sizeof (cinfo.url), "http://www.test.com/test_url") ;
  1123. snprintf (cinfo.tag_text, sizeof (cinfo.tag_text), "tag text test!\r\n") ;
  1124. cinfo.tag_text_size = strlen (cinfo.tag_text) ;
  1125. file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
  1126. frames = sfinfo.frames ;
  1127. if (sf_command (file, SFC_SET_CART_INFO, &cinfo, sizeof (cinfo)) != SF_FALSE)
  1128. { printf ("\n\nLine %d : sf_command (SFC_SET_CART_INFO) should have failed but didn't.\n\n", __LINE__) ;
  1129. exit (1) ;
  1130. } ;
  1131. sf_close (file) ;
  1132. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  1133. sf_close (file) ;
  1134. exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo.frames, frames) ;
  1135. unlink (filename) ;
  1136. puts ("ok") ;
  1137. } /* cart_rdwr_test */
  1138. /*==============================================================================
  1139. */
  1140. static void
  1141. channel_map_test (const char *filename, int filetype)
  1142. { SNDFILE *file ;
  1143. SF_INFO sfinfo ;
  1144. int channel_map_read [4], channel_map_write [4] =
  1145. { SF_CHANNEL_MAP_LEFT, SF_CHANNEL_MAP_RIGHT, SF_CHANNEL_MAP_LFE,
  1146. SF_CHANNEL_MAP_REAR_CENTER
  1147. } ;
  1148. print_test_name ("channel_map_test", filename) ;
  1149. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1150. sfinfo.samplerate = 11025 ;
  1151. sfinfo.format = filetype ;
  1152. sfinfo.channels = ARRAY_LEN (channel_map_read) ;
  1153. switch (filetype & SF_FORMAT_TYPEMASK)
  1154. { /* WAVEX and RF64 have a default channel map, even if you don't specify one. */
  1155. case SF_FORMAT_WAVEX :
  1156. case SF_FORMAT_RF64 :
  1157. /* Write file without channel map. */
  1158. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  1159. test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
  1160. sf_close (file) ;
  1161. /* Read file making default channel map exists. */
  1162. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  1163. exit_if_true (
  1164. sf_command (file, SFC_GET_CHANNEL_MAP_INFO, channel_map_read, sizeof (channel_map_read)) == SF_FALSE,
  1165. "\n\nLine %d : sf_command (SFC_GET_CHANNEL_MAP_INFO) should not have failed.\n\n", __LINE__
  1166. ) ;
  1167. check_log_buffer_or_die (file, __LINE__) ;
  1168. sf_close (file) ;
  1169. break ;
  1170. default :
  1171. break ;
  1172. } ;
  1173. /* Write file with a channel map. */
  1174. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  1175. exit_if_true (
  1176. sf_command (file, SFC_SET_CHANNEL_MAP_INFO, channel_map_write, sizeof (channel_map_write)) == SF_FALSE,
  1177. "\n\nLine %d : sf_command (SFC_SET_CHANNEL_MAP_INFO) failed.\n\n", __LINE__
  1178. ) ;
  1179. test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
  1180. sf_close (file) ;
  1181. /* Read file making sure no channel map exists. */
  1182. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  1183. exit_if_true (
  1184. sf_command (file, SFC_GET_CHANNEL_MAP_INFO, channel_map_read, sizeof (channel_map_read)) != SF_TRUE,
  1185. "\n\nLine %d : sf_command (SFC_GET_CHANNEL_MAP_INFO) failed.\n\n", __LINE__
  1186. ) ;
  1187. check_log_buffer_or_die (file, __LINE__) ;
  1188. sf_close (file) ;
  1189. exit_if_true (
  1190. memcmp (channel_map_read, channel_map_write, sizeof (channel_map_read)) != 0,
  1191. "\n\nLine %d : Channel map read does not match channel map written.\n\n", __LINE__
  1192. ) ;
  1193. unlink (filename) ;
  1194. puts ("ok") ;
  1195. } /* channel_map_test */
  1196. static void
  1197. raw_needs_endswap_test (const char *filename, int filetype)
  1198. { static int subtypes [] =
  1199. { SF_FORMAT_FLOAT, SF_FORMAT_DOUBLE,
  1200. SF_FORMAT_PCM_16, SF_FORMAT_PCM_24, SF_FORMAT_PCM_32
  1201. } ;
  1202. SNDFILE *file ;
  1203. SF_INFO sfinfo ;
  1204. unsigned k ;
  1205. int needs_endswap ;
  1206. print_test_name (__func__, filename) ;
  1207. for (k = 0 ; k < ARRAY_LEN (subtypes) ; k++)
  1208. {
  1209. if (filetype == (SF_ENDIAN_LITTLE | SF_FORMAT_AIFF))
  1210. switch (subtypes [k])
  1211. { /* Little endian AIFF does not AFAIK support fl32 and fl64. */
  1212. case SF_FORMAT_FLOAT :
  1213. case SF_FORMAT_DOUBLE :
  1214. continue ;
  1215. default :
  1216. break ;
  1217. } ;
  1218. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1219. sfinfo.samplerate = 11025 ;
  1220. sfinfo.format = filetype | subtypes [k] ;
  1221. sfinfo.channels = 1 ;
  1222. file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  1223. test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
  1224. sf_close (file) ;
  1225. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  1226. file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  1227. needs_endswap = sf_command (file, SFC_RAW_DATA_NEEDS_ENDSWAP, NULL, 0) ;
  1228. switch (filetype)
  1229. { case SF_FORMAT_WAV :
  1230. case SF_FORMAT_WAVEX :
  1231. case SF_FORMAT_AIFF | SF_ENDIAN_LITTLE :
  1232. exit_if_true (needs_endswap != CPU_IS_BIG_ENDIAN,
  1233. "\n\nLine %d : SFC_RAW_DATA_NEEDS_ENDSWAP failed for (%d | %d).\n\n", __LINE__, filetype, k) ;
  1234. break ;
  1235. case SF_FORMAT_AIFF :
  1236. case SF_FORMAT_WAV | SF_ENDIAN_BIG :
  1237. exit_if_true (needs_endswap != CPU_IS_LITTLE_ENDIAN,
  1238. "\n\nLine %d : SFC_RAW_DATA_NEEDS_ENDSWAP failed for (%d | %d).\n\n", __LINE__, filetype, k) ;
  1239. break ;
  1240. default :
  1241. printf ("\n\nLine %d : bad format value %d.\n\n", __LINE__, filetype) ;
  1242. exit (1) ;
  1243. break ;
  1244. } ;
  1245. sf_close (file) ;
  1246. } ;
  1247. unlink (filename) ;
  1248. puts ("ok") ;
  1249. } /* raw_needs_endswap_test */