/* ScummVM - Scumm Interpreter * Copyright (C) 2001-2003 The ScummVM project * * 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. * * $Header$ * */ #include "audiostream.h" #include "mixer.h" #include "common/engine.h" template static inline int16 readSample(const byte *ptr) { if (is16Bit) { if (isUnsigned) return (int16)(READ_BE_UINT16(ptr) ^ 0x8000); else return (int16)READ_BE_UINT16(ptr); } else { if (isUnsigned) return (int8)(*ptr ^ 0x80) << 8; else return (int8)*ptr << 8; } } #pragma mark - #pragma mark --- LinearMemoryStream --- #pragma mark - template class LinearMemoryStream : public AudioInputStream { protected: const byte *_ptr; const byte *_end; public: LinearMemoryStream(const byte *ptr, uint len) : _ptr(ptr), _end(ptr+len) { if (stereo) // Stereo requires even sized data assert(len % 2 == 0); } int16 read() { assert(_ptr < _end); int16 val = readSample(_ptr); _ptr += (is16Bit ? 2 : 1); return val; } int size() const { return (_end - _ptr) / (is16Bit ? 2 : 1); } bool isStereo() const { return stereo; } }; #pragma mark - #pragma mark --- WrappedMemoryStream --- #pragma mark - // Wrapped memory stream, to be used by the ChannelStream class (and possibly others?) template class WrappedMemoryStream : public WrappedAudioInputStream { protected: byte *_bufferStart; byte *_bufferEnd; byte *_pos; byte *_end; public: WrappedMemoryStream(uint bufferSize); ~WrappedMemoryStream() { free(_bufferStart); } int16 read(); int size() const; bool isStereo() const { return stereo; } void append(const byte *data, uint32 len); }; template WrappedMemoryStream::WrappedMemoryStream(uint bufferSize) { if (stereo) // Stereo requires an even sized buffer assert(bufferSize % 2 == 0); _bufferStart = (byte *)malloc(bufferSize); _pos = _end = _bufferStart; _bufferEnd = _bufferStart + bufferSize; } template int16 WrappedMemoryStream::read() { assert(_pos != _end); int16 val = readSample(_pos); _pos += (is16Bit ? 2 : 1); // Wrap around? if (_pos >= _bufferEnd) _pos = _pos - (_bufferEnd - _bufferStart); return val; } template int WrappedMemoryStream::size() const { int len = _end - _pos; if (len < 0) len += (_bufferEnd - _bufferStart); return len / (is16Bit ? 2 : 1); } template void WrappedMemoryStream::append(const byte *data, uint32 len) { if (_end + len > _bufferEnd) { // Wrap-around case uint32 size_to_end_of_buffer = _bufferEnd - _end; len -= size_to_end_of_buffer; if ((_end < _pos) || (_bufferStart + len >= _pos)) { debug(2, "WrappedMemoryStream: buffer overflow (A)"); return; } memcpy(_end, data, size_to_end_of_buffer); memcpy(_bufferStart, data + size_to_end_of_buffer, len); _end = _bufferStart + len; } else { if ((_end < _pos) && (_end + len >= _pos)) { debug(2, "WrappedMemoryStream: buffer overflow (B)"); return; } memcpy(_end, data, len); _end += len; } } #pragma mark - #pragma mark --- Input stream factories --- #pragma mark - template static AudioInputStream *makeLinearInputStream(const byte *ptr, uint32 len, bool isUnsigned, bool is16Bit) { if (isUnsigned) { if (is16Bit) return new LinearMemoryStream(ptr, len); else return new LinearMemoryStream(ptr, len); } else { if (is16Bit) return new LinearMemoryStream(ptr, len); else return new LinearMemoryStream(ptr, len); } } template static WrappedAudioInputStream *makeWrappedInputStream(uint32 len, bool isUnsigned, bool is16Bit) { if (isUnsigned) { if (is16Bit) return new WrappedMemoryStream(len); else return new WrappedMemoryStream(len); } else { if (is16Bit) return new WrappedMemoryStream(len); else return new WrappedMemoryStream(len); } } AudioInputStream *makeLinearInputStream(byte _flags, const byte *ptr, uint32 len) { if (_flags & SoundMixer::FLAG_STEREO) { return makeLinearInputStream(ptr, len, _flags & SoundMixer::FLAG_UNSIGNED, _flags & SoundMixer::FLAG_16BITS); } else { return makeLinearInputStream(ptr, len, _flags & SoundMixer::FLAG_UNSIGNED, _flags & SoundMixer::FLAG_16BITS); } } WrappedAudioInputStream *makeWrappedInputStream(byte _flags, uint32 len) { if (_flags & SoundMixer::FLAG_STEREO) { return makeWrappedInputStream(len, _flags & SoundMixer::FLAG_UNSIGNED, _flags & SoundMixer::FLAG_16BITS); } else { return makeWrappedInputStream(len, _flags & SoundMixer::FLAG_UNSIGNED, _flags & SoundMixer::FLAG_16BITS); } }