scummvm/sound/mixer.h

219 lines
5.2 KiB
C
Raw Normal View History

/* ScummVM - Scumm Interpreter
* Copyright (C) 2001 Ludvig Strigeus
* Copyright (C) 2001/2002 The ScummVM project
*
* 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$
*
*/
#ifndef MIXER_H
#define MIXER_H
#include <stdio.h>
#ifdef COMPRESSED_SOUND_FILE
#include <mad.h>
#endif
typedef uint32 PlayingSoundHandle;
2002-08-24 15:31:37 +00:00
class File;
class SoundMixer {
2002-07-07 18:04:03 +00:00
private:
class Channel {
public:
2002-08-24 15:31:37 +00:00
bool _toBeDestroyed;
virtual void mix(int16 *data, uint len) = 0;
2002-07-07 18:04:03 +00:00
void destroy() {
2002-08-24 15:31:37 +00:00
_toBeDestroyed = true;
}
virtual void realDestroy() = 0;
virtual void append(void *sound, uint32 size);
#ifdef COMPRESSED_SOUND_FILE
2002-08-24 15:31:37 +00:00
virtual bool soundFinished();
#endif
};
2002-08-24 15:31:37 +00:00
class ChannelRaw : public Channel {
SoundMixer * _mixer;
void * _ptr;
uint32 _pos;
uint32 _size;
2002-08-24 15:31:37 +00:00
uint32 _fpSpeed;
uint32 _fpPos;
uint32 _realSize, _rate;
byte _flags;
2002-07-07 18:04:03 +00:00
public:
2002-08-24 15:31:37 +00:00
ChannelRaw(SoundMixer * mixer, void * sound, uint32 size, uint rate, byte flags);
void mix(int16 * data, uint len);
void realDestroy();
};
2002-08-24 15:31:37 +00:00
class ChannelStream : public Channel {
SoundMixer * _mixer;
byte * _ptr;
byte * _endOfData;
byte * _pos;
uint32 _fpSpeed;
uint32 _fpPos;
uint32 _bufferSize;
uint32 _rate;
byte _flags;
public:
2002-08-24 15:31:37 +00:00
ChannelStream(SoundMixer * mixer, void * sound, uint32 size, uint rate, byte flags);
void append(void * sound, uint32 size);
void mix(int16 * data, uint len);
void realDestroy();
};
#ifdef COMPRESSED_SOUND_FILE
2002-08-24 15:31:37 +00:00
class ChannelMP3 : public Channel {
SoundMixer * _mixer;
void *_ptr;
struct mad_stream _stream;
struct mad_frame _frame;
struct mad_synth _synth;
2002-08-24 15:31:37 +00:00
uint32 _silenceCut;
uint32 _posInFrame;
uint32 _position;
uint32 _size;
byte _flags;
public:
2002-08-24 15:31:37 +00:00
ChannelMP3(SoundMixer * mixer, void *sound, uint size, byte flags);
void mix(int16 * data, uint len);
void realDestroy();
};
2002-08-24 15:31:37 +00:00
class ChannelMP3CDMusic:public Channel {
SoundMixer * _mixer;
void * _ptr;
struct mad_stream _stream;
struct mad_frame _frame;
struct mad_synth _synth;
2002-08-24 15:31:37 +00:00
uint32 _posInFrame;
uint32 _size;
2002-08-24 15:31:37 +00:00
uint32 _bufferSize;
mad_timer_t _duration;
File * _file;
bool _initialized;
2002-08-24 15:31:37 +00:00
public:
ChannelMP3CDMusic(SoundMixer * mixer, File * file, mad_timer_t duration);
2002-08-24 15:31:37 +00:00
void mix(int16 * data, uint len);
void realDestroy();
bool soundFinished();
};
#endif
2002-08-24 15:31:37 +00:00
static void onGenerateSamples(void * s, byte * samples, int len);
public:
2002-08-24 15:31:37 +00:00
typedef void PremixProc (void * param, int16 * data, uint len);
2002-08-24 15:31:37 +00:00
OSystem * _syst;
void * _mutex;
2002-07-07 18:04:03 +00:00
2002-08-24 15:31:37 +00:00
uint _outputRate;
2002-08-24 15:31:37 +00:00
int16 * _volumeTable;
int _musicVolume;
bool _paused;
enum {
NUM_CHANNELS = 16,
};
2002-08-24 15:31:37 +00:00
void * _premixParam;
PremixProc * _premixProc;
2002-08-24 15:31:37 +00:00
Channel * _channels[NUM_CHANNELS];
PlayingSoundHandle * _handles[NUM_CHANNELS];
SoundMixer();
~SoundMixer();
2002-08-24 15:31:37 +00:00
int insertAt(PlayingSoundHandle * handle, int index, Channel * chan);
void append(void * data, uint32 len);
void unInsert(Channel * chan);
/* start playing a raw sound */
enum {
/* Do *NOT* change any of these flags without looking at the code in mixer.cpp */
2002-07-07 18:04:03 +00:00
FLAG_UNSIGNED = 1, /* unsigned samples */
FLAG_STEREO = 2, /* sound is in stereo */
FLAG_16BITS = 4, /* sound is 16 bits wide */
FLAG_AUTOFREE = 8, /* sound buffer is freed automagically at the end of playing */
FLAG_FILE = 16, /* sound is a FILE * that's read from */
};
2002-08-24 15:31:37 +00:00
int playRaw(PlayingSoundHandle * handle, void * sound, uint32 size, uint rate, byte flags);
int playStream(PlayingSoundHandle * handle, int index, void * sound, uint32 size, uint rate,
2002-07-07 18:04:03 +00:00
byte flags);
#ifdef COMPRESSED_SOUND_FILE
2002-08-24 15:31:37 +00:00
int playMP3(PlayingSoundHandle * handle, void * sound, uint32 size, byte flags);
int playMP3CDTrack(PlayingSoundHandle * handle, File * file, mad_timer_t duration);
#endif
/* Premix procedure, useful when using fmopl adlib */
2002-08-24 15:31:37 +00:00
void setupPremix(void * param, PremixProc * proc);
/* mix */
2002-08-24 15:31:37 +00:00
void mix(int16 * buf, uint len);
/* stop all currently playing sounds */
2002-08-24 15:31:37 +00:00
void stopAll();
/* stop playing a specific sound */
void stop(PlayingSoundHandle psh);
void stop(int index);
/* append to existing sound */
2002-08-24 15:31:37 +00:00
int append(int index, void * sound, uint32 size, uint rate, byte flags);
/* is any channel active? */
2002-08-24 15:31:37 +00:00
bool hasActiveChannel();
/* bind to the OSystem object => mixer will be
* invoked automatically when samples need
* to be generated */
2002-08-24 15:31:37 +00:00
bool bindToSystem(OSystem *syst);
/* set the volume, 0-256 */
2002-08-24 15:31:37 +00:00
void setVolume(int volume);
void setMusicVolume(int volume);
/* pause - unpause */
void pause(bool paused);
};
2002-07-07 18:04:03 +00:00
struct MP3OffsetTable { /* Compressed Sound (.SO3) */
int org_offset;
int new_offset;
int num_tags;
int compressed_size;
};
#endif