soundsrv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. // Emacs style mode select -*- C++ -*-
  2. //-----------------------------------------------------------------------------
  3. //
  4. // $Id: soundsrv.c,v 1.3 1997/01/29 22:40:44 b1 Exp $
  5. //
  6. // Copyright (C) 1993-1996 by id Software, Inc.
  7. //
  8. // This source is available for distribution and/or modification
  9. // only under the terms of the DOOM Source Code License as
  10. // published by id Software. All rights reserved.
  11. //
  12. // The source is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
  15. // for more details.
  16. //
  17. //
  18. // $Log: soundsrv.c,v $
  19. // Revision 1.3 1997/01/29 22:40:44 b1
  20. // Reformatting, S (sound) module files.
  21. //
  22. // Revision 1.2 1997/01/21 19:00:07 b1
  23. // First formatting run:
  24. // using Emacs cc-mode.el indentation for C++ now.
  25. //
  26. // Revision 1.1 1997/01/19 17:22:50 b1
  27. // Initial check in DOOM sources as of Jan. 10th, 1997
  28. //
  29. //
  30. // DESCRIPTION:
  31. // UNIX soundserver, run as a separate process,
  32. // started by DOOM program.
  33. // Originally conceived fopr SGI Irix,
  34. // mostly used with Linux voxware.
  35. //
  36. //-----------------------------------------------------------------------------
  37. static const char rcsid[] = "$Id: soundsrv.c,v 1.3 1997/01/29 22:40:44 b1 Exp $";
  38. #include <math.h>
  39. #include <sys/types.h>
  40. #include <stdio.h>
  41. #include <fcntl.h>
  42. #include <sys/ioctl.h>
  43. #include <unistd.h>
  44. #include <stdlib.h>
  45. #include <malloc.h>
  46. #include <sys/stat.h>
  47. #include <sys/time.h>
  48. #include "sounds.h"
  49. #include "soundsrv.h"
  50. #include "wadread.h"
  51. //
  52. // Department of Redundancy Department.
  53. //
  54. typedef struct wadinfo_struct
  55. {
  56. // should be IWAD
  57. char identification[4];
  58. int numlumps;
  59. int infotableofs;
  60. } wadinfo_t;
  61. typedef struct filelump_struct
  62. {
  63. int filepos;
  64. int size;
  65. char name[8];
  66. } filelump_t;
  67. // an internal time keeper
  68. static int mytime = 0;
  69. // number of sound effects
  70. int numsounds;
  71. // longest sound effect
  72. int longsound;
  73. // lengths of all sound effects
  74. int lengths[NUMSFX];
  75. // mixing buffer
  76. signed short mixbuffer[MIXBUFFERSIZE];
  77. // file descriptor of sfx device
  78. int sfxdevice;
  79. // file descriptor of music device
  80. int musdevice;
  81. // the channel data pointers
  82. unsigned char* channels[8];
  83. // the channel step amount
  84. unsigned int channelstep[8];
  85. // 0.16 bit remainder of last step
  86. unsigned int channelstepremainder[8];
  87. // the channel data end pointers
  88. unsigned char* channelsend[8];
  89. // time that the channel started playing
  90. int channelstart[8];
  91. // the channel handles
  92. int channelhandles[8];
  93. // the channel left volume lookup
  94. int* channelleftvol_lookup[8];
  95. // the channel right volume lookup
  96. int* channelrightvol_lookup[8];
  97. // sfx id of the playing sound effect
  98. int channelids[8];
  99. int snd_verbose=1;
  100. int steptable[256];
  101. int vol_lookup[128*256];
  102. static void derror(char* msg)
  103. {
  104. fprintf(stderr, "error: %s\n", msg);
  105. exit(-1);
  106. }
  107. int mix(void)
  108. {
  109. register int dl;
  110. register int dr;
  111. register unsigned int sample;
  112. signed short* leftout;
  113. signed short* rightout;
  114. signed short* leftend;
  115. int step;
  116. leftout = mixbuffer;
  117. rightout = mixbuffer+1;
  118. step = 2;
  119. leftend = mixbuffer + SAMPLECOUNT*step;
  120. // mix into the mixing buffer
  121. while (leftout != leftend)
  122. {
  123. dl = 0;
  124. dr = 0;
  125. if (channels[0])
  126. {
  127. sample = *channels[0];
  128. dl += channelleftvol_lookup[0][sample];
  129. dr += channelrightvol_lookup[0][sample];
  130. channelstepremainder[0] += channelstep[0];
  131. channels[0] += channelstepremainder[0] >> 16;
  132. channelstepremainder[0] &= 65536-1;
  133. if (channels[0] >= channelsend[0])
  134. channels[0] = 0;
  135. }
  136. if (channels[1])
  137. {
  138. sample = *channels[1];
  139. dl += channelleftvol_lookup[1][sample];
  140. dr += channelrightvol_lookup[1][sample];
  141. channelstepremainder[1] += channelstep[1];
  142. channels[1] += channelstepremainder[1] >> 16;
  143. channelstepremainder[1] &= 65536-1;
  144. if (channels[1] >= channelsend[1])
  145. channels[1] = 0;
  146. }
  147. if (channels[2])
  148. {
  149. sample = *channels[2];
  150. dl += channelleftvol_lookup[2][sample];
  151. dr += channelrightvol_lookup[2][sample];
  152. channelstepremainder[2] += channelstep[2];
  153. channels[2] += channelstepremainder[2] >> 16;
  154. channelstepremainder[2] &= 65536-1;
  155. if (channels[2] >= channelsend[2])
  156. channels[2] = 0;
  157. }
  158. if (channels[3])
  159. {
  160. sample = *channels[3];
  161. dl += channelleftvol_lookup[3][sample];
  162. dr += channelrightvol_lookup[3][sample];
  163. channelstepremainder[3] += channelstep[3];
  164. channels[3] += channelstepremainder[3] >> 16;
  165. channelstepremainder[3] &= 65536-1;
  166. if (channels[3] >= channelsend[3])
  167. channels[3] = 0;
  168. }
  169. if (channels[4])
  170. {
  171. sample = *channels[4];
  172. dl += channelleftvol_lookup[4][sample];
  173. dr += channelrightvol_lookup[4][sample];
  174. channelstepremainder[4] += channelstep[4];
  175. channels[4] += channelstepremainder[4] >> 16;
  176. channelstepremainder[4] &= 65536-1;
  177. if (channels[4] >= channelsend[4])
  178. channels[4] = 0;
  179. }
  180. if (channels[5])
  181. {
  182. sample = *channels[5];
  183. dl += channelleftvol_lookup[5][sample];
  184. dr += channelrightvol_lookup[5][sample];
  185. channelstepremainder[5] += channelstep[5];
  186. channels[5] += channelstepremainder[5] >> 16;
  187. channelstepremainder[5] &= 65536-1;
  188. if (channels[5] >= channelsend[5])
  189. channels[5] = 0;
  190. }
  191. if (channels[6])
  192. {
  193. sample = *channels[6];
  194. dl += channelleftvol_lookup[6][sample];
  195. dr += channelrightvol_lookup[6][sample];
  196. channelstepremainder[6] += channelstep[6];
  197. channels[6] += channelstepremainder[6] >> 16;
  198. channelstepremainder[6] &= 65536-1;
  199. if (channels[6] >= channelsend[6])
  200. channels[6] = 0;
  201. }
  202. if (channels[7])
  203. {
  204. sample = *channels[7];
  205. dl += channelleftvol_lookup[7][sample];
  206. dr += channelrightvol_lookup[7][sample];
  207. channelstepremainder[7] += channelstep[7];
  208. channels[7] += channelstepremainder[7] >> 16;
  209. channelstepremainder[7] &= 65536-1;
  210. if (channels[7] >= channelsend[7])
  211. channels[7] = 0;
  212. }
  213. // Has been char instead of short.
  214. // if (dl > 127) *leftout = 127;
  215. // else if (dl < -128) *leftout = -128;
  216. // else *leftout = dl;
  217. // if (dr > 127) *rightout = 127;
  218. // else if (dr < -128) *rightout = -128;
  219. // else *rightout = dr;
  220. if (dl > 0x7fff)
  221. *leftout = 0x7fff;
  222. else if (dl < -0x8000)
  223. *leftout = -0x8000;
  224. else
  225. *leftout = dl;
  226. if (dr > 0x7fff)
  227. *rightout = 0x7fff;
  228. else if (dr < -0x8000)
  229. *rightout = -0x8000;
  230. else
  231. *rightout = dr;
  232. leftout += step;
  233. rightout += step;
  234. }
  235. return 1;
  236. }
  237. void
  238. grabdata
  239. ( int c,
  240. char** v )
  241. {
  242. int i;
  243. char* name;
  244. char* doom1wad;
  245. char* doomwad;
  246. char* doomuwad;
  247. char* doom2wad;
  248. char* doom2fwad;
  249. // Now where are TNT and Plutonia. Yuck.
  250. // char *home;
  251. char* doomwaddir;
  252. doomwaddir = getenv("DOOMWADDIR");
  253. if (!doomwaddir)
  254. doomwaddir = ".";
  255. doom1wad = malloc(strlen(doomwaddir)+1+9+1);
  256. sprintf(doom1wad, "%s/doom1.wad", doomwaddir);
  257. doom2wad = malloc(strlen(doomwaddir)+1+9+1);
  258. sprintf(doom2wad, "%s/doom2.wad", doomwaddir);
  259. doom2fwad = malloc(strlen(doomwaddir)+1+10+1);
  260. sprintf(doom2fwad, "%s/doom2f.wad", doomwaddir);
  261. doomuwad = malloc(strlen(doomwaddir)+1+8+1);
  262. sprintf(doomuwad, "%s/doomu.wad", doomwaddir);
  263. doomwad = malloc(strlen(doomwaddir)+1+8+1);
  264. sprintf(doomwad, "%s/doom.wad", doomwaddir);
  265. // home = getenv("HOME");
  266. // if (!home)
  267. // derror("Please set $HOME to your home directory");
  268. // sprintf(basedefault, "%s/.doomrc", home);
  269. for (i=1 ; i<c ; i++)
  270. {
  271. if (!strcmp(v[i], "-quiet"))
  272. {
  273. snd_verbose = 0;
  274. }
  275. }
  276. numsounds = NUMSFX;
  277. longsound = 0;
  278. if (! access(doom2fwad, R_OK) )
  279. name = doom2fwad;
  280. else if (! access(doom2wad, R_OK) )
  281. name = doom2wad;
  282. else if (! access(doomuwad, R_OK) )
  283. name = doomuwad;
  284. else if (! access(doomwad, R_OK) )
  285. name = doomwad;
  286. else if (! access(doom1wad, R_OK) )
  287. name = doom1wad;
  288. // else if (! access(DEVDATA "doom2.wad", R_OK) )
  289. // name = DEVDATA "doom2.wad";
  290. // else if (! access(DEVDATA "doom.wad", R_OK) )
  291. // name = DEVDATA "doom.wad";
  292. else
  293. {
  294. fprintf(stderr, "Could not find wadfile anywhere\n");
  295. exit(-1);
  296. }
  297. openwad(name);
  298. if (snd_verbose)
  299. fprintf(stderr, "loading from [%s]\n", name);
  300. for (i=1 ; i<NUMSFX ; i++)
  301. {
  302. if (!S_sfx[i].link)
  303. {
  304. S_sfx[i].data = getsfx(S_sfx[i].name, &lengths[i]);
  305. if (longsound < lengths[i]) longsound = lengths[i];
  306. } else {
  307. S_sfx[i].data = S_sfx[i].link->data;
  308. lengths[i] = lengths[(S_sfx[i].link - S_sfx)/sizeof(sfxinfo_t)];
  309. }
  310. // test only
  311. // {
  312. // int fd;
  313. // char name[10];
  314. // sprintf(name, "sfx%d", i);
  315. // fd = open(name, O_WRONLY|O_CREAT, 0644);
  316. // write(fd, S_sfx[i].data, lengths[i]);
  317. // close(fd);
  318. // }
  319. }
  320. }
  321. static struct timeval last={0,0};
  322. //static struct timeval now;
  323. static struct timezone whocares;
  324. void updatesounds(void)
  325. {
  326. mix();
  327. I_SubmitOutputBuffer(mixbuffer, SAMPLECOUNT);
  328. }
  329. int
  330. addsfx
  331. ( int sfxid,
  332. int volume,
  333. int step,
  334. int seperation )
  335. {
  336. static unsigned short handlenums = 0;
  337. int i;
  338. int rc = -1;
  339. int oldest = mytime;
  340. int oldestnum = 0;
  341. int slot;
  342. int rightvol;
  343. int leftvol;
  344. // play these sound effects
  345. // only one at a time
  346. if ( sfxid == sfx_sawup
  347. || sfxid == sfx_sawidl
  348. || sfxid == sfx_sawful
  349. || sfxid == sfx_sawhit
  350. || sfxid == sfx_stnmov
  351. || sfxid == sfx_pistol )
  352. {
  353. for (i=0 ; i<8 ; i++)
  354. {
  355. if (channels[i] && channelids[i] == sfxid)
  356. {
  357. channels[i] = 0;
  358. break;
  359. }
  360. }
  361. }
  362. for (i=0 ; i<8 && channels[i] ; i++)
  363. {
  364. if (channelstart[i] < oldest)
  365. {
  366. oldestnum = i;
  367. oldest = channelstart[i];
  368. }
  369. }
  370. if (i == 8)
  371. slot = oldestnum;
  372. else
  373. slot = i;
  374. channels[slot] = (unsigned char *) S_sfx[sfxid].data;
  375. channelsend[slot] = channels[slot] + lengths[sfxid];
  376. if (!handlenums)
  377. handlenums = 100;
  378. channelhandles[slot] = rc = handlenums++;
  379. channelstep[slot] = step;
  380. channelstepremainder[slot] = 0;
  381. channelstart[slot] = mytime;
  382. // (range: 1 - 256)
  383. seperation += 1;
  384. // (x^2 seperation)
  385. leftvol =
  386. volume - (volume*seperation*seperation)/(256*256);
  387. seperation = seperation - 257;
  388. // (x^2 seperation)
  389. rightvol =
  390. volume - (volume*seperation*seperation)/(256*256);
  391. // sanity check
  392. if (rightvol < 0 || rightvol > 127)
  393. derror("rightvol out of bounds");
  394. if (leftvol < 0 || leftvol > 127)
  395. derror("leftvol out of bounds");
  396. // get the proper lookup table piece
  397. // for this volume level
  398. channelleftvol_lookup[slot] = &vol_lookup[leftvol*256];
  399. channelrightvol_lookup[slot] = &vol_lookup[rightvol*256];
  400. channelids[slot] = sfxid;
  401. return rc;
  402. }
  403. void outputushort(int num)
  404. {
  405. static unsigned char buff[5] = { 0, 0, 0, 0, '\n' };
  406. static char* badbuff = "xxxx\n";
  407. // outputs a 16-bit # in hex or "xxxx" if -1.
  408. if (num < 0)
  409. {
  410. write(1, badbuff, 5);
  411. }
  412. else
  413. {
  414. buff[0] = num>>12;
  415. buff[0] += buff[0] > 9 ? 'a'-10 : '0';
  416. buff[1] = (num>>8) & 0xf;
  417. buff[1] += buff[1] > 9 ? 'a'-10 : '0';
  418. buff[2] = (num>>4) & 0xf;
  419. buff[2] += buff[2] > 9 ? 'a'-10 : '0';
  420. buff[3] = num & 0xf;
  421. buff[3] += buff[3] > 9 ? 'a'-10 : '0';
  422. write(1, buff, 5);
  423. }
  424. }
  425. void initdata(void)
  426. {
  427. int i;
  428. int j;
  429. int* steptablemid = steptable + 128;
  430. for (i=0 ;
  431. i<sizeof(channels)/sizeof(unsigned char *) ;
  432. i++)
  433. {
  434. channels[i] = 0;
  435. }
  436. gettimeofday(&last, &whocares);
  437. for (i=-128 ; i<128 ; i++)
  438. steptablemid[i] = pow(2.0, (i/64.0))*65536.0;
  439. // generates volume lookup tables
  440. // which also turn the unsigned samples
  441. // into signed samples
  442. // for (i=0 ; i<128 ; i++)
  443. // for (j=0 ; j<256 ; j++)
  444. // vol_lookup[i*256+j] = (i*(j-128))/127;
  445. for (i=0 ; i<128 ; i++)
  446. for (j=0 ; j<256 ; j++)
  447. vol_lookup[i*256+j] = (i*(j-128)*256)/127;
  448. }
  449. void quit(void)
  450. {
  451. I_ShutdownMusic();
  452. I_ShutdownSound();
  453. exit(0);
  454. }
  455. fd_set fdset;
  456. fd_set scratchset;
  457. int
  458. main
  459. ( int c,
  460. char** v )
  461. {
  462. int done = 0;
  463. int rc;
  464. int nrc;
  465. int sndnum;
  466. int handle = 0;
  467. unsigned char commandbuf[10];
  468. struct timeval zerowait = { 0, 0 };
  469. int step;
  470. int vol;
  471. int sep;
  472. int i;
  473. int waitingtofinish=0;
  474. // get sound data
  475. grabdata(c, v);
  476. // init any data
  477. initdata();
  478. I_InitSound(11025, 16);
  479. I_InitMusic();
  480. if (snd_verbose)
  481. fprintf(stderr, "ready\n");
  482. // parse commands and play sounds until done
  483. FD_ZERO(&fdset);
  484. FD_SET(0, &fdset);
  485. while (!done)
  486. {
  487. mytime++;
  488. if (!waitingtofinish)
  489. {
  490. do {
  491. scratchset = fdset;
  492. rc = select(FD_SETSIZE, &scratchset, 0, 0, &zerowait);
  493. if (rc > 0)
  494. {
  495. // fprintf(stderr, "select is true\n");
  496. // got a command
  497. nrc = read(0, commandbuf, 1);
  498. if (!nrc)
  499. {
  500. done = 1;
  501. rc = 0;
  502. }
  503. else
  504. {
  505. if (snd_verbose)
  506. fprintf(stderr, "cmd: %c", commandbuf[0]);
  507. switch (commandbuf[0])
  508. {
  509. case 'p':
  510. // play a new sound effect
  511. read(0, commandbuf, 9);
  512. if (snd_verbose)
  513. {
  514. commandbuf[9]=0;
  515. fprintf(stderr, "%s\n", commandbuf);
  516. }
  517. commandbuf[0] -=
  518. commandbuf[0]>='a' ? 'a'-10 : '0';
  519. commandbuf[1] -=
  520. commandbuf[1]>='a' ? 'a'-10 : '0';
  521. commandbuf[2] -=
  522. commandbuf[2]>='a' ? 'a'-10 : '0';
  523. commandbuf[3] -=
  524. commandbuf[3]>='a' ? 'a'-10 : '0';
  525. commandbuf[4] -=
  526. commandbuf[4]>='a' ? 'a'-10 : '0';
  527. commandbuf[5] -=
  528. commandbuf[5]>='a' ? 'a'-10 : '0';
  529. commandbuf[6] -=
  530. commandbuf[6]>='a' ? 'a'-10 : '0';
  531. commandbuf[7] -=
  532. commandbuf[7]>='a' ? 'a'-10 : '0';
  533. // p<snd#><step><vol><sep>
  534. sndnum = (commandbuf[0]<<4) + commandbuf[1];
  535. step = (commandbuf[2]<<4) + commandbuf[3];
  536. step = steptable[step];
  537. vol = (commandbuf[4]<<4) + commandbuf[5];
  538. sep = (commandbuf[6]<<4) + commandbuf[7];
  539. handle = addsfx(sndnum, vol, step, sep);
  540. // returns the handle
  541. // outputushort(handle);
  542. break;
  543. case 'q':
  544. read(0, commandbuf, 1);
  545. waitingtofinish = 1; rc = 0;
  546. break;
  547. case 's':
  548. {
  549. int fd;
  550. read(0, commandbuf, 3);
  551. commandbuf[2] = 0;
  552. fd = open((char*)commandbuf, O_CREAT|O_WRONLY, 0644);
  553. commandbuf[0] -= commandbuf[0]>='a' ? 'a'-10 : '0';
  554. commandbuf[1] -= commandbuf[1]>='a' ? 'a'-10 : '0';
  555. sndnum = (commandbuf[0]<<4) + commandbuf[1];
  556. write(fd, S_sfx[sndnum].data, lengths[sndnum]);
  557. close(fd);
  558. }
  559. break;
  560. default:
  561. fprintf(stderr, "Did not recognize command\n");
  562. break;
  563. }
  564. }
  565. }
  566. else if (rc < 0)
  567. {
  568. quit();
  569. }
  570. } while (rc > 0);
  571. }
  572. updatesounds();
  573. if (waitingtofinish)
  574. {
  575. for(i=0 ; i<8 && !channels[i] ; i++);
  576. if (i==8)
  577. done=1;
  578. }
  579. }
  580. quit();
  581. return 0;
  582. }