____                _       __
    / __ )____  _____   | |     / /___ ___________
   / __  / __ \/ ___/   | | /| / / __ `/ ___/ ___/
  / /_/ / /_/ (__  )    | |/ |/ / /_/ / /  (__  )
 /_____/\____/____/     |__/|__/\__,_/_/  /____/

       A futuristic real-time strategy game.
          This file is part of Bos Wars.
(C) Copyright 2001-2007 by the Bos Wars and Stratagus Project. Distributed under the "GNU General Public License"

sound_server.cpp File Reference

The sound server (hardware layer and so on). More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stratagus.h"
#include "SDL.h"
#include "sound_server.h"
#include "iolib.h"
#include "iocompat.h"

Go to the source code of this file.

Classes

struct  SoundChannel

Defines

#define MaxChannels   32
 How many channels are supported.

Functions

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 *udata, Uint8 *stream, int len)
static int FillChannel (CSample *sample, unsigned char volume, char stereo)
int SetChannelVolume (int channel, int volume)
 Set the channel stereo.
int SetChannelStereo (int channel, int stereo)
 Set the channel's callback for when a sound finishes playing.
void SetChannelFinishedCallback (int channel, void(*callback)(int channel))
 Get the sample playing on a channel.
CSampleGetChannelSample (int channel)
 Stop a channel.
void StopChannel (int channel)
 Stop all channels.
void StopAllChannels ()
 Load a sample.
CSampleLoadSample (const std::string &name)
 Play a sample.
int PlaySample (CSample *sample)
 Play a sound file.
int PlaySoundFile (const std::string &name)
 Set effects volume.
void SetEffectsVolume (int volume)
 Get effects volume.
int GetEffectsVolume (void)
 Set effects enabled.
void SetEffectsEnabled (bool enabled)
 Check if effects are enabled.
bool IsEffectsEnabled (void)
 Set the music finished callback.
void SetMusicFinishedCallback (void(*callback)(void))
 Play a music file.
int PlayMusic (CSample *sample)
 Play a music file.
int PlayMusic (const std::string &file)
 Stop music playing.
void StopMusic (void)
 Set music volume.
void SetMusicVolume (int volume)
 Get music volume.
int GetMusicVolume (void)
 Set music enabled.
void SetMusicEnabled (bool enabled)
 Check if music is enabled.
bool IsMusicEnabled (void)
 Check if music is playing.
bool IsMusicPlaying (void)
 Check if sound is enabled.
bool SoundEnabled (void)
 Initialize the sound card.
static int InitSdlSound (int freq, int size)
int InitSound (void)
 Cleanup sound.
void QuitSound (void)

Variables

static bool SoundInitialized
 is sound initialized
static bool MusicPlaying
 flag true if playing music
static int EffectsVolume = 128
 effects sound volume
static int MusicVolume = 128
 music volume
static bool MusicEnabled = true
static bool EffectsEnabled = true
 Channels for sound effects and unit speach.
static SoundChannel Channels [MaxChannels]
static int NextFreeChannel
struct {
   CSample *   Sample
 Music sample.
   void(*   FinishedCallback )()
 Callback for when music finishes playing.
MusicChannel
static int * MixerBuffer
static int MixerBufferSize


Detailed Description

The sound server (hardware layer and so on).

Definition in file sound_server.cpp.


Define Documentation

#define MaxChannels   32


Function Documentation

static void ChannelFinished ( int  channel  )  [static]

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

Clip mix to output stereo 16 signed bit.

Parameters:
mix signed 32 bit input.
size number of samples in input.
output clipped 16 signed bit output buffer.

Definition at line 283 of file sound_server.cpp.

Referenced by MixIntoBuffer().

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:
src Source buffer
dest Destination buffer
frequency Frequency of source
chansize Bitrate in bytes per channel of source
channels Number of channels of source
bytes Number of compressed bytes to read
Returns:
Number of bytes written in 'dest'

Definition at line 107 of file sound_server.cpp.

Referenced by MixMusicToStereo32(), and MixSampleToStereo32().

static void FillAudio ( void *  udata,
Uint8 *  stream,
int  len 
) [static]

Fill buffer for the sound card.

See also:
SDL_OpenAudio
Parameters:
udata the pointer stored in userdata field of SDL_AudioSpec.
stream pointer to buffer you want to fill with information.
len is length of audio buffer in bytes.

Definition at line 339 of file sound_server.cpp.

References MixIntoBuffer().

Referenced by InitSdlSound().

static int FillChannel ( CSample sample,
unsigned char  volume,
char  stereo 
) [static]

CSample* GetChannelSample ( int  channel  ) 

Stop a channel.

Get the sample playing on a channel

Definition at line 465 of file sound_server.cpp.

References MaxChannels, and SoundChannel::Sample.

Referenced by PlaySoundFileCallback().

int GetEffectsVolume ( void   ) 

Set effects enabled.

Get effects volume

Definition at line 596 of file sound_server.cpp.

References EffectsVolume.

Referenced by tolua_stratagus_GetEffectsVolume00().

int GetMusicVolume ( void   ) 

Set music enabled.

Get music volume

Definition at line 723 of file sound_server.cpp.

References MusicVolume.

Referenced by tolua_stratagus_GetMusicVolume00(), and UiToggleMusic().

static int InitSdlSound ( int  freq,
int  size 
) [static]

Initialize sound card hardware part with SDL.

Parameters:
freq Sample frequency (44100,22050,11025 hz).
size Sample size (8bit, 16bit)
Returns:
True if failure, false if everything ok.

Definition at line 777 of file sound_server.cpp.

References _C_, DebugPrint, and FillAudio().

Referenced by InitSound().

int InitSound ( void   ) 

Cleanup sound.

Initialize sound card.

Returns:
True if failure, false if everything ok.

Definition at line 810 of file sound_server.cpp.

References InitSdlSound(), MaxChannels, SoundChannel::Point, and SoundInitialized.

Referenced by main1().

bool IsEffectsEnabled ( void   ) 

Set the music finished callback.

Check if effects are enabled

Definition at line 612 of file sound_server.cpp.

References EffectsEnabled.

Referenced by tolua_stratagus_IsEffectsEnabled00(), and UiToggleSound().

bool IsMusicEnabled ( void   ) 

Check if music is playing.

Check if music is enabled

Definition at line 744 of file sound_server.cpp.

References MusicEnabled.

Referenced by CheckMusicFinished(), PlayMusic(), and tolua_stratagus_IsMusicEnabled00().

bool IsMusicPlaying ( void   ) 

Check if sound is enabled.

Check if music is playing

Definition at line 752 of file sound_server.cpp.

References MusicPlaying.

CSample* LoadSample ( const std::string &  name  ) 

Play a sample.

Load a sample

Parameters:
name File name of sample (short version).
Returns:
General sample loaded from file into memory.
Todo:
Add streaming, caching support.

Definition at line 517 of file sound_server.cpp.

References LibraryFileName(), LoadVorbis(), LoadWav(), and PlayAudioLoadInMemory.

Referenced by PlayFile(), PlaySoundFile(), and RegisterSound().

static int MixChannelsToStereo32 ( int *  buffer,
int  size 
) [static]

Mix channels to stereo 32 bit.

Parameters:
buffer Buffer for mixed samples.
size Number of samples that fits into buffer.
Returns:
How many channels become free after mixing them.

Definition at line 251 of file sound_server.cpp.

References Assert, ChannelFinished(), MaxChannels, MixSampleToStereo32(), SoundChannel::Point, and Sample.

Referenced by MixIntoBuffer().

static void MixIntoBuffer ( void *  buffer,
int  samples 
) [static]

Mix into buffer.

Parameters:
buffer Buffer to be filled with samples. Buffer must be big enough.
samples Number of samples.

Definition at line 307 of file sound_server.cpp.

References ClipMixToStereo16(), EffectsEnabled, MixChannelsToStereo32(), MixerBuffer, MixerBufferSize, MixMusicToStereo32(), and MusicEnabled.

Referenced by FillAudio().

static void MixMusicToStereo32 ( int *  buffer,
int  size 
) [static]

Mix music to stereo 32 bit.

Parameters:
buffer Buffer for mixed samples.
size Number 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.

Definition at line 138 of file sound_server.cpp.

References Assert, ConvertToStereo32(), int(), MaxVolume, MusicChannel, MusicPlaying, and MusicVolume.

Referenced by MixIntoBuffer().

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:
sample Input sample
index Position into input sample
volume Volume of the input sample
stereo Stereo (left/right) position of sample
buffer Output buffer
size Size 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.

Definition at line 200 of file sound_server.cpp.

References Assert, CSample::Buffer, CSample::Channels, ConvertToStereo32(), EffectsVolume, CSample::Frequency, int(), CSample::Len, MaxVolume, CSample::SampleSize, and SOUND_BUFFER_SIZE.

Referenced by MixChannelsToStereo32().

int PlayMusic ( const std::string &  file  ) 

Stop music playing.

Play a music file.

Parameters:
file Name of music file, format is automatically detected.
Returns:
0 if music is playing, -1 if not.

Definition at line 656 of file sound_server.cpp.

References _C_, DebugPrint, IsMusicEnabled(), LibraryFileName(), LoadVorbis(), LoadWav(), MusicChannel, MusicPlaying, PlayAudioStream, SoundEnabled(), and StopMusic().

int PlayMusic ( CSample sample  ) 

Play a music file.

Play a music file.

Parameters:
sample Music sample.
Returns:
0 if music is playing, -1 if not.

Definition at line 636 of file sound_server.cpp.

References DebugPrint, MusicChannel, MusicPlaying, and StopMusic().

Referenced by PlayMovie(), ShowTitleScreens(), and tolua_stratagus_PlayMusic00().

int PlaySample ( CSample sample  ) 

Play a sound file.

Play a sound sample

Parameters:
sample Sample to play
Returns:
Channel number, -1 for error

Definition at line 545 of file sound_server.cpp.

References EffectsEnabled, EffectsVolume, FillChannel(), MaxChannels, NextFreeChannel, and SoundEnabled().

Referenced by PlayFile(), PlayGameSound(), PlayMissileSound(), PlaySoundFile(), and PlayUnitSound().

int PlaySoundFile ( const std::string &  name  ) 

Set effects volume.

Play a sound file

Parameters:
name Filename of a sound to play
Returns:
Channel number the sound is playing on, -1 for error

Definition at line 568 of file sound_server.cpp.

References LoadSample(), and PlaySample().

void QuitSound ( void   ) 

Cleanup sound server.

Definition at line 834 of file sound_server.cpp.

References MixerBuffer, and SoundInitialized.

Referenced by Exit().

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

Get the sample playing on a channel.

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

Parameters:
channel Channel to set
callback Callback to call when the sound finishes

Definition at line 453 of file sound_server.cpp.

References SoundChannel::FinishedCallback, and MaxChannels.

Referenced by PlayFile().

int SetChannelStereo ( int  channel,
int  stereo 
)

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

Set the channel stereo

Parameters:
channel Channel to set
stereo -128 to 127, out of range will not set the stereo
Returns:
Current stereo of the channel, -1 for error

Definition at line 423 of file sound_server.cpp.

References MaxChannels, and SoundChannel::Stereo.

Referenced by PlayMissileSound(), PlayUnitSound(), and tolua_stratagus_SetChannelStereo00().

int SetChannelVolume ( int  channel,
int  volume 
)

Set the channel stereo.

Set the channel volume

Parameters:
channel Channel to set
volume New volume, <0 will not set the volume
Returns:
Current volume of the channel, -1 for error

Definition at line 393 of file sound_server.cpp.

References MaxChannels, MaxVolume, and SoundChannel::Volume.

Referenced by PlayFile(), PlayGameSound(), PlayMissileSound(), PlayUnitSound(), and tolua_stratagus_SetChannelVolume00().

void SetEffectsEnabled ( bool  enabled  ) 

Check if effects are enabled.

Set effects enabled

Definition at line 604 of file sound_server.cpp.

References EffectsEnabled.

Referenced by tolua_stratagus_SetEffectsEnabled00(), and UiToggleSound().

void SetEffectsVolume ( int  volume  ) 

Get effects volume.

Set the global sound volume.

Parameters:
volume the sound volume 0-255

Definition at line 582 of file sound_server.cpp.

References EffectsVolume, and MaxVolume.

Referenced by tolua_stratagus_SetEffectsVolume00().

void SetMusicEnabled ( bool  enabled  ) 

Check if music is enabled.

Set music enabled

Definition at line 731 of file sound_server.cpp.

References MusicEnabled, and StopMusic().

Referenced by tolua_stratagus_SetMusicEnabled00(), and UiToggleSound().

void SetMusicFinishedCallback ( void(*)(void)  callback  ) 

Play a music file.

Set the music finished callback

Definition at line 624 of file sound_server.cpp.

References MusicChannel.

Referenced by InitMusic().

void SetMusicVolume ( int  volume  ) 

Get music volume.

Set the music volume.

Parameters:
volume the music volume 0-255

Definition at line 709 of file sound_server.cpp.

References MaxVolume, and MusicVolume.

Referenced by tolua_stratagus_SetMusicVolume00(), and UiToggleMusic().

bool SoundEnabled ( void   ) 

Initialize the sound card.

Check if sound is enabled

Definition at line 764 of file sound_server.cpp.

References SoundInitialized.

Referenced by CheckMusicFinished(), ChooseSample(), CreateGame(), InitSoundClient(), LoadModules(), MapUnitSounds(), PlayMusic(), PlaySample(), UiToggleMusic(), and UiToggleSound().

void StopAllChannels (  ) 

Load a sample.

Stop all channels

Definition at line 495 of file sound_server.cpp.

References ChannelFinished(), and MaxChannels.

Referenced by tolua_stratagus_StopAllChannels00().

void StopChannel ( int  channel  ) 

Stop all channels.

Stop a channel

Parameters:
channel Channel to stop

Definition at line 479 of file sound_server.cpp.

References ChannelFinished(), and MaxChannels.

Referenced by tolua_stratagus_StopChannel00().

void StopMusic ( void   ) 

Set music volume.

Stop the current playing music.

Definition at line 691 of file sound_server.cpp.

References MusicChannel, and MusicPlaying.

Referenced by CheckMusicFinished(), Exit(), PlayMovie(), PlayMusic(), SetMusicEnabled(), ShowTitleScreens(), and tolua_stratagus_StopMusic00().


Variable Documentation

SoundChannel Channels[MaxChannels] [static]

Definition at line 77 of file sound_server.cpp.

bool EffectsEnabled = true [static]

Channels for sound effects and unit speach.

Definition at line 61 of file sound_server.cpp.

Referenced by IsEffectsEnabled(), MixIntoBuffer(), PlaySample(), and SetEffectsEnabled().

int EffectsVolume = 128 [static]

effects sound volume

Definition at line 57 of file sound_server.cpp.

Referenced by GetEffectsVolume(), MixSampleToStereo32(), PlaySample(), and SetEffectsVolume().

void(* FinishedCallback)()

Callback for when music finishes playing.

Referenced by ChannelFinished().

int* MixerBuffer [static]

Definition at line 87 of file sound_server.cpp.

Referenced by MixIntoBuffer(), and QuitSound().

int MixerBufferSize [static]

Definition at line 88 of file sound_server.cpp.

Referenced by MixIntoBuffer().

struct { ... } MusicChannel [static]

bool MusicEnabled = true [static]

Definition at line 60 of file sound_server.cpp.

Referenced by IsMusicEnabled(), MixIntoBuffer(), and SetMusicEnabled().

bool MusicPlaying [static]

flag true if playing music

Definition at line 55 of file sound_server.cpp.

Referenced by IsMusicPlaying(), MixMusicToStereo32(), PlayMusic(), and StopMusic().

int MusicVolume = 128 [static]

music volume

Definition at line 58 of file sound_server.cpp.

Referenced by GetMusicVolume(), MixMusicToStereo32(), and SetMusicVolume().

int NextFreeChannel [static]

Definition at line 78 of file sound_server.cpp.

Referenced by ChannelFinished(), FillChannel(), and PlaySample().

Music sample.

Definition at line 81 of file sound_server.cpp.

Referenced by MixChannelsToStereo32().

bool SoundInitialized [static]

is sound initialized

Definition at line 54 of file sound_server.cpp.

Referenced by InitSound(), QuitSound(), and SoundEnabled().


Generated on Sat Feb 21 00:28:19 2009 for Bos Wars by  doxygen 1.5.6