source: golgotha/src/golg/sound_man.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 9.2 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "sound_man.hh"
10#include "loaders/wav_load.hh"
11#include "memory/malloc.hh"
12#include "math/random.hh"
13#include "objs/sfx_obj.hh"
14#include "music/stream.hh"
15#include "file/file.hh"
16#include "time/profile.hh"
17#include "string/string.hh"
18#include "sound/sfx_id.hh"
19#include "lisp/lisp.hh"
20#include "lisp/li_init.hh"
21#include "lisp/li_dialog.hh"
22#include "lisp/li_class.hh"
23#include "status/status.hh"
24
25g1_sound_manager_class g1_sound_man;
26
27i4_stream_wav_player *g1_music_stream=0;
28static int music_count_down=0;
29
30
31g1_sound_manager_class::g1_sound_manager_class()
32{
33  sfx_obj_list=0;
34  loop_current_song=i4_F;
35}
36
37void g1_sound_manager_class::next_song()
38{
39  if (g1_music_stream)
40  {
41    delete g1_music_stream;
42    g1_music_stream=0;
43  }
44}
45
46void g1_sound_manager_class::pause()
47{
48  for (g1_sfx_obj_class *s=sfx_obj_list; s; s=s->next_sfx)
49    if (s->stream)
50      s->stream->pause();
51
52  if (g1_music_stream)
53    g1_music_stream->pause();
54}
55
56void g1_sound_manager_class::unpause()
57{
58  for (g1_sfx_obj_class *s=sfx_obj_list; s; s=s->next_sfx)
59    if (s->stream)
60      s->stream->unpause();
61
62  if (g1_music_stream)
63    g1_music_stream->unpause();
64}
65
66inline float dist_comp(i4_3d_vector &a, i4_3d_vector &b)
67{
68  return (((a.x-b.x)*(a.x-b.x)) + ((a.y-b.y)*(a.y-b.y)) + ((a.z-b.z)*(a.z-b.z)));
69}
70
71static int get_music_volume()
72{
73  li_object *music=li_get_value("music");
74  if (music)
75  {
76    li_class *c=li_class::get(music,0);
77    return li_get_int(c->value("volume"),0);
78  }
79  else return 0;
80}
81
82static void set_music_volume(int vol)
83{
84 
85  li_object *music=li_get_value("music");
86  if (music)
87  {
88    li_class *c=li_class::get(music,0);
89
90    if (vol>I4_SOUND_VOLUME_LEVELS-1)
91      vol=I4_SOUND_VOLUME_LEVELS-1;
92    if (vol<0) vol=0;
93
94    if (g1_music_stream)
95      g1_music_stream->set_volume(vol);
96
97    c->set("volume", new li_int(vol));
98  }
99}
100
101
102
103static void restart_song(char *newfn)
104{
105  if (g1_music_stream)
106  {
107    delete g1_music_stream;
108    g1_music_stream=0;
109  }
110 
111  char fn[100];
112  sprintf(fn, "%s/%s", s1_get_sfx_path(), newfn);
113
114  if (i4_sound_man==&i4_null_sound)
115    music_count_down=100;
116  else
117  {
118
119    i4_file_class *fp=i4_open(fn, I4_SUPPORT_ASYNC | I4_READ);
120    if (fp)
121    {   
122      g1_music_stream=new i4_stream_wav_player(fp, 256*1024, i4_F, i4_T);
123      if (g1_music_stream->load_failed())
124      {
125        delete g1_music_stream;
126        g1_music_stream=0;
127      }
128      else
129        set_music_volume(get_music_volume());
130
131    }
132    else
133      music_count_down=100;  // wait 10 seconds before trying again
134  }
135}
136
137
138
139li_object *g1_set_song(li_object *o, li_environment *env)
140{
141  li_class *oldv=li_class::get(li_second(o,env),env);
142  li_class *newv=li_class::get(li_first(o,env),env);
143
144  char *oldfn=li_get_string(oldv->value("songs"),env);
145  char *newfn=li_get_string(newv->value("songs"),env);
146
147
148  if (strcmp(oldfn,newfn))
149    restart_song(newfn);
150     
151
152  if (g1_music_stream)
153  {
154    int vol=li_get_int(newv->value("volume"),env);
155    g1_music_stream->set_volume(vol);
156  }
157
158
159  return 0;
160}
161
162li_object *g1_edit_music(li_object *o, li_environment *env)
163{
164  li_create_dialog("Music",
165                   li_get_value("music"),
166                   0,
167                   g1_set_song);
168  return 0;
169}
170
171li_object *g1_music_up(li_object *o, li_environment *env)
172{
173  set_music_volume(get_music_volume()+8);
174  return 0;
175}
176
177
178li_object *g1_music_down(li_object *o, li_environment *env)
179{
180  set_music_volume(get_music_volume()-8);
181  return 0;
182}
183
184li_automatic_add_function(g1_edit_music, "edit_music");
185li_automatic_add_function(g1_music_up, "music_up");
186li_automatic_add_function(g1_music_down, "music_down");
187
188// should be calle by main game every tick, check to see if a new narative should be played
189void g1_sound_manager_class::poll(i4_bool game_is_running)
190
191  i4_3d_vector listener;
192  s1_get_camera_pos(listener);
193  for (g1_sfx_obj_class *sfx=sfx_obj_list; sfx; sfx=sfx->next_sfx)
194    sfx->dist_from_camera_sqrd=(sfx->x - listener.x) * (sfx->x - listener.x) +
195                               (sfx->y - listener.y) * (sfx->y - listener.y) +
196                               (sfx->h - listener.z) * (sfx->h - listener.z);
197
198
199  if (game_is_running)
200  {   
201    if (!g1_music_stream)  // no more music, go to the next song
202    {
203      if (music_count_down)  // this prevents trying to open a missing music file quickly
204        music_count_down--;
205      else
206      {
207        li_object *music=li_get_value("music");
208        if (music)
209        {
210          li_class *c=li_class::get(music,0);
211          li_object *list=li_cdr(li_class_get_property_list(c->type(), li_get_symbol("songs")),0), *o;
212          li_object *cur_song=c->value("songs");
213
214          for (o=list; o && li_car(o,0)!=cur_song; o=li_cdr(o,0));
215
216          if (o && !loop_current_song)
217            o=li_cdr(o,0);
218       
219          if (!o)
220            o=list;
221   
222          if (o)   
223          {
224            cur_song=li_car(o,0);
225            c->set("songs", cur_song);
226          }
227          else
228            cur_song=0;
229
230          if (cur_song)
231            restart_song(li_get_string(cur_song,0));
232
233        }
234      }
235    }
236
237    if (g1_music_stream)
238      if (!g1_music_stream->poll())
239      {
240        delete g1_music_stream;
241        g1_music_stream=0;
242      }
243
244    for (g1_sfx_obj_class *s=sfx_obj_list; s; s=s->next_sfx)
245    {
246      if (s->stream)
247      {
248        float d=s->max_hearable_distance;
249        if (s->dist_from_camera_sqrd>d*d || !s->stream->poll())
250        {
251          delete s->stream;
252          s->stream=0;
253          s->current_delay=s->restart_delay;
254          if (s->random_restart_delay)
255            s->current_delay+=(i4_rand()%s->random_restart_delay);         
256        }
257      }
258
259      if (!s->stream)
260      {
261        if (s->current_delay)
262          s->current_delay--;
263        else if (s->dist_from_camera_sqrd<
264                 s->max_hearable_distance*s->max_hearable_distance && s->filename)
265        {
266          if (i4_sound_man!=&i4_null_sound)
267          {
268            i4_file_class *fp=i4_open(*s->filename, I4_READ | I4_SUPPORT_ASYNC | I4_NO_BUFFER);
269            if (!fp)
270            {
271              i4_filename_struct fs;
272              i4_split_path(*s->filename, fs);
273
274              char fn[256];
275              sprintf(fn, "%s/ambient/%s.%s", s1_get_sfx_path(), fs.filename, fs.extension);
276              fp=i4_open(fn, I4_READ | I4_SUPPORT_ASYNC | I4_NO_BUFFER);
277            }
278
279            if (fp)
280            {
281              i4_bool loop=i4_F;
282              if (s->restart_delay==0 && s->random_restart_delay==0)
283                loop=i4_T;
284
285              s->stream=new i4_stream_wav_player(fp, 64*1024, loop, i4_T);
286              if (s->stream->load_failed())
287              {
288                delete s->stream;
289                s->stream=0;
290                s->current_delay=10000;
291              }
292            }
293            else s->current_delay=10000;
294          }
295        }
296      }
297
298      if (s->stream)
299      {
300        float d=sqrt(s->dist_from_camera_sqrd);
301        i4_float volume_scale = 1.f - (d / (float)s->max_hearable_distance);
302
303        sw32 vol = i4_f_to_i(volume_scale * (float)s->max_volume);
304       
305        s->stream->set_volume(vol);
306      }
307
308    }
309  }
310}
311
312void g1_sound_manager_class::add_sfx_to_list(g1_sfx_obj_class *sfx)
313{
314  sfx->next_sfx=sfx_obj_list;
315  sfx_obj_list=sfx;
316}
317
318void g1_sound_manager_class::remove_sfx_from_list(g1_sfx_obj_class *sfx)
319{
320  g1_sfx_obj_class *last=0, *p;
321  for (p=sfx_obj_list; p && p!=sfx;)
322  {
323    last=p;
324    p=p->next_sfx;
325  }
326 
327  I4_ASSERT(p, "remove_sfx_from_list : not in list");
328  if (last)
329    last->next_sfx=p->next_sfx;
330  else sfx_obj_list=p->next_sfx;
331}
332
333
334
335li_object *g1_list_sfx(li_object *o, li_environment *env)
336{
337  i4_file_class *fp=i4_open("sfx_list.txt", I4_WRITE);
338  if (fp)
339  {
340    s1_save_sfx_list(fp);
341
342    for (g1_sfx_obj_class *s=g1_sound_man.sfx_obj_list; s; s=s->next_sfx)
343    {
344      char fname[256];
345      i4_os_string(*s->filename, fname, 256);
346   
347      fp->printf("filename '%s' : location (%f,%f,%f), global_id=%d\n",
348                 fname, s->x, s->y, s->h, s->global_id);
349
350    }
351    delete fp;
352  }
353
354  return 0;
355}
356
357
358
359class g1_sound_man_updater : public i4_idle_class
360
361public:
362  virtual void idle()
363  {
364    if (g1_music_stream)
365      g1_sound_man.poll(i4_T);             // update sound effects (play next narative sfx)
366  }
367
368
369} g1_sound_man_updater_instance;
370
371
372
373static li_object *next_song(li_object *o, li_environment *env)
374{
375  g1_sound_man.next_song();
376  return 0;
377}
378
379
380li_automatic_add_function(g1_list_sfx, "list_sfx");
381li_automatic_add_function(next_song, "next_song");
Note: See TracBrowser for help on using the repository browser.