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.
|
2002-04-21 17:46:42 +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 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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-04-21 17:46:42 +00:00
|
|
|
*
|
2006-02-11 10:05:31 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2002-04-21 17:46:42 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-07-05 16:01:55 +00:00
|
|
|
#include "common/util.h"
|
2005-01-10 22:06:49 +00:00
|
|
|
#include "common/system.h"
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
#include "sound/mixer_intern.h"
|
2003-08-05 21:46:56 +00:00
|
|
|
#include "sound/rate.h"
|
2003-08-06 17:13:04 +00:00
|
|
|
#include "sound/audiostream.h"
|
2003-07-28 20:42:54 +00:00
|
|
|
|
2003-08-05 23:03:42 +00:00
|
|
|
|
2005-05-10 23:48:48 +00:00
|
|
|
namespace Audio {
|
|
|
|
|
2003-08-05 23:03:42 +00:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark --- Channel classes ---
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Channels used by the sound mixer.
|
|
|
|
*/
|
2003-06-22 01:55:53 +00:00
|
|
|
class Channel {
|
2004-12-27 00:27:00 +00:00
|
|
|
public:
|
2005-05-10 23:48:48 +00:00
|
|
|
const Mixer::SoundType _type;
|
2005-03-12 20:26:49 +00:00
|
|
|
SoundHandle _handle;
|
2003-12-17 01:32:00 +00:00
|
|
|
private:
|
2005-05-10 23:48:48 +00:00
|
|
|
Mixer *_mixer;
|
2003-12-26 02:19:31 +00:00
|
|
|
bool _autofreeStream;
|
2004-11-28 22:15:09 +00:00
|
|
|
bool _permanent;
|
2003-08-31 20:26:21 +00:00
|
|
|
byte _volume;
|
2004-01-29 18:15:27 +00:00
|
|
|
int8 _balance;
|
2007-06-30 21:02:58 +00:00
|
|
|
int _pauseLevel;
|
2003-12-22 19:08:19 +00:00
|
|
|
int _id;
|
2004-02-12 16:25:28 +00:00
|
|
|
uint32 _samplesConsumed;
|
|
|
|
uint32 _samplesDecoded;
|
|
|
|
uint32 _mixerTimeStamp;
|
2008-11-09 13:28:59 +00:00
|
|
|
uint32 _pauseStartTime;
|
|
|
|
uint32 _pauseTime;
|
2003-08-31 20:26:21 +00:00
|
|
|
|
2003-12-17 01:32:00 +00:00
|
|
|
protected:
|
|
|
|
RateConverter *_converter;
|
2004-01-03 14:10:13 +00:00
|
|
|
AudioStream *_input;
|
2003-12-17 01:32:00 +00:00
|
|
|
|
2003-06-22 01:55:53 +00:00
|
|
|
public:
|
2005-05-10 23:48:48 +00:00
|
|
|
Channel(Mixer *mixer, Mixer::SoundType type, AudioStream *input, bool autofreeStream, bool reverseStereo = false, int id = -1, bool permanent = false);
|
2003-12-22 19:08:19 +00:00
|
|
|
virtual ~Channel();
|
2003-12-17 02:19:24 +00:00
|
|
|
|
2003-12-22 19:08:19 +00:00
|
|
|
void mix(int16 *data, uint len);
|
2003-12-17 02:19:24 +00:00
|
|
|
|
2004-11-28 22:15:09 +00:00
|
|
|
bool isPermanent() const {
|
|
|
|
return _permanent;
|
|
|
|
}
|
2003-12-22 19:08:19 +00:00
|
|
|
bool isFinished() const {
|
|
|
|
return _input->endOfStream();
|
2003-12-17 02:19:24 +00:00
|
|
|
}
|
2003-12-16 15:34:17 +00:00
|
|
|
void pause(bool paused) {
|
2007-07-22 11:54:35 +00:00
|
|
|
//assert((paused && _pauseLevel >= 0) || (!paused && _pauseLevel));
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-06-30 21:02:58 +00:00
|
|
|
if (paused)
|
|
|
|
_pauseLevel++;
|
2007-07-22 11:54:35 +00:00
|
|
|
else if (_pauseLevel > 0)
|
2007-06-30 21:02:58 +00:00
|
|
|
_pauseLevel--;
|
2008-11-09 13:28:59 +00:00
|
|
|
|
|
|
|
if (_pauseLevel > 0)
|
|
|
|
_pauseStartTime = g_system->getMillis();
|
|
|
|
else
|
|
|
|
_pauseTime += (g_system->getMillis() - _pauseStartTime);
|
2003-09-02 13:48:20 +00:00
|
|
|
}
|
2003-12-16 15:34:17 +00:00
|
|
|
bool isPaused() {
|
2007-06-30 21:02:58 +00:00
|
|
|
return _pauseLevel != 0;
|
2003-09-02 13:48:20 +00:00
|
|
|
}
|
2004-01-29 18:15:27 +00:00
|
|
|
void setVolume(const byte volume) {
|
2003-08-31 20:26:21 +00:00
|
|
|
_volume = volume;
|
|
|
|
}
|
2004-01-29 18:15:27 +00:00
|
|
|
void setBalance(const int8 balance) {
|
|
|
|
_balance = balance;
|
2003-08-05 23:03:42 +00:00
|
|
|
}
|
2003-12-22 19:08:19 +00:00
|
|
|
int getId() const {
|
|
|
|
return _id;
|
|
|
|
}
|
2004-02-12 16:25:28 +00:00
|
|
|
uint32 getElapsedTime();
|
2003-08-05 02:05:37 +00:00
|
|
|
};
|
|
|
|
|
2004-10-11 22:01:21 +00:00
|
|
|
|
2003-08-05 23:03:42 +00:00
|
|
|
#pragma mark -
|
2005-05-10 23:48:48 +00:00
|
|
|
#pragma mark --- Mixer ---
|
2003-08-05 23:03:42 +00:00
|
|
|
#pragma mark -
|
2003-06-15 01:56:47 +00:00
|
|
|
|
2003-08-05 18:17:26 +00:00
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
MixerImpl::MixerImpl(OSystem *system)
|
|
|
|
: _syst(system), _sampleRate(0), _mixerReady(false), _handleSeed(0) {
|
2003-06-22 13:48:47 +00:00
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
int i;
|
2003-06-22 13:48:47 +00:00
|
|
|
|
2004-12-27 00:27:00 +00:00
|
|
|
for (i = 0; i < ARRAYSIZE(_volumeForSoundType); i++)
|
2004-12-27 23:33:19 +00:00
|
|
|
_volumeForSoundType[i] = kMaxMixerVolume;
|
2003-06-22 13:48:47 +00:00
|
|
|
|
2003-06-22 14:30:32 +00:00
|
|
|
for (i = 0; i != NUM_CHANNELS; i++)
|
2003-12-22 19:08:19 +00:00
|
|
|
_channels[i] = 0;
|
2002-08-18 21:42:22 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
MixerImpl::~MixerImpl() {
|
2007-02-20 18:50:17 +00:00
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++)
|
|
|
|
delete _channels[i];
|
2002-08-18 21:42:22 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
void MixerImpl::setReady(bool ready) {
|
|
|
|
_mixerReady = ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint MixerImpl::getOutputRate() const {
|
|
|
|
return _sampleRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MixerImpl::setOutputRate(uint sampleRate) {
|
|
|
|
if (_sampleRate != 0 && _sampleRate != sampleRate)
|
|
|
|
error("Changing the Audio::Mixer output sample rate is not supported");
|
|
|
|
_sampleRate = sampleRate;
|
2006-10-22 15:42:29 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
void MixerImpl::insertChannel(SoundHandle *handle, Channel *chan) {
|
2003-12-22 19:08:19 +00:00
|
|
|
|
2003-06-22 01:34:28 +00:00
|
|
|
int index = -1;
|
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++) {
|
2003-12-22 19:08:19 +00:00
|
|
|
if (_channels[i] == 0) {
|
2003-06-22 01:34:28 +00:00
|
|
|
index = i;
|
|
|
|
break;
|
2002-08-24 15:31:37 +00:00
|
|
|
}
|
|
|
|
}
|
2005-05-08 21:49:52 +00:00
|
|
|
if (index == -1) {
|
2008-06-28 15:28:29 +00:00
|
|
|
warning("MixerImpl::out of mixer slots");
|
2003-06-22 01:34:28 +00:00
|
|
|
delete chan;
|
2003-12-21 00:44:31 +00:00
|
|
|
return;
|
2002-06-02 20:30:21 +00:00
|
|
|
}
|
2003-06-22 01:34:28 +00:00
|
|
|
|
2002-06-02 20:30:21 +00:00
|
|
|
_channels[index] = chan;
|
2005-03-22 18:29:02 +00:00
|
|
|
chan->_handle._val = index + (_handleSeed * NUM_CHANNELS);
|
2005-03-12 20:26:49 +00:00
|
|
|
_handleSeed++;
|
|
|
|
if (handle) {
|
|
|
|
*handle = chan->_handle;
|
|
|
|
}
|
2002-05-12 16:53:13 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
void MixerImpl::playRaw(
|
2007-02-03 19:05:53 +00:00
|
|
|
SoundType type,
|
|
|
|
SoundHandle *handle,
|
|
|
|
void *sound,
|
|
|
|
uint32 size, uint rate, byte flags,
|
|
|
|
int id, byte volume, int8 balance,
|
|
|
|
uint32 loopStart, uint32 loopEnd) {
|
2003-12-21 00:44:31 +00:00
|
|
|
|
|
|
|
// Create the input stream
|
2007-03-02 15:26:22 +00:00
|
|
|
AudioStream *input = makeLinearInputStream((byte *)sound, size, rate, flags, loopStart, loopEnd);
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2007-03-02 14:49:07 +00:00
|
|
|
// Play it
|
2007-03-10 15:04:06 +00:00
|
|
|
playInputStream(type, handle, input, id, volume, balance, true, false, ((flags & Mixer::FLAG_REVERSE_STEREO) != 0));
|
2002-04-14 18:13:08 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
void MixerImpl::playInputStream(
|
2007-02-03 19:05:53 +00:00
|
|
|
SoundType type,
|
|
|
|
SoundHandle *handle,
|
|
|
|
AudioStream *input,
|
|
|
|
int id, byte volume, int8 balance,
|
|
|
|
bool autofreeStream,
|
2007-03-02 14:49:07 +00:00
|
|
|
bool permanent,
|
|
|
|
bool reverseStereo) {
|
2003-12-19 00:32:47 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
|
2003-12-23 19:14:57 +00:00
|
|
|
if (input == 0) {
|
|
|
|
warning("input stream is 0");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent duplicate sounds
|
|
|
|
if (id != -1) {
|
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++)
|
|
|
|
if (_channels[i] != 0 && _channels[i]->getId() == id) {
|
2003-12-26 02:19:31 +00:00
|
|
|
if (autofreeStream)
|
|
|
|
delete input;
|
2003-12-23 19:14:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-19 00:32:47 +00:00
|
|
|
// Create the channel
|
2007-03-02 14:49:07 +00:00
|
|
|
Channel *chan = new Channel(this, type, input, autofreeStream, reverseStereo, id, permanent);
|
2004-01-29 18:15:27 +00:00
|
|
|
chan->setVolume(volume);
|
|
|
|
chan->setBalance(balance);
|
2003-12-21 00:44:31 +00:00
|
|
|
insertChannel(handle, chan);
|
2003-12-19 00:32:47 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
void MixerImpl::mixCallback(byte *samples, uint len) {
|
|
|
|
assert(samples);
|
|
|
|
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2008-12-22 11:22:15 +00:00
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
int16 *buf = (int16 *)samples;
|
|
|
|
len >>= 2;
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2006-10-22 15:42:29 +00:00
|
|
|
// Since the mixer callback has been called, the mixer must be ready...
|
|
|
|
_mixerReady = true;
|
2003-07-14 20:09:14 +00:00
|
|
|
|
2003-12-19 00:32:47 +00:00
|
|
|
// zero the buf
|
|
|
|
memset(buf, 0, 2 * len * sizeof(int16));
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2007-02-20 18:50:17 +00:00
|
|
|
// mix all channels
|
2007-02-03 19:05:53 +00:00
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++)
|
|
|
|
if (_channels[i]) {
|
|
|
|
if (_channels[i]->isFinished()) {
|
|
|
|
delete _channels[i];
|
|
|
|
_channels[i] = 0;
|
|
|
|
} else if (!_channels[i]->isPaused())
|
|
|
|
_channels[i]->mix(buf, len);
|
|
|
|
}
|
2002-04-14 18:13:08 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
void MixerImpl::stopAll() {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2007-02-15 12:47:09 +00:00
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++) {
|
2007-02-20 18:50:17 +00:00
|
|
|
if (_channels[i] != 0 && !_channels[i]->isPermanent()) {
|
|
|
|
delete _channels[i];
|
|
|
|
_channels[i] = 0;
|
2003-12-22 19:08:19 +00:00
|
|
|
}
|
2007-02-15 12:47:09 +00:00
|
|
|
}
|
2002-04-14 18:13:08 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
void MixerImpl::stopID(int id) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-06-21 23:29:34 +00:00
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++) {
|
2003-12-22 19:08:19 +00:00
|
|
|
if (_channels[i] != 0 && _channels[i]->getId() == id) {
|
|
|
|
delete _channels[i];
|
|
|
|
_channels[i] = 0;
|
2003-03-18 21:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
void MixerImpl::stopHandle(SoundHandle handle) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2003-07-14 20:09:14 +00:00
|
|
|
// Simply ignore stop requests for handles of sounds that already terminated
|
2005-03-22 18:29:02 +00:00
|
|
|
const int index = handle._val % NUM_CHANNELS;
|
|
|
|
if (!_channels[index] || _channels[index]->_handle._val != handle._val)
|
2003-07-14 20:09:14 +00:00
|
|
|
return;
|
|
|
|
|
2005-03-12 20:26:49 +00:00
|
|
|
delete _channels[index];
|
|
|
|
_channels[index] = 0;
|
2003-07-14 20:09:14 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
void MixerImpl::setChannelVolume(SoundHandle handle, byte volume) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2005-03-22 18:29:02 +00:00
|
|
|
const int index = handle._val % NUM_CHANNELS;
|
|
|
|
if (!_channels[index] || _channels[index]->_handle._val != handle._val)
|
2003-08-31 20:26:21 +00:00
|
|
|
return;
|
|
|
|
|
2005-03-12 20:26:49 +00:00
|
|
|
_channels[index]->setVolume(volume);
|
2003-08-31 20:26:21 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
void MixerImpl::setChannelBalance(SoundHandle handle, int8 balance) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2005-03-22 18:29:02 +00:00
|
|
|
const int index = handle._val % NUM_CHANNELS;
|
|
|
|
if (!_channels[index] || _channels[index]->_handle._val != handle._val)
|
2003-08-31 20:26:21 +00:00
|
|
|
return;
|
|
|
|
|
2005-03-12 20:26:49 +00:00
|
|
|
_channels[index]->setBalance(balance);
|
2003-08-31 20:26:21 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
uint32 MixerImpl::getSoundElapsedTime(SoundHandle handle) {
|
2004-02-12 16:25:28 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
|
2005-03-22 18:29:02 +00:00
|
|
|
const int index = handle._val % NUM_CHANNELS;
|
|
|
|
if (!_channels[index] || _channels[index]->_handle._val != handle._val)
|
2004-02-12 16:25:28 +00:00
|
|
|
return 0;
|
|
|
|
|
2005-03-12 20:26:49 +00:00
|
|
|
return _channels[index]->getElapsedTime();
|
2004-02-12 16:25:28 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
void MixerImpl::pauseAll(bool paused) {
|
2007-02-03 19:05:53 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++) {
|
|
|
|
if (_channels[i] != 0) {
|
|
|
|
_channels[i]->pause(paused);
|
|
|
|
}
|
|
|
|
}
|
2003-08-09 19:19:27 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
void MixerImpl::pauseID(int id, bool paused) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-02 13:48:20 +00:00
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++) {
|
2003-12-22 19:08:19 +00:00
|
|
|
if (_channels[i] != 0 && _channels[i]->getId() == id) {
|
2003-09-02 13:48:20 +00:00
|
|
|
_channels[i]->pause(paused);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
void MixerImpl::pauseHandle(SoundHandle handle, bool paused) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-02 13:48:20 +00:00
|
|
|
|
2007-02-03 19:05:53 +00:00
|
|
|
// Simply ignore (un)pause requests for sounds that already terminated
|
2005-03-22 18:29:02 +00:00
|
|
|
const int index = handle._val % NUM_CHANNELS;
|
|
|
|
if (!_channels[index] || _channels[index]->_handle._val != handle._val)
|
2003-09-02 13:48:20 +00:00
|
|
|
return;
|
|
|
|
|
2005-03-12 20:26:49 +00:00
|
|
|
_channels[index]->pause(paused);
|
2003-09-02 13:48:20 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
bool MixerImpl::isSoundIDActive(int id) {
|
2004-09-19 13:54:24 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2004-09-19 12:22:47 +00:00
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++)
|
|
|
|
if (_channels[i] && _channels[i]->getId() == id)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
int MixerImpl::getSoundID(SoundHandle handle) {
|
2005-04-04 11:43:25 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
const int index = handle._val % NUM_CHANNELS;
|
2005-04-04 17:43:00 +00:00
|
|
|
if (_channels[index] && _channels[index]->_handle._val == handle._val)
|
2005-04-04 11:43:25 +00:00
|
|
|
return _channels[index]->getId();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
bool MixerImpl::isSoundHandleActive(SoundHandle handle) {
|
2005-03-23 16:41:44 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2005-03-22 18:29:02 +00:00
|
|
|
const int index = handle._val % NUM_CHANNELS;
|
|
|
|
return _channels[index] && _channels[index]->_handle._val == handle._val;
|
2005-03-12 20:26:49 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
bool MixerImpl::hasActiveChannelOfType(SoundType type) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-06-21 23:29:34 +00:00
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++)
|
2004-12-28 11:19:36 +00:00
|
|
|
if (_channels[i] && _channels[i]->_type == type)
|
2002-04-14 18:13:08 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
void MixerImpl::setVolumeForSoundType(SoundType type, int volume) {
|
2004-12-27 00:27:00 +00:00
|
|
|
assert(0 <= type && type < ARRAYSIZE(_volumeForSoundType));
|
|
|
|
|
2002-07-28 15:03:45 +00:00
|
|
|
// Check range
|
2004-12-27 23:33:19 +00:00
|
|
|
if (volume > kMaxMixerVolume)
|
|
|
|
volume = kMaxMixerVolume;
|
2002-07-28 15:03:45 +00:00
|
|
|
else if (volume < 0)
|
|
|
|
volume = 0;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-28 23:02:28 +00:00
|
|
|
// TODO: Maybe we should do logarithmic (not linear) volume
|
|
|
|
// scaling? See also Player_V2::setMasterVolume
|
2002-07-28 15:03:45 +00:00
|
|
|
|
2004-12-27 00:27:00 +00:00
|
|
|
_volumeForSoundType[type] = volume;
|
2002-04-14 18:13:08 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
int MixerImpl::getVolumeForSoundType(SoundType type) const {
|
2004-12-27 00:27:00 +00:00
|
|
|
assert(0 <= type && type < ARRAYSIZE(_volumeForSoundType));
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-12-27 00:27:00 +00:00
|
|
|
return _volumeForSoundType[type];
|
2002-07-21 06:55:33 +00:00
|
|
|
}
|
|
|
|
|
2003-08-05 02:05:37 +00:00
|
|
|
|
2003-08-05 23:03:42 +00:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark --- Channel implementations ---
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
2005-05-10 23:48:48 +00:00
|
|
|
Channel::Channel(Mixer *mixer, Mixer::SoundType type, AudioStream *input,
|
2004-12-27 00:27:00 +00:00
|
|
|
bool autofreeStream, bool reverseStereo, int id, bool permanent)
|
2005-03-12 20:26:49 +00:00
|
|
|
: _type(type), _mixer(mixer), _autofreeStream(autofreeStream),
|
2007-06-30 21:02:58 +00:00
|
|
|
_volume(Mixer::kMaxChannelVolume), _balance(0), _pauseLevel(0), _id(id), _samplesConsumed(0),
|
2008-11-09 13:28:59 +00:00
|
|
|
_samplesDecoded(0), _mixerTimeStamp(0), _pauseStartTime(0), _pauseTime(0), _converter(0),
|
|
|
|
_input(input), _permanent(permanent) {
|
2003-12-22 19:08:19 +00:00
|
|
|
assert(mixer);
|
|
|
|
assert(input);
|
|
|
|
|
|
|
|
// Get a rate converter instance
|
|
|
|
_converter = makeRateConverter(_input->getRate(), mixer->getOutputRate(), _input->isStereo(), reverseStereo);
|
|
|
|
}
|
|
|
|
|
2003-08-05 23:03:42 +00:00
|
|
|
Channel::~Channel() {
|
|
|
|
delete _converter;
|
2003-12-26 02:19:31 +00:00
|
|
|
if (_autofreeStream)
|
|
|
|
delete _input;
|
2003-08-05 23:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* len indicates the number of sample *pairs*. So a value of
|
|
|
|
10 means that the buffer contains twice 10 sample, each
|
|
|
|
16 bits, for a total of 40 bytes.
|
|
|
|
*/
|
|
|
|
void Channel::mix(int16 *data, uint len) {
|
|
|
|
assert(_input);
|
2003-12-19 01:30:19 +00:00
|
|
|
|
2003-12-22 19:08:19 +00:00
|
|
|
if (_input->endOfData()) {
|
2003-12-19 01:30:19 +00:00
|
|
|
// TODO: call drain method
|
2003-08-05 23:03:42 +00:00
|
|
|
} else {
|
|
|
|
assert(_converter);
|
2003-09-05 23:27:11 +00:00
|
|
|
|
2004-01-29 18:15:27 +00:00
|
|
|
// From the channel balance/volume and the global volume, we compute
|
|
|
|
// the effective volume for the left and right channel. Note the
|
|
|
|
// slightly odd divisor: the 255 reflects the fact that the maximal
|
|
|
|
// value for _volume is 255, while the 127 is there because the
|
|
|
|
// balance value ranges from -127 to 127. The mixer (music/sound)
|
2005-02-06 20:35:06 +00:00
|
|
|
// volume is in the range 0 - kMaxMixerVolume.
|
2003-09-05 23:27:11 +00:00
|
|
|
// Hence, the vol_l/vol_r values will be in that range, too
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-12-27 00:27:00 +00:00
|
|
|
int vol = _mixer->getVolumeForSoundType(_type) * _volume;
|
2004-01-29 18:15:27 +00:00
|
|
|
st_volume_t vol_l, vol_r;
|
|
|
|
|
|
|
|
if (_balance == 0) {
|
2005-05-10 23:48:48 +00:00
|
|
|
vol_l = vol / Mixer::kMaxChannelVolume;
|
|
|
|
vol_r = vol / Mixer::kMaxChannelVolume;
|
2004-01-29 18:15:27 +00:00
|
|
|
} else if (_balance < 0) {
|
2005-05-10 23:48:48 +00:00
|
|
|
vol_l = vol / Mixer::kMaxChannelVolume;
|
|
|
|
vol_r = ((127 + _balance) * vol) / (Mixer::kMaxChannelVolume * 127);
|
2004-01-29 18:15:27 +00:00
|
|
|
} else {
|
2005-05-10 23:48:48 +00:00
|
|
|
vol_l = ((127 - _balance) * vol) / (Mixer::kMaxChannelVolume * 127);
|
|
|
|
vol_r = vol / Mixer::kMaxChannelVolume;
|
2004-01-29 18:15:27 +00:00
|
|
|
}
|
2003-09-05 23:27:11 +00:00
|
|
|
|
2004-02-12 16:25:28 +00:00
|
|
|
_samplesConsumed = _samplesDecoded;
|
2004-09-28 20:19:37 +00:00
|
|
|
_mixerTimeStamp = g_system->getMillis();
|
2008-11-09 13:28:59 +00:00
|
|
|
_pauseTime = 0;
|
2004-02-12 16:25:28 +00:00
|
|
|
|
2003-09-05 23:27:11 +00:00
|
|
|
_converter->flow(*_input, data, len, vol_l, vol_r);
|
2004-02-12 16:25:28 +00:00
|
|
|
|
|
|
|
_samplesDecoded += len;
|
2003-08-05 23:03:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-12 16:25:28 +00:00
|
|
|
uint32 Channel::getElapsedTime() {
|
|
|
|
if (_mixerTimeStamp == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Convert the number of samples into a time duration. To avoid
|
|
|
|
// overflow, this has to be done in a somewhat non-obvious way.
|
|
|
|
|
2008-06-28 15:28:29 +00:00
|
|
|
uint32 rate = _mixer->getOutputRate();
|
2004-02-12 16:25:28 +00:00
|
|
|
|
|
|
|
uint32 seconds = _samplesConsumed / rate;
|
|
|
|
uint32 milliseconds = (1000 * (_samplesConsumed % rate)) / rate;
|
|
|
|
|
2008-11-09 13:28:59 +00:00
|
|
|
uint32 delta = g_system->getMillis() - _mixerTimeStamp - _pauseTime;
|
2004-02-12 16:25:28 +00:00
|
|
|
|
|
|
|
// In theory it would seem like a good idea to limit the approximation
|
|
|
|
// so that it never exceeds the theoretical upper bound set by
|
|
|
|
// _samplesDecoded. Meanwhile, back in the real world, doing so makes
|
|
|
|
// the Broken Sword cutscenes noticeably jerkier. I guess the mixer
|
|
|
|
// isn't invoked at the regular intervals that I first imagined.
|
|
|
|
|
|
|
|
return 1000 * seconds + milliseconds + delta;
|
|
|
|
}
|
2005-05-10 23:48:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // End of namespace Audio
|