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$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-08-05 23:03:42 +00:00
|
|
|
#ifndef SOUND_MIXER_H
|
|
|
|
#define SOUND_MIXER_H
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2003-08-01 12:21:04 +00:00
|
|
|
#include "stdafx.h"
|
2003-09-07 20:23:38 +00:00
|
|
|
#include "common/scummsys.h"
|
|
|
|
#include "common/system.h"
|
|
|
|
|
2002-10-27 01:12:10 +00:00
|
|
|
#ifdef USE_MAD
|
2002-04-16 18:33:04 +00:00
|
|
|
#include <mad.h>
|
|
|
|
#endif
|
2002-10-27 01:12:10 +00:00
|
|
|
#ifdef USE_VORBIS
|
|
|
|
#include <vorbis/vorbisfile.h>
|
|
|
|
#endif
|
2002-09-08 01:08:12 +00:00
|
|
|
|
2003-06-15 01:56:47 +00:00
|
|
|
|
2003-08-30 20:36:44 +00:00
|
|
|
typedef uint32 PlayingSoundHandle;
|
2002-08-24 15:31:37 +00:00
|
|
|
|
2003-06-21 23:29:34 +00:00
|
|
|
class Channel;
|
2002-09-02 07:53:43 +00:00
|
|
|
class File;
|
2002-04-17 20:23:45 +00:00
|
|
|
|
2003-06-15 01:56:47 +00:00
|
|
|
class SoundMixer {
|
2003-07-25 01:19:14 +00:00
|
|
|
friend class Channel;
|
2003-06-15 01:56:47 +00:00
|
|
|
public:
|
|
|
|
typedef void PremixProc (void *param, int16 *data, uint len);
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2003-06-15 01:56:47 +00:00
|
|
|
enum {
|
|
|
|
NUM_CHANNELS = 16
|
2002-10-27 01:12:10 +00:00
|
|
|
};
|
|
|
|
|
2003-07-25 01:19:14 +00:00
|
|
|
enum {
|
|
|
|
FLAG_UNSIGNED = 1 << 0, // unsigned samples (default: signed)
|
|
|
|
FLAG_STEREO = 1 << 1, // sound is in stereo (default: mono)
|
|
|
|
FLAG_16BITS = 1 << 2, // sound is 16 bits wide (default: 8bit)
|
|
|
|
FLAG_AUTOFREE = 1 << 3, // sound buffer is freed automagically at the end of playing
|
|
|
|
FLAG_REVERSE_STEREO = 1 << 4, // reverse the left and right stereo channel
|
2003-12-16 02:11:04 +00:00
|
|
|
FLAG_LOOP = 1 << 5, // loop the audio
|
|
|
|
FLAG_LITTLE_ENDIAN = 1 << 6 // sample is little endian (default: big endian)
|
2003-10-24 23:09:01 +00:00
|
|
|
|
2003-07-25 01:19:14 +00:00
|
|
|
};
|
|
|
|
|
2003-06-15 01:56:47 +00:00
|
|
|
private:
|
2003-03-06 18:30:44 +00:00
|
|
|
OSystem *_syst;
|
2003-07-05 15:19:11 +00:00
|
|
|
OSystem::MutexRef _mutex;
|
2002-07-07 18:04:03 +00:00
|
|
|
|
2003-06-15 01:56:47 +00:00
|
|
|
void *_premixParam;
|
|
|
|
PremixProc *_premixProc;
|
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
uint _outputRate;
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2003-07-25 01:19:14 +00:00
|
|
|
int _globalVolume;
|
2002-08-24 15:31:37 +00:00
|
|
|
int _musicVolume;
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2003-09-06 10:47:30 +00:00
|
|
|
bool _paused;
|
2002-05-01 22:22:22 +00:00
|
|
|
|
2003-03-06 18:30:44 +00:00
|
|
|
Channel *_channels[NUM_CHANNELS];
|
2002-06-03 21:20:11 +00:00
|
|
|
|
2003-06-15 01:56:47 +00:00
|
|
|
public:
|
2002-08-18 21:42:22 +00:00
|
|
|
SoundMixer();
|
|
|
|
~SoundMixer();
|
|
|
|
|
2003-09-05 20:48:32 +00:00
|
|
|
/** bind to the OSystem object => mixer will be
|
|
|
|
* invoked automatically when samples need
|
|
|
|
* to be generated */
|
|
|
|
bool bindToSystem(OSystem *syst);
|
|
|
|
|
2003-09-18 16:01:33 +00:00
|
|
|
/**
|
2003-10-05 17:42:55 +00:00
|
|
|
* Set the premix procedure. This is mainly used for the adlib music, but
|
|
|
|
* is not limited to it. The premix proc is invoked by the mixer whenever
|
|
|
|
* it needs to generate any data, before any other mixing takes place. The
|
|
|
|
* premixer than has a chanve to fill the mix buffer with data (usually
|
|
|
|
* music samples). It should generate the specified number of 16bit stereo
|
|
|
|
* samples (i.e. len * 4 bytes). The endianess of these samples shall be
|
|
|
|
* the native endianess.
|
2003-09-18 16:01:33 +00:00
|
|
|
*/
|
|
|
|
void setupPremix(PremixProc *proc, void *param);
|
2003-09-05 20:48:32 +00:00
|
|
|
|
2002-12-25 00:36:04 +00:00
|
|
|
// start playing a raw sound
|
2003-08-01 12:49:24 +00:00
|
|
|
int playRaw(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags,
|
2003-09-05 22:09:56 +00:00
|
|
|
int id = -1, byte volume = 255, int8 pan = 0, uint32 loopStart = 0, uint32 loopEnd = 0);
|
2002-10-27 01:12:10 +00:00
|
|
|
#ifdef USE_MAD
|
2003-09-05 21:06:32 +00:00
|
|
|
int playMP3(PlayingSoundHandle *handle, File *file, uint32 size, byte volume = 255, int8 pan = 0);
|
|
|
|
int playMP3CDTrack(PlayingSoundHandle *handle, File *file, mad_timer_t duration, byte volume = 255, int8 pan = 0);
|
2002-04-16 18:33:04 +00:00
|
|
|
#endif
|
2002-10-27 01:12:10 +00:00
|
|
|
#ifdef USE_VORBIS
|
2003-09-05 21:06:32 +00:00
|
|
|
int playVorbis(PlayingSoundHandle *handle, OggVorbis_File *ov_file, int duration, bool is_cd_track, byte volume = 255, int8 pan = 0);
|
2002-10-27 01:12:10 +00:00
|
|
|
#endif
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2003-09-05 20:48:32 +00:00
|
|
|
/** Start a new stream. */
|
2003-09-05 21:06:32 +00:00
|
|
|
int newStream(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, uint32 buffer_size, byte volume = 255, int8 pan = 0);
|
2003-09-05 20:48:32 +00:00
|
|
|
|
|
|
|
/** Append to an existing stream. */
|
|
|
|
void appendStream(PlayingSoundHandle handle, void *sound, uint32 size);
|
|
|
|
|
|
|
|
/** Mark a stream as finished - it will play all its remaining data, then stop. */
|
|
|
|
void endStream(PlayingSoundHandle handle);
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2003-06-15 01:56:47 +00:00
|
|
|
/** stop all currently playing sounds */
|
2002-08-24 15:31:37 +00:00
|
|
|
void stopAll();
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2003-07-14 20:09:14 +00:00
|
|
|
/** stop playing the given channel */
|
2003-09-06 10:47:30 +00:00
|
|
|
void stopChannel(int channel);
|
2002-04-14 18:13:08 +00:00
|
|
|
|
2003-07-14 20:09:14 +00:00
|
|
|
/** stop playing the sound with given ID */
|
2003-03-18 21:46:44 +00:00
|
|
|
void stopID(int id);
|
|
|
|
|
2003-07-14 20:09:14 +00:00
|
|
|
/** stop playing the channel for the given handle */
|
|
|
|
void stopHandle(PlayingSoundHandle handle);
|
|
|
|
|
2003-09-05 20:48:32 +00:00
|
|
|
/** pause/unpause all channels */
|
|
|
|
void pauseAll(bool paused);
|
|
|
|
|
2003-09-02 13:48:20 +00:00
|
|
|
/** pause/unpause the given channel */
|
|
|
|
void pauseChannel(int index, bool paused);
|
|
|
|
|
|
|
|
/** pause/unpause the sound with the given ID */
|
|
|
|
void pauseID(int id, bool paused);
|
|
|
|
|
|
|
|
/** pause/unpause the channel for the given handle */
|
|
|
|
void pauseHandle(PlayingSoundHandle handle, bool paused);
|
|
|
|
|
2003-09-05 20:48:32 +00:00
|
|
|
/** set the channel volume for the given handle (0 - 255) */
|
2003-08-31 20:26:21 +00:00
|
|
|
void setChannelVolume(PlayingSoundHandle handle, byte volume);
|
|
|
|
|
2003-09-05 20:48:32 +00:00
|
|
|
/** set the channel pan for the given handle (-127 ... 0 ... 127) (left ... center ... right)*/
|
2003-08-31 20:26:21 +00:00
|
|
|
void setChannelPan(PlayingSoundHandle handle, int8 pan);
|
|
|
|
|
2003-06-22 01:55:53 +00:00
|
|
|
/** Check whether any SFX channel is active.*/
|
|
|
|
bool hasActiveSFXChannel();
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2003-06-22 01:34:28 +00:00
|
|
|
/** set the global volume, 0-256 */
|
2002-08-24 15:31:37 +00:00
|
|
|
void setVolume(int volume);
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2003-07-25 01:19:14 +00:00
|
|
|
/** query the global volume, 0-256 */
|
|
|
|
int getVolume() const { return _globalVolume; }
|
2003-06-22 01:34:28 +00:00
|
|
|
|
|
|
|
/** set the music volume, 0-256 */
|
2002-08-24 15:31:37 +00:00
|
|
|
void setMusicVolume(int volume);
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2003-07-25 01:19:14 +00:00
|
|
|
/** query the music volume, 0-256 */
|
|
|
|
int getMusicVolume() const { return _musicVolume; }
|
2003-11-08 23:05:04 +00:00
|
|
|
|
2003-07-25 01:19:14 +00:00
|
|
|
/** query the output rate in kHz */
|
|
|
|
uint getOutputRate() const { return _outputRate; }
|
2002-05-01 22:22:22 +00:00
|
|
|
|
2003-06-21 23:29:34 +00:00
|
|
|
private:
|
2003-06-22 01:34:28 +00:00
|
|
|
int insertChannel(PlayingSoundHandle *handle, Channel *chan);
|
2003-07-06 17:00:09 +00:00
|
|
|
|
2003-09-05 20:48:32 +00:00
|
|
|
/** main mixer method */
|
2003-07-06 17:00:09 +00:00
|
|
|
void mix(int16 * buf, uint len);
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2002-08-18 16:10:38 +00:00
|
|
|
#endif
|