floatto24.c
3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Audio File Library
Copyright (C) 2001, Silicon Graphics, Inc.
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.
*/
/*
floatto24.c
This program creates a BICSF floating-point sound file and then
reads the sample data back as 24-bit data (i.e. 32-bit integers
with the high 8 bits equal to the sign extension of the lower
24 bits).
*/
#include <audiofile.h>
#include <stdio.h>
#include <stdlib.h>
#define TEST_FILE "/tmp/test.sf"
#define FRAME_COUNT 10
/*
When converted to samples with width 24 bits, the samples
should have the following values:
*/
const float samples[] =
{
0,
0.5,
-0.5,
0,
1,
-1,
-0.25,
0.25,
0.75,
-0.75
};
const int referenceConvertedSamples[] =
{
0,
4194303, /* = (2^23 - 1) / 2 */
-4194303,
0,
8388607, /* = 2^23 - 1 */
-8388607,
-2097151,
2097151, /* = (2^23 - 1) / 4 */
6291455, /* = (2^23 - 1) * 3 / 4 */
-6291455
};
int main (int argc, char **argv)
{
AFfilehandle file;
AFfilesetup setup;
AFframecount framesWritten, framesRead;
int readsamples[FRAME_COUNT] = {-1000, -1001, -1002, -1003,
-1004, -1005, -1006, -1007};
int i;
if ((setup = afNewFileSetup()) == AF_NULL_FILESETUP)
{
fprintf(stderr, "Could not allocate file setup.\n");
exit(EXIT_FAILURE);
}
afInitFileFormat(setup, AF_FILE_IRCAM);
afInitChannels(setup, AF_DEFAULT_TRACK, 1);
afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_FLOAT, 32);
file = afOpenFile(TEST_FILE, "w", setup);
if (file == AF_NULL_FILEHANDLE)
{
printf("could not open file for writing\n");
exit(-1);
}
framesWritten = afWriteFrames(file, AF_DEFAULT_TRACK, (void *) samples,
FRAME_COUNT);
if (framesWritten != FRAME_COUNT)
{
fprintf(stderr, "Wrong number of frames read.\n");
exit(EXIT_FAILURE);
}
if (afCloseFile(file) != 0)
{
fprintf(stderr, "Closing file returned non-zero status.\n");
exit(EXIT_FAILURE);
}
file = afOpenFile(TEST_FILE, "r", AF_NULL_FILESETUP);
if (file == AF_NULL_FILEHANDLE)
{
fprintf(stderr, "Could not open file for writing.\n");
exit(EXIT_FAILURE);
}
if (afSetVirtualSampleFormat(file, AF_DEFAULT_TRACK,
AF_SAMPFMT_TWOSCOMP, 24) != 0)
{
fprintf(stderr, "afSetVirtualSampleFormat returned non-zero status.\n");
exit(EXIT_FAILURE);
}
framesRead = afReadFrames(file, AF_DEFAULT_TRACK, readsamples,
FRAME_COUNT);
if (framesRead != FRAME_COUNT)
{
fprintf(stderr, "Wrong number of frames read.\n");
exit(EXIT_FAILURE);
}
for (i=0; i<framesRead; i++)
{
#ifdef DEBUG
printf("[%d] = %d\n", i, readsamples[i]);
#endif
if (readsamples[i] == -1000 - i)
{
fprintf(stderr, "Data in destination array untouched.\n");
exit(EXIT_FAILURE);
}
/*
Ensure that the high-order 8 bits represent
sign extension: only 0x00 (+) or 0xff (-) is
valid.
*/
if ((readsamples[i] & 0xff000000) != 0x000000 &&
(readsamples[i] & 0xff000000) != 0xff000000)
{
fprintf(stderr, "Data is not within range of "
"{-2^23, ..., 2^23-1}.\n");
exit(EXIT_FAILURE);
}
if (readsamples[i] != referenceConvertedSamples[i])
{
fprintf(stderr, "Data doesn't match reference data.\n");
exit(EXIT_FAILURE);
}
}
if (afCloseFile(file) != 0)
{
fprintf(stderr, "Closing file returned non-zero status.\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}