1 // -----------------------------------------------
4 // Copyright (c) 2004, Kronoman
5 // In loving memory of my father
6 // -----------------------------------------------
8 // -----------------------------------------------
9 // This system wraps around Allegro and DUMB,
10 // so I can use the high level sound routines,
11 // global adjust volume, disable volume, etc
12 // -----------------------------------------------
13 // The formulae for the sound volume is:
15 // 255 -- D (digital volume)
16 // V ---- X1 ==> X1 = (V * D) / 255
18 // 255 -- G (global volume, final result)
19 // X1 --- X2 ==> X2 = (X1 * G) / 255
21 // ==> X2 = (( (V * D) / 255) * G ) / 255
25 // X2 --- X3 ==> X3 = (X2 * 1.0f) / 255.0f ==> X2 / 255.0f
26 // -----------------------------------------------
28 #include <allegro.h> // Allegro : http://alleg.sf.net/
29 #include <aldumb.h> // DUMB : http://dumb.sf.net/
32 // -----------------------------------------------
33 // static members and methods
34 // -----------------------------------------------
36 bool CSoundWrapper::global_enabled
= true; // enables the sound system ?
37 int CSoundWrapper::global_volume
= 255;
39 void CSoundWrapper::global_set_volume(int v
)
41 if ( v
< 0 || v
> 255)
47 int CSoundWrapper::global_get_volume()
52 void CSoundWrapper::global_set_enabled(bool s
)
57 bool CSoundWrapper::global_is_enabled()
59 return global_enabled
;
62 // -----------------------------------------------
64 // -----------------------------------------------
67 CSoundWrapper::CSoundWrapper()
76 CSoundWrapper::~CSoundWrapper()
78 // nothing to do for now
81 // -----------------------------------------------
82 // digital sample playing
83 // -----------------------------------------------
84 int CSoundWrapper::play_sample(const SAMPLE
*spl
, int vol
, int pan
, int freq
, int loop
)
86 if (spl
== NULL
|| volume_d
< 1 || global_volume
< 1 || !enabled
|| !global_enabled
)
87 return -1; // error man :P
89 int v
= (((vol
* volume_d
) / 255) * global_volume
) / 255;
91 return ::play_sample(spl
, v
, pan
, freq
, loop
); // call real allegro function
94 // -----------------------------------------------
96 // -----------------------------------------------
97 void CSoundWrapper::set_volume_d(int v
)
105 int CSoundWrapper::get_volume_d()
107 return this->volume_d
;
110 // -----------------------------------------------
112 // -----------------------------------------------
114 // -----------------------------------------------
115 // call this before playing, dat should be a pointer to a music object of a datafile
116 // dat should be something like:
117 // DATAFILE *dat = load_datafile("smurf.dat");
118 // DUH *myduh = (DUH *)dat[GAME_MUSIC].dat;
119 // -----------------------------------------------
120 void CSoundWrapper::music_load(DUH
*dat
)
122 // DEBUG -- revisar esto, o algo
126 // -----------------------------------------------
128 // -----------------------------------------------
129 void CSoundWrapper::music_start()
134 if (!enabled
|| !global_enabled
)
137 dp
= al_start_duh(duh
, 2, 0, ((float)(volume_m
/ 255) * global_volume
) / 255.0 , 4096, 11025);
140 // -----------------------------------------------
142 // -----------------------------------------------
144 void CSoundWrapper::music_pause()
146 if (duh
== NULL
|| dp
== NULL
)
152 // -----------------------------------------------
154 // -----------------------------------------------
156 void CSoundWrapper::music_resume()
158 if (duh
== NULL
) // no hay musica
161 if (dp
== NULL
) // no estaba tocando
170 // -----------------------------------------------
172 // _must_ be called at regular intervals to hear the music
173 // -----------------------------------------------
174 void CSoundWrapper::music_poll()
182 if (enabled
&& global_enabled
&& global_volume
> 0)
188 // -----------------------------------------------
190 // -----------------------------------------------
191 void CSoundWrapper::music_stop()
193 if (duh
== NULL
|| dp
== NULL
)
201 // -----------------------------------------------
203 // -----------------------------------------------
204 void CSoundWrapper::set_volume_m(int v
)
206 if ( v
< 0 || v
> 255)
212 al_duh_set_volume(dp
, ((float)(volume_m
/ 255) * global_volume
) / 255.0 );
215 int CSoundWrapper::get_volume_m()
220 // -----------------------------------------------
221 // for all the object
222 // -----------------------------------------------
223 void CSoundWrapper::set_enabled(bool s
)
228 bool CSoundWrapper::is_enabled()
230 return this->enabled
;