2003-12-12 21:17:31 +00:00
|
|
|
// Residual - Virtual machine to run LucasArts' 3D adventure games
|
2005-01-01 10:23:18 +00:00
|
|
|
// Copyright (C) 2003-2005 The ScummVM-Residual Team (www.scummvm.org)
|
2003-12-12 21:17:31 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library 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
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
2005-01-01 12:27:57 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "bits.h"
|
|
|
|
#include "debug.h"
|
2003-12-12 21:17:31 +00:00
|
|
|
|
2005-01-01 12:27:57 +00:00
|
|
|
#include "mixer/mixer.h"
|
|
|
|
#include "mixer/rate.h"
|
|
|
|
#include "mixer/audiostream.h"
|
2003-12-12 21:17:31 +00:00
|
|
|
|
2006-02-05 16:11:12 +00:00
|
|
|
#include <SDL.h>
|
|
|
|
|
2003-12-12 21:17:31 +00:00
|
|
|
SoundMixer *g_mixer = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Channels used by the sound mixer.
|
|
|
|
*/
|
|
|
|
class Channel {
|
2004-02-01 10:48:44 +00:00
|
|
|
private:
|
2003-12-12 21:17:31 +00:00
|
|
|
SoundMixer *_mixer;
|
|
|
|
PlayingSoundHandle *_handle;
|
2004-02-01 10:48:44 +00:00
|
|
|
bool _autofreeStream;
|
2004-12-10 21:13:03 +00:00
|
|
|
bool _permanent;
|
2003-12-12 21:17:31 +00:00
|
|
|
byte _volume;
|
2004-02-01 10:48:44 +00:00
|
|
|
int8 _balance;
|
2003-12-12 21:17:31 +00:00
|
|
|
bool _paused;
|
2004-02-01 10:48:44 +00:00
|
|
|
int _id;
|
2004-12-10 21:13:03 +00:00
|
|
|
uint32 _samplesConsumed;
|
|
|
|
uint32 _samplesDecoded;
|
2004-02-01 10:48:44 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
RateConverter *_converter;
|
|
|
|
AudioStream *_input;
|
2003-12-12 21:17:31 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
Channel(SoundMixer *mixer, PlayingSoundHandle *handle, bool isMusic, int id = -1);
|
2004-12-10 21:13:03 +00:00
|
|
|
Channel(SoundMixer *mixer, PlayingSoundHandle *handle, AudioStream *input, bool autofreeStream, bool isMusic, bool reverseStereo = false, int id = -1, bool permanent = false);
|
2003-12-12 21:17:31 +00:00
|
|
|
virtual ~Channel();
|
2004-02-01 10:48:44 +00:00
|
|
|
|
|
|
|
void mix(int16 *data, uint len);
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
bool isPermanent() const {
|
|
|
|
return _permanent;
|
|
|
|
}
|
2004-02-01 10:48:44 +00:00
|
|
|
bool isFinished() const {
|
|
|
|
return _input->endOfStream();
|
|
|
|
}
|
|
|
|
void pause(bool paused) {
|
2003-12-12 21:17:31 +00:00
|
|
|
_paused = paused;
|
|
|
|
}
|
2004-02-01 10:48:44 +00:00
|
|
|
bool isPaused() {
|
2003-12-12 21:17:31 +00:00
|
|
|
return _paused;
|
|
|
|
}
|
2004-02-01 10:48:44 +00:00
|
|
|
void setVolume(const byte volume) {
|
2003-12-12 21:17:31 +00:00
|
|
|
_volume = volume;
|
|
|
|
}
|
2004-02-01 10:48:44 +00:00
|
|
|
void setBalance(const int8 balance) {
|
|
|
|
_balance = balance;
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
2004-02-01 10:48:44 +00:00
|
|
|
int getId() const {
|
|
|
|
return _id;
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
2004-12-10 21:13:03 +00:00
|
|
|
uint32 getElapsedTime();
|
2003-12-12 21:17:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
SoundMixer::SoundMixer() {
|
2004-12-10 20:33:31 +00:00
|
|
|
_mutex = createMutex();
|
2004-12-10 21:13:03 +00:00
|
|
|
_premixChannel = NULL;
|
2004-02-01 10:48:44 +00:00
|
|
|
_outputRate = 22050;
|
2003-12-12 21:17:31 +00:00
|
|
|
_globalVolume = 0;
|
|
|
|
_paused = false;
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++)
|
2003-12-12 21:17:31 +00:00
|
|
|
_channels[i] = NULL;
|
2004-02-01 10:48:44 +00:00
|
|
|
|
|
|
|
_mixerReady = setSoundProc(mixCallback, this);
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SoundMixer::~SoundMixer() {
|
|
|
|
SDL_CloseAudio();
|
2004-12-10 21:13:03 +00:00
|
|
|
stopAll(true);
|
|
|
|
|
|
|
|
delete _premixChannel;
|
|
|
|
_premixChannel = NULL;
|
|
|
|
|
2004-12-10 20:33:31 +00:00
|
|
|
deleteMutex(_mutex);
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
bool SoundMixer::isPaused() {
|
|
|
|
return _paused;
|
|
|
|
}
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
bool SoundMixer::setSoundProc(SoundProc proc, void *param) {
|
2003-12-12 21:17:31 +00:00
|
|
|
SDL_AudioSpec desired;
|
|
|
|
|
|
|
|
memset(&desired, 0, sizeof(desired));
|
|
|
|
|
|
|
|
desired.freq = 22050;
|
|
|
|
desired.format = AUDIO_S16SYS;
|
|
|
|
desired.channels = 2;
|
|
|
|
desired.samples = 2048;
|
|
|
|
desired.callback = proc;
|
|
|
|
desired.userdata = param;
|
2004-02-01 10:48:44 +00:00
|
|
|
|
2003-12-12 21:17:31 +00:00
|
|
|
if (SDL_OpenAudio(&desired, NULL) != 0) {
|
2004-02-01 10:48:44 +00:00
|
|
|
return false;
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
SDL_PauseAudio(0);
|
|
|
|
return true;
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
void SoundMixer::setupPremix(AudioStream *stream) {
|
2003-12-12 21:17:31 +00:00
|
|
|
StackLock lock(_mutex);
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
delete _premixChannel;
|
|
|
|
_premixChannel = NULL;
|
2003-12-12 21:17:31 +00:00
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
if (stream == NULL)
|
2003-12-12 21:17:31 +00:00
|
|
|
return;
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
// Create the channel
|
|
|
|
_premixChannel = new Channel(this, NULL, stream, false, true);
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
void SoundMixer::insertChannel(PlayingSoundHandle *handle, Channel *chan) {
|
2003-12-12 21:17:31 +00:00
|
|
|
int index = -1;
|
2004-12-10 21:13:03 +00:00
|
|
|
|
2003-12-12 21:17:31 +00:00
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++) {
|
2004-12-10 21:13:03 +00:00
|
|
|
if (_channels[i] == NULL) {
|
2003-12-12 21:17:31 +00:00
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-12-10 21:13:03 +00:00
|
|
|
if (index == -1) {
|
2003-12-12 21:17:31 +00:00
|
|
|
warning("SoundMixer::out of mixer slots");
|
|
|
|
delete chan;
|
2004-02-01 10:48:44 +00:00
|
|
|
return;
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_channels[index] = chan;
|
|
|
|
if (handle)
|
2004-02-01 10:48:44 +00:00
|
|
|
handle->setIndex(index);
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
void SoundMixer::playRaw(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags,
|
|
|
|
int id, byte volume, int8 balance, uint32 loopStart, uint32 loopEnd) {
|
2003-12-12 21:17:31 +00:00
|
|
|
StackLock lock(_mutex);
|
|
|
|
|
|
|
|
// Prevent duplicate sounds
|
|
|
|
if (id != -1) {
|
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++)
|
2004-12-10 21:13:03 +00:00
|
|
|
if (_channels[i] != NULL && _channels[i]->getId() == id) {
|
2004-02-01 10:48:44 +00:00
|
|
|
if ((flags & SoundMixer::FLAG_AUTOFREE) != 0)
|
|
|
|
free(sound);
|
|
|
|
return;
|
|
|
|
}
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
// Create the input stream
|
|
|
|
AudioStream *input;
|
|
|
|
if (flags & SoundMixer::FLAG_LOOP) {
|
|
|
|
if (loopEnd == 0) {
|
|
|
|
input = makeLinearInputStream(rate, flags, (byte *)sound, size, 0, size);
|
|
|
|
} else {
|
|
|
|
assert(loopStart < loopEnd && loopEnd <= size);
|
|
|
|
input = makeLinearInputStream(rate, flags, (byte *)sound, size, loopStart, loopEnd - loopStart);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
input = makeLinearInputStream(rate, flags, (byte *)sound, size, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the channel
|
|
|
|
Channel *chan = new Channel(this, handle, input, true, false, (flags & SoundMixer::FLAG_REVERSE_STEREO) != 0, id);
|
|
|
|
chan->setVolume(volume);
|
|
|
|
chan->setBalance(balance);
|
|
|
|
insertChannel(handle, chan);
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
void SoundMixer::playInputStream(PlayingSoundHandle *handle, AudioStream *input, bool isMusic,
|
|
|
|
int id, byte volume, int8 balance, bool autofreeStream, bool permanent) {
|
2003-12-12 21:17:31 +00:00
|
|
|
StackLock lock(_mutex);
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
if (input == NULL) {
|
|
|
|
warning("input stream is NULL");
|
2004-02-01 10:48:44 +00:00
|
|
|
return;
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
// Prevent duplicate sounds
|
|
|
|
if (id != -1) {
|
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++)
|
2004-12-10 21:13:03 +00:00
|
|
|
if (_channels[i] != NULL && _channels[i]->getId() == id) {
|
2004-02-01 10:48:44 +00:00
|
|
|
if (autofreeStream)
|
|
|
|
delete input;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the channel
|
2004-12-10 21:13:03 +00:00
|
|
|
Channel *chan = new Channel(this, handle, input, autofreeStream, isMusic, false, id, permanent);
|
2004-02-01 10:48:44 +00:00
|
|
|
chan->setVolume(volume);
|
|
|
|
chan->setBalance(balance);
|
|
|
|
insertChannel(handle, chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundMixer::mix(int16 *buf, uint len) {
|
|
|
|
StackLock lock(_mutex);
|
|
|
|
|
|
|
|
// zero the buf
|
|
|
|
memset(buf, 0, 2 * len * sizeof(int16));
|
|
|
|
|
2003-12-12 21:17:31 +00:00
|
|
|
if (!_paused) {
|
2004-12-10 21:13:03 +00:00
|
|
|
if (_premixChannel)
|
|
|
|
_premixChannel->mix(buf, len);
|
2004-02-01 10:48:44 +00:00
|
|
|
|
2003-12-12 21:17:31 +00:00
|
|
|
// now mix all channels
|
2004-12-10 21:13:03 +00:00
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++) {
|
2004-02-01 10:48:44 +00:00
|
|
|
if (_channels[i]) {
|
|
|
|
if (_channels[i]->isFinished()) {
|
|
|
|
delete _channels[i];
|
2004-12-10 21:13:03 +00:00
|
|
|
_channels[i] = NULL;
|
2004-02-01 10:48:44 +00:00
|
|
|
} else if (!_channels[i]->isPaused())
|
|
|
|
_channels[i]->mix(buf, len);
|
|
|
|
}
|
2004-12-10 21:13:03 +00:00
|
|
|
}
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundMixer::mixCallback(void *s, byte *samples, int len) {
|
|
|
|
assert(s);
|
|
|
|
assert(samples);
|
|
|
|
// Len is the number of bytes in the buffer; we divide it by
|
|
|
|
// four to get the number of samples (stereo 16 bit).
|
|
|
|
((SoundMixer *)s)->mix((int16 *)samples, len >> 2);
|
|
|
|
}
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
void SoundMixer::stopAll(bool force) {
|
2003-12-12 21:17:31 +00:00
|
|
|
StackLock lock(_mutex);
|
2004-12-10 21:13:03 +00:00
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++) {
|
|
|
|
if (_channels[i] != NULL) {
|
|
|
|
if (force || !_channels[i]->isPermanent()) {
|
|
|
|
delete _channels[i];
|
|
|
|
_channels[i] = NULL;
|
|
|
|
}
|
2004-02-01 10:48:44 +00:00
|
|
|
}
|
2004-12-10 21:13:03 +00:00
|
|
|
}
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundMixer::stopID(int id) {
|
|
|
|
StackLock lock(_mutex);
|
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++) {
|
2004-12-10 21:13:03 +00:00
|
|
|
if (_channels[i] != NULL && _channels[i]->getId() == id) {
|
2004-02-01 10:48:44 +00:00
|
|
|
delete _channels[i];
|
2004-12-10 21:13:03 +00:00
|
|
|
_channels[i] = NULL;
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundMixer::stopHandle(PlayingSoundHandle handle) {
|
|
|
|
StackLock lock(_mutex);
|
|
|
|
|
|
|
|
// Simply ignore stop requests for handles of sounds that already terminated
|
2004-02-01 10:48:44 +00:00
|
|
|
if (!handle.isActive())
|
2003-12-12 21:17:31 +00:00
|
|
|
return;
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
int index = handle.getIndex();
|
2003-12-12 21:17:31 +00:00
|
|
|
|
|
|
|
if ((index < 0) || (index >= NUM_CHANNELS)) {
|
|
|
|
warning("soundMixer::stopHandle has invalid index %d", index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
if (_channels[index]) {
|
|
|
|
delete _channels[index];
|
2004-12-10 21:13:03 +00:00
|
|
|
_channels[index] = NULL;
|
2004-02-01 10:48:44 +00:00
|
|
|
}
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundMixer::setChannelVolume(PlayingSoundHandle handle, byte volume) {
|
|
|
|
StackLock lock(_mutex);
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
if (!handle.isActive())
|
2003-12-12 21:17:31 +00:00
|
|
|
return;
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
int index = handle.getIndex();
|
2003-12-12 21:17:31 +00:00
|
|
|
|
|
|
|
if ((index < 0) || (index >= NUM_CHANNELS)) {
|
|
|
|
warning("soundMixer::setChannelVolume has invalid index %d", index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_channels[index])
|
2004-02-01 10:48:44 +00:00
|
|
|
_channels[index]->setVolume(volume);
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
void SoundMixer::setChannelBalance(PlayingSoundHandle handle, int8 balance) {
|
2003-12-12 21:17:31 +00:00
|
|
|
StackLock lock(_mutex);
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
if (!handle.isActive())
|
2003-12-12 21:17:31 +00:00
|
|
|
return;
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
int index = handle.getIndex();
|
2003-12-12 21:17:31 +00:00
|
|
|
|
|
|
|
if ((index < 0) || (index >= NUM_CHANNELS)) {
|
2004-02-01 10:48:44 +00:00
|
|
|
warning("soundMixer::setChannelBalance has invalid index %d", index);
|
2003-12-12 21:17:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_channels[index])
|
2004-02-01 10:48:44 +00:00
|
|
|
_channels[index]->setBalance(balance);
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundMixer::pauseAll(bool paused) {
|
|
|
|
_paused = paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundMixer::pauseID(int id, bool paused) {
|
|
|
|
StackLock lock(_mutex);
|
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++) {
|
2004-12-10 21:13:03 +00:00
|
|
|
if (_channels[i] != NULL && _channels[i]->getId() == id) {
|
2003-12-12 21:17:31 +00:00
|
|
|
_channels[i]->pause(paused);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundMixer::pauseHandle(PlayingSoundHandle handle, bool paused) {
|
|
|
|
StackLock lock(_mutex);
|
|
|
|
|
|
|
|
// Simply ignore pause/unpause requests for handles of sound that alreayd terminated
|
2004-02-01 10:48:44 +00:00
|
|
|
if (!handle.isActive())
|
2003-12-12 21:17:31 +00:00
|
|
|
return;
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
int index = handle.getIndex();
|
2003-12-12 21:17:31 +00:00
|
|
|
|
|
|
|
if ((index < 0) || (index >= NUM_CHANNELS)) {
|
|
|
|
warning("soundMixer::pauseHandle has invalid index %d", index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_channels[index])
|
|
|
|
_channels[index]->pause(paused);
|
|
|
|
}
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
bool SoundMixer::isSoundIDActive(int id) {
|
|
|
|
StackLock lock(_mutex);
|
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++)
|
|
|
|
if (_channels[i] && _channels[i]->getId() == id)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-12-12 21:17:31 +00:00
|
|
|
void SoundMixer::setVolume(int volume) {
|
|
|
|
// Check range
|
|
|
|
if (volume > 256)
|
|
|
|
volume = 256;
|
|
|
|
else if (volume < 0)
|
|
|
|
volume = 0;
|
2004-12-10 21:13:03 +00:00
|
|
|
|
2003-12-12 21:17:31 +00:00
|
|
|
_globalVolume = volume;
|
|
|
|
}
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
|
2005-01-12 22:37:24 +00:00
|
|
|
Channel::Channel(SoundMixer *mixer, PlayingSoundHandle *handle, bool /*isMusic*/, int id)
|
2004-12-10 21:13:03 +00:00
|
|
|
: _mixer(mixer), _handle(handle), _autofreeStream(true),
|
|
|
|
_volume(255), _balance(0), _paused(false), _id(id), _samplesConsumed(0),
|
|
|
|
_samplesDecoded(0), _converter(0), _input(NULL) {
|
2004-02-01 10:48:44 +00:00
|
|
|
assert(mixer);
|
|
|
|
}
|
|
|
|
|
|
|
|
Channel::Channel(SoundMixer *mixer, PlayingSoundHandle *handle, AudioStream *input,
|
2005-01-12 22:37:24 +00:00
|
|
|
bool autofreeStream, bool /*isMusic*/, bool reverseStereo, int id, bool permanent)
|
2004-12-10 21:13:03 +00:00
|
|
|
: _mixer(mixer), _handle(handle), _autofreeStream(autofreeStream),
|
2005-01-12 22:37:24 +00:00
|
|
|
_permanent(permanent), _volume(255), _balance(0), _paused(false), _id(id),
|
|
|
|
_samplesConsumed(0), _samplesDecoded(0), _converter(0), _input(input) {
|
2004-02-01 10:48:44 +00:00
|
|
|
assert(mixer);
|
|
|
|
assert(input);
|
|
|
|
|
|
|
|
// Get a rate converter instance
|
|
|
|
_converter = makeRateConverter(_input->getRate(), mixer->getOutputRate(), _input->isStereo(), reverseStereo);
|
|
|
|
}
|
|
|
|
|
2003-12-12 21:17:31 +00:00
|
|
|
Channel::~Channel() {
|
|
|
|
delete _converter;
|
2004-02-01 10:48:44 +00:00
|
|
|
if (_autofreeStream)
|
|
|
|
delete _input;
|
2003-12-12 21:17:31 +00:00
|
|
|
if (_handle)
|
2004-02-01 10:48:44 +00:00
|
|
|
_handle->resetIndex();
|
2003-12-12 21:17:31 +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);
|
2004-02-01 10:48:44 +00:00
|
|
|
|
|
|
|
if (_input->endOfData()) {
|
2003-12-12 21:17:31 +00:00
|
|
|
// TODO: call drain method
|
|
|
|
} else {
|
|
|
|
assert(_converter);
|
|
|
|
|
2004-02-01 10:48:44 +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)
|
|
|
|
// volume is in the range 0 - 256.
|
2003-12-12 21:17:31 +00:00
|
|
|
// Hence, the vol_l/vol_r values will be in that range, too
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
int vol = _mixer->getVolume() * _volume;
|
|
|
|
st_volume_t vol_l, vol_r;
|
|
|
|
|
|
|
|
if (_balance == 0) {
|
|
|
|
vol_l = vol / 255;
|
|
|
|
vol_r = vol / 255;
|
|
|
|
} else if (_balance < 0) {
|
|
|
|
vol_l = vol / 255;
|
|
|
|
vol_r = ((127 + _balance) * vol) / (255 * 127);
|
2003-12-12 21:17:31 +00:00
|
|
|
} else {
|
2004-02-01 10:48:44 +00:00
|
|
|
vol_l = ((127 - _balance) * vol) / (255 * 127);
|
|
|
|
vol_r = vol / 255;
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
_samplesConsumed = _samplesDecoded;
|
2003-12-12 21:17:31 +00:00
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
_converter->flow(*_input, data, len, vol_l, vol_r);
|
2003-12-12 21:17:31 +00:00
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
_samplesDecoded += len;
|
|
|
|
}
|
2003-12-12 21:17:31 +00:00
|
|
|
}
|