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 "scumm/cdda.h"
24 #include "common/stream.h"
25 #include "audio/audiostream.h"
26 
27 namespace Scumm {
28 
29 
30 #pragma mark -
31 #pragma mark --- CDDA stream ---
32 #pragma mark -
33 
34 #define START_OF_CDDA_DATA 800
35 #define BLOCK_SIZE 1177
36 
37 class CDDAStream : public Audio::SeekableAudioStream {
38 private:
39 	Common::SeekableReadStream *_stream;
40 	DisposeAfterUse::Flag _disposeAfterUse;
41 	byte _shiftLeft;
42 	byte _shiftRight;
43 	uint32 _pos;
44 	Audio::Timestamp _length;
45 
46 public:
47 	CDDAStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse);
48 	~CDDAStream() override;
49 
50 	int readBuffer(int16 *buffer, const int numSamples) override;
isStereo() const51 	bool isStereo() const override { return true; }
getRate() const52 	int getRate() const override { return 44100; }
endOfData() const53 	bool endOfData() const override { return _stream->eos(); }
54 	bool seek(const Audio::Timestamp &where) override;
getLength() const55 	Audio::Timestamp getLength() const override { return _length; }
56 };
57 
CDDAStream(Common::SeekableReadStream * stream,DisposeAfterUse::Flag disposeAfterUse)58 CDDAStream::CDDAStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse) :
59 	_stream(stream), _disposeAfterUse(disposeAfterUse), _pos(START_OF_CDDA_DATA) {
60 	_stream->seek(START_OF_CDDA_DATA, SEEK_SET);
61 	// The total size of CDDA.SOU is 289,808,802 bytes or (289808802 - 800) / 1177 = 246226 blocks
62 	// We also deduct the shift values to return the correct length
63 	uint32 blocks = (_stream->size() - START_OF_CDDA_DATA) / BLOCK_SIZE;
64 	_length = Audio::Timestamp(0, (_stream->size() - START_OF_CDDA_DATA - blocks) / (isStereo() ? 2 : 1), getRate());
65 }
66 
~CDDAStream()67 CDDAStream::~CDDAStream() {
68 	if (_disposeAfterUse == DisposeAfterUse::YES)
69 		delete _stream;
70 }
71 
seek(const Audio::Timestamp & where)72 bool CDDAStream::seek(const Audio::Timestamp &where) {
73 	const uint32 seekSample = convertTimeToStreamPos(where, getRate(), isStereo()).totalNumberOfFrames();
74 	uint32 blocks = seekSample / 1176;
75 
76 	// Before seeking, read the shift values from the beginning of that block
77 	_stream->seek(START_OF_CDDA_DATA + blocks * BLOCK_SIZE, SEEK_SET);
78 	byte shiftVal = _stream->readByte();
79 	_shiftLeft = shiftVal >> 4;
80 	_shiftRight = shiftVal & 0x0F;
81 
82 	_pos = START_OF_CDDA_DATA + blocks + seekSample;
83 	return _stream->seek(_pos, SEEK_SET);
84 }
85 
readBuffer(int16 * buffer,const int numSamples)86 int CDDAStream::readBuffer(int16 *buffer, const int numSamples) {
87 	int samples;
88 
89 	for (samples = 0 ; samples < numSamples && !_stream->eos() ; ) {
90 		if (!((_pos - START_OF_CDDA_DATA) % BLOCK_SIZE)) {
91 			byte shiftVal = _stream->readByte();
92 			_shiftLeft = shiftVal >> 4;
93 			_shiftRight = shiftVal & 0x0F;
94 			_pos++;
95 		}
96 		buffer[samples++] = _stream->readSByte() << _shiftLeft;
97 		buffer[samples++] = _stream->readSByte() << _shiftRight;
98 		_pos += 2;
99 	}
100 	return samples;
101 }
102 
103 #pragma mark -
104 #pragma mark --- CDDA factory functions ---
105 #pragma mark -
106 
makeCDDAStream(Common::SeekableReadStream * stream,DisposeAfterUse::Flag disposeAfterUse)107 Audio::SeekableAudioStream *makeCDDAStream(
108 	Common::SeekableReadStream *stream,
109 	DisposeAfterUse::Flag disposeAfterUse) {
110 	Audio::SeekableAudioStream *s = new CDDAStream(stream, disposeAfterUse);
111 	if (s && s->endOfData()) {
112 		delete s;
113 		return 0;
114 	} else {
115 		return s;
116 	}
117 	return 0;
118 }
119 
120 } // End of namespace Scumm
121