_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes
sound_server.cpp File Reference
#include "stratagus.h"
#include "sound_server.h"
#include "fluidsynth.h"
#include "iocompat.h"
#include "iolib.h"
#include "unit.h"
#include "SDL.h"

Classes

struct  SoundChannel
 Channels for sound effects and unit speech. More...
 

sound_server.cpp - The sound server (hardware layer and so on)

#define MaxChannels   64
 
static bool SoundInitialized
 
static bool MusicPlaying
 is sound initialized More...
 
static int EffectsVolume = 128
 flag true if playing music More...
 
static int MusicVolume = 128
 effects sound volume More...
 
static bool MusicEnabled = true
 music volume More...
 
static bool EffectsEnabled = true
 
static SoundChannel Channels [MaxChannels]
 
static int NextFreeChannel
 
struct {
   CSample *   Sample
 
   void(*   FinishedCallback )()
 Music sample. More...
 
MusicChannel
 
struct {
   SDL_AudioSpec   Format
 
   SDL_mutex *   Lock
 
   SDL_cond *   Cond
 
   SDL_Thread *   Thread
 
   int *   MixerBuffer
 
   Uint8 *   Buffer
 
   bool   Running
 
Audio
 
static void ChannelFinished (int channel)
 
static int ConvertToStereo32 (const char *src, char *dest, int frequency, int chansize, int channels, int bytes)
 
static void MixMusicToStereo32 (int *buffer, int size)
 
static int MixSampleToStereo32 (CSample *sample, int index, unsigned char volume, char stereo, int *buffer, int size)
 
static int MixChannelsToStereo32 (int *buffer, int size)
 
static void ClipMixToStereo16 (const int *mix, int size, short *output)
 
static void MixIntoBuffer (void *buffer, int samples)
 
static void FillAudio (void *, Uint8 *stream, int len)
 
static int FillThread (void *)
 
bool SampleIsPlaying (CSample *sample)
 Check, if this sample is already playing. More...
 
bool UnitSoundIsPlaying (Origin *origin)
 Check if this unit plays some sound. More...
 
static int FillChannel (CSample *sample, unsigned char volume, char stereo, Origin *origin)
 
int SetChannelVolume (int channel, int volume)
 Load a MIDI file. More...
 
int SetChannelStereo (int channel, int stereo)
 Set the channel stereo. More...
 
void SetChannelFinishedCallback (int channel, void(*callback)(int channel))
 Set the channel's callback for when a sound finishes playing. More...
 
CSampleGetChannelSample (int channel)
 Get the sample playing on a channel. More...
 
void StopChannel (int channel)
 Stop a channel. More...
 
void StopAllChannels ()
 Stop all channels. More...
 
static CSampleLoadSample (const char *name, enum _play_audio_flags_ flag)
 
CSampleLoadSample (const std::string &name)
 Load a sample. More...
 
int PlaySample (CSample *sample, Origin *origin)
 Play a sample. More...
 
int PlaySoundFile (const std::string &name)
 Play a sound file. More...
 
void SetEffectsVolume (int volume)
 Set effects volume. More...
 
int GetEffectsVolume ()
 Get effects volume. More...
 
void SetEffectsEnabled (bool enabled)
 Set effects enabled. More...
 
bool IsEffectsEnabled ()
 Check if effects are enabled. More...
 
void SetMusicFinishedCallback (void(*callback)())
 Set the music finished callback. More...
 
int PlayMusic (CSample *sample)
 Play a music file. More...
 
int PlayMusic (const std::string &file)
 Play a music file. More...
 
void StopMusic ()
 Stop music playing. More...
 
void SetMusicVolume (int volume)
 Set music volume. More...
 
int GetMusicVolume ()
 Get music volume. More...
 
void SetMusicEnabled (bool enabled)
 Set music enabled. More...
 
bool IsMusicEnabled ()
 Check if music is enabled. More...
 
bool IsMusicPlaying ()
 Check if music is playing. More...
 
bool SoundEnabled ()
 Check if sound is enabled. More...
 
static int InitSdlSound (int freq, int size)
 
int InitSound ()
 Initialize the sound card. More...
 
void QuitSound ()
 Cleanup sound. More...
 

Macro Definition Documentation

#define MaxChannels   64

Function Documentation

static void ChannelFinished ( int  channel)
static

A channel is finished playing

static void ClipMixToStereo16 ( const int *  mix,
int  size,
short *  output 
)
static

Clip mix to output stereo 16 signed bit.

Parameters
mixsigned 32 bit input.
sizenumber of samples in input.
outputclipped 16 signed bit output buffer.
static int ConvertToStereo32 ( const char *  src,
char *  dest,
int  frequency,
int  chansize,
int  channels,
int  bytes 
)
static

Convert RAW sound data to 44100 hz, Stereo, 16 bits per channel

Parameters
srcSource buffer
destDestination buffer
frequencyFrequency of source
chansizeBitrate in bytes per channel of source
channelsNumber of channels of source
bytesNumber of compressed bytes to read
Returns
Number of bytes written in 'dest'
static void FillAudio ( void *  ,
Uint8 *  stream,
int  len 
)
static

Fill buffer for the sound card.

See also
SDL_OpenAudio
Parameters
udatathe pointer stored in userdata field of SDL_AudioSpec.
streampointer to buffer you want to fill with information.
lenis length of audio buffer in bytes.
static int FillChannel ( CSample sample,
unsigned char  volume,
char  stereo,
Origin origin 
)
static

Put a sound request in the next free channel.

static int FillThread ( void *  )
static

Fill audio thread.

CSample* GetChannelSample ( int  channel)

Get the sample playing on a channel.

Get the sample playing on a channel

int GetEffectsVolume ( )

Get effects volume.

Get effects volume

int GetMusicVolume ( )

Get music volume.

Get music volume

static int InitSdlSound ( int  freq,
int  size 
)
static

Initialize sound card hardware part with SDL.

Parameters
freqSample frequency (44100,22050,11025 hz).
sizeSample size (8bit, 16bit)
Returns
True if failure, false if everything ok.
int InitSound ( )

Initialize the sound card.

Initialize sound card.

Returns
True if failure, false if everything ok.
bool IsEffectsEnabled ( )

Check if effects are enabled.

Check if effects are enabled

bool IsMusicEnabled ( )

Check if music is enabled.

Check if music is enabled

bool IsMusicPlaying ( )

Check if music is playing.

Check if music is playing

static CSample* LoadSample ( const char *  name,
enum _play_audio_flags_  flag 
)
static
CSample* LoadSample ( const std::string &  name)

Load a sample.

Load a sample

Parameters
nameFile name of sample (short version).
Returns
General sample loaded from file into memory.
Todo:
Add streaming, caching support.
static int MixChannelsToStereo32 ( int *  buffer,
int  size 
)
static

Mix channels to stereo 32 bit.

Parameters
bufferBuffer for mixed samples.
sizeNumber of samples that fits into buffer.
Returns
How many channels become free after mixing them.
static void MixIntoBuffer ( void *  buffer,
int  samples 
)
static

Mix into buffer.

Parameters
bufferBuffer to be filled with samples. Buffer must be big enough.
samplesNumber of samples.
static void MixMusicToStereo32 ( int *  buffer,
int  size 
)
static

Mix music to stereo 32 bit.

Parameters
bufferBuffer for mixed samples.
sizeNumber of samples that fits into buffer.
Todo:
this functions can be called from inside the SDL audio callback, which is bad, the buffer should be precalculated.
static int MixSampleToStereo32 ( CSample sample,
int  index,
unsigned char  volume,
char  stereo,
int *  buffer,
int  size 
)
static

Mix sample to buffer.

The input samples are adjusted by the local volume and resampled to the output frequence.

Parameters
sampleInput sample
indexPosition into input sample
volumeVolume of the input sample
stereoStereo (left/right) position of sample
bufferOutput buffer
sizeSize of output buffer (in samples per channel)
Returns
The number of bytes used to fill buffer
Todo:
Can mix faster if signed 8 bit buffers are used.
int PlayMusic ( CSample sample)

Play a music file.

Play a music file.

Parameters
sampleMusic sample.
Returns
0 if music is playing, -1 if not.
int PlayMusic ( const std::string &  file)

Play a music file.

Play a music file.

Parameters
fileName of music file, format is automatically detected.
Returns
0 if music is playing, -1 if not.
int PlaySample ( CSample sample,
Origin origin 
)

Play a sample.

Play a sound sample

Parameters
sampleSample to play
Returns
Channel number, -1 for error
int PlaySoundFile ( const std::string &  name)

Play a sound file.

Play a sound file

Parameters
nameFilename of a sound to play
Returns
Channel number the sound is playing on, -1 for error
void QuitSound ( )

Cleanup sound.

Cleanup sound server.

bool SampleIsPlaying ( CSample sample)

Check, if this sample is already playing.

Check if this sound is already playing

void SetChannelFinishedCallback ( int  channel,
void(*)(int channel)  callback 
)

Set the channel's callback for when a sound finishes playing.

Set the channel's callback for when a sound finishes playing

Parameters
channelChannel to set
callbackCallback to call when the sound finishes
int SetChannelStereo ( int  channel,
int  stereo 
)

Set the channel stereo.

Set the channel stereo

Parameters
channelChannel to set
stereo-128 to 127, out of range will not set the stereo
Returns
Current stereo of the channel, -1 for error
int SetChannelVolume ( int  channel,
int  volume 
)

Load a MIDI file.

Set the channel volume

Parameters
channelChannel to set
volumeNew volume, <0 will not set the volume
Returns
Current volume of the channel, -1 for error
void SetEffectsEnabled ( bool  enabled)

Set effects enabled.

Set effects enabled

void SetEffectsVolume ( int  volume)

Set effects volume.

Set the global sound volume.

Parameters
volumethe sound volume 0-255
void SetMusicEnabled ( bool  enabled)

Set music enabled.

Set music enabled

void SetMusicFinishedCallback ( void(*)()  callback)

Set the music finished callback.

Set the music finished callback

void SetMusicVolume ( int  volume)

Set music volume.

Set the music volume.

Parameters
volumethe music volume 0-255
bool SoundEnabled ( )

Check if sound is enabled.

Check if sound is enabled

void StopAllChannels ( )

Stop all channels.

Stop all channels

void StopChannel ( int  channel)

Stop a channel.

Stop a channel

Parameters
channelChannel to stop
void StopMusic ( )

Stop music playing.

Stop the current playing music.

bool UnitSoundIsPlaying ( Origin origin)

Check if this unit plays some sound.

Variable Documentation

struct { ... } Audio
Uint8* Buffer
SoundChannel Channels[MaxChannels]
static
SDL_cond* Cond
bool EffectsEnabled = true
static
int EffectsVolume = 128
static

flag true if playing music

void(* FinishedCallback)()

Music sample.

SDL_AudioSpec Format
SDL_mutex* Lock
int* MixerBuffer
struct { ... } MusicChannel
bool MusicEnabled = true
static

music volume

bool MusicPlaying
static

is sound initialized

int MusicVolume = 128
static

effects sound volume

int NextFreeChannel
static
bool Running
CSample* Sample
bool SoundInitialized
static
SDL_Thread* Thread
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.