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
|
|
|
|
|
|
|
|
#ifndef SOUND_MIXER_H
|
|
|
|
#define SOUND_MIXER_H
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../bits.h"
|
|
|
|
#include <SDL.h>
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
class AudioStream;
|
2003-12-12 21:17:31 +00:00
|
|
|
class Channel;
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
class PlayingSoundHandle {
|
2003-12-12 21:17:31 +00:00
|
|
|
friend class Channel;
|
2004-02-01 10:48:44 +00:00
|
|
|
friend class SoundMixer;
|
|
|
|
int val;
|
|
|
|
int getIndex() const { return val - 1; }
|
|
|
|
void setIndex(int i) { val = i + 1; }
|
|
|
|
void resetIndex() { val = 0; }
|
|
|
|
public:
|
|
|
|
PlayingSoundHandle() { resetIndex(); }
|
|
|
|
bool isActive() const { return val > 0; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class SoundMixer {
|
2003-12-12 21:17:31 +00:00
|
|
|
public:
|
|
|
|
enum {
|
2004-12-10 21:13:03 +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-12-12 21:17:31 +00:00
|
|
|
};
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
private:
|
2003-12-12 21:17:31 +00:00
|
|
|
enum {
|
2004-12-10 21:13:03 +00:00
|
|
|
NUM_CHANNELS = 16
|
2003-12-12 21:17:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MutexRef _mutex;
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
Channel *_premixChannel;
|
2003-12-12 21:17:31 +00:00
|
|
|
|
|
|
|
uint _outputRate;
|
2004-12-10 21:13:03 +00:00
|
|
|
|
2003-12-12 21:17:31 +00:00
|
|
|
int _globalVolume;
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
bool _paused;
|
|
|
|
|
2003-12-12 21:17:31 +00:00
|
|
|
Channel *_channels[NUM_CHANNELS];
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
bool _mixerReady;
|
|
|
|
|
2003-12-12 21:17:31 +00:00
|
|
|
public:
|
|
|
|
SoundMixer();
|
|
|
|
~SoundMixer();
|
|
|
|
|
2004-12-10 21:13:03 +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...
|
|
|
|
*
|
|
|
|
* @return whether the mixer is ready and setup
|
|
|
|
*/
|
2004-02-01 10:48:44 +00:00
|
|
|
bool isReady() const { return _mixerReady; };
|
2003-12-12 21:17:31 +00:00
|
|
|
|
|
|
|
|
2004-02-01 10:48:44 +00:00
|
|
|
|
2004-12-10 21:13:03 +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.
|
|
|
|
*/
|
|
|
|
void setupPremix(AudioStream *stream);
|
2003-12-12 21:17:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2004-12-10 21:13:03 +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.
|
|
|
|
*/
|
|
|
|
void playRaw(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags,
|
|
|
|
int id = -1, byte volume = 255, int8 balance = 0,
|
|
|
|
uint32 loopStart = 0, uint32 loopEnd = 0);
|
2003-12-12 21:17:31 +00:00
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
/**
|
|
|
|
* Start playing the given audio input stream.
|
|
|
|
*/
|
|
|
|
void playInputStream(PlayingSoundHandle *handle, AudioStream *input, bool isMusic,
|
|
|
|
int id = -1, byte volume = 255, int8 balance = 0,
|
|
|
|
bool autofreeStream = true, bool permanent = false);
|
2003-12-12 21:17:31 +00:00
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop all currently playing sounds.
|
|
|
|
*/
|
|
|
|
void stopAll(bool force = false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop playing the sound with given ID.
|
|
|
|
*
|
|
|
|
* @param id the ID of the sound to affect
|
|
|
|
*/
|
2003-12-12 21:17:31 +00:00
|
|
|
void stopID(int id);
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
/**
|
|
|
|
* Stop playing the sound corresponding to the given handle.
|
|
|
|
*
|
|
|
|
* @param handle the sound to affect
|
|
|
|
*/
|
2003-12-12 21:17:31 +00:00
|
|
|
void stopHandle(PlayingSoundHandle handle);
|
|
|
|
|
2004-12-10 21:13:03 +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-12-12 21:17:31 +00:00
|
|
|
void pauseAll(bool paused);
|
|
|
|
|
2004-12-10 21:13:03 +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
|
|
|
|
*/
|
2003-12-12 21:17:31 +00:00
|
|
|
void pauseID(int id, bool paused);
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2003-12-12 21:17:31 +00:00
|
|
|
void pauseHandle(PlayingSoundHandle handle, bool paused);
|
|
|
|
|
2004-12-10 21:13:03 +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
|
|
|
|
*/
|
|
|
|
bool isSoundIDActive(int id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the mixer is paused (using pauseAll).
|
|
|
|
*
|
|
|
|
* @return true if the mixer is paused
|
|
|
|
*/
|
|
|
|
bool isPaused();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the channel volume for the given handle.
|
|
|
|
*
|
|
|
|
* @param handle the sound to affect
|
|
|
|
* @param volume the new channel volume (0 - 255)
|
|
|
|
*/
|
2003-12-12 21:17:31 +00:00
|
|
|
void setChannelVolume(PlayingSoundHandle handle, byte volume);
|
|
|
|
|
2004-12-10 21:13:03 +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)
|
|
|
|
*/
|
2004-02-01 10:48:44 +00:00
|
|
|
void setChannelBalance(PlayingSoundHandle handle, int8 balance);
|
2003-12-12 21:17:31 +00:00
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
/**
|
|
|
|
* Get approximation of for how long the channel has been playing.
|
|
|
|
*/
|
|
|
|
uint32 getSoundElapsedTime(PlayingSoundHandle handle);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the global volume.
|
|
|
|
*
|
|
|
|
* @param volume the new global volume, 0-256
|
|
|
|
*/
|
2003-12-12 21:17:31 +00:00
|
|
|
void setVolume(int volume);
|
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
/**
|
|
|
|
* Query the global volume.
|
|
|
|
*
|
|
|
|
* @return the global music volume, 0-256
|
|
|
|
*/
|
2003-12-12 21:17:31 +00:00
|
|
|
int getVolume() const { return _globalVolume; }
|
|
|
|
|
2004-12-10 21:13:03 +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-12-12 21:17:31 +00:00
|
|
|
uint getOutputRate() const { return _outputRate; }
|
|
|
|
|
|
|
|
private:
|
2004-12-10 21:13:03 +00:00
|
|
|
typedef void (*SoundProc)(void *param, byte *buf, int len);
|
2004-02-01 10:48:44 +00:00
|
|
|
bool setSoundProc(SoundProc proc, void *param);
|
|
|
|
|
|
|
|
void insertChannel(PlayingSoundHandle *handle, Channel *chan);
|
2003-12-12 21:17:31 +00:00
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
/**
|
|
|
|
* Internal main method -- all the actual mixing work is done from here.
|
|
|
|
*/
|
2004-02-01 10:48:44 +00:00
|
|
|
void mix(int16 * buf, uint len);
|
2003-12-12 21:17:31 +00:00
|
|
|
|
2004-12-10 21:13:03 +00:00
|
|
|
/**
|
|
|
|
* The mixer callback function, passed on to OSystem::setSoundCallback().
|
|
|
|
* This simply calls the mix() method.
|
|
|
|
*/
|
2003-12-12 21:17:31 +00:00
|
|
|
static void mixCallback(void *s, byte *samples, int len);
|
|
|
|
};
|
|
|
|
|
2005-01-01 10:23:18 +00:00
|
|
|
extern SoundMixer *g_mixer;
|
|
|
|
|
2003-12-12 21:17:31 +00:00
|
|
|
#endif
|