1 | /* |
---|
2 | Audio File Library |
---|
3 | |
---|
4 | Copyright 1998-1999, Michael Pruett <michael@68k.org> |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or |
---|
7 | modify it under the terms of the GNU General Public License as |
---|
8 | published by the Free Software Foundation; either version 2 of |
---|
9 | the License, or (at your option) any later version. |
---|
10 | |
---|
11 | This program is distributed in the hope that it will be |
---|
12 | useful, but WITHOUT ANY WARRANTY; without even the implied |
---|
13 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
---|
14 | PURPOSE. See the GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public |
---|
17 | License along with this program; if not, write to the Free |
---|
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
---|
19 | MA 02111-1307, USA. |
---|
20 | */ |
---|
21 | |
---|
22 | /* |
---|
23 | writewave.c |
---|
24 | |
---|
25 | This program tests the validity of the MS RIFF Wave format reading |
---|
26 | and writing code. |
---|
27 | */ |
---|
28 | |
---|
29 | #ifdef HAVE_CONFIG_H |
---|
30 | #include <config.h> |
---|
31 | #endif |
---|
32 | |
---|
33 | #include <stdlib.h> |
---|
34 | #include <unistd.h> |
---|
35 | #include <stdio.h> |
---|
36 | |
---|
37 | #ifdef __USE_SGI_HEADERS__ |
---|
38 | #include <dmedia/audiofile.h> |
---|
39 | #else |
---|
40 | #include <audiofile.h> |
---|
41 | #endif |
---|
42 | |
---|
43 | #define TEST_FILE "/tmp/test.wave" |
---|
44 | |
---|
45 | void cleanup (void) |
---|
46 | { |
---|
47 | #ifndef DEBUG |
---|
48 | unlink(TEST_FILE); |
---|
49 | #endif |
---|
50 | } |
---|
51 | |
---|
52 | void ensure (int condition, const char *message) |
---|
53 | { |
---|
54 | if (!condition) |
---|
55 | { |
---|
56 | printf("%s.\n", message); |
---|
57 | cleanup(); |
---|
58 | exit(-1); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | int main (int argc, char **argv) |
---|
63 | { |
---|
64 | AFfilehandle file; |
---|
65 | AFfilesetup setup; |
---|
66 | u_int16_t samples[] = {11, 51, 101, 501, 1001, 5001, 10001, 50001}; |
---|
67 | int i; |
---|
68 | int sampleFormat, sampleWidth; |
---|
69 | int framesRead, framesWritten; |
---|
70 | |
---|
71 | setup = afNewFileSetup(); |
---|
72 | afInitFileFormat(setup, AF_FILE_WAVE); |
---|
73 | afInitChannels(setup, AF_DEFAULT_TRACK, 1); |
---|
74 | afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16); |
---|
75 | |
---|
76 | file = afOpenFile(TEST_FILE, "w", setup); |
---|
77 | ensure(file != AF_NULL_FILEHANDLE, "unable to open file for writing"); |
---|
78 | |
---|
79 | framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, samples, 8); |
---|
80 | ensure(framesWritten == 8, |
---|
81 | "number of frames written does not match number of frames requested"); |
---|
82 | |
---|
83 | ensure(afCloseFile(file) == 0, "error closing file"); |
---|
84 | afFreeFileSetup(setup); |
---|
85 | |
---|
86 | file = afOpenFile(TEST_FILE, "r", NULL); |
---|
87 | ensure(file != AF_NULL_FILEHANDLE, "unable to open file for reading"); |
---|
88 | |
---|
89 | ensure(afGetFileFormat(file, NULL) == AF_FILE_WAVE, |
---|
90 | "test file not created as Wave"); |
---|
91 | |
---|
92 | afGetSampleFormat(file, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth); |
---|
93 | ensure(sampleFormat == AF_SAMPFMT_TWOSCOMP, |
---|
94 | "test file not two's complement"); |
---|
95 | ensure(sampleWidth == 16, |
---|
96 | "test file sample format is not 16-bit"); |
---|
97 | |
---|
98 | ensure(afGetChannels(file, AF_DEFAULT_TRACK) == 1, |
---|
99 | "test file doesn't have exactly one channel"); |
---|
100 | |
---|
101 | ensure(afGetByteOrder(file, AF_DEFAULT_TRACK) == AF_BYTEORDER_LITTLEENDIAN, |
---|
102 | "test file not little-endian"); |
---|
103 | |
---|
104 | for (i=0; i<8; i++) |
---|
105 | { |
---|
106 | u_int16_t temporary; |
---|
107 | |
---|
108 | framesRead = afReadFrames(file, AF_DEFAULT_TRACK, &temporary, 1); |
---|
109 | ensure(framesRead == 1, |
---|
110 | "number of frames read does not match number of frames requested"); |
---|
111 | |
---|
112 | ensure(temporary == samples[i], |
---|
113 | "data written to file doesn't match data read from file"); |
---|
114 | } |
---|
115 | |
---|
116 | ensure(afCloseFile(file) == 0, "error closing file"); |
---|
117 | |
---|
118 | cleanup(); |
---|
119 | |
---|
120 | printf("writewave test passed.\n"); |
---|
121 | |
---|
122 | exit(0); |
---|
123 | } |
---|