source: trunk/third/audiofile/test/testfloat.c @ 17099

Revision 17099, 3.4 KB checked in by ghudson, 23 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r17098, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2        Audio File Library
3
4        Copyright (C) 2001, Silicon Graphics, Inc.
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        testfloat.c
24
25        This program tests floating-point reading and writing for
26        the AIFF-C, WAVE, NeXT .snd, and IRCAM file formats.
27*/
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <audiofile.h>
32
33#define TEST_FILE "/tmp/test.float"
34
35const float samples[] =
36        {1.0, 0.6, -0.3, 0.95, 0.2, -0.6, 0.9, 0.4, -0.22, 0.125, 0.1, -0.4};
37#define SAMPLE_COUNT (sizeof (samples) / sizeof (float))
38
39void testfloat (int fileFormat);
40
41void cleanup (void)
42{
43        unlink(TEST_FILE);
44}
45
46void ensure (int condition, const char *message)
47{
48        if (!condition)
49        {
50                printf("%s.\n", message);
51                cleanup();
52                exit(EXIT_FAILURE);
53        }
54}
55
56int main (int argc, char **argv)
57{
58        /* These file formats support floating-point audio data. */
59        int     fileFormatCount = 4;
60        int     fileFormats[] =
61                {AF_FILE_AIFFC, AF_FILE_WAVE, AF_FILE_NEXTSND, AF_FILE_IRCAM};
62        char    *formatNames[] = {"AIFF-C", "WAVE", "NeXT .snd", "IRCAM"};
63        int     i;
64
65        for (i=0; i<fileFormatCount; i++)
66        {
67                printf("testfloat: testing %s\n", formatNames[i]);
68                testfloat(fileFormats[i]);
69        }
70
71        printf("testfloat passed\n");
72        exit(EXIT_SUCCESS);
73}
74
75void testfloat (int fileFormat)
76{
77        AFfilesetup     setup;
78        AFfilehandle    file;
79        int             framesWritten, framesRead;
80        const int       frameCount = SAMPLE_COUNT/2;
81        float           readsamples[SAMPLE_COUNT];
82        int             i;
83        int             sampleFormat, sampleWidth;
84
85        setup = afNewFileSetup();
86
87        afInitFileFormat(setup, fileFormat);
88        afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_FLOAT, 32);
89        afInitChannels(setup, AF_DEFAULT_TRACK, 2);
90
91        file = afOpenFile(TEST_FILE, "w", setup);
92        ensure(file != AF_NULL_FILEHANDLE, "could not open file for writing");
93
94        afFreeFileSetup(setup);
95
96        framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, samples,
97                frameCount);
98        ensure(framesWritten == frameCount, "number of frames written does not match number of frames requested");
99
100        ensure(afCloseFile(file) == 0, "error closing file");
101
102        file = afOpenFile(TEST_FILE, "r", AF_NULL_FILESETUP);
103        ensure(file != AF_NULL_FILEHANDLE, "could not open file for reading");
104
105        ensure(afGetChannels(file, AF_DEFAULT_TRACK) == 2,
106                "file doesn't have exactly two channels");
107        afGetSampleFormat(file, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth);
108        ensure(sampleFormat == AF_SAMPFMT_FLOAT && sampleWidth == 32,
109                "file doesn't contain 32-bit floating-point data");
110        ensure(afGetFileFormat(file, NULL) == fileFormat,
111                "file format doesn't match format requested");
112
113        framesRead = afReadFrames(file, AF_DEFAULT_TRACK, readsamples,
114                frameCount);
115        ensure(framesRead == frameCount, "number of frames read does not match number of frames requested");
116
117        for (i=0; i<SAMPLE_COUNT; i++)
118        {
119                ensure(readsamples[i] == samples[i],
120                        "data written to file doesn't match data read");
121        }
122
123        ensure(afCloseFile(file) == 0, "error closing file");
124}
Note: See TracBrowser for help on using the repository browser.