1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "bladerunner/aud_stream.h"
24 
25 #include "bladerunner/audio_cache.h"
26 
27 #include "common/util.h"
28 
29 namespace BladeRunner {
30 
AudStream(byte * data,int overrideFrequency)31 AudStream::AudStream(byte *data, int overrideFrequency) {
32 	_hash  = 0;
33 	_cache = nullptr;
34 	_overrideFrequency = overrideFrequency;
35 
36 	init(data);
37 }
38 
AudStream(AudioCache * cache,int32 hash,int overrideFrequency)39 AudStream::AudStream(AudioCache *cache, int32 hash, int overrideFrequency) {
40 	assert(cache != nullptr);
41 
42 	_cache = cache;
43 	_hash  = hash;
44 	_overrideFrequency = overrideFrequency;
45 
46 	_cache->incRef(_hash);
47 
48 	init(_cache->findByHash(_hash));
49 }
50 
init(byte * data)51 void AudStream::init(byte *data) {
52 	_data = data;
53 	_frequency = READ_LE_UINT16(_data);
54 	_size = READ_LE_UINT32(_data + 2);
55 	_sizeDecompressed = READ_LE_UINT32(_data + 6);
56 	_flags = *(_data + 10);
57 	_compressionType = *(_data + 11);
58 
59 	_end = _data + _size + 12;
60 	assert(_end - _data >= 12);
61 
62 	_deafBlockRemain = 0;
63 	_p = _data + 12;
64 }
65 
~AudStream()66 AudStream::~AudStream() {
67 	if (_cache) {
68 		_cache->decRef(_hash);
69 	}
70 }
71 
readBuffer(int16 * buffer,const int numSamples)72 int AudStream::readBuffer(int16 *buffer, const int numSamples) {
73 	int samplesRead = 0;
74 
75 	if (_compressionType == 99) {
76 		assert(numSamples % 2 == 0);
77 
78 		while (samplesRead < numSamples) {
79 			if (_deafBlockRemain == 0) {
80 				if (_end - _p == 0)
81 					break;
82 
83 				assert(_end - _p >= 6);
84 
85 				uint16 blockSize     = READ_LE_UINT16(_p);
86 				uint16 blockOutSize  = READ_LE_UINT16(_p + 2);
87 				uint32 sig           = READ_LE_UINT32(_p + 4);
88 				_p += 8;
89 
90 				assert(sig == 0xdeaf);
91 				assert(_end - _p >= blockSize);
92 
93 				// TODO: Previously we asserted that
94 				// blockOutSize == 4 * blockSize, but
95 				// occasionally, at the end of an AUD,
96 				// we see blockOutSize == 4 * blockSize + 2
97 				// Investigate how BLADE.EXE handles this.
98 
99 				assert(blockOutSize / 4 == blockSize);
100 
101 				_deafBlockRemain = blockSize;
102 			}
103 
104 			assert(_end - _p >= _deafBlockRemain);
105 
106 			int bytesConsumed = MIN<int>(_deafBlockRemain, (numSamples - samplesRead) / 2);
107 			if (buffer) {
108 				_decoder.decode(_p, bytesConsumed, buffer + samplesRead, false);
109 			} else {
110 				_decoder.decode(_p, bytesConsumed, nullptr, false);
111 			}
112 			_p += bytesConsumed;
113 			_deafBlockRemain -= bytesConsumed;
114 
115 			samplesRead += 2 * bytesConsumed;
116 		}
117 	} else {
118 		samplesRead = MIN(numSamples, (int)(_end - _p) / 2);
119 		if (buffer) {
120 			for (int i = 0; i < samplesRead; ++i, _p += 2) {
121 				buffer[i] = READ_LE_UINT16(_p);
122 			}
123 		}
124 	}
125 
126 	return samplesRead;
127 }
128 
getBytesPerSecond() const129 int AudStream::getBytesPerSecond() const {
130 	int bytesPerSecond = _overrideFrequency > 0 ? _overrideFrequency : _frequency;
131 	if (_flags & 1) { // 16 bit
132 		bytesPerSecond *= 2;
133 	}
134 	if (_flags & 2) { // stereo
135 		bytesPerSecond *= 2;
136 	}
137 	return bytesPerSecond;
138 }
139 
startAtSecond(uint32 startSecond)140 bool AudStream::startAtSecond(uint32 startSecond) {
141 	uint32 audStreamLengthMillis = getLength();
142 	if (startSecond == 0 || startSecond * 1000 > audStreamLengthMillis || audStreamLengthMillis == 0) {
143 		return false;
144 	}
145 	if (rewind()) {
146 		int samplesPerSecond = _overrideFrequency > 0 ? _overrideFrequency : _frequency;
147 		readBuffer(nullptr, startSecond * samplesPerSecond);
148 		return true;
149 	}
150 	return false;
151 }
152 
rewind()153 bool AudStream::rewind() {
154 	_p = _data + 12;
155 	_decoder.setParameters(0, 0);
156 	return true;
157 }
158 
159 /**
160 * Returns audio length in milliseconds
161 */
getLength() const162 uint32 AudStream::getLength() const {
163 	// since everything is 44100, we easily get overflows with ints
164 	// thus we must use doubles
165 	int bytesPerSecond = getBytesPerSecond();
166 	if (bytesPerSecond <= 0) {
167 		return 0u;
168 	}
169 	double res = (double)_sizeDecompressed * 1000.0 / (double)bytesPerSecond;
170 	return (uint32)res;
171 }
172 
173 } // End of namespace BladeRunner
174