r663: This commit was generated by cvs2svn to compensate for changes in r662,
[cinelerra_cv.git] / libsndfile / tests / headerless_test.c
blobcfcfc720449a9ed5cbb1c577ce6f9b34acf581b3
1 /*
2 ** Copyright (C) 1999-2003 Erik de Castro Lopo <erikd@zip.com.au>
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.
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <math.h>
26 #include <sndfile.h>
28 #include "utils.h"
30 #define BUFFER_SIZE (100)
32 int
33 main (void)
34 { static short buffer [BUFFER_SIZE] ;
35 SNDFILE *file ;
36 SF_INFO sfinfo ;
37 int k, filetype ;
38 char *filename = "headerless.wav" ;
40 printf (" header-less test : ") ;
41 fflush (stdout) ;
43 for (k = 0 ; k < BUFFER_SIZE ; k++)
44 buffer [k] = k ;
46 filetype = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
48 sfinfo.samplerate = 32000 ;
49 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
50 sfinfo.channels = 1 ;
51 sfinfo.format = filetype ;
53 if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
54 { printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;
55 fflush (stdout) ;
56 puts (sf_strerror (NULL)) ;
57 exit (1) ;
58 } ;
60 if ((k = sf_write_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
61 { printf ("Line %d: sf_write_short failed with short write (%d => %d).\n", __LINE__, BUFFER_SIZE, k) ;
62 fflush (stdout) ;
63 puts (sf_strerror (file)) ;
64 exit (1) ;
65 } ;
67 sf_close (file) ;
69 memset (buffer, 0, sizeof (buffer)) ;
71 /* Read as RAW but get the bit width and endian-ness correct. */
72 sfinfo.format = filetype = SF_ENDIAN_LITTLE | SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
74 if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
75 { printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;
76 fflush (stdout) ;
77 puts (sf_strerror (NULL)) ;
78 exit (1) ;
79 } ;
81 if (sfinfo.format != filetype)
82 { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
83 exit (1) ;
84 } ;
86 if (sfinfo.frames < BUFFER_SIZE)
87 { printf ("Line %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
88 exit (1) ;
89 } ;
91 if (sfinfo.channels != 1)
92 { printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
93 exit (1) ;
94 } ;
96 check_log_buffer_or_die (file) ;
98 if ((k = sf_read_short (file, buffer, BUFFER_SIZE)) != BUFFER_SIZE)
99 { printf ("Line %d: short read (%d).\n", __LINE__, k) ;
100 exit (1) ;
103 for (k = 0 ; k < BUFFER_SIZE - 22 ; k++)
104 if (buffer [k + 22] != k)
105 { printf ("Line %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__, k, k, buffer [k]) ;
106 exit (1) ;
109 printf ("ok\n") ;
110 unlink (filename) ;
112 return 0;
113 } /* main */