2007-05-30 21:56:52 +00:00
|
|
|
/* 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.
|
2003-11-29 12:11:01 +00:00
|
|
|
*
|
2021-12-26 17:47:58 +00:00
|
|
|
* 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.
|
2014-02-18 01:34:17 +00:00
|
|
|
*
|
2003-11-29 12:11:01 +00:00
|
|
|
* 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.
|
2014-02-18 01:34:17 +00:00
|
|
|
*
|
2003-11-29 12:11:01 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-12-26 17:47:58 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2003-11-29 12:11:01 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-10-30 21:27:42 +00:00
|
|
|
// Disable symbol overrides for FILE and fseek as those are used in the
|
|
|
|
// Vorbis headers.
|
|
|
|
#define FORBIDDEN_SYMBOL_EXCEPTION_FILE
|
|
|
|
#define FORBIDDEN_SYMBOL_EXCEPTION_fseek
|
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/decoders/vorbis.h"
|
2004-01-03 01:25:45 +00:00
|
|
|
|
2005-08-10 12:42:56 +00:00
|
|
|
#ifdef USE_VORBIS
|
2004-01-03 01:25:45 +00:00
|
|
|
|
2011-08-06 14:30:52 +00:00
|
|
|
#include "common/ptr.h"
|
2008-09-05 11:41:39 +00:00
|
|
|
#include "common/stream.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
2003-11-29 12:11:01 +00:00
|
|
|
#include "common/util.h"
|
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/audiostream.h"
|
2004-01-03 01:25:45 +00:00
|
|
|
|
2005-06-30 09:14:36 +00:00
|
|
|
#ifdef USE_TREMOR
|
2011-04-22 19:43:42 +00:00
|
|
|
#ifdef USE_TREMOLO
|
|
|
|
#include <tremolo/ivorbisfile.h>
|
2006-04-07 10:44:20 +00:00
|
|
|
#else
|
2005-06-30 09:14:36 +00:00
|
|
|
#include <tremor/ivorbisfile.h>
|
2006-04-07 10:44:20 +00:00
|
|
|
#endif
|
2005-06-30 09:14:36 +00:00
|
|
|
#else
|
2011-08-06 07:59:02 +00:00
|
|
|
#define OV_EXCLUDE_STATIC_CALLBACKS
|
2004-01-03 01:25:45 +00:00
|
|
|
#include <vorbis/vorbisfile.h>
|
2005-06-30 09:14:36 +00:00
|
|
|
#endif
|
2004-01-03 01:25:45 +00:00
|
|
|
|
|
|
|
|
2006-04-29 22:33:31 +00:00
|
|
|
namespace Audio {
|
|
|
|
|
2007-02-22 10:51:47 +00:00
|
|
|
// These are wrapper functions to allow using a SeekableReadStream object to
|
2003-11-29 12:11:01 +00:00
|
|
|
// provide data to the OggVorbis_File object.
|
|
|
|
|
2007-02-22 10:51:47 +00:00
|
|
|
static size_t read_stream_wrap(void *ptr, size_t size, size_t nmemb, void *datasource) {
|
|
|
|
Common::SeekableReadStream *stream = (Common::SeekableReadStream *)datasource;
|
2005-12-01 19:14:38 +00:00
|
|
|
|
2007-02-22 10:51:47 +00:00
|
|
|
uint32 result = stream->read(ptr, size * nmemb);
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-02-22 10:51:47 +00:00
|
|
|
return result / size;
|
2003-11-29 12:11:01 +00:00
|
|
|
}
|
|
|
|
|
2007-02-22 10:51:47 +00:00
|
|
|
static int seek_stream_wrap(void *datasource, ogg_int64_t offset, int whence) {
|
|
|
|
Common::SeekableReadStream *stream = (Common::SeekableReadStream *)datasource;
|
2009-07-01 20:51:04 +00:00
|
|
|
stream->seek((int32)offset, whence);
|
2007-02-22 10:51:47 +00:00
|
|
|
return stream->pos();
|
2003-11-29 12:11:01 +00:00
|
|
|
}
|
|
|
|
|
2007-02-22 10:51:47 +00:00
|
|
|
static int close_stream_wrap(void *datasource) {
|
2010-02-03 08:59:10 +00:00
|
|
|
// Do nothing -- we leave it up to the VorbisStream to free memory as appropriate.
|
2003-11-29 12:11:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-22 10:51:47 +00:00
|
|
|
static long tell_stream_wrap(void *datasource) {
|
|
|
|
Common::SeekableReadStream *stream = (Common::SeekableReadStream *)datasource;
|
|
|
|
return stream->pos();
|
2003-11-29 12:11:01 +00:00
|
|
|
}
|
|
|
|
|
2007-02-22 10:51:47 +00:00
|
|
|
static ov_callbacks g_stream_wrap = {
|
|
|
|
read_stream_wrap, seek_stream_wrap, close_stream_wrap, tell_stream_wrap
|
2003-11-29 12:11:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-02-22 10:51:47 +00:00
|
|
|
|
2003-12-19 00:32:47 +00:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark --- Ogg Vorbis stream ---
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
2010-02-03 08:59:10 +00:00
|
|
|
class VorbisStream : public SeekableAudioStream {
|
2007-02-22 10:51:47 +00:00
|
|
|
protected:
|
2011-08-06 14:30:52 +00:00
|
|
|
Common::DisposablePtr<Common::SeekableReadStream> _inStream;
|
2007-02-22 10:51:47 +00:00
|
|
|
|
|
|
|
bool _isStereo;
|
|
|
|
int _rate;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2010-01-07 14:22:24 +00:00
|
|
|
Timestamp _length;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2007-02-22 14:30:12 +00:00
|
|
|
OggVorbis_File _ovFile;
|
|
|
|
|
|
|
|
int16 _buffer[4096];
|
|
|
|
const int16 *_bufferEnd;
|
|
|
|
const int16 *_pos;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2003-12-19 00:32:47 +00:00
|
|
|
public:
|
2007-02-22 10:51:47 +00:00
|
|
|
// startTime / duration are in milliseconds
|
2010-02-03 08:59:10 +00:00
|
|
|
VorbisStream(Common::SeekableReadStream *inStream, DisposeAfterUse::Flag dispose);
|
|
|
|
~VorbisStream();
|
2004-06-28 22:35:22 +00:00
|
|
|
|
2021-11-13 20:26:42 +00:00
|
|
|
int readBuffer(int16 *buffer, const int numSamples) override;
|
2003-12-19 00:32:47 +00:00
|
|
|
|
2021-11-13 20:26:42 +00:00
|
|
|
bool endOfData() const override { return _pos >= _bufferEnd; }
|
|
|
|
bool isStereo() const override { return _isStereo; }
|
|
|
|
int getRate() const override { return _rate; }
|
2004-01-17 14:20:32 +00:00
|
|
|
|
2021-11-13 20:26:42 +00:00
|
|
|
bool seek(const Timestamp &where) override;
|
|
|
|
Timestamp getLength() const override { return _length; }
|
2007-02-22 14:30:12 +00:00
|
|
|
protected:
|
2009-09-23 21:14:37 +00:00
|
|
|
bool refill();
|
2003-12-19 00:32:47 +00:00
|
|
|
};
|
|
|
|
|
2010-02-03 08:59:10 +00:00
|
|
|
VorbisStream::VorbisStream(Common::SeekableReadStream *inStream, DisposeAfterUse::Flag dispose) :
|
2011-08-06 14:30:52 +00:00
|
|
|
_inStream(inStream, dispose),
|
2010-01-07 14:22:24 +00:00
|
|
|
_length(0, 1000),
|
2011-08-07 08:19:30 +00:00
|
|
|
_bufferEnd(ARRAYEND(_buffer)) {
|
2003-12-19 00:32:47 +00:00
|
|
|
|
2021-11-13 19:23:55 +00:00
|
|
|
int res = ov_open_callbacks(inStream, &_ovFile, nullptr, 0, g_stream_wrap);
|
2009-09-23 21:14:37 +00:00
|
|
|
if (res < 0) {
|
|
|
|
warning("Could not create Vorbis stream (%d)", res);
|
|
|
|
_pos = _bufferEnd;
|
|
|
|
return;
|
|
|
|
}
|
2003-12-19 00:32:47 +00:00
|
|
|
|
|
|
|
// Read in initial data
|
2009-09-23 21:14:37 +00:00
|
|
|
if (!refill())
|
|
|
|
return;
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2007-02-22 10:51:47 +00:00
|
|
|
// Setup some header information
|
|
|
|
_isStereo = ov_info(&_ovFile, -1)->channels >= 2;
|
|
|
|
_rate = ov_info(&_ovFile, -1)->rate;
|
2010-01-07 23:50:18 +00:00
|
|
|
|
|
|
|
#ifdef USE_TREMOR
|
|
|
|
_length = Timestamp(ov_time_total(&_ovFile, -1), getRate());
|
|
|
|
#else
|
|
|
|
_length = Timestamp(uint32(ov_time_total(&_ovFile, -1) * 1000.0), getRate());
|
|
|
|
#endif
|
2003-12-19 00:32:47 +00:00
|
|
|
}
|
|
|
|
|
2010-02-03 08:59:10 +00:00
|
|
|
VorbisStream::~VorbisStream() {
|
2007-02-22 10:51:47 +00:00
|
|
|
ov_clear(&_ovFile);
|
2004-06-28 22:35:22 +00:00
|
|
|
}
|
|
|
|
|
2010-02-03 08:59:10 +00:00
|
|
|
int VorbisStream::readBuffer(int16 *buffer, const int numSamples) {
|
2010-01-07 14:22:24 +00:00
|
|
|
int samples = 0;
|
2007-02-22 14:30:12 +00:00
|
|
|
while (samples < numSamples && _pos < _bufferEnd) {
|
2004-01-18 20:57:45 +00:00
|
|
|
const int len = MIN(numSamples - samples, (int)(_bufferEnd - _pos));
|
2003-12-19 00:32:47 +00:00
|
|
|
memcpy(buffer, _pos, len * 2);
|
|
|
|
buffer += len;
|
|
|
|
_pos += len;
|
|
|
|
samples += len;
|
|
|
|
if (_pos >= _bufferEnd) {
|
2009-09-23 21:14:37 +00:00
|
|
|
if (!refill())
|
|
|
|
break;
|
2003-12-19 00:32:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return samples;
|
|
|
|
}
|
|
|
|
|
2010-02-03 08:59:10 +00:00
|
|
|
bool VorbisStream::seek(const Timestamp &where) {
|
2010-01-28 09:44:21 +00:00
|
|
|
// Vorbisfile uses the sample pair number, thus we always use "false" for the isStereo parameter
|
|
|
|
// of the convertTimeToStreamPos helper.
|
|
|
|
int res = ov_pcm_seek(&_ovFile, convertTimeToStreamPos(where, getRate(), false).totalNumberOfFrames());
|
2010-01-06 16:11:21 +00:00
|
|
|
if (res) {
|
2010-01-05 02:27:24 +00:00
|
|
|
warning("Error seeking in Vorbis stream (%d)", res);
|
|
|
|
_pos = _bufferEnd;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return refill();
|
|
|
|
}
|
|
|
|
|
2010-02-03 08:59:10 +00:00
|
|
|
bool VorbisStream::refill() {
|
2003-12-19 00:32:47 +00:00
|
|
|
// Read the samples
|
|
|
|
uint len_left = sizeof(_buffer);
|
|
|
|
char *read_pos = (char *)_buffer;
|
|
|
|
|
2007-02-22 14:30:12 +00:00
|
|
|
while (len_left > 0) {
|
2007-02-22 10:51:47 +00:00
|
|
|
long result;
|
2010-01-07 14:22:24 +00:00
|
|
|
|
2007-02-22 10:51:47 +00:00
|
|
|
#ifdef USE_TREMOR
|
|
|
|
// Tremor ov_read() always returns data as signed 16 bit interleaved PCM
|
2008-01-27 19:47:41 +00:00
|
|
|
// in host byte order. As such, it does not take arguments to request
|
2007-02-22 10:51:47 +00:00
|
|
|
// specific signedness, byte order or bit depth as in Vorbisfile.
|
|
|
|
result = ov_read(&_ovFile, read_pos, len_left,
|
|
|
|
NULL);
|
|
|
|
#else
|
2003-12-19 00:32:47 +00:00
|
|
|
#ifdef SCUMM_BIG_ENDIAN
|
2007-02-22 10:51:47 +00:00
|
|
|
result = ov_read(&_ovFile, read_pos, len_left,
|
2003-12-19 00:32:47 +00:00
|
|
|
1,
|
2007-02-22 10:51:47 +00:00
|
|
|
2, // 16 bit
|
|
|
|
1, // signed
|
|
|
|
NULL);
|
2003-12-19 00:32:47 +00:00
|
|
|
#else
|
2007-02-22 10:51:47 +00:00
|
|
|
result = ov_read(&_ovFile, read_pos, len_left,
|
2003-12-19 00:32:47 +00:00
|
|
|
0,
|
|
|
|
2, // 16 bit
|
|
|
|
1, // signed
|
2021-11-13 19:23:55 +00:00
|
|
|
nullptr);
|
2007-02-22 10:51:47 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2003-12-19 00:32:47 +00:00
|
|
|
if (result == OV_HOLE) {
|
|
|
|
// Possibly recoverable, just warn about it
|
|
|
|
warning("Corrupted data in Vorbis file");
|
2009-09-23 21:14:37 +00:00
|
|
|
} else if (result == 0) {
|
2010-01-07 14:22:24 +00:00
|
|
|
//warning("End of file while reading from Vorbis file");
|
|
|
|
//_pos = _bufferEnd;
|
|
|
|
//return false;
|
|
|
|
break;
|
2009-09-23 21:14:37 +00:00
|
|
|
} else if (result < 0) {
|
|
|
|
warning("Error reading from Vorbis stream (%d)", int(result));
|
|
|
|
_pos = _bufferEnd;
|
2003-12-19 00:32:47 +00:00
|
|
|
// Don't delete it yet, that causes problems in
|
|
|
|
// the CD player emulation code.
|
2009-09-23 21:14:37 +00:00
|
|
|
return false;
|
2003-12-19 00:32:47 +00:00
|
|
|
} else {
|
|
|
|
len_left -= result;
|
|
|
|
read_pos += result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_pos = _buffer;
|
|
|
|
_bufferEnd = (int16 *)read_pos;
|
2009-09-23 21:14:37 +00:00
|
|
|
|
|
|
|
return true;
|
2003-12-19 00:32:47 +00:00
|
|
|
}
|
|
|
|
|
2007-02-22 18:38:05 +00:00
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark --- Ogg Vorbis factory functions ---
|
|
|
|
#pragma mark -
|
|
|
|
|
2010-01-05 23:59:28 +00:00
|
|
|
SeekableAudioStream *makeVorbisStream(
|
|
|
|
Common::SeekableReadStream *stream,
|
2010-01-16 21:36:08 +00:00
|
|
|
DisposeAfterUse::Flag disposeAfterUse) {
|
2010-02-03 08:59:10 +00:00
|
|
|
SeekableAudioStream *s = new VorbisStream(stream, disposeAfterUse);
|
2010-02-03 08:58:52 +00:00
|
|
|
if (s && s->endOfData()) {
|
|
|
|
delete s;
|
2021-11-13 19:23:55 +00:00
|
|
|
return nullptr;
|
2010-02-03 08:58:52 +00:00
|
|
|
} else {
|
|
|
|
return s;
|
|
|
|
}
|
2010-01-05 23:59:28 +00:00
|
|
|
}
|
2007-02-22 14:30:12 +00:00
|
|
|
|
2006-04-29 22:33:31 +00:00
|
|
|
} // End of namespace Audio
|
|
|
|
|
|
|
|
#endif // #ifdef USE_VORBIS
|