sound_man.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include "sound_man.hh"
  9. #include "loaders/wav_load.hh"
  10. #include "memory/malloc.hh"
  11. #include "math/random.hh"
  12. #include "objs/sfx_obj.hh"
  13. #include "music/stream.hh"
  14. #include "file/file.hh"
  15. #include "time/profile.hh"
  16. #include "string/string.hh"
  17. #include "sound/sfx_id.hh"
  18. #include "lisp/lisp.hh"
  19. #include "lisp/li_init.hh"
  20. #include "lisp/li_dialog.hh"
  21. #include "lisp/li_class.hh"
  22. #include "status/status.hh"
  23. g1_sound_manager_class g1_sound_man;
  24. i4_stream_wav_player *g1_music_stream=0;
  25. static int music_count_down=0;
  26. g1_sound_manager_class::g1_sound_manager_class()
  27. {
  28. sfx_obj_list=0;
  29. loop_current_song=i4_F;
  30. }
  31. void g1_sound_manager_class::next_song()
  32. {
  33. if (g1_music_stream)
  34. {
  35. delete g1_music_stream;
  36. g1_music_stream=0;
  37. }
  38. }
  39. void g1_sound_manager_class::pause()
  40. {
  41. for (g1_sfx_obj_class *s=sfx_obj_list; s; s=s->next_sfx)
  42. if (s->stream)
  43. s->stream->pause();
  44. if (g1_music_stream)
  45. g1_music_stream->pause();
  46. }
  47. void g1_sound_manager_class::unpause()
  48. {
  49. for (g1_sfx_obj_class *s=sfx_obj_list; s; s=s->next_sfx)
  50. if (s->stream)
  51. s->stream->unpause();
  52. if (g1_music_stream)
  53. g1_music_stream->unpause();
  54. }
  55. inline float dist_comp(i4_3d_vector &a, i4_3d_vector &b)
  56. {
  57. 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)));
  58. }
  59. static int get_music_volume()
  60. {
  61. li_object *music=li_get_value("music");
  62. if (music)
  63. {
  64. li_class *c=li_class::get(music,0);
  65. return li_get_int(c->value("volume"),0);
  66. }
  67. else return 0;
  68. }
  69. static void set_music_volume(int vol)
  70. {
  71. li_object *music=li_get_value("music");
  72. if (music)
  73. {
  74. li_class *c=li_class::get(music,0);
  75. if (vol>I4_SOUND_VOLUME_LEVELS-1)
  76. vol=I4_SOUND_VOLUME_LEVELS-1;
  77. if (vol<0) vol=0;
  78. if (g1_music_stream)
  79. g1_music_stream->set_volume(vol);
  80. c->set("volume", new li_int(vol));
  81. }
  82. }
  83. static void restart_song(char *newfn)
  84. {
  85. if (g1_music_stream)
  86. {
  87. delete g1_music_stream;
  88. g1_music_stream=0;
  89. }
  90. char fn[100];
  91. sprintf(fn, "%s/%s", s1_get_sfx_path(), newfn);
  92. if (i4_sound_man==&i4_null_sound)
  93. music_count_down=100;
  94. else
  95. {
  96. i4_file_class *fp=i4_open(fn, I4_SUPPORT_ASYNC | I4_READ);
  97. if (fp)
  98. {
  99. g1_music_stream=new i4_stream_wav_player(fp, 256*1024, i4_F, i4_T);
  100. if (g1_music_stream->load_failed())
  101. {
  102. delete g1_music_stream;
  103. g1_music_stream=0;
  104. }
  105. else
  106. set_music_volume(get_music_volume());
  107. }
  108. else
  109. music_count_down=100; // wait 10 seconds before trying again
  110. }
  111. }
  112. li_object *g1_set_song(li_object *o, li_environment *env)
  113. {
  114. li_class *oldv=li_class::get(li_second(o,env),env);
  115. li_class *newv=li_class::get(li_first(o,env),env);
  116. char *oldfn=li_get_string(oldv->value("songs"),env);
  117. char *newfn=li_get_string(newv->value("songs"),env);
  118. if (strcmp(oldfn,newfn))
  119. restart_song(newfn);
  120. if (g1_music_stream)
  121. {
  122. int vol=li_get_int(newv->value("volume"),env);
  123. g1_music_stream->set_volume(vol);
  124. }
  125. return 0;
  126. }
  127. li_object *g1_edit_music(li_object *o, li_environment *env)
  128. {
  129. li_create_dialog("Music",
  130. li_get_value("music"),
  131. 0,
  132. g1_set_song);
  133. return 0;
  134. }
  135. li_object *g1_music_up(li_object *o, li_environment *env)
  136. {
  137. set_music_volume(get_music_volume()+8);
  138. return 0;
  139. }
  140. li_object *g1_music_down(li_object *o, li_environment *env)
  141. {
  142. set_music_volume(get_music_volume()-8);
  143. return 0;
  144. }
  145. li_automatic_add_function(g1_edit_music, "edit_music");
  146. li_automatic_add_function(g1_music_up, "music_up");
  147. li_automatic_add_function(g1_music_down, "music_down");
  148. // should be calle by main game every tick, check to see if a new narative should be played
  149. void g1_sound_manager_class::poll(i4_bool game_is_running)
  150. {
  151. i4_3d_vector listener;
  152. s1_get_camera_pos(listener);
  153. for (g1_sfx_obj_class *sfx=sfx_obj_list; sfx; sfx=sfx->next_sfx)
  154. sfx->dist_from_camera_sqrd=(sfx->x - listener.x) * (sfx->x - listener.x) +
  155. (sfx->y - listener.y) * (sfx->y - listener.y) +
  156. (sfx->h - listener.z) * (sfx->h - listener.z);
  157. if (game_is_running)
  158. {
  159. if (!g1_music_stream) // no more music, go to the next song
  160. {
  161. if (music_count_down) // this prevents trying to open a missing music file quickly
  162. music_count_down--;
  163. else
  164. {
  165. li_object *music=li_get_value("music");
  166. if (music)
  167. {
  168. li_class *c=li_class::get(music,0);
  169. li_object *list=li_cdr(li_class_get_property_list(c->type(), li_get_symbol("songs")),0), *o;
  170. li_object *cur_song=c->value("songs");
  171. for (o=list; o && li_car(o,0)!=cur_song; o=li_cdr(o,0));
  172. if (o && !loop_current_song)
  173. o=li_cdr(o,0);
  174. if (!o)
  175. o=list;
  176. if (o)
  177. {
  178. cur_song=li_car(o,0);
  179. c->set("songs", cur_song);
  180. }
  181. else
  182. cur_song=0;
  183. if (cur_song)
  184. restart_song(li_get_string(cur_song,0));
  185. }
  186. }
  187. }
  188. if (g1_music_stream)
  189. if (!g1_music_stream->poll())
  190. {
  191. delete g1_music_stream;
  192. g1_music_stream=0;
  193. }
  194. for (g1_sfx_obj_class *s=sfx_obj_list; s; s=s->next_sfx)
  195. {
  196. if (s->stream)
  197. {
  198. float d=s->max_hearable_distance;
  199. if (s->dist_from_camera_sqrd>d*d || !s->stream->poll())
  200. {
  201. delete s->stream;
  202. s->stream=0;
  203. s->current_delay=s->restart_delay;
  204. if (s->random_restart_delay)
  205. s->current_delay+=(i4_rand()%s->random_restart_delay);
  206. }
  207. }
  208. if (!s->stream)
  209. {
  210. if (s->current_delay)
  211. s->current_delay--;
  212. else if (s->dist_from_camera_sqrd<
  213. s->max_hearable_distance*s->max_hearable_distance && s->filename)
  214. {
  215. if (i4_sound_man!=&i4_null_sound)
  216. {
  217. i4_file_class *fp=i4_open(*s->filename, I4_READ | I4_SUPPORT_ASYNC | I4_NO_BUFFER);
  218. if (!fp)
  219. {
  220. i4_filename_struct fs;
  221. i4_split_path(*s->filename, fs);
  222. char fn[256];
  223. sprintf(fn, "%s/ambient/%s.%s", s1_get_sfx_path(), fs.filename, fs.extension);
  224. fp=i4_open(fn, I4_READ | I4_SUPPORT_ASYNC | I4_NO_BUFFER);
  225. }
  226. if (fp)
  227. {
  228. i4_bool loop=i4_F;
  229. if (s->restart_delay==0 && s->random_restart_delay==0)
  230. loop=i4_T;
  231. s->stream=new i4_stream_wav_player(fp, 64*1024, loop, i4_T);
  232. if (s->stream->load_failed())
  233. {
  234. delete s->stream;
  235. s->stream=0;
  236. s->current_delay=10000;
  237. }
  238. }
  239. else s->current_delay=10000;
  240. }
  241. }
  242. }
  243. if (s->stream)
  244. {
  245. float d=sqrt(s->dist_from_camera_sqrd);
  246. i4_float volume_scale = 1.f - (d / (float)s->max_hearable_distance);
  247. sw32 vol = i4_f_to_i(volume_scale * (float)s->max_volume);
  248. s->stream->set_volume(vol);
  249. }
  250. }
  251. }
  252. }
  253. void g1_sound_manager_class::add_sfx_to_list(g1_sfx_obj_class *sfx)
  254. {
  255. sfx->next_sfx=sfx_obj_list;
  256. sfx_obj_list=sfx;
  257. }
  258. void g1_sound_manager_class::remove_sfx_from_list(g1_sfx_obj_class *sfx)
  259. {
  260. g1_sfx_obj_class *last=0, *p;
  261. for (p=sfx_obj_list; p && p!=sfx;)
  262. {
  263. last=p;
  264. p=p->next_sfx;
  265. }
  266. I4_ASSERT(p, "remove_sfx_from_list : not in list");
  267. if (last)
  268. last->next_sfx=p->next_sfx;
  269. else sfx_obj_list=p->next_sfx;
  270. }
  271. li_object *g1_list_sfx(li_object *o, li_environment *env)
  272. {
  273. i4_file_class *fp=i4_open("sfx_list.txt", I4_WRITE);
  274. if (fp)
  275. {
  276. s1_save_sfx_list(fp);
  277. for (g1_sfx_obj_class *s=g1_sound_man.sfx_obj_list; s; s=s->next_sfx)
  278. {
  279. char fname[256];
  280. i4_os_string(*s->filename, fname, 256);
  281. fp->printf("filename '%s' : location (%f,%f,%f), global_id=%d\n",
  282. fname, s->x, s->y, s->h, s->global_id);
  283. }
  284. delete fp;
  285. }
  286. return 0;
  287. }
  288. class g1_sound_man_updater : public i4_idle_class
  289. {
  290. public:
  291. virtual void idle()
  292. {
  293. if (g1_music_stream)
  294. g1_sound_man.poll(i4_T); // update sound effects (play next narative sfx)
  295. }
  296. } g1_sound_man_updater_instance;
  297. static li_object *next_song(li_object *o, li_environment *env)
  298. {
  299. g1_sound_man.next_song();
  300. return 0;
  301. }
  302. li_automatic_add_function(g1_list_sfx, "list_sfx");
  303. li_automatic_add_function(next_song, "next_song");