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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/*
Audio File Library
Copyright (C) 2004, Michael Pruett <michael@68k.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307 USA.
*/
/*
iffwrite.c
This file contains routines for writing IFF/8SVX format sound
files.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <assert.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include "afinternal.h"
#include "audiofile.h"
#include "byteorder.h"
#include "util.h"
#include "setup.h"
#include "iff.h"
status _af_iff_update (AFfilehandle file);
static status WriteVHDR (AFfilehandle file);
static status WriteMiscellaneous (AFfilehandle file);
static status WriteBODY (AFfilehandle file);
static _IFFinfo *iff_info_new (void)
{
_IFFinfo *iff = _af_calloc(1, sizeof (_IFFinfo));
iff->miscellaneousPosition = 0;
iff->VHDR_offset = 0;
iff->BODY_offset = 0;
return iff;
}
status _af_iff_write_init (AFfilesetup setup, AFfilehandle file)
{
u_int32_t fileSize = 0;
if (_af_filesetup_make_handle(setup, file) == AF_FAIL)
return AF_FAIL;
file->formatSpecific = iff_info_new();
af_fwrite("FORM", 4, 1, file->fh);
af_write_uint32_be(&fileSize, file->fh);
af_fwrite("8SVX", 4, 1, file->fh);
WriteVHDR(file);
WriteMiscellaneous(file);
WriteBODY(file);
return AF_SUCCEED;
}
status _af_iff_update (AFfilehandle file)
{
u_int32_t length;
WriteVHDR(file);
WriteMiscellaneous(file);
WriteBODY(file);
/* Get the length of the file. */
length = af_flength(file->fh);
length -= 8;
/* Set the length of the FORM chunk. */
af_fseek(file->fh, 4, SEEK_SET);
af_write_uint32_be(&length, file->fh);
return AF_SUCCEED;
}
static status WriteVHDR (const AFfilehandle file)
{
_Track *track;
_IFFinfo *iff;
u_int32_t chunkSize;
u_int32_t oneShotSamples, repeatSamples, samplesPerRepeat;
u_int16_t sampleRate;
u_int8_t octaves, compression;
u_int32_t volume;
iff = (_IFFinfo *) file->formatSpecific;
/*
If VHDR_offset hasn't been set yet, set it to the
current offset.
*/
if (iff->VHDR_offset == 0)
iff->VHDR_offset = af_ftell(file->fh);
else
af_fseek(file->fh, iff->VHDR_offset, SEEK_SET);
track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK);
af_fwrite("VHDR", 4, 1, file->fh);
chunkSize = 20;
af_write_uint32_be(&chunkSize, file->fh);
/*
IFF/8SVX files have only one audio channel, so the
number of samples is equal to the number of frames.
*/
oneShotSamples = track->totalfframes;
af_write_uint32_be(&oneShotSamples, file->fh);
repeatSamples = 0;
af_write_uint32_be(&repeatSamples, file->fh);
samplesPerRepeat = 0;
af_write_uint32_be(&samplesPerRepeat, file->fh);
sampleRate = track->f.sampleRate;
af_write_uint16_be(&sampleRate, file->fh);
octaves = 0;
compression = 0;
af_fwrite(&octaves, 1, 1, file->fh);
af_fwrite(&compression, 1, 1, file->fh);
/* Volume is in fixed-point notation; 65536 means gain of 1.0. */
volume = 65536;
af_write_uint32_be(&volume, file->fh);
return AF_SUCCEED;
}
static status WriteBODY (AFfilehandle file)
{
_Track *track;
u_int32_t chunkSize;
_IFFinfo *iff = (_IFFinfo *) file->formatSpecific;
track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK);
if (iff->BODY_offset == 0)
iff->BODY_offset = af_ftell(file->fh);
else
af_fseek(file->fh, iff->BODY_offset, SEEK_SET);
af_fwrite("BODY", 4, 1, file->fh);
/*
IFF/8SVX supports only one channel, so the number of
frames is equal to the number of samples, and each
sample is one byte.
*/
chunkSize = track->totalfframes;
af_write_uint32_be(&chunkSize, file->fh);
if (track->fpos_first_frame == 0)
track->fpos_first_frame = af_ftell(file->fh);
/* Add a pad byte to the end of the chunk if the chunk size is odd. */
if ((chunkSize % 2) == 1)
{
u_int8_t zero = 0;
af_fseek(file->fh, iff->BODY_offset + 8 + chunkSize, SEEK_SET);
af_fwrite(&zero, 1, 1, file->fh);
}
return AF_SUCCEED;
}
/*
WriteMiscellaneous writes all the miscellaneous data chunks in a
file handle structure to an IFF/8SVX file.
*/
static status WriteMiscellaneous (AFfilehandle file)
{
_IFFinfo *iff;
int i;
iff = (_IFFinfo *) file->formatSpecific;
if (iff->miscellaneousPosition == 0)
iff->miscellaneousPosition = af_ftell(file->fh);
else
af_fseek(file->fh, iff->miscellaneousPosition, SEEK_SET);
for (i=0; i<file->miscellaneousCount; i++)
{
_Miscellaneous *misc = &file->miscellaneous[i];
u_int32_t chunkType, chunkSize;
u_int8_t padByte = 0;
switch (misc->type)
{
case AF_MISC_NAME:
memcpy(&chunkType, "NAME", 4); break;
case AF_MISC_AUTH:
memcpy(&chunkType, "AUTH", 4); break;
case AF_MISC_COPY:
memcpy(&chunkType, "(c) ", 4); break;
case AF_MISC_ANNO:
memcpy(&chunkType, "ANNO", 4); break;
}
af_fwrite(&chunkType, 4, 1, file->fh);
chunkSize = misc->size;
af_write_uint32_be(&chunkSize, file->fh);
/*
Write the miscellaneous buffer and then a pad byte
if necessary. If the buffer is null, skip the space
for now.
*/
if (misc->buffer != NULL)
af_fwrite(misc->buffer, misc->size, 1, file->fh);
else
af_fseek(file->fh, misc->size, SEEK_CUR);
if (misc->size % 2 != 0)
af_fwrite(&padByte, 1, 1, file->fh);
}
return AF_SUCCEED;
}
|