2002-04-21 17:46:42 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
|
|
|
* Copyright (C) 2001 Ludvig Strigeus
|
2003-03-06 21:46:56 +00:00
|
|
|
* Copyright (C) 2001-2003 The ScummVM project
|
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
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-04-14 18:13:08 +00:00
|
|
|
#include "stdafx.h"
|
2002-09-08 01:08:12 +00:00
|
|
|
#include "common/file.h"
|
2003-07-05 16:01:55 +00:00
|
|
|
#include "common/util.h"
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2003-08-05 21:46:56 +00:00
|
|
|
#include "sound/mixer.h"
|
|
|
|
#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
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark --- Channel classes ---
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Channels used by the sound mixer.
|
|
|
|
*/
|
2003-06-22 01:55:53 +00:00
|
|
|
class Channel {
|
|
|
|
protected:
|
|
|
|
SoundMixer *_mixer;
|
2003-07-06 16:52:09 +00:00
|
|
|
PlayingSoundHandle *_handle;
|
2003-08-02 02:41:31 +00:00
|
|
|
RateConverter *_converter;
|
|
|
|
AudioInputStream *_input;
|
2003-08-31 20:26:21 +00:00
|
|
|
byte _volume;
|
|
|
|
int8 _pan;
|
2003-09-02 13:48:20 +00:00
|
|
|
bool _paused;
|
2003-08-31 20:26:21 +00:00
|
|
|
|
2003-06-22 01:55:53 +00:00
|
|
|
public:
|
|
|
|
int _id;
|
2003-08-05 23:03:42 +00:00
|
|
|
|
2003-07-06 16:52:09 +00:00
|
|
|
Channel(SoundMixer *mixer, PlayingSoundHandle *handle)
|
2003-09-05 06:22:10 +00:00
|
|
|
: _mixer(mixer), _handle(handle), _converter(0), _input(0), _volume(0), _pan(0), _paused(false), _id(-1) {
|
2003-07-06 16:52:09 +00:00
|
|
|
assert(mixer);
|
|
|
|
}
|
2003-08-05 23:03:42 +00:00
|
|
|
virtual ~Channel();
|
2003-08-05 18:17:26 +00:00
|
|
|
void destroy();
|
2003-08-05 23:03:42 +00:00
|
|
|
virtual void mix(int16 *data, uint len);
|
2003-09-02 13:48:20 +00:00
|
|
|
virtual void pause(bool paused) {
|
|
|
|
_paused = paused;
|
|
|
|
}
|
|
|
|
virtual bool isPaused() {
|
|
|
|
return _paused;
|
|
|
|
}
|
2003-08-31 20:26:21 +00:00
|
|
|
virtual void setChannelVolume(const byte volume) {
|
|
|
|
_volume = volume;
|
|
|
|
}
|
|
|
|
virtual void setChannelPan(const int8 pan) {
|
|
|
|
_pan = pan;
|
|
|
|
}
|
2003-08-05 23:03:42 +00:00
|
|
|
virtual int getVolume() const {
|
|
|
|
return isMusicChannel() ? _mixer->getMusicVolume() : _mixer->getVolume();
|
|
|
|
}
|
2003-08-05 02:05:37 +00:00
|
|
|
virtual bool isMusicChannel() const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ChannelRaw : public Channel {
|
|
|
|
byte *_ptr;
|
|
|
|
public:
|
2003-08-31 20:26:21 +00:00
|
|
|
ChannelRaw(SoundMixer *mixer, PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, byte volume, int8 pan, int id, uint32 loopStart, uint32 loopEnd);
|
2003-08-05 02:05:37 +00:00
|
|
|
~ChannelRaw();
|
|
|
|
bool isMusicChannel() const { return false; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class ChannelStream : public Channel {
|
|
|
|
bool _finished;
|
|
|
|
public:
|
2003-08-31 20:26:21 +00:00
|
|
|
ChannelStream(SoundMixer *mixer, PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, uint32 buffer_size, byte volume, int8 pan);
|
2003-08-05 02:05:37 +00:00
|
|
|
void mix(int16 *data, uint len);
|
|
|
|
void append(void *sound, uint32 size);
|
|
|
|
bool isMusicChannel() const { return true; }
|
|
|
|
void finish() { _finished = true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef USE_MAD
|
|
|
|
class ChannelMP3 : public Channel {
|
|
|
|
public:
|
2003-09-05 06:22:10 +00:00
|
|
|
ChannelMP3(SoundMixer *mixer, PlayingSoundHandle *handle, File *file, uint size, byte volume, int8 pan);
|
2003-08-05 02:05:37 +00:00
|
|
|
bool isMusicChannel() const { return false; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class ChannelMP3CDMusic : public Channel {
|
|
|
|
public:
|
2003-09-05 06:22:10 +00:00
|
|
|
ChannelMP3CDMusic(SoundMixer *mixer, PlayingSoundHandle *handle, File *file, mad_timer_t duration, byte volume, int8 pan);
|
2003-08-05 02:05:37 +00:00
|
|
|
bool isMusicChannel() const { return true; }
|
|
|
|
};
|
|
|
|
#endif // USE_MAD
|
|
|
|
|
|
|
|
#ifdef USE_VORBIS
|
|
|
|
class ChannelVorbis : public Channel {
|
|
|
|
bool _is_cd_track;
|
|
|
|
public:
|
2003-09-05 06:22:10 +00:00
|
|
|
ChannelVorbis(SoundMixer *mixer, PlayingSoundHandle *handle, OggVorbis_File *ov_file, int duration, bool is_cd_track, byte volume, int8 pan);
|
2003-08-05 02:05:37 +00:00
|
|
|
bool isMusicChannel() const { return _is_cd_track; }
|
|
|
|
};
|
2003-08-05 23:03:42 +00:00
|
|
|
#endif // USE_VORBIS
|
2003-08-05 02:05:37 +00:00
|
|
|
|
|
|
|
|
2003-08-05 23:03:42 +00:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark --- SoundMixer ---
|
|
|
|
#pragma mark -
|
2003-06-15 01:56:47 +00:00
|
|
|
|
2003-08-05 18:17:26 +00:00
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
SoundMixer::SoundMixer() {
|
2003-06-22 13:48:47 +00:00
|
|
|
_syst = 0;
|
|
|
|
_mutex = 0;
|
|
|
|
|
|
|
|
_premixParam = 0;
|
|
|
|
_premixProc = 0;
|
2003-06-22 14:30:32 +00:00
|
|
|
int i = 0;
|
2003-06-22 13:48:47 +00:00
|
|
|
|
|
|
|
_outputRate = 0;
|
2003-04-30 12:51:07 +00:00
|
|
|
|
2003-07-25 01:19:14 +00:00
|
|
|
_globalVolume = 0;
|
2003-06-22 13:48:47 +00:00
|
|
|
_musicVolume = 0;
|
|
|
|
|
|
|
|
_paused = false;
|
|
|
|
|
2003-06-22 14:30:32 +00:00
|
|
|
for (i = 0; i != NUM_CHANNELS; i++)
|
2002-10-27 19:32:36 +00:00
|
|
|
_channels[i] = NULL;
|
2002-08-18 21:42:22 +00:00
|
|
|
}
|
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
SoundMixer::~SoundMixer() {
|
2003-06-09 01:19:44 +00:00
|
|
|
_syst->clear_sound_proc();
|
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++) {
|
|
|
|
delete _channels[i];
|
|
|
|
}
|
2003-06-22 01:34:28 +00:00
|
|
|
_syst->delete_mutex(_mutex);
|
2002-08-18 21:42:22 +00:00
|
|
|
}
|
|
|
|
|
2003-09-05 20:48:32 +00:00
|
|
|
bool SoundMixer::bindToSystem(OSystem *syst) {
|
|
|
|
_syst = syst;
|
|
|
|
_mutex = _syst->create_mutex();
|
|
|
|
_outputRate = (uint) syst->property(OSystem::PROP_GET_SAMPLE_RATE, 0);
|
|
|
|
|
|
|
|
if (_outputRate == 0)
|
|
|
|
error("OSystem returned invalid sample rate");
|
|
|
|
|
|
|
|
return syst->set_sound_proc(mixCallback, this, OSystem::SOUND_16BIT);
|
|
|
|
}
|
|
|
|
|
2003-09-18 16:01:33 +00:00
|
|
|
void SoundMixer::setupPremix(PremixProc *proc, void *param) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-05 20:48:32 +00:00
|
|
|
_premixParam = param;
|
|
|
|
_premixProc = proc;
|
|
|
|
}
|
|
|
|
|
2003-09-01 14:02:45 +00:00
|
|
|
int SoundMixer::newStream(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, uint32 buffer_size, byte volume, int8 pan) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-01 14:02:45 +00:00
|
|
|
return insertChannel(handle, new ChannelStream(this, handle, sound, size, rate, flags, buffer_size, volume, pan));
|
2003-07-25 01:19:14 +00:00
|
|
|
}
|
|
|
|
|
2003-09-01 14:02:45 +00:00
|
|
|
void SoundMixer::appendStream(PlayingSoundHandle handle, void *sound, uint32 size) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-01 14:02:45 +00:00
|
|
|
|
|
|
|
if (handle == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int index = handle - 1;
|
|
|
|
|
|
|
|
if ((index < 0) || (index >= NUM_CHANNELS)) {
|
|
|
|
warning("soundMixer::appendStream has invalid index %d", index);
|
|
|
|
return;
|
|
|
|
}
|
2002-06-03 21:20:11 +00:00
|
|
|
|
2003-07-12 11:33:13 +00:00
|
|
|
ChannelStream *chan;
|
2003-07-12 17:43:39 +00:00
|
|
|
#if !defined(_WIN32_WCE) && !defined(__PALM_OS__)
|
2003-07-12 11:33:13 +00:00
|
|
|
chan = dynamic_cast<ChannelStream *>(_channels[index]);
|
|
|
|
#else
|
|
|
|
chan = (ChannelStream*)_channels[index];
|
|
|
|
#endif
|
2002-06-02 20:30:21 +00:00
|
|
|
if (!chan) {
|
2003-09-01 14:02:45 +00:00
|
|
|
error("Trying to append to nonexistant streamer : %d", index);
|
2002-06-03 21:20:11 +00:00
|
|
|
} else {
|
2003-06-22 11:47:03 +00:00
|
|
|
chan->append(sound, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-01 14:02:45 +00:00
|
|
|
void SoundMixer::endStream(PlayingSoundHandle handle) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-01 14:02:45 +00:00
|
|
|
|
|
|
|
// Simply ignore stop requests for handles of sounds that already terminated
|
|
|
|
if (handle == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int index = handle - 1;
|
2003-06-22 11:47:03 +00:00
|
|
|
|
2003-09-01 14:02:45 +00:00
|
|
|
if ((index < 0) || (index >= NUM_CHANNELS)) {
|
|
|
|
warning("soundMixer::endStream has invalid index %d", index);
|
2003-08-30 18:12:49 +00:00
|
|
|
return;
|
2003-09-01 14:02:45 +00:00
|
|
|
}
|
2003-08-30 18:12:49 +00:00
|
|
|
|
2003-07-12 11:33:13 +00:00
|
|
|
ChannelStream *chan;
|
2003-07-12 17:43:39 +00:00
|
|
|
#if !defined(_WIN32_WCE) && !defined(__PALM_OS__)
|
2003-07-12 11:33:13 +00:00
|
|
|
chan = dynamic_cast<ChannelStream *>(_channels[index]);
|
|
|
|
#else
|
|
|
|
chan = (ChannelStream*)_channels[index];
|
|
|
|
#endif
|
2003-06-22 11:47:03 +00:00
|
|
|
if (!chan) {
|
|
|
|
error("Trying to end a nonexistant streamer : %d", index);
|
|
|
|
} else {
|
|
|
|
chan->finish();
|
2002-06-02 20:30:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-22 01:34:28 +00:00
|
|
|
int SoundMixer::insertChannel(PlayingSoundHandle *handle, Channel *chan) {
|
|
|
|
int index = -1;
|
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++) {
|
|
|
|
if (_channels[i] == NULL) {
|
|
|
|
index = i;
|
|
|
|
break;
|
2002-08-24 15:31:37 +00:00
|
|
|
}
|
|
|
|
}
|
2003-06-22 01:34:28 +00:00
|
|
|
if(index == -1) {
|
|
|
|
warning("SoundMixer::out of mixer slots");
|
|
|
|
delete chan;
|
|
|
|
return -1;
|
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;
|
|
|
|
if (handle)
|
2003-08-30 20:25:07 +00:00
|
|
|
*handle = index + 1;
|
2002-06-02 20:30:21 +00:00
|
|
|
return index;
|
2002-05-12 16:53:13 +00:00
|
|
|
}
|
|
|
|
|
2003-09-05 22:09:56 +00:00
|
|
|
int SoundMixer::playRaw(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, int id, byte volume, int8 pan, uint32 loopStart, uint32 loopEnd) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-07-05 16:01:55 +00:00
|
|
|
|
2003-07-04 14:10:44 +00:00
|
|
|
// Prevent duplicate sounds
|
2003-07-04 20:08:02 +00:00
|
|
|
if (id != -1) {
|
2003-07-05 16:01:55 +00:00
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++)
|
2003-07-06 06:36:26 +00:00
|
|
|
if (_channels[i] != NULL && _channels[i]->_id == id)
|
2003-07-05 16:01:55 +00:00
|
|
|
return -1;
|
2003-07-04 20:08:02 +00:00
|
|
|
}
|
2003-07-04 14:10:44 +00:00
|
|
|
|
2003-08-31 20:26:21 +00:00
|
|
|
return insertChannel(handle, new ChannelRaw(this, handle, sound, size, rate, flags, volume, pan, id, loopStart, loopEnd));
|
2002-04-14 18:13:08 +00:00
|
|
|
}
|
|
|
|
|
2002-10-27 01:12:10 +00:00
|
|
|
#ifdef USE_MAD
|
2003-09-05 06:22:10 +00:00
|
|
|
int SoundMixer::playMP3(PlayingSoundHandle *handle, File *file, uint32 size, byte volume, int8 pan) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-05 06:22:10 +00:00
|
|
|
return insertChannel(handle, new ChannelMP3(this, handle, file, size, volume, pan));
|
2002-04-16 18:33:04 +00:00
|
|
|
}
|
2003-09-05 06:22:10 +00:00
|
|
|
int SoundMixer::playMP3CDTrack(PlayingSoundHandle *handle, File *file, mad_timer_t duration, byte volume, int8 pan) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-05 06:22:10 +00:00
|
|
|
return insertChannel(handle, new ChannelMP3CDMusic(this, handle, file, duration, volume, pan));
|
2002-04-17 20:23:45 +00:00
|
|
|
}
|
2002-04-16 18:33:04 +00:00
|
|
|
#endif
|
|
|
|
|
2002-10-27 01:12:10 +00:00
|
|
|
#ifdef USE_VORBIS
|
2003-09-05 06:22:10 +00:00
|
|
|
int SoundMixer::playVorbis(PlayingSoundHandle *handle, OggVorbis_File *ov_file, int duration, bool is_cd_track, byte volume, int8 pan) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-05 06:22:10 +00:00
|
|
|
return insertChannel(handle, new ChannelVorbis(this, handle, ov_file, duration, is_cd_track, volume, pan));
|
2002-10-27 01:12:10 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
void SoundMixer::mix(int16 *buf, uint len) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-07-14 20:09:14 +00:00
|
|
|
|
2003-09-16 07:06:17 +00:00
|
|
|
if (_premixProc && !_paused) {
|
2002-08-24 15:31:37 +00:00
|
|
|
_premixProc(_premixParam, buf, len);
|
2002-04-14 18:13:08 +00:00
|
|
|
} else {
|
2003-05-23 16:47:45 +00:00
|
|
|
// zero the buf out
|
2002-05-18 14:53:19 +00:00
|
|
|
memset(buf, 0, 2 * len * sizeof(int16));
|
2002-04-14 18:13:08 +00:00
|
|
|
}
|
|
|
|
|
2003-09-06 10:47:30 +00:00
|
|
|
if (!_paused) {
|
2003-06-21 23:29:34 +00:00
|
|
|
// now mix all channels
|
2003-05-23 16:47:45 +00:00
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++)
|
2003-09-02 13:48:20 +00:00
|
|
|
if (_channels[i] && !_channels[i]->isPaused())
|
2003-07-06 16:52:09 +00:00
|
|
|
_channels[i]->mix(buf, len);
|
2003-05-23 16:47:45 +00:00
|
|
|
}
|
2002-04-14 18:13:08 +00:00
|
|
|
}
|
|
|
|
|
2003-07-25 01:19:14 +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).
|
2002-07-07 18:04:03 +00:00
|
|
|
((SoundMixer *)s)->mix((int16 *)samples, len >> 2);
|
2002-04-14 18:13:08 +00:00
|
|
|
}
|
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
void SoundMixer::stopAll() {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2002-07-07 18:04:03 +00:00
|
|
|
for (int i = 0; i != NUM_CHANNELS; i++)
|
2002-04-14 18:13:08 +00:00
|
|
|
if (_channels[i])
|
|
|
|
_channels[i]->destroy();
|
|
|
|
}
|
|
|
|
|
2003-09-06 10:47:30 +00:00
|
|
|
void SoundMixer::stopChannel(int index) {
|
2002-10-15 21:55:04 +00:00
|
|
|
if ((index < 0) || (index >= NUM_CHANNELS)) {
|
|
|
|
warning("soundMixer::stop has invalid index %d", index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2002-04-27 07:42:14 +00:00
|
|
|
if (_channels[index])
|
|
|
|
_channels[index]->destroy();
|
|
|
|
}
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2003-03-18 21:46:44 +00:00
|
|
|
void SoundMixer::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-03-18 21:46:44 +00:00
|
|
|
if (_channels[i] != NULL && _channels[i]->_id == id) {
|
|
|
|
_channels[i]->destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-14 20:09:14 +00:00
|
|
|
void SoundMixer::stopHandle(PlayingSoundHandle handle) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-07-14 20:09:14 +00:00
|
|
|
|
|
|
|
// Simply ignore stop requests for handles of sounds that already terminated
|
2003-08-30 20:25:07 +00:00
|
|
|
if (handle == 0)
|
2003-07-14 20:09:14 +00:00
|
|
|
return;
|
|
|
|
|
2003-08-30 20:25:07 +00:00
|
|
|
int index = handle - 1;
|
2003-08-30 18:12:49 +00:00
|
|
|
|
2003-07-14 20:09:14 +00:00
|
|
|
if ((index < 0) || (index >= NUM_CHANNELS)) {
|
|
|
|
warning("soundMixer::stopHandle has invalid index %d", index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_channels[index])
|
|
|
|
_channels[index]->destroy();
|
|
|
|
}
|
|
|
|
|
2003-08-31 20:26:21 +00:00
|
|
|
void SoundMixer::setChannelVolume(PlayingSoundHandle handle, byte volume) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-08-31 20:26:21 +00:00
|
|
|
|
|
|
|
if (handle == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int index = handle - 1;
|
|
|
|
|
|
|
|
if ((index < 0) || (index >= NUM_CHANNELS)) {
|
|
|
|
warning("soundMixer::setChannelVolume has invalid index %d", index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_channels[index])
|
|
|
|
_channels[index]->setChannelVolume(volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundMixer::setChannelPan(PlayingSoundHandle handle, int8 pan) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-08-31 20:26:21 +00:00
|
|
|
|
|
|
|
if (handle == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int index = handle - 1;
|
|
|
|
|
|
|
|
if ((index < 0) || (index >= NUM_CHANNELS)) {
|
|
|
|
warning("soundMixer::setChannelVolume has invalid index %d", index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_channels[index])
|
|
|
|
_channels[index]->setChannelPan(pan);
|
|
|
|
}
|
|
|
|
|
2003-09-05 20:48:32 +00:00
|
|
|
void SoundMixer::pauseAll(bool paused) {
|
2003-09-06 10:47:30 +00:00
|
|
|
_paused = paused;
|
2003-08-09 19:19:27 +00:00
|
|
|
}
|
|
|
|
|
2003-09-02 13:48:20 +00:00
|
|
|
void SoundMixer::pauseChannel(int index, bool paused) {
|
|
|
|
if ((index < 0) || (index >= NUM_CHANNELS)) {
|
|
|
|
warning("soundMixer::pauseChannel has invalid index %d", index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-02 13:48:20 +00:00
|
|
|
if (_channels[index])
|
|
|
|
_channels[index]->pause(paused);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundMixer::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++) {
|
|
|
|
if (_channels[i] != NULL && _channels[i]->_id == id) {
|
|
|
|
_channels[i]->pause(paused);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-02 22:38:53 +00:00
|
|
|
void SoundMixer::pauseHandle(PlayingSoundHandle handle, bool paused) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-02 13:48:20 +00:00
|
|
|
|
|
|
|
// Simply ignore pause/unpause requests for handles of sound that alreayd terminated
|
|
|
|
if (handle == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int index = handle - 1;
|
|
|
|
|
|
|
|
if ((index < 0) || (index >= NUM_CHANNELS)) {
|
|
|
|
warning("soundMixer::pauseHandle has invalid index %d", index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_channels[index])
|
2003-09-02 22:38:53 +00:00
|
|
|
_channels[index]->pause(paused);
|
2003-09-02 13:48:20 +00:00
|
|
|
}
|
|
|
|
|
2003-06-22 01:55:53 +00:00
|
|
|
bool SoundMixer::hasActiveSFXChannel() {
|
|
|
|
// FIXME/TODO: We need to distinguish between SFX and music channels
|
|
|
|
// (and maybe also voice) here to work properly in iMuseDigital
|
|
|
|
// games. In the past that was achieve using the _beginSlots hack.
|
|
|
|
// Since we don't have that anymore, it's not that simple anymore.
|
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-06-22 01:55:53 +00:00
|
|
|
if (_channels[i] && !_channels[i]->isMusicChannel())
|
2002-04-14 18:13:08 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
void SoundMixer::setVolume(int volume) {
|
2002-07-28 15:03:45 +00:00
|
|
|
// Check range
|
|
|
|
if (volume > 256)
|
|
|
|
volume = 256;
|
|
|
|
else if (volume < 0)
|
|
|
|
volume = 0;
|
|
|
|
|
2003-07-25 01:19:14 +00:00
|
|
|
_globalVolume = volume;
|
2002-04-14 18:13:08 +00:00
|
|
|
}
|
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
void SoundMixer::setMusicVolume(int volume) {
|
2002-07-28 15:03:45 +00:00
|
|
|
// Check range
|
|
|
|
if (volume > 256)
|
|
|
|
volume = 256;
|
|
|
|
else if (volume < 0)
|
|
|
|
volume = 0;
|
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
_musicVolume = volume;
|
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 -
|
|
|
|
|
|
|
|
|
|
|
|
Channel::~Channel() {
|
|
|
|
delete _converter;
|
|
|
|
delete _input;
|
|
|
|
if (_handle)
|
2003-08-30 20:25:07 +00:00
|
|
|
*_handle = 0;
|
2003-08-05 23:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Channel::destroy() {
|
|
|
|
for (int i = 0; i != SoundMixer::NUM_CHANNELS; i++)
|
|
|
|
if (_mixer->_channels[i] == this)
|
|
|
|
_mixer->_channels[i] = 0;
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
if (_input->eos()) {
|
|
|
|
// TODO: call drain method
|
|
|
|
destroy();
|
|
|
|
} else {
|
|
|
|
assert(_converter);
|
2003-09-05 23:27:11 +00:00
|
|
|
|
|
|
|
// The pan value ranges from -127 to +127. That's 255 different values.
|
|
|
|
// From the channel pan/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 254 is there
|
|
|
|
// because the maximal left/right pan value is 2*127 = 254.
|
|
|
|
// The value getVolume() returns is in the range 0 - 256.
|
|
|
|
// Hence, the vol_l/vol_r values will be in that range, too
|
|
|
|
|
|
|
|
int vol = getVolume() * _volume;
|
|
|
|
st_volume_t vol_l = (127 - _pan) * vol / (255 * 254);
|
|
|
|
st_volume_t vol_r = (127 + _pan) * vol / (255 * 254);
|
|
|
|
|
|
|
|
_converter->flow(*_input, data, len, vol_l, vol_r);
|
2003-08-05 23:03:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-05 02:05:37 +00:00
|
|
|
/* RAW mixer */
|
2003-08-31 20:26:21 +00:00
|
|
|
ChannelRaw::ChannelRaw(SoundMixer *mixer, PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, byte volume, int8 pan, int id, uint32 loopStart, uint32 loopEnd)
|
2003-08-05 02:05:37 +00:00
|
|
|
: Channel(mixer, handle) {
|
|
|
|
_id = id;
|
|
|
|
_ptr = (byte *)sound;
|
2003-08-31 20:26:21 +00:00
|
|
|
_volume = volume;
|
|
|
|
_pan = pan;
|
2003-08-05 02:05:37 +00:00
|
|
|
|
|
|
|
// Create the input stream
|
|
|
|
if (flags & SoundMixer::FLAG_LOOP) {
|
|
|
|
if (loopEnd == 0) {
|
|
|
|
_input = makeLinearInputStream(flags, _ptr, size, 0, size);
|
|
|
|
} else {
|
|
|
|
assert(loopStart < loopEnd && loopEnd <= size);
|
|
|
|
_input = makeLinearInputStream(flags, _ptr, size, loopStart, loopEnd - loopStart);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_input = makeLinearInputStream(flags, _ptr, size, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a rate converter instance
|
|
|
|
_converter = makeRateConverter(rate, mixer->getOutputRate(), _input->isStereo(), (flags & SoundMixer::FLAG_REVERSE_STEREO) != 0);
|
|
|
|
|
|
|
|
if (!(flags & SoundMixer::FLAG_AUTOFREE))
|
|
|
|
_ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChannelRaw::~ChannelRaw() {
|
|
|
|
free(_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
ChannelStream::ChannelStream(SoundMixer *mixer, PlayingSoundHandle *handle,
|
|
|
|
void *sound, uint32 size, uint rate,
|
2003-08-31 20:26:21 +00:00
|
|
|
byte flags, uint32 buffer_size, byte volume, int8 pan)
|
2003-08-05 02:05:37 +00:00
|
|
|
: Channel(mixer, handle) {
|
2003-08-31 20:26:21 +00:00
|
|
|
_volume = volume;
|
|
|
|
_pan = pan;
|
2003-08-05 02:05:37 +00:00
|
|
|
assert(size <= buffer_size);
|
|
|
|
|
|
|
|
// Create the input stream
|
|
|
|
_input = makeWrappedInputStream(flags, buffer_size);
|
|
|
|
|
|
|
|
// Append the initial data
|
|
|
|
((WrappedAudioInputStream *)_input)->append((const byte *)sound, size);
|
|
|
|
|
|
|
|
// Get a rate converter instance
|
|
|
|
_converter = makeRateConverter(rate, mixer->getOutputRate(), _input->isStereo(), (flags & SoundMixer::FLAG_REVERSE_STEREO) != 0);
|
|
|
|
|
|
|
|
_finished = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelStream::append(void *data, uint32 len) {
|
|
|
|
((WrappedAudioInputStream *)_input)->append((const byte *)data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelStream::mix(int16 *data, uint len) {
|
|
|
|
assert(_input);
|
|
|
|
if (_input->eos()) {
|
|
|
|
// TODO: call drain method
|
|
|
|
|
|
|
|
// Normally, the stream stays around even if all its data is used up.
|
|
|
|
// This is in case more data is streamed into it. To make the stream
|
|
|
|
// go away, one can either stop() it (which takes effect immediately,
|
|
|
|
// ignoring any remaining sound data), or finish() it, which means
|
|
|
|
// it will finish playing before it terminates itself.
|
|
|
|
if (_finished) {
|
|
|
|
destroy();
|
|
|
|
}
|
2003-09-05 23:27:11 +00:00
|
|
|
} else {
|
|
|
|
// Invoke the parent implementation.
|
|
|
|
Channel::mix(data, len);
|
2003-08-05 02:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_MAD
|
2003-09-05 06:22:10 +00:00
|
|
|
ChannelMP3::ChannelMP3(SoundMixer *mixer, PlayingSoundHandle *handle, File *file, uint size, byte volume, int8 pan)
|
2003-08-05 02:05:37 +00:00
|
|
|
: Channel(mixer, handle) {
|
2003-09-05 06:22:10 +00:00
|
|
|
_volume = volume;
|
|
|
|
_pan = pan;
|
2003-08-05 02:05:37 +00:00
|
|
|
// Create the input stream
|
|
|
|
_input = makeMP3Stream(file, mad_timer_zero, size);
|
|
|
|
|
|
|
|
// Get a rate converter instance
|
|
|
|
_converter = makeRateConverter(_input->getRate(), mixer->getOutputRate(), _input->isStereo());
|
|
|
|
}
|
|
|
|
|
2003-09-05 06:22:10 +00:00
|
|
|
ChannelMP3CDMusic::ChannelMP3CDMusic(SoundMixer *mixer, PlayingSoundHandle *handle, File *file, mad_timer_t duration, byte volume, int8 pan)
|
2003-08-05 02:05:37 +00:00
|
|
|
: Channel(mixer, handle) {
|
2003-09-05 06:22:10 +00:00
|
|
|
_volume = volume;
|
|
|
|
_pan = pan;
|
2003-08-05 02:05:37 +00:00
|
|
|
// Create the input stream
|
|
|
|
_input = makeMP3Stream(file, duration, 0);
|
|
|
|
|
|
|
|
// Get a rate converter instance
|
|
|
|
_converter = makeRateConverter(_input->getRate(), mixer->getOutputRate(), _input->isStereo());
|
|
|
|
}
|
|
|
|
#endif // USE_MAD
|
|
|
|
|
|
|
|
#ifdef USE_VORBIS
|
2003-09-05 06:22:10 +00:00
|
|
|
ChannelVorbis::ChannelVorbis(SoundMixer *mixer, PlayingSoundHandle *handle, OggVorbis_File *ov_file, int duration, bool is_cd_track, byte volume, int8 pan)
|
2003-08-05 02:05:37 +00:00
|
|
|
: Channel(mixer, handle) {
|
2003-09-05 06:22:10 +00:00
|
|
|
_volume = volume;
|
|
|
|
_pan = pan;
|
2003-08-05 02:05:37 +00:00
|
|
|
// Create the input stream
|
|
|
|
_input = makeVorbisStream(ov_file, duration);
|
|
|
|
|
|
|
|
// Get a rate converter instance
|
|
|
|
_converter = makeRateConverter(_input->getRate(), mixer->getOutputRate(), _input->isStereo());
|
|
|
|
_is_cd_track = is_cd_track;
|
|
|
|
}
|
|
|
|
#endif // USE_VORBIS
|