summaryrefslogtreecommitdiff
path: root/test/writewave.c
diff options
context:
space:
mode:
authorGraydon, Tracy <tracy.graydon@intel.com>2012-11-28 18:07:32 -0800
committerGraydon, Tracy <tracy.graydon@intel.com>2012-11-28 18:07:32 -0800
commitf9151a123d7943fb7e944a38b1aa835c03b0188e (patch)
treee7dfd84705f5db614d08449cb3e70c4dad74ff97 /test/writewave.c
downloadaudiofile-f9151a123d7943fb7e944a38b1aa835c03b0188e.tar.gz
audiofile-f9151a123d7943fb7e944a38b1aa835c03b0188e.tar.bz2
audiofile-f9151a123d7943fb7e944a38b1aa835c03b0188e.zip
Diffstat (limited to 'test/writewave.c')
-rw-r--r--test/writewave.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/test/writewave.c b/test/writewave.c
new file mode 100644
index 0000000..65afede
--- /dev/null
+++ b/test/writewave.c
@@ -0,0 +1,123 @@
+/*
+ Audio File Library
+
+ Copyright 1998-1999, Michael Pruett <michael@68k.org>
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of
+ the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be
+ useful, but WITHOUT ANY WARRANTY; without even the implied
+ warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ PURPOSE. See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public
+ License along with this program; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ MA 02111-1307, USA.
+*/
+
+/*
+ writewave.c
+
+ This program tests the validity of the MS RIFF Wave format reading
+ and writing code.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdio.h>
+
+#ifdef __USE_SGI_HEADERS__
+#include <dmedia/audiofile.h>
+#else
+#include <audiofile.h>
+#endif
+
+#define TEST_FILE "/tmp/test.wave"
+
+void cleanup (void)
+{
+#ifndef DEBUG
+ unlink(TEST_FILE);
+#endif
+}
+
+void ensure (int condition, const char *message)
+{
+ if (!condition)
+ {
+ printf("%s.\n", message);
+ cleanup();
+ exit(EXIT_FAILURE);
+ }
+}
+
+int main (int argc, char **argv)
+{
+ AFfilehandle file;
+ AFfilesetup setup;
+ u_int16_t samples[] = {11, 51, 101, 501, 1001, 5001, 10001, 50001};
+ int i;
+ int sampleFormat, sampleWidth;
+ int framesRead, framesWritten;
+
+ setup = afNewFileSetup();
+ afInitFileFormat(setup, AF_FILE_WAVE);
+ afInitChannels(setup, AF_DEFAULT_TRACK, 1);
+ afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+
+ file = afOpenFile(TEST_FILE, "w", setup);
+ ensure(file != AF_NULL_FILEHANDLE, "unable to open file for writing");
+
+ framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, samples, 8);
+ ensure(framesWritten == 8,
+ "number of frames written does not match number of frames requested");
+
+ ensure(afCloseFile(file) == 0, "error closing file");
+ afFreeFileSetup(setup);
+
+ file = afOpenFile(TEST_FILE, "r", NULL);
+ ensure(file != AF_NULL_FILEHANDLE, "unable to open file for reading");
+
+ ensure(afGetFileFormat(file, NULL) == AF_FILE_WAVE,
+ "test file not created as Wave");
+
+ afGetSampleFormat(file, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth);
+ ensure(sampleFormat == AF_SAMPFMT_TWOSCOMP,
+ "test file not two's complement");
+ ensure(sampleWidth == 16,
+ "test file sample format is not 16-bit");
+
+ ensure(afGetChannels(file, AF_DEFAULT_TRACK) == 1,
+ "test file doesn't have exactly one channel");
+
+ ensure(afGetByteOrder(file, AF_DEFAULT_TRACK) == AF_BYTEORDER_LITTLEENDIAN,
+ "test file not little-endian");
+
+ for (i=0; i<8; i++)
+ {
+ u_int16_t temporary;
+
+ framesRead = afReadFrames(file, AF_DEFAULT_TRACK, &temporary, 1);
+ ensure(framesRead == 1,
+ "number of frames read does not match number of frames requested");
+
+ ensure(temporary == samples[i],
+ "data written to file doesn't match data read from file");
+ }
+
+ ensure(afCloseFile(file) == 0, "error closing file");
+
+ cleanup();
+
+ printf("writewave test passed.\n");
+
+ exit(0);
+}