1 | /* |
---|
2 | Audio File Library |
---|
3 | |
---|
4 | Copyright (C) 2000, Michael Pruett <michael@68k.org> |
---|
5 | Copyright (C) 2001, Silicon Graphics, Inc. |
---|
6 | |
---|
7 | This program is free software; you can redistribute it and/or |
---|
8 | modify it under the terms of the GNU General Public License as |
---|
9 | published by the Free Software Foundation; either version 2 of |
---|
10 | the License, or (at your option) any later version. |
---|
11 | |
---|
12 | This program is distributed in the hope that it will be |
---|
13 | useful, but WITHOUT ANY WARRANTY; without even the implied |
---|
14 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
---|
15 | PURPOSE. See the GNU General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU General Public |
---|
18 | License along with this program; if not, write to the Free |
---|
19 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
---|
20 | MA 02111-1307, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | /* |
---|
24 | writeulaw.c |
---|
25 | |
---|
26 | The writeulaw program performs sanity testing on the Audio File |
---|
27 | Library's G.711 u-law compression by writing and then reading |
---|
28 | back known data to a file to make sure the two sets of data agree. |
---|
29 | |
---|
30 | This program writes a set of data which is invariant under G.711 |
---|
31 | u-law compression to a file and then reads that set of data back. |
---|
32 | |
---|
33 | The data read from that file should match the data written |
---|
34 | exactly. |
---|
35 | |
---|
36 | If this test fails, something in the Audio File Library is broken. |
---|
37 | */ |
---|
38 | |
---|
39 | #ifdef HAVE_CONFIG_H |
---|
40 | #include <config.h> |
---|
41 | #endif |
---|
42 | |
---|
43 | #ifdef __USE_SGI_HEADERS__ |
---|
44 | #include <dmedia/audiofile.h> |
---|
45 | #else |
---|
46 | #include <audiofile.h> |
---|
47 | #endif |
---|
48 | |
---|
49 | #include <stdio.h> |
---|
50 | #include <unistd.h> |
---|
51 | |
---|
52 | #define TEST_FILE "/tmp/test.ulaw" |
---|
53 | |
---|
54 | #define FRAME_COUNT 16 |
---|
55 | #define SAMPLE_COUNT FRAME_COUNT |
---|
56 | |
---|
57 | void testulaw (int fileFormat); |
---|
58 | |
---|
59 | void cleanup (void) |
---|
60 | { |
---|
61 | #ifndef DEBUG |
---|
62 | unlink(TEST_FILE); |
---|
63 | #endif |
---|
64 | } |
---|
65 | |
---|
66 | void ensure (int condition, const char *message) |
---|
67 | { |
---|
68 | if (!condition) |
---|
69 | { |
---|
70 | printf("%s.\n", message); |
---|
71 | cleanup(); |
---|
72 | exit(-1); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | int main (int argc, char **argv) |
---|
77 | { |
---|
78 | printf("writeulaw: testing NeXT .snd.\n"); |
---|
79 | testulaw(AF_FILE_NEXTSND); |
---|
80 | printf("writeulaw: testing AIFF-C.\n"); |
---|
81 | testulaw(AF_FILE_AIFFC); |
---|
82 | printf("writeulaw: testing WAVE.\n"); |
---|
83 | testulaw(AF_FILE_WAVE); |
---|
84 | |
---|
85 | printf("writeulaw test passed.\n"); |
---|
86 | |
---|
87 | exit(0); |
---|
88 | } |
---|
89 | |
---|
90 | void testulaw (int fileFormat) |
---|
91 | { |
---|
92 | AFfilehandle file; |
---|
93 | AFfilesetup setup; |
---|
94 | u_int16_t samples[] = {8, 16, 80, 120, 180, 780, 924, 988, |
---|
95 | 1116, 1436, 1884, 8828, 9852, 15996, 19836, 32124}; |
---|
96 | u_int16_t readsamples[SAMPLE_COUNT]; |
---|
97 | AFframecount framesWritten, framesRead; |
---|
98 | int i; |
---|
99 | |
---|
100 | setup = afNewFileSetup(); |
---|
101 | |
---|
102 | afInitCompression(setup, AF_DEFAULT_TRACK, AF_COMPRESSION_G711_ULAW); |
---|
103 | afInitFileFormat(setup, fileFormat); |
---|
104 | afInitChannels(setup, AF_DEFAULT_TRACK, 1); |
---|
105 | |
---|
106 | file = afOpenFile(TEST_FILE, "w", setup); |
---|
107 | afFreeFileSetup(setup); |
---|
108 | |
---|
109 | ensure(afGetCompression(file, AF_DEFAULT_TRACK) == |
---|
110 | AF_COMPRESSION_G711_ULAW, |
---|
111 | "test file not created with G.711 u-law compression"); |
---|
112 | |
---|
113 | ensure(file != AF_NULL_FILEHANDLE, "unable to open file for writing"); |
---|
114 | |
---|
115 | framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, samples, |
---|
116 | FRAME_COUNT); |
---|
117 | |
---|
118 | ensure(framesWritten == FRAME_COUNT, |
---|
119 | "number of frames requested does not match number of frames written"); |
---|
120 | afCloseFile(file); |
---|
121 | |
---|
122 | /* Open the file for reading and verify the data. */ |
---|
123 | file = afOpenFile(TEST_FILE, "r", NULL); |
---|
124 | ensure(file != AF_NULL_FILEHANDLE, "unable to open file for reading"); |
---|
125 | |
---|
126 | ensure(afGetFileFormat(file, NULL) == fileFormat, |
---|
127 | "test file format incorrect"); |
---|
128 | |
---|
129 | ensure(afGetCompression(file, AF_DEFAULT_TRACK) == |
---|
130 | AF_COMPRESSION_G711_ULAW, |
---|
131 | "test file not opened with G.711 u-law compression"); |
---|
132 | |
---|
133 | framesRead = afReadFrames(file, AF_DEFAULT_TRACK, readsamples, |
---|
134 | FRAME_COUNT); |
---|
135 | |
---|
136 | ensure(framesRead == FRAME_COUNT, |
---|
137 | "number of frames read does not match number of frames requested"); |
---|
138 | |
---|
139 | #ifdef DEBUG |
---|
140 | for (i=0; i<SAMPLE_COUNT; i++) |
---|
141 | printf("readsamples[%d]: %d\n", i, readsamples[i]); |
---|
142 | for (i=0; i<SAMPLE_COUNT; i++) |
---|
143 | printf("samples[%d]: %d\n", i, samples[i]); |
---|
144 | #endif |
---|
145 | |
---|
146 | for (i=0; i<SAMPLE_COUNT; i++) |
---|
147 | { |
---|
148 | ensure(samples[i] == readsamples[i], |
---|
149 | "data written does not match data read"); |
---|
150 | } |
---|
151 | |
---|
152 | /* G.711 compression uses one byte per sample. */ |
---|
153 | ensure(afGetTrackBytes(file, AF_DEFAULT_TRACK) == SAMPLE_COUNT, |
---|
154 | "track byte count is incorrect"); |
---|
155 | |
---|
156 | ensure(afGetFrameCount(file, AF_DEFAULT_TRACK) == FRAME_COUNT, |
---|
157 | "frame count is incorrect"); |
---|
158 | |
---|
159 | ensure(afGetChannels(file, AF_DEFAULT_TRACK) == 1, |
---|
160 | "channel count is incorrect"); |
---|
161 | |
---|
162 | ensure(afCloseFile(file) == 0, "error closing file"); |
---|
163 | |
---|
164 | cleanup(); |
---|
165 | } |
---|