-
Notifications
You must be signed in to change notification settings - Fork 2
/
SndResource.cpp
410 lines (332 loc) · 9.07 KB
/
SndResource.cpp
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* SndResource.cpp
Copyright (C) 2008 by Gregory Smith
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.
This license is contained in the file "COPYING", which is included
with this source code; it is available online at
http://www.gnu.org/licenses/gpl.html
*/
#include <sndfile.h>
#include "ferro/AStream.h"
#include "SndResource.h"
#include <stdio.h> // SEEK_SET, SEEK_END, SEEK_CUR
using namespace atque;
static const int kBufferSize = 8192;
bool SndResource::Load(const std::vector<uint8>& data)
{
AIStreamBE stream(&data[0], data.size());
uint16 format;
stream >> format;
if (format != 1 && format != 2)
return false;
if (format == 1) {
uint16 num_data_formats;
stream >> num_data_formats;
stream.ignore(num_data_formats * 6);
}
else if (format == 2)
{
stream.ignore(2);
}
uint16 num_commands;
stream >> num_commands;
for (int i = 0; i < num_commands; i++)
{
uint16 cmd;
uint16 param1;
uint32 param2;
stream >> cmd;
stream >> param1;
stream >> param2;
if (cmd == 0x8051)
{
AIStreamBE sample(&data[param2], data.size() - param2);
if (data[param2 + 20] == 0x00)
{
return UnpackStandardSystem7Header(sample);
}
else if (data[param2 + 20] == 0xff || data[param2 + 20] == 0xfe)
{
return UnpackExtendedSystem7Header(sample);
}
}
}
return false;
}
std::vector<uint8> SndResource::Save() const
{
uint32 length = 10 + 10 + data_.size() + 64;
std::vector<uint8> result(length);
AOStreamBE stream(&result[0], result.size());
// resource header
int16 format = 1;
int16 data_formats = 1;
int16 data_type = 5;
uint32 initialization_options = (stereo_ ? 0x00c0 : 0x0080);
stream << format
<< data_formats
<< data_type
<< initialization_options;
// command
int16 num_commands = 1;
int16 command = 0x8051;
uint16 param1 = 0;
uint32 param2 = 20;
stream << num_commands
<< command
<< param1
<< param2;
// extended sound header
uint32 ptr = 0;
int32 num_channels = (stereo_ ? 2 : 1);
uint32 loop_start = 0;
uint32 loop_end = 0;
uint8 header_type = 0xff;
uint8 baseFrequency = 0;
int32 num_frames = data_.size() / bytes_per_frame_;
int16 sample_size = (sixteen_bit_ ? 16 : 8);
stream << ptr
<< num_channels
<< rate_
<< loop_start
<< loop_end
<< header_type
<< baseFrequency
<< num_frames;
stream.ignore(22);
stream << sample_size;
stream.write(&data_[0], data_.size());
return result;
}
// all this just to read a file from memory!?
struct sf_adapter
{
public:
sf_adapter(std::vector<uint8>& data) : data_(data), p_(data.begin()) { }
static sf_count_t get_filelen(void *pv) {
return ((sf_adapter*) pv)->_get_filelen();
}
static sf_count_t seek(sf_count_t offset, int whence, void *pv) {
return ((sf_adapter*) pv)->_seek(offset, whence);
}
static sf_count_t read(void *ptr, sf_count_t count, void *pv) {
return ((sf_adapter*) pv)->_read(ptr, count);
}
static sf_count_t write(const void *ptr, sf_count_t count, void *pv) {
return ((sf_adapter*) pv)->_write(ptr, count);
}
static sf_count_t tell(void *pv) {
return ((sf_adapter*) pv)->_tell();
}
private:
std::vector<uint8>& data_;
std::vector<uint8>::iterator p_;
sf_count_t _get_filelen() {
return data_.size();
}
sf_count_t _seek(sf_count_t offset, int whence) {
if (whence == SEEK_SET)
p_ = data_.begin() + offset;
else if (whence == SEEK_END)
p_ = data_.end() - offset;
else if (whence == SEEK_CUR)
p_ += offset;
return ((p_ >= data_.begin() && p_ <= data_.end()) ? 0 : -1);
}
sf_count_t _read(void *ptr, sf_count_t count) {
if (p_ >= data_.end()) return -1;
char *dst = reinterpret_cast<char *>(ptr);
int i = 0;
for (; i < count && p_ < data_.end(); ++i)
{
*(dst++) = *(p_++);
}
return i;
}
sf_count_t _write(const void *ptr, sf_count_t count) {
if (p_ >= data_.end()) return -1;
const char *src = reinterpret_cast<const char *>(ptr);
int i = 0;
for (; i < count && p_ < data_.end(); ++i)
{
*(p_++) = *(src++);
}
return i;
}
sf_count_t _tell() {
return p_ - data_.begin();
}
};
bool SndResource::Import(const std::string& path)
{
SF_INFO inputInfo;
SNDFILE *infile = sf_open(path.c_str(), SFM_READ, &inputInfo);
if (!infile) return false;
sixteen_bit_ = !(inputInfo.format & (SF_FORMAT_PCM_S8 | SF_FORMAT_PCM_U8));
rate_ = static_cast<uint32>(inputInfo.samplerate) << 16;
stereo_ = (inputInfo.channels == 2);
signed_8bit_ = false;
bytes_per_frame_ = (sixteen_bit_ ? 2 : 1) * (stereo_ ? 2 : 1);
SF_INFO outputInfo;
outputInfo.samplerate = inputInfo.samplerate;
outputInfo.channels = stereo_ ? 2 : 1;
if (sixteen_bit_)
{
outputInfo.format = SF_FORMAT_PCM_16 | SF_FORMAT_RAW | SF_ENDIAN_BIG;
}
else
{
outputInfo.format = SF_FORMAT_PCM_U8 | SF_FORMAT_RAW | SF_ENDIAN_BIG;
}
SF_VIRTUAL_IO virtual_io = {
&sf_adapter::get_filelen,
&sf_adapter::seek,
&sf_adapter::read,
&sf_adapter::write,
&sf_adapter::tell };
data_.resize(inputInfo.frames * bytes_per_frame_);
sf_adapter adapter(data_);
SNDFILE *outfile = sf_open_virtual(&virtual_io, SFM_WRITE, &outputInfo, &adapter);
if (!outfile)
return false;
int frames_remaining = inputInfo.frames;
while (frames_remaining)
{
int buf[kBufferSize * 2];
int frames = std::min(kBufferSize, frames_remaining);
if (sf_readf_int(infile, buf, frames) != frames)
{
std::cerr << "Read error" << std::endl;
return false;
}
if (sf_writef_int(outfile, buf, frames) != frames)
{
std::cerr << "Write error" << std::endl;
return false;
}
frames_remaining -= frames;
}
sf_close(infile);
sf_close(outfile);
return true;
}
void SndResource::Export(const std::string& path) const
{
// open an output file
int inputFormat;
int outputFormat;
if (sixteen_bit_)
{
inputFormat = outputFormat = SF_FORMAT_PCM_16;
}
else if (signed_8bit_)
{
inputFormat = SF_FORMAT_PCM_S8;
outputFormat = SF_FORMAT_PCM_U8;
}
else
inputFormat = outputFormat = SF_FORMAT_PCM_U8;
SF_INFO outputInfo;
outputInfo.samplerate = static_cast<int32>(rate_ >> 16);
outputInfo.channels = stereo_ ? 2 : 1;
outputInfo.format = SF_FORMAT_WAV | outputFormat;
SNDFILE *outfile = sf_open(path.c_str(), SFM_WRITE, &outputInfo);
SF_VIRTUAL_IO virtual_io = {
&sf_adapter::get_filelen,
&sf_adapter::seek,
&sf_adapter::read,
&sf_adapter::write,
&sf_adapter::tell };
sf_adapter adapter(const_cast<std::vector<uint8>& >(data_));
SF_INFO inputInfo;
inputInfo.samplerate = static_cast<int32>(rate_ >> 16);
inputInfo.channels = stereo_ ? 2 : 1;
inputInfo.format = SF_FORMAT_RAW | inputFormat | SF_ENDIAN_BIG;
SNDFILE *infile = sf_open_virtual(&virtual_io, SFM_READ, &inputInfo, &adapter);
int frames_remaining = data_.size() / bytes_per_frame_;
while (frames_remaining)
{
int buf[kBufferSize * 2];
int frames = std::min(kBufferSize, frames_remaining);
if (sf_readf_int(infile, buf, frames) != frames)
{
std::cerr << "Read error" << std::endl;
return;
}
if (sf_writef_int(outfile, buf, frames) != frames)
{
std::cerr << "Write error (" << sf_strerror(outfile) << ")" << std::endl;
return;
}
frames_remaining -= frames;
}
sf_close(infile);
sf_close(outfile);
}
bool SndResource::UnpackStandardSystem7Header(AIStreamBE& stream)
{
bytes_per_frame_ = 1;
signed_8bit_ = false;
sixteen_bit_ = false;
stereo_ = false;
stream.ignore(4); // sample pointer
int32 length;
stream >> length;
stream >> rate_;
stream.ignore(8); // loop_start, loop_end
stream.ignore(2);
data_.resize(length);
stream.read(&data_[0], data_.size());
return true;
}
bool SndResource::UnpackExtendedSystem7Header(AIStreamBE& stream)
{
signed_8bit_ = false;
stream.ignore(4); // sample pointer
int32 num_channels;
stream >> num_channels;
stereo_ = (num_channels == 2);
stream >> rate_;
stream.ignore(8); // loop_start, loop_end;
uint8 header_type;
stream >> header_type;
stream.ignore(1); // baseFrequency
int32 num_frames;
stream >> num_frames;
if (header_type == 0xfe)
{
stream.ignore(10); // AIFF rate
stream.ignore(4); // marker chunk
uint32 format;
stream >> format;
stream.ignore(4 * 3); // future use, ptr, ptr
int16 comp_id;
stream >> comp_id;
if (format != FOUR_CHARS_TO_INT('t','w','o','s') || comp_id != -1)
{
std::cerr << "Unsupported compression" << std::endl;
return false;
}
signed_8bit_ = true;
stream.ignore(4);
}
else
{
stream.ignore(22);
}
int16 sample_size;
stream >> sample_size;
if (header_type != 0xfe)
stream.ignore(14);
sixteen_bit_ = (sample_size == 16);
bytes_per_frame_ = (sixteen_bit_ ? 2 : 1) * (stereo_ ? 2 : 1);
data_.resize(num_frames * bytes_per_frame_);
stream.read(&data_[0], data_.size());
return true;
}