File: sonarc_audio_sample.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (246 lines) | stat: -rw-r--r-- 6,991 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
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "ultima/ultima8/misc/common_types.h"
#include "ultima/ultima8/audio/sonarc_audio_sample.h"
#include "common/memstream.h"
#include "audio/audiostream.h"
#include "audio/decoders/raw.h"

namespace Ultima {
namespace Ultima8 {

bool SonarcAudioSample::_generatedOneTable = false;
int SonarcAudioSample::_oneTable[256];

SonarcAudioSample::SonarcAudioSample(uint8 const *buffer, uint32 size) :
	AudioSample(buffer, size, 8, false, true), _srcOffset(0x20) {
	if (!_generatedOneTable) GenerateOneTable();

	_length = *_buffer;
	_length |= *(_buffer + 1) << 8;
	_length |= *(_buffer + 2) << 16;
	_length |= *(_buffer + 3) << 24;

	_sampleRate  = *(_buffer + 4);
	_sampleRate |= *(_buffer + 5) << 8;

	// Get frame bytes... we need to compensate for 'large' files
	uint32 frame_bytes = *(_buffer + _srcOffset);
	frame_bytes |= (*(_buffer + _srcOffset + 1)) << 8;

	if (frame_bytes == 0x20 && _length > 32767) {
		_srcOffset += 0x100;
	}

	// Get Num Frame Samples
	_frameSize = *(_buffer + _srcOffset + 2);
	_frameSize |= (*(_buffer + _srcOffset + 3)) << 8;
}

SonarcAudioSample::~SonarcAudioSample(void) {
}

//
// Sonarc Audio Decompressor
//

void SonarcAudioSample::GenerateOneTable() {
	// _oneTable[x] gives the number of consecutive 1's on the low side of x
	for (int i = 0; i < 256; ++i)
		_oneTable[i] = 0;

	for (int power = 2; power < 32; power *= 2)
		for (int col = power - 1; col < 16; col += power)
			for (int row = 0; row < 16; ++row)
				_oneTable[row * 16 + col]++;

	for (int i = 0; i < 16; ++i)
		_oneTable[i * 16 + 15] += _oneTable[i];
}

void SonarcAudioSample::decode_EC(int mode, int samplecount,
								  const uint8 *source, int sourcesize,
								  uint8 *dest) {
	bool zerospecial = false;
	uint32 data = 0;
	int inputbits = 0; // current 'fill rate' of data window

	if (mode >= 7) {
		mode -= 7;
		zerospecial = true;
	}

	while (samplecount) {
		// fill data window
		while (sourcesize && inputbits <= 24) {
			data |= (*source++) << inputbits;
			sourcesize--;
			inputbits += 8;
		}

		if (zerospecial && !(data & 0x1)) {
			*dest++ = 0x80; // output zero
			data >>= 1;
			inputbits--;
		} else {
			if (zerospecial) {
				data >>= 1; // strip one
				inputbits--;
			}

			uint8 lowByte = data & 0xFF;
			int ones = _oneTable[lowByte];

			if (ones == 0) {
				data >>= 1; // strip zero
				// low byte contains (mode+1) _bits of the sample
				const uint8 usample = data & 0xFF;
				int8 sample = usample << (7 - mode);
				sample >>= (7 - mode); // sign extend
				*dest++ = (uint8)(sample + 0x80);
				data >>= mode + 1;
				inputbits -= mode + 2;
			} else if (ones < 7 - mode) {
				data >>= ones + 1; // strip ones and zero
				// low byte contains (mode+ones) _bits of the sample
				const uint8 usample = data & 0xFF;
				int8 sample = usample << (7 - mode - ones);
				sample &= 0x7F;
				if (!(sample & 0x40))
					sample |= 0x80; // reconstruct sign bit
				sample >>= (7 - mode - ones); // sign extend
				*dest++ = (uint8)(sample + 0x80);
				data >>= (mode + ones);
				inputbits -= mode + 2 * ones + 1;
			} else {
				data >>= (7 - mode); // strip ones
				// low byte contains 7 _bits of the sample
				int8 sample = data & 0xFF;
				sample &= 0x7F;
				if (!(sample & 0x40))
					sample |= 0x80; // reconstruct sign bit
				*dest++ = (uint8)(sample + 0x80);
				data >>= 7;
				inputbits -= 2 * 7 - mode;
			}
		}
		samplecount--;
	}
}

void SonarcAudioSample::decode_LPC(int order, int nsamples,
								   uint8 *dest, const uint8 *factors) {
	uint8 *startdest = dest;
	dest -= order;

	// basic linear predictive (de)coding
	// the errors this produces are fixed by decode_EC

	for (int i = 0; i < nsamples; ++i) {
		uint8 *loopdest = dest++;
		int accum = 0;
		for (int j = order - 1; j >= 0; --j) {
			int8 val1 = (loopdest < startdest) ? 0 : (*loopdest);
			loopdest++;
			val1 ^= 0x80;
			int16 val2 = factors[j * 2] + (factors[j * 2 + 1] << 8);
			accum += (int)val1 * val2;
		}

		accum += 0x00000800;
		*loopdest -= (int8)((accum >> 12) & 0xFF);
	}
}

int SonarcAudioSample::audio_decode(const uint8 *source, uint8 *dest) {
	int size = source[0] + (source[1] << 8);
	uint16 checksum = 0;
	for (int i = 0; i < size / 2; ++i) {
		uint16 val = source[2 * i] + (source[2 * i + 1] << 8);
		checksum ^= val;
	}

	if (checksum != 0xACED) return -1;

	int order = source[7];
	int mode = source[6] - 8;
	int samplecount = source[2] + (source[3] << 8);

	decode_EC(mode, samplecount,
	          source + 8 + 2 * order, size - 8 - 2 * order,
	          dest);
	decode_LPC(order, samplecount, dest, source + 8);

	// Try to fix a number of clipped samples
	for (int i = 1; i < samplecount; ++i)
		if (dest[i] == 0 && dest[i - 1] > 192) dest[i] = 0xFF;


	return 0;
}

uint32 SonarcAudioSample::decompressFrame(SonarcDecompData *decomp, uint8 *samples) const {
	if (decomp->_pos == _bufferSize) return 0;
	if (decomp->_samplePos == _length) return 0;

	// Get Frame size
	uint32 frame_bytes  = *(_buffer + decomp->_pos);
	frame_bytes |= (*(_buffer + decomp->_pos + 1)) << 8;

	// Get Num Frame Samples
	uint32 frame_samples  = *(_buffer + decomp->_pos + 2);
	frame_samples |= (*(_buffer + decomp->_pos + 3)) << 8;

	audio_decode(_buffer + decomp->_pos, samples);

	decomp->_pos += frame_bytes;
	decomp->_samplePos += frame_samples;

	return frame_samples;
}

Audio::SeekableAudioStream *SonarcAudioSample::makeStream() const {
	// Init the _sample decompressor
	SonarcDecompData decomp;
	decomp._pos = _srcOffset;
	decomp._samplePos = 0;

	// Get the data for the _sample
	Common::MemoryWriteStreamDynamic streamData(DisposeAfterUse::NO);
	uint8 *framePtr = new uint8[_frameSize * 2];

	uint32 frameSize;
	while ((frameSize = decompressFrame(&decomp, framePtr)) != 0)
		streamData.write(framePtr, frameSize);

	delete[] framePtr;

	// Create the _sample
	return Audio::makeRawStream(
		new Common::MemoryReadStream(streamData.getData(), streamData.size(), DisposeAfterUse::YES),
		getRate(),
		isStereo() ? Audio::FLAG_STEREO | Audio::FLAG_UNSIGNED : Audio::FLAG_UNSIGNED,
		DisposeAfterUse::YES);
}

} // End of namespace Ultima8
} // End of namespace Ultima