2002-04-21 17:46:42 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
|
|
|
* Copyright (C) 2001 Ludvig Strigeus
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2001-2006 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
|
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-08-05 23:03:42 +00:00
|
|
|
#ifndef SOUND_MIXER_H
|
|
|
|
#define SOUND_MIXER_H
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2005-06-24 15:23:51 +00:00
|
|
|
#include "common/stdafx.h"
|
2003-09-07 20:23:38 +00:00
|
|
|
#include "common/scummsys.h"
|
2005-01-10 22:06:49 +00:00
|
|
|
#include "common/mutex.h"
|
2003-09-07 20:23:38 +00:00
|
|
|
|
2003-06-15 01:56:47 +00:00
|
|
|
|
2005-01-10 22:06:49 +00:00
|
|
|
class OSystem;
|
2002-04-17 20:23:45 +00:00
|
|
|
|
2005-05-11 00:01:44 +00:00
|
|
|
|
|
|
|
namespace Audio {
|
|
|
|
|
2006-04-29 22:33:31 +00:00
|
|
|
class AudioStream;
|
2005-05-11 00:01:44 +00:00
|
|
|
class Channel;
|
|
|
|
class Mixer;
|
|
|
|
|
2005-03-22 18:29:02 +00:00
|
|
|
class SoundHandle {
|
2006-10-21 12:44:10 +00:00
|
|
|
friend class Channel;
|
|
|
|
friend class Mixer;
|
2005-03-22 18:29:02 +00:00
|
|
|
uint32 _val;
|
|
|
|
public:
|
|
|
|
inline SoundHandle() : _val(0xFFFFFFFF) {}
|
|
|
|
};
|
|
|
|
|
2005-05-10 23:48:48 +00:00
|
|
|
class Mixer {
|
2003-06-15 01:56:47 +00:00
|
|
|
public:
|
2003-07-25 01:19:14 +00:00
|
|
|
enum {
|
2004-11-27 16:26:54 +00:00
|
|
|
/** unsigned samples (default: signed) */
|
|
|
|
FLAG_UNSIGNED = 1 << 0,
|
|
|
|
|
|
|
|
/** sound is 16 bits wide (default: 8bit) */
|
|
|
|
FLAG_16BITS = 1 << 1,
|
|
|
|
|
|
|
|
/** sample is little endian (default: big endian) */
|
|
|
|
FLAG_LITTLE_ENDIAN = 1 << 2,
|
|
|
|
|
|
|
|
/** sound is in stereo (default: mono) */
|
|
|
|
FLAG_STEREO = 1 << 3,
|
|
|
|
|
|
|
|
/** reverse the left and right stereo channel */
|
|
|
|
FLAG_REVERSE_STEREO = 1 << 4,
|
|
|
|
|
|
|
|
/** sound buffer is freed automagically at the end of playing */
|
|
|
|
FLAG_AUTOFREE = 1 << 5,
|
|
|
|
|
|
|
|
/** loop the audio */
|
|
|
|
FLAG_LOOP = 1 << 6
|
2003-07-25 01:19:14 +00:00
|
|
|
};
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-12-27 00:27:00 +00:00
|
|
|
enum SoundType {
|
2005-03-09 18:12:54 +00:00
|
|
|
kPlainSoundType = 0,
|
2004-12-27 00:27:00 +00:00
|
|
|
|
2005-03-09 18:12:54 +00:00
|
|
|
kMusicSoundType = 1,
|
|
|
|
kSFXSoundType = 2,
|
|
|
|
kSpeechSoundType = 3
|
2004-12-27 00:27:00 +00:00
|
|
|
};
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-12-27 23:33:19 +00:00
|
|
|
enum {
|
|
|
|
kMaxChannelVolume = 255,
|
|
|
|
kMaxMixerVolume = 256
|
|
|
|
};
|
2003-07-25 01:19:14 +00:00
|
|
|
|
2003-06-15 01:56:47 +00:00
|
|
|
private:
|
2004-11-28 23:24:32 +00:00
|
|
|
enum {
|
|
|
|
NUM_CHANNELS = 16
|
|
|
|
};
|
|
|
|
|
2003-03-06 18:30:44 +00:00
|
|
|
OSystem *_syst;
|
2005-01-28 22:05:51 +00:00
|
|
|
Common::Mutex _mutex;
|
2002-07-07 18:04:03 +00:00
|
|
|
|
2004-10-11 22:01:21 +00:00
|
|
|
Channel *_premixChannel;
|
2003-06-15 01:56:47 +00:00
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
uint _outputRate;
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2004-12-27 00:27:00 +00:00
|
|
|
int _volumeForSoundType[4];
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2003-09-06 10:47:30 +00:00
|
|
|
bool _paused;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-03-12 20:26:49 +00:00
|
|
|
uint32 _handleSeed;
|
2003-03-06 18:30:44 +00:00
|
|
|
Channel *_channels[NUM_CHANNELS];
|
2002-06-03 21:20:11 +00:00
|
|
|
|
2003-12-24 17:42:22 +00:00
|
|
|
bool _mixerReady;
|
|
|
|
|
2003-06-15 01:56:47 +00:00
|
|
|
public:
|
2005-05-10 23:48:48 +00:00
|
|
|
Mixer();
|
|
|
|
~Mixer();
|
2002-08-18 21:42:22 +00:00
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
|
|
|
|
|
2003-12-24 17:42:22 +00:00
|
|
|
/**
|
|
|
|
* Is the mixer ready and setup? This may not be the case on systems which
|
|
|
|
* don't support digital sound output. In that case, the mixer proc may
|
|
|
|
* never be called. That in turn can cause breakage in games which use the
|
|
|
|
* premix callback for syncing. In particular, the Adlib MIDI emulation...
|
2004-11-27 15:58:18 +00:00
|
|
|
*
|
|
|
|
* @return whether the mixer is ready and setup
|
2003-12-24 17:42:22 +00:00
|
|
|
*/
|
|
|
|
bool isReady() const { return _mixerReady; };
|
2003-09-05 20:48:32 +00:00
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
|
|
|
|
|
2004-10-11 22:01:21 +00:00
|
|
|
/**
|
|
|
|
* Set the premix stream. This is mainly used for the adlib music, but
|
|
|
|
* is not limited to it. The premix stream is invoked by the mixer whenever
|
|
|
|
* it needs to generate any data, before any other mixing takes place.
|
|
|
|
*/
|
2005-03-09 18:12:54 +00:00
|
|
|
void setupPremix(AudioStream *stream, SoundType type = kPlainSoundType);
|
2004-10-11 22:01:21 +00:00
|
|
|
|
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start playing the given raw sound data.
|
|
|
|
* Internally, this simply creates an audio input stream wrapping the data
|
|
|
|
* (using the makeLinearInputStream factory function), which is then
|
|
|
|
* passed on to playInputStream.
|
|
|
|
*/
|
2005-03-12 18:56:09 +00:00
|
|
|
void playRaw(SoundHandle *handle,
|
2004-12-27 00:27:00 +00:00
|
|
|
void *sound, uint32 size, uint rate, byte flags,
|
2004-11-28 23:02:28 +00:00
|
|
|
int id = -1, byte volume = 255, int8 balance = 0,
|
2004-12-27 00:27:00 +00:00
|
|
|
uint32 loopStart = 0, uint32 loopEnd = 0,
|
2005-03-09 18:12:54 +00:00
|
|
|
SoundType type = kSFXSoundType);
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
/**
|
|
|
|
* Start playing the given audio input stream.
|
|
|
|
*/
|
2005-03-12 18:56:09 +00:00
|
|
|
void playInputStream(SoundType type, SoundHandle *handle, AudioStream *input,
|
2004-11-28 23:02:28 +00:00
|
|
|
int id = -1, byte volume = 255, int8 balance = 0,
|
|
|
|
bool autofreeStream = true, bool permanent = false);
|
2003-12-19 00:32:47 +00:00
|
|
|
|
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop all currently playing sounds.
|
|
|
|
*/
|
2004-11-28 22:15:09 +00:00
|
|
|
void stopAll(bool force = false);
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
/**
|
|
|
|
* Stop playing the sound with given ID.
|
|
|
|
*
|
|
|
|
* @param id the ID of the sound to affect
|
|
|
|
*/
|
2003-03-18 21:46:44 +00:00
|
|
|
void stopID(int id);
|
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
/**
|
|
|
|
* Stop playing the sound corresponding to the given handle.
|
|
|
|
*
|
|
|
|
* @param handle the sound to affect
|
|
|
|
*/
|
2005-03-12 18:56:09 +00:00
|
|
|
void stopHandle(SoundHandle handle);
|
2003-07-14 20:09:14 +00:00
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pause/unpause the mixer (this temporarily stops all audio processing,
|
|
|
|
* including all regular channels and the premix channel).
|
|
|
|
*
|
|
|
|
* @param paused true to pause the mixer, false to unpause it
|
|
|
|
*/
|
2003-09-05 20:48:32 +00:00
|
|
|
void pauseAll(bool paused);
|
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
/**
|
|
|
|
* Pause/unpause the sound with the given ID.
|
|
|
|
*
|
|
|
|
* @param id the ID of the sound to affect
|
|
|
|
* @param paused true to pause the sound, false to unpause it
|
|
|
|
*/
|
|
|
|
void pauseID(int id, bool paused);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pause/unpause the sound corresponding to the given handle.
|
|
|
|
*
|
|
|
|
* @param handle the sound to affect
|
|
|
|
* @param paused true to pause the sound, false to unpause it
|
|
|
|
*/
|
2005-03-12 18:56:09 +00:00
|
|
|
void pauseHandle(SoundHandle handle, bool paused);
|
2004-11-27 15:58:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a sound with the given ID is active.
|
|
|
|
*
|
|
|
|
* @param id the ID of the sound to query
|
|
|
|
* @return true if the sound is active
|
|
|
|
*/
|
2004-09-19 12:22:47 +00:00
|
|
|
bool isSoundIDActive(int id);
|
|
|
|
|
2005-04-04 11:43:25 +00:00
|
|
|
/**
|
|
|
|
* Get the sound ID of handle sound
|
|
|
|
*
|
|
|
|
* @param handle sound to query
|
|
|
|
* @return sound ID if active
|
|
|
|
*/
|
2005-04-04 17:43:00 +00:00
|
|
|
int getSoundID(SoundHandle handle);
|
2005-04-04 11:43:25 +00:00
|
|
|
|
2005-03-12 18:56:09 +00:00
|
|
|
/**
|
|
|
|
* Check if a sound with the given hANDLE is active.
|
|
|
|
*
|
2005-04-04 11:43:25 +00:00
|
|
|
* @param handle sound to query
|
2005-03-12 18:56:09 +00:00
|
|
|
* @return true if the sound is active
|
|
|
|
*/
|
2005-03-12 20:26:49 +00:00
|
|
|
bool isSoundHandleActive(SoundHandle handle);
|
2005-03-12 18:56:09 +00:00
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
/**
|
|
|
|
* Check if the mixer is paused (using pauseAll).
|
|
|
|
*
|
|
|
|
* @return true if the mixer is paused
|
|
|
|
*/
|
2004-09-04 13:13:23 +00:00
|
|
|
bool isPaused();
|
|
|
|
|
2003-09-02 13:48:20 +00:00
|
|
|
|
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
/**
|
|
|
|
* Set the channel volume for the given handle.
|
|
|
|
*
|
|
|
|
* @param handle the sound to affect
|
|
|
|
* @param volume the new channel volume (0 - 255)
|
|
|
|
*/
|
2005-03-12 18:56:09 +00:00
|
|
|
void setChannelVolume(SoundHandle handle, byte volume);
|
2003-08-31 20:26:21 +00:00
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
/**
|
|
|
|
* Set the channel balance for the given handle.
|
|
|
|
*
|
|
|
|
* @param handle the sound to affect
|
|
|
|
* @param balance the new channel balance:
|
|
|
|
* (-127 ... 0 ... 127) corresponds to (left ... center ... right)
|
|
|
|
*/
|
2005-03-12 18:56:09 +00:00
|
|
|
void setChannelBalance(SoundHandle handle, int8 balance);
|
2003-08-31 20:26:21 +00:00
|
|
|
|
2005-01-30 11:03:49 +00:00
|
|
|
/**
|
|
|
|
* Get approximation of for how long the Sound ID has been playing.
|
|
|
|
*/
|
|
|
|
uint32 getSoundElapsedTimeOfSoundID(int id);
|
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
/**
|
|
|
|
* Get approximation of for how long the channel has been playing.
|
|
|
|
*/
|
2005-03-12 18:56:09 +00:00
|
|
|
uint32 getSoundElapsedTime(SoundHandle handle);
|
2004-02-12 16:25:28 +00:00
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
/**
|
2004-12-27 00:27:00 +00:00
|
|
|
* Check whether any channel of the given sound type is active.
|
|
|
|
* For example, this can be used to check whether any SFX sound
|
2005-03-09 18:12:54 +00:00
|
|
|
* is currently playing, by checking for type kSFXSoundType.
|
2004-11-27 15:58:18 +00:00
|
|
|
*
|
2004-12-27 00:27:00 +00:00
|
|
|
* @param type the sound type to look for
|
|
|
|
* @return true if any channels of the specified type are active.
|
2004-11-27 15:58:18 +00:00
|
|
|
*/
|
2004-12-27 00:27:00 +00:00
|
|
|
bool hasActiveChannelOfType(SoundType type);
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
/**
|
2004-12-27 00:27:00 +00:00
|
|
|
* Set the volume for the given sound type.
|
2004-11-27 15:58:18 +00:00
|
|
|
*
|
2005-05-05 15:59:24 +00:00
|
|
|
* @param type the sound type
|
2005-02-06 20:35:06 +00:00
|
|
|
* @param volume the new global volume, 0-kMaxMixerVolume
|
2004-11-27 15:58:18 +00:00
|
|
|
*/
|
2004-12-27 00:27:00 +00:00
|
|
|
void setVolumeForSoundType(SoundType type, int volume);
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
/**
|
|
|
|
* Query the global volume.
|
|
|
|
*
|
2005-05-05 15:59:24 +00:00
|
|
|
* @param type the sound type
|
2005-02-06 20:35:06 +00:00
|
|
|
* @return the global music volume, 0-kMaxMixerVolume
|
2004-11-27 15:58:18 +00:00
|
|
|
*/
|
2004-12-27 00:27:00 +00:00
|
|
|
int getVolumeForSoundType(SoundType type) const;
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
/**
|
|
|
|
* Query the system's audio output sample rate. This returns
|
|
|
|
* the same value as OSystem::getOutputSampleRate().
|
|
|
|
*
|
|
|
|
* @return the output sample rate in Hz
|
|
|
|
*/
|
2003-07-25 01:19:14 +00:00
|
|
|
uint getOutputRate() const { return _outputRate; }
|
2002-05-01 22:22:22 +00:00
|
|
|
|
2003-06-21 23:29:34 +00:00
|
|
|
private:
|
2005-03-12 18:56:09 +00:00
|
|
|
void insertChannel(SoundHandle *handle, Channel *chan);
|
2003-07-06 17:00:09 +00:00
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
/**
|
|
|
|
* Internal main method -- all the actual mixing work is done from here.
|
|
|
|
*/
|
2003-07-06 17:00:09 +00:00
|
|
|
void mix(int16 * buf, uint len);
|
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
/**
|
|
|
|
* The mixer callback function, passed on to OSystem::setSoundCallback().
|
|
|
|
* This simply calls the mix() method.
|
|
|
|
*/
|
2003-07-25 01:19:14 +00:00
|
|
|
static void mixCallback(void *s, byte *samples, int len);
|
2002-04-14 18:13:08 +00:00
|
|
|
};
|
|
|
|
|
2005-05-10 23:48:48 +00:00
|
|
|
|
|
|
|
} // End of namespace Audio
|
|
|
|
|
2002-08-18 16:10:38 +00:00
|
|
|
#endif
|