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 "common/stream.h"
24 #include "common/util.h"
25
26 #include "tinsel/adpcm.h"
27
28 namespace Tinsel {
29
30 static const double TinselFilterTable[4][2] = {
31 {0, 0 },
32 {0.9375, 0},
33 {1.796875, -0.8125},
34 {1.53125, -0.859375}
35 };
36
readBufferTinselHeader()37 void Tinsel_ADPCMStream::readBufferTinselHeader() {
38 uint8 start = _stream->readByte();
39 uint8 filterVal = (start & 0xC0) >> 6;
40
41 if ((start & 0x20) != 0) {
42 //Lower 6 bit are negative
43
44 // Negate
45 start = ~(start | 0xC0) + 1;
46
47 _status.predictor = (unsigned long long int)1 << start;
48 } else {
49 // Lower 6 bit are positive
50
51 // Truncate
52 start &= 0x1F;
53
54 _status.predictor = ((double) 1.0) / ((unsigned long long int)1 << start);
55 }
56
57 _status.K0 = TinselFilterTable[filterVal][0];
58 _status.K1 = TinselFilterTable[filterVal][1];
59 }
60
decodeTinsel(int16 code,double eVal)61 int16 Tinsel_ADPCMStream::decodeTinsel(int16 code, double eVal) {
62 double sample;
63
64 sample = (double)code;
65 sample *= eVal * _status.predictor;
66 sample += (_status.d0 * _status.K0) + (_status.d1 * _status.K1);
67
68 _status.d1 = _status.d0;
69 _status.d0 = sample;
70
71 return (int16) CLIP<double>(sample, -32768.0, 32767.0);
72 }
73
readBuffer(int16 * buffer,const int numSamples)74 int Tinsel4_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
75 int samples;
76 uint16 data;
77 const double eVal = 1.142822265;
78
79 samples = 0;
80
81 assert(numSamples % 2 == 0);
82
83 while (samples < numSamples && !_stream->eos() && _stream->pos() < _endpos) {
84 if (_blockPos[0] == _blockAlign) {
85 readBufferTinselHeader();
86 _blockPos[0] = 0;
87 }
88
89 for (; samples < numSamples && _blockPos[0] < _blockAlign && !_stream->eos() && _stream->pos() < _endpos; samples += 2, _blockPos[0]++) {
90 // Read 1 byte = 8 bits = two 4 bit blocks
91 data = _stream->readByte();
92 buffer[samples] = decodeTinsel((data << 8) & 0xF000, eVal);
93 buffer[samples+1] = decodeTinsel((data << 12) & 0xF000, eVal);
94 }
95 }
96
97 return samples;
98 }
99
readBuffer(int16 * buffer,const int numSamples)100 int Tinsel6_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
101 int samples;
102 const double eVal = 1.032226562;
103
104 samples = 0;
105
106 while (samples < numSamples && !_stream->eos() && _stream->pos() < _endpos) {
107 if (_blockPos[0] == _blockAlign) {
108 readBufferTinselHeader();
109 _blockPos[0] = 0;
110 _chunkPos = 0;
111 }
112
113 for (; samples < numSamples && _blockPos[0] < _blockAlign && !_stream->eos() && _stream->pos() < _endpos; samples++, _chunkPos = (_chunkPos + 1) % 4) {
114
115 switch (_chunkPos) {
116 case 0:
117 _chunkData = _stream->readByte();
118 buffer[samples] = decodeTinsel((_chunkData << 8) & 0xFC00, eVal);
119 break;
120 case 1:
121 _chunkData = (_chunkData << 8) | (_stream->readByte());
122 buffer[samples] = decodeTinsel((_chunkData << 6) & 0xFC00, eVal);
123 _blockPos[0]++;
124 break;
125 case 2:
126 _chunkData = (_chunkData << 8) | (_stream->readByte());
127 buffer[samples] = decodeTinsel((_chunkData << 4) & 0xFC00, eVal);
128 _blockPos[0]++;
129 break;
130 case 3:
131 _chunkData = (_chunkData << 8);
132 buffer[samples] = decodeTinsel((_chunkData << 2) & 0xFC00, eVal);
133 _blockPos[0]++;
134 break;
135 }
136
137 }
138
139 }
140
141 return samples;
142 }
143
readBuffer(int16 * buffer,const int numSamples)144 int Tinsel8_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
145 int samples;
146 byte data;
147 const double eVal = 1.007843258;
148
149 samples = 0;
150
151 while (samples < numSamples && !_stream->eos() && _stream->pos() < _endpos) {
152 if (_blockPos[0] == _blockAlign) {
153 readBufferTinselHeader();
154 _blockPos[0] = 0;
155 }
156
157 for (; samples < numSamples && _blockPos[0] < _blockAlign && !_stream->eos() && _stream->pos() < _endpos; samples++, _blockPos[0]++) {
158 // Read 1 byte = 8 bits = one 8 bit block
159 data = _stream->readByte();
160 buffer[samples] = decodeTinsel(data << 8, eVal);
161 }
162 }
163
164 return samples;
165 }
166
167
168 } // End of namespace Tinsel
169