File: WavAudioSample.cc

package info (click to toggle)
exult 1.12.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky
  • size: 43,608 kB
  • sloc: cpp: 169,917; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 539; objc: 416
file content (212 lines) | stat: -rw-r--r-- 4,329 bytes parent folder | download | duplicates (2)
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
/*
Copyright (C) 2010-2022 The Exult Team

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.
*/
#include "pent_include.h"

#include "WavAudioSample.h"

#include "databuf.h"

#include <cstring>

#ifdef __GNUC__
#	pragma GCC diagnostic push
#	pragma GCC diagnostic ignored "-Wold-style-cast"
#	pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif    // __GNUC__
#include <SDL.h>
#ifdef __GNUC__
#	pragma GCC diagnostic pop
#endif    // __GNUC__

using namespace Pentagram;

WavAudioSample::WavAudioSample(std::unique_ptr<uint8[]> buffer_, uint32 size)
		: RawAudioSample(std::move(buffer_), 0, 0, false, false) {
	uint32 pos_fmt  = 0;
	uint32 size_fmt = 0;

	uint32 pos_data  = 0;
	uint32 size_data = 0;

	IBufferDataView ds(buffer, size);

	char buf[4];

	ds.seek(0);

	ds.read(buf, 4);
	if (std::memcmp(buf, "RIFF", 4) != 0) {
		return;
	}

	const uint32 riff_size = ds.read4();
	if (riff_size != size - 8) {
		return;
	}

	ds.read(buf, 4);
	if (std::memcmp(buf, "WAVE", 4) != 0) {
		return;
	}

	while (ds.getPos() < riff_size + 8) {
		ds.read(buf, 4);
		const uint32 chunk_size = ds.read4();

		if (!std::memcmp(buf, "fmt ", 4)) {
			pos_fmt  = ds.getPos();
			size_fmt = chunk_size;

			if (pos_data) {
				break;
			}
		} else if (!std::memcmp(buf, "data", 4)) {
			pos_data  = ds.getPos();
			size_data = chunk_size;

			if (pos_fmt) {
				break;
			}
		}

		ds.skip(chunk_size);
	}

	if (!pos_fmt || !pos_data || size_fmt < 0x10) {
		return;
	}

	ds.seek(pos_fmt);
	const uint16 format_tag = ds.read2();
	const uint16 channels   = ds.read2();
	sample_rate             = ds.read4();
	// uint32 bytes_per_second = ds.read4();
	// uint16 block_align = ds.read2();
	ds.skip(6);
	const uint16 bits_per_sample = ds.read2();
	// uint16 extra_bytes = ds.read2();
	ds.skip(2);

	if (format_tag != 1) {
		return;
	}
	if (channels != 1 && channels != 2) {
		return;
	}
	if (bits_per_sample != 16 && bits_per_sample != 8) {
		return;
	}

	stereo = channels == 2;
	bits   = bits_per_sample;

	buffer_limit = pos_data + size_data;
	start_pos    = pos_data;

	length = size_data / (bits_per_sample / (stereo ? 4 : 8));

#if SDL_BYTEORDER == SDL_LIL_ENDIAN
	byte_swap = false;
#else
	byte_swap = true;
#endif

	signeddata = bits == 16;
}

bool WavAudioSample::isThis(IDataSource* ds) {
	char buf[4];
	ds->seek(0);

	ds->read(buf, 4);
	if (std::memcmp(buf, "RIFF", 4) != 0) {
		return false;
	}

	const uint32 riff_size = ds->read4();

	if (riff_size != ds->getAvail()) {
		return false;
	}

	ds->read(buf, 4);
	if (std::memcmp(buf, "WAVE", 4) != 0) {
		return false;
	}

	uint32 pos_fmt  = 0;
	uint32 size_fmt = 0;

	uint32 pos_data = 0;
	// uint32 size_data = 0;

	while (ds->getAvail() > 0) {
		ds->read(buf, 4);
		const uint32 chunk_size = ds->read4();

		if (!std::memcmp(buf, "fmt ", 4)) {
			pos_fmt  = ds->getPos();
			size_fmt = chunk_size;

			if (pos_data) {
				break;
			}
		} else if (!std::memcmp(buf, "data", 4)) {
			if (pos_data) {
				return false;
			}

			pos_data = ds->getPos();
			// size_data = chunk_size;

			if (pos_fmt) {
				break;
			}
		}

		ds->skip(chunk_size);
	}

	if (!pos_fmt || !pos_data || size_fmt < 0x10) {
		return false;
	}

	ds->seek(pos_fmt);

	const uint16 format_tag = ds->read2();
	const uint16 channels   = ds->read2();
	// uint32 sample_rate = ds->read4();
	// uint32 bytes_per_second = ds->read4();;
	// uint16 block_align = ds->read2();
	ds->skip(10);
	const uint16 bits_per_sample = ds->read2();
	// uint16 extra_bytes = ds->read2();
	ds->skip(2);

	if (format_tag != 1) {
		return false;
	}
	if (channels != 1 && channels != 2) {
		return false;
	}
	if (bits_per_sample != 16 && bits_per_sample != 8) {
		return false;
	}

	return true;
}