SndUnixT.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /** EMULib Emulation Library *********************************/
  2. /** **/
  3. /** SndUnix.c **/
  4. /** **/
  5. /** This file contains standard sound generation routines **/
  6. /** for Unix using /dev/dsp and /dev/audio. **/
  7. /** **/
  8. /** Copyright (C) Marat Fayzullin 1996-2002 **/
  9. /** You are not allowed to distribute this software **/
  10. /** commercially. Please, notify me, if you make any **/
  11. /** changes to this file. **/
  12. /*************************************************************/
  13. #include "Sound.h"
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <fcntl.h>
  18. #include <pthread.h>
  19. #include <sys/ioctl.h>
  20. #include <os_dependent.h>
  21. #ifdef SUN_AUDIO
  22. #include <sys/audioio.h>
  23. #include <sys/conf.h>
  24. #include <stropts.h>
  25. #define AUDIO_CONV(A) (ULAW[0xFF&(128+(A))])
  26. static unsigned char ULAW[256] =
  27. {
  28. 31, 31, 31, 32, 32, 32, 32, 33,
  29. 33, 33, 33, 34, 34, 34, 34, 35,
  30. 35, 35, 35, 36, 36, 36, 36, 37,
  31. 37, 37, 37, 38, 38, 38, 38, 39,
  32. 39, 39, 39, 40, 40, 40, 40, 41,
  33. 41, 41, 41, 42, 42, 42, 42, 43,
  34. 43, 43, 43, 44, 44, 44, 44, 45,
  35. 45, 45, 45, 46, 46, 46, 46, 47,
  36. 47, 47, 47, 48, 48, 49, 49, 50,
  37. 50, 51, 51, 52, 52, 53, 53, 54,
  38. 54, 55, 55, 56, 56, 57, 57, 58,
  39. 58, 59, 59, 60, 60, 61, 61, 62,
  40. 62, 63, 63, 64, 65, 66, 67, 68,
  41. 69, 70, 71, 72, 73, 74, 75, 76,
  42. 77, 78, 79, 81, 83, 85, 87, 89,
  43. 91, 93, 95, 99, 103, 107, 111, 119,
  44. 255, 247, 239, 235, 231, 227, 223, 221,
  45. 219, 217, 215, 213, 211, 209, 207, 206,
  46. 205, 204, 203, 202, 201, 200, 199, 198,
  47. 219, 217, 215, 213, 211, 209, 207, 206,
  48. 205, 204, 203, 202, 201, 200, 199, 198,
  49. 197, 196, 195, 194, 193, 192, 191, 191,
  50. 190, 190, 189, 189, 188, 188, 187, 187,
  51. 186, 186, 185, 185, 184, 184, 183, 183,
  52. 182, 182, 181, 181, 180, 180, 179, 179,
  53. 178, 178, 177, 177, 176, 176, 175, 175,
  54. 175, 175, 174, 174, 174, 174, 173, 173,
  55. 173, 173, 172, 172, 172, 172, 171, 171,
  56. 171, 171, 170, 170, 170, 170, 169, 169,
  57. 169, 169, 168, 168, 168, 168, 167, 167,
  58. 167, 167, 166, 166, 166, 166, 165, 165,
  59. 165, 165, 164, 164, 164, 164, 163, 163
  60. };
  61. #else /* SUN_AUDIO */
  62. #ifdef __FreeBSD__
  63. #include <machine/soundcard.h>
  64. #endif
  65. #ifdef __NetBSD__
  66. #include <soundcard.h>
  67. #endif
  68. #ifdef __linux__
  69. #include <sys/soundcard.h>
  70. #endif
  71. #define AUDIO_CONV(A) (128+(A))
  72. #endif /* SUN_AUDIO */
  73. static pthread_t ThreadID;
  74. static int SoundFD;
  75. static int SoundRate = 0;
  76. static int MasterVolume = 64;
  77. static int MasterSwitch = (1<<SND_CHANNELS)-1;
  78. static int LoopFreq = 25;
  79. static int NoiseGen = 1;
  80. static int Suspended = 0;
  81. static struct
  82. {
  83. int Type; /* Channel type (SND_*) */
  84. int Freq; /* Channel frequency (Hz) */
  85. int Volume; /* Channel volume (0..255) */
  86. signed char *Data; /* Wave data (-128..127 each) */
  87. int Length; /* Wave length in Data */
  88. int Rate; /* Wave playback rate (or 0Hz) */
  89. int Pos; /* Wave current position in Data */
  90. int Count; /* Phase counter */
  91. } CH[SND_CHANNELS];
  92. static void UnixSetWave(int Channel, signed char *Data,int Length,int Freq);
  93. static void UnixSetSound(int Channel,int NewType);
  94. static void UnixDrum(int Type,int Force);
  95. static void UnixSetChannels(int Volume,int Switch);
  96. static void UnixSound(int Channel,int NewFreq,int NewVolume);
  97. static int OpenSoundDevice(int Rate,int Verbose);
  98. static void *DSPLoop(void *Arg);
  99. /** StopSound() **********************************************/
  100. /** Temporarily suspend sound. **/
  101. /*************************************************************/
  102. void StopSound(void) { Suspended=1; }
  103. /** ResumeSound() ********************************************/
  104. /** Resume sound after StopSound(). **/
  105. /*************************************************************/
  106. void ResumeSound(void) { Suspended=0; }
  107. /** OpenSoundDevice() ****************************************/
  108. /** Open /dev/dsp with a given level of sound quality. **/
  109. /** Returns 0 if failed or sound quality (Mode). **/
  110. /*************************************************************/
  111. static int OpenSoundDevice(int Rate,int Verbose)
  112. {
  113. int I,J,K;
  114. #ifdef SUN_AUDIO
  115. if(Verbose) console_printf(Console_Default, " Opening /dev/audio...");
  116. if((SoundFD=open("/dev/audio",O_WRONLY | O_NONBLOCK))==-1)
  117. {
  118. if(Verbose) puts("FAILED");
  119. return(0);
  120. }
  121. /*
  122. ** Sun's specific initialization should be here...
  123. ** We assume, that it's set to 8000Hz u-law mono right now.
  124. */
  125. #else /* SUN_AUDIO */
  126. /* At first, we need to open /dev/dsp: */
  127. if(Verbose) console_printf(Console_Default, " Opening /dev/dsp...");
  128. I=((SoundFD=open("/dev/dsp",O_WRONLY))<0);
  129. /* Set 8-bit sound */
  130. if(!I)
  131. {
  132. if(Verbose) console_printf(Console_Default, "OK\n Setting mode: 8bit...");
  133. J=AFMT_U8;
  134. I=(ioctl(SoundFD,SNDCTL_DSP_SETFMT,&J)<0);
  135. }
  136. /* Set mono sound */
  137. if(!I)
  138. {
  139. if(Verbose) console_printf(Console_Default, "mono...");
  140. J=0;
  141. I=(ioctl(SoundFD,SNDCTL_DSP_STEREO,&J)<0);
  142. }
  143. /* Set sampling rate */
  144. if(!I)
  145. {
  146. if(Verbose) console_printf(Console_Default, "OK\n Setting sampling rate: %dHz...",Rate);
  147. I=(ioctl(SoundFD,SNDCTL_DSP_SPEED,&Rate)<0);
  148. if(Verbose) console_printf(Console_Default, "(got %dHz)...",Rate);
  149. }
  150. /* Here we set the number of buffers to use */
  151. if(!I)
  152. {
  153. if(Verbose)
  154. printf
  155. (
  156. "OK\n Adjusting buffers: %d buffers %d bytes each...",
  157. SND_BUFFERS,1<<SND_BITS
  158. );
  159. /* Set buffer length and number of buffers */
  160. J=K=SND_BITS|(SND_BUFFERS<<16);
  161. I=(ioctl(SoundFD,SNDCTL_DSP_SETFRAGMENT,&J)<0);
  162. /* Buffer length as n, not 2^n! */
  163. if((J&0xFFFF)<16) J=(J&0xFFFF0000)|(1<<(J&0xFFFF));
  164. K=(1<<SND_BITS)|(SND_BUFFERS<<16);
  165. /* If something went wrong... */
  166. if(J!=K)
  167. {
  168. if((J>>16)<SND_BUFFERS) I=-1;
  169. if((J&0xFFFF)!=(1<<SND_BITS)) I=-1;
  170. }
  171. }
  172. /* If something failed, fall out */
  173. if(I) { if(Verbose) puts("FAILED");return(0); }
  174. #endif /* SUN_AUDIO */
  175. if(Verbose) puts("OK");
  176. return(Rate);
  177. }
  178. /** DSPLoop() ************************************************/
  179. /** Main loop of the sound server. **/
  180. /*************************************************************/
  181. static void *DSPLoop(void *Arg)
  182. {
  183. int Wave[SND_BUFSIZE];
  184. unsigned char Buf[SND_BUFSIZE];
  185. register int J,I,K,L,M,N,L1,L2,A1,A2,V;
  186. int FreqCount;
  187. int ret;
  188. L = N = A2 = 0;
  189. for(J=0;J<SND_CHANNELS;J++)
  190. {
  191. CH[J].Type = SND_MELODIC;
  192. CH[J].Count = 0;
  193. CH[J].Volume = 0;
  194. CH[J].Freq = 0;
  195. }
  196. FreqCount=SoundRate/SND_BUFSIZE;
  197. for(;;FreqCount-=LoopFreq)
  198. {
  199. /* If suspending sound... */
  200. if(Suspended)
  201. {
  202. /* Close sound device */
  203. #ifndef SUN_AUDIO
  204. ioctl(SoundFD,SNDCTL_DSP_RESET);
  205. #endif
  206. close(SoundFD);
  207. /* Suspend execution until Suspended=0 */
  208. while(Suspended) sleep(1);
  209. /* Reopen sound device */
  210. SoundRate=OpenSoundDevice(SoundRate,0);
  211. }
  212. /* Waveform generator */
  213. for(J=0,M=MasterSwitch;M&&(J<SND_CHANNELS);J++,M>>=1)
  214. if(CH[J].Freq&&(V=CH[J].Volume)&&(M&1))
  215. switch(CH[J].Type)
  216. {
  217. case SND_NOISE: /* White Noise */
  218. /* For high frequencies, recompute volume */
  219. if(CH[J].Freq<=SoundRate) K=0x10000*CH[J].Freq/SoundRate;
  220. else { V=V*SoundRate/CH[J].Freq;K=0x10000; }
  221. L1=CH[J].Count;
  222. V<<=7;
  223. for(I=0;I<SND_BUFSIZE;I++)
  224. {
  225. L1+=K;
  226. if(L1&0xFFFF0000)
  227. {
  228. L1&=0xFFFF;
  229. if((NoiseGen<<=1)&0x80000000) NoiseGen^=0x08000001;
  230. }
  231. Wave[I]+=NoiseGen&1? V:-V;
  232. }
  233. CH[J].Count=L1;
  234. break;
  235. case SND_WAVE: /* Custom Waveform */
  236. /* Waveform data must have correct length! */
  237. if(CH[J].Length<=0) break;
  238. /* Start counting */
  239. K = CH[J].Rate>0? (SoundRate<<15)/CH[J].Freq/CH[J].Rate
  240. : (SoundRate<<15)/CH[J].Freq/CH[J].Length;
  241. L1 = CH[J].Pos%CH[J].Length;
  242. L2 = CH[J].Count;
  243. A1 = CH[J].Data[L1]*V;
  244. /* If expecting interpolation... */
  245. if(L2<K)
  246. {
  247. /* Compute interpolation parameters */
  248. A2 = CH[J].Data[(L1+1)%CH[J].Length]*V;
  249. L = (L2>>15)+1;
  250. N = ((K-(L2&0x7FFF))>>15)+1;
  251. }
  252. /* Add waveform to the buffer */
  253. for(I=0;I<SND_BUFSIZE;I++)
  254. if(L2<K)
  255. {
  256. /* Interpolate linearly */
  257. Wave[I]+=A1+L*(A2-A1)/N;
  258. /* Next waveform step */
  259. L2+=0x8000;
  260. /* Next interpolation step */
  261. L++;
  262. }
  263. else
  264. {
  265. L1 = (L1+L2/K)%CH[J].Length;
  266. L2 = (L2%K)+0x8000;
  267. A1 = CH[J].Data[L1]*V;
  268. Wave[I]+=A1;
  269. /* If expecting interpolation... */
  270. if(L2<K)
  271. {
  272. /* Compute interpolation parameters */
  273. A2 = CH[J].Data[(L1+1)%CH[J].Length]*V;
  274. L = 1;
  275. N = ((K-L2)>>15)+1;
  276. }
  277. }
  278. /* End counting */
  279. CH[J].Pos = L1;
  280. CH[J].Count = L2;
  281. break;
  282. case SND_QS_DU0:
  283. /* Do not allow frequencies that are too high */
  284. if(CH[J].Freq>=SoundRate/3) break;
  285. K=0x10000*CH[J].Freq/SoundRate;
  286. L1=CH[J].Count;
  287. V<<=7;
  288. for(I=0;I<SND_BUFSIZE;I++)
  289. {
  290. L2=L1+K;
  291. Wave[I]+=L1&0x2000?(L2&0x8000? V:0):(L2&0x8000? 0:-V);
  292. L1=L2;
  293. }
  294. CH[J].Count=L1;
  295. break;
  296. case SND_QS_DU1:
  297. /* Do not allow frequencies that are too high */
  298. if(CH[J].Freq>=SoundRate/3) break;
  299. K=0x10000*CH[J].Freq/SoundRate;
  300. L1=CH[J].Count;
  301. V<<=7;
  302. for(I=0;I<SND_BUFSIZE;I++)
  303. {
  304. L2=L1+K;
  305. Wave[I]+=L1&0x4000?(L2&0x8000? V:0):(L2&0x8000? 0:-V);
  306. L1=L2;
  307. }
  308. CH[J].Count=L1;
  309. break;
  310. case SND_QS_DU3:
  311. /* Do not allow frequencies that are too high */
  312. if(CH[J].Freq>=SoundRate/3) break;
  313. K=0x10000*CH[J].Freq/SoundRate;
  314. L1=CH[J].Count;
  315. V<<=7;
  316. for(I=0;I<SND_BUFSIZE;I++)
  317. {
  318. L2=L1+K;
  319. Wave[I]+=L1&0xC000?(L2&0x4000? V:0):(L2&0xC000? 0:-V);
  320. L1=L2;
  321. }
  322. CH[J].Count=L1;
  323. break;
  324. case SND_QS_DU2:
  325. case SND_MELODIC: /* Melodic Sound */
  326. default: /* Default Sound */
  327. /* Do not allow frequencies that are too high */
  328. if(CH[J].Freq>=SoundRate/3) break;
  329. K=0x10000*CH[J].Freq/SoundRate;
  330. L1=CH[J].Count;
  331. V<<=7;
  332. for(I=0;I<SND_BUFSIZE;I++)
  333. {
  334. L2=L1+K;
  335. Wave[I]+=L1&0x8000? (L2&0x8000? V:0):(L2&0x8000? 0:-V);
  336. L1=L2;
  337. }
  338. CH[J].Count=L1;
  339. break;
  340. case SND_TRIANGLE: /* Default Sound */
  341. /* Do not allow frequencies that are too high */
  342. if(CH[J].Freq>=SoundRate/3) break;
  343. K=0x10000*CH[J].Freq/SoundRate;
  344. L1=CH[J].Count;
  345. V<<=7;
  346. for(I=0;I<SND_BUFSIZE;I++)
  347. {
  348. L2=L1+K;
  349. Wave[I]+= L1&0x2000?V:-V /*(L2&0x8000? V:0):(L2&0x8000? 0:-V)*/;
  350. L1=L2;
  351. }
  352. CH[J].Count=L1;
  353. break;
  354. }
  355. /* Mix and convert waveforms */
  356. for(J=0;J<SND_BUFSIZE;J++)
  357. {
  358. I=(Wave[J]*MasterVolume)>>16;
  359. I=I<-128? -128:I>127? 127:I;
  360. Buf[J]=AUDIO_CONV(I);
  361. Wave[J]=0;
  362. }
  363. if(SoundFD==-1) sleep(1);
  364. else
  365. {
  366. #ifdef SUN_AUDIO
  367. /* Flush output first, don't care about return status. After this
  368. ** write next buffer of audio data. This method produces a horrible
  369. ** click on each buffer :( Any ideas, how to fix this?
  370. */
  371. ioctl(SoundFD,AUDIO_DRAIN);
  372. write(SoundFD,Buf,SND_BUFSIZE);
  373. #else
  374. /* We'll block here until next DMA buffer becomes free. It happens
  375. ** once per (1<<SND_BITS)/SoundRate seconds.
  376. */
  377. ret = write(SoundFD,Buf,SND_BUFSIZE);
  378. #endif
  379. }
  380. }
  381. return(0);
  382. }
  383. /** InitSound() **********************************************/
  384. /** Initialize DSP. Returns Rate on success, 0 otherwise. **/
  385. /** Mode is 0 to skip initialization (will be silent). **/
  386. /*************************************************************/
  387. int InitSound(int Rate,int Verbose)
  388. {
  389. /* If sound was initialized, kill it */
  390. TrashSound();
  391. /* Silence requested */
  392. if(Rate<=0) return(0);
  393. /* Synthesis rate should be at least 8kHz */
  394. if(Rate<8192) Rate=44100;
  395. /* Initialize things */
  396. SoundRate = 0;
  397. SoundFD = -1;
  398. ThreadID = 0;
  399. Suspended = 0;
  400. /* Set driver functions */
  401. SndDriver.SetSound = UnixSetSound;
  402. SndDriver.Drum = UnixDrum;
  403. SndDriver.SetChannels = UnixSetChannels;
  404. SndDriver.Sound = UnixSound;
  405. SndDriver.SetWave = UnixSetWave;
  406. /* Open sound device */
  407. if(Verbose) puts("Starting sound server:");
  408. if(!(Rate=OpenSoundDevice(Rate,Verbose))) return(0);
  409. /* Create DSPLoop() thread */
  410. if(Verbose) console_printf(Console_Default, " Creating thread...");
  411. if(pthread_create(&ThreadID,0,DSPLoop,0))
  412. { if(Verbose) puts("FAILED");return(0); }
  413. /* Detach the thread */
  414. pthread_detach(ThreadID);
  415. /* Done */
  416. if(Verbose) puts("OK");
  417. return(SoundRate=Rate);
  418. }
  419. /** TrashSound() *********************************************/
  420. /** Shut DSP down. **/
  421. /*************************************************************/
  422. void TrashSound(void)
  423. {
  424. StopSound();
  425. console_printf(Console_Default, "%s: Kill thread...\n", __func__);
  426. if(ThreadID) pthread_cancel(ThreadID);
  427. console_printf(Console_Default, "%s: close /dev/xxx ...\n", __func__);
  428. if(SoundFD!=-1) close(SoundFD);
  429. SoundRate = 0;
  430. SoundFD = -1;
  431. ThreadID = 0;
  432. }
  433. /** UnixSound() **********************************************/
  434. /** Generate sound of given frequency (Hz) and volume **/
  435. /** (0..255) via given channel. **/
  436. /*************************************************************/
  437. void UnixSound(int Channel,int NewFreq,int NewVolume)
  438. {
  439. if((Channel<0)||(Channel>=SND_CHANNELS)) return;
  440. if(!NewVolume||!NewFreq) { NewVolume=0;NewFreq=0; }
  441. CH[Channel].Volume = NewVolume;
  442. CH[Channel].Freq = NewFreq;
  443. }
  444. /** UnixSetChannels() ****************************************/
  445. /** Set master volume (0..255) and turn channels on/off. **/
  446. /** Each bit in Toggle corresponds to a channel (1=on). **/
  447. /*************************************************************/
  448. void UnixSetChannels(int MVolume,int MSwitch)
  449. {
  450. /* Set new MasterSwitch value */
  451. MasterSwitch = MSwitch;
  452. MasterVolume = MVolume;
  453. }
  454. /** UnixSetSound() *******************************************/
  455. /** Set sound type (SND_NOISE/SND_MELODIC) for a given **/
  456. /** channel. **/
  457. /*************************************************************/
  458. void UnixSetSound(int Channel,int NewType)
  459. {
  460. if((Channel<0)||(Channel>=SND_CHANNELS)) return;
  461. CH[Channel].Type = NewType;
  462. }
  463. /** UnixSetWave() ********************************************/
  464. /** Set waveform for a given channel. The channel will be **/
  465. /** marked with sound type SND_WAVE. Set Rate=0 if you want **/
  466. /** waveform to be an instrument or set it to the waveform **/
  467. /** own playback rate. **/
  468. /*************************************************************/
  469. void UnixSetWave(int Channel, signed char *Data, int Length, int Freq)
  470. {
  471. if((Channel<0)||(Channel>=SND_CHANNELS)||(Length<=0)) return;
  472. CH[Channel].Type = SND_WAVE;
  473. CH[Channel].Length = Length;
  474. CH[Channel].Rate = Freq;
  475. CH[Channel].Pos = 0;
  476. CH[Channel].Count = 0;
  477. CH[Channel].Data = Data;
  478. }
  479. /** UnixDrum() ***********************************************/
  480. /** Hit a drum of a given type with given force. **/
  481. /*************************************************************/
  482. void UnixDrum(int Type,int Force)
  483. {
  484. /* This function is currently empty */
  485. }