SndUnixT.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. #ifdef UNIX
  14. #include "Sound.h"
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <fcntl.h>
  19. #include <pthread.h>
  20. #include <sys/ioctl.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 Rate);
  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. for(J=0;J<SND_CHANNELS;J++)
  188. {
  189. CH[J].Type = SND_MELODIC;
  190. CH[J].Count = 0;
  191. CH[J].Volume = 0;
  192. CH[J].Freq = 0;
  193. }
  194. FreqCount=SoundRate/SND_BUFSIZE;
  195. for(;;FreqCount-=LoopFreq)
  196. {
  197. /* If suspending sound... */
  198. if(Suspended)
  199. {
  200. /* Close sound device */
  201. #ifndef SUN_AUDIO
  202. ioctl(SoundFD,SNDCTL_DSP_RESET);
  203. #endif
  204. close(SoundFD);
  205. /* Suspend execution until Suspended=0 */
  206. while(Suspended) sleep(1);
  207. /* Reopen sound device */
  208. SoundRate=OpenSoundDevice(SoundRate,0);
  209. }
  210. /* Waveform generator */
  211. for(J=0,M=MasterSwitch;M&&(J<SND_CHANNELS);J++,M>>=1)
  212. if(CH[J].Freq&&(V=CH[J].Volume)&&(M&1))
  213. switch(CH[J].Type)
  214. {
  215. case SND_NOISE: /* White Noise */
  216. /* For high frequencies, recompute volume */
  217. if(CH[J].Freq<=SoundRate) K=0x10000*CH[J].Freq/SoundRate;
  218. else { V=V*SoundRate/CH[J].Freq;K=0x10000; }
  219. L1=CH[J].Count;
  220. V<<=7;
  221. for(I=0;I<SND_BUFSIZE;I++)
  222. {
  223. L1+=K;
  224. if(L1&0xFFFF0000)
  225. {
  226. L1&=0xFFFF;
  227. if((NoiseGen<<=1)&0x80000000) NoiseGen^=0x08000001;
  228. }
  229. Wave[I]+=NoiseGen&1? V:-V;
  230. }
  231. CH[J].Count=L1;
  232. break;
  233. case SND_WAVE: /* Custom Waveform */
  234. /* Waveform data must have correct length! */
  235. if(CH[J].Length<=0) break;
  236. /* Start counting */
  237. K = CH[J].Rate>0? (SoundRate<<15)/CH[J].Freq/CH[J].Rate
  238. : (SoundRate<<15)/CH[J].Freq/CH[J].Length;
  239. L1 = CH[J].Pos%CH[J].Length;
  240. L2 = CH[J].Count;
  241. A1 = CH[J].Data[L1]*V;
  242. /* If expecting interpolation... */
  243. if(L2<K)
  244. {
  245. /* Compute interpolation parameters */
  246. A2 = CH[J].Data[(L1+1)%CH[J].Length]*V;
  247. L = (L2>>15)+1;
  248. N = ((K-(L2&0x7FFF))>>15)+1;
  249. }
  250. /* Add waveform to the buffer */
  251. for(I=0;I<SND_BUFSIZE;I++)
  252. if(L2<K)
  253. {
  254. /* Interpolate linearly */
  255. Wave[I]+=A1+L*(A2-A1)/N;
  256. /* Next waveform step */
  257. L2+=0x8000;
  258. /* Next interpolation step */
  259. L++;
  260. }
  261. else
  262. {
  263. L1 = (L1+L2/K)%CH[J].Length;
  264. L2 = (L2%K)+0x8000;
  265. A1 = CH[J].Data[L1]*V;
  266. Wave[I]+=A1;
  267. /* If expecting interpolation... */
  268. if(L2<K)
  269. {
  270. /* Compute interpolation parameters */
  271. A2 = CH[J].Data[(L1+1)%CH[J].Length]*V;
  272. L = 1;
  273. N = ((K-L2)>>15)+1;
  274. }
  275. }
  276. /* End counting */
  277. CH[J].Pos = L1;
  278. CH[J].Count = L2;
  279. break;
  280. case SND_QS_DU0:
  281. /* Do not allow frequencies that are too high */
  282. if(CH[J].Freq>=SoundRate/3) break;
  283. K=0x10000*CH[J].Freq/SoundRate;
  284. L1=CH[J].Count;
  285. V<<=7;
  286. for(I=0;I<SND_BUFSIZE;I++)
  287. {
  288. L2=L1+K;
  289. Wave[I]+=L1&0x2000?(L2&0x8000? V:0):(L2&0x8000? 0:-V);
  290. L1=L2;
  291. }
  292. CH[J].Count=L1;
  293. break;
  294. case SND_QS_DU1:
  295. /* Do not allow frequencies that are too high */
  296. if(CH[J].Freq>=SoundRate/3) break;
  297. K=0x10000*CH[J].Freq/SoundRate;
  298. L1=CH[J].Count;
  299. V<<=7;
  300. for(I=0;I<SND_BUFSIZE;I++)
  301. {
  302. L2=L1+K;
  303. Wave[I]+=L1&0x4000?(L2&0x8000? V:0):(L2&0x8000? 0:-V);
  304. L1=L2;
  305. }
  306. CH[J].Count=L1;
  307. break;
  308. case SND_QS_DU3:
  309. /* Do not allow frequencies that are too high */
  310. if(CH[J].Freq>=SoundRate/3) break;
  311. K=0x10000*CH[J].Freq/SoundRate;
  312. L1=CH[J].Count;
  313. V<<=7;
  314. for(I=0;I<SND_BUFSIZE;I++)
  315. {
  316. L2=L1+K;
  317. Wave[I]+=L1&0xC000?(L2&0x4000? V:0):(L2&0xC000? 0:-V);
  318. L1=L2;
  319. }
  320. CH[J].Count=L1;
  321. break;
  322. case SND_QS_DU2:
  323. case SND_MELODIC: /* Melodic Sound */
  324. default: /* Default Sound */
  325. /* Do not allow frequencies that are too high */
  326. if(CH[J].Freq>=SoundRate/3) break;
  327. K=0x10000*CH[J].Freq/SoundRate;
  328. L1=CH[J].Count;
  329. V<<=7;
  330. for(I=0;I<SND_BUFSIZE;I++)
  331. {
  332. L2=L1+K;
  333. Wave[I]+=L1&0x8000? (L2&0x8000? V:0):(L2&0x8000? 0:-V);
  334. L1=L2;
  335. }
  336. CH[J].Count=L1;
  337. break;
  338. case SND_TRIANGLE: /* Default Sound */
  339. /* Do not allow frequencies that are too high */
  340. if(CH[J].Freq>=SoundRate/3) break;
  341. K=0x10000*CH[J].Freq/SoundRate;
  342. L1=CH[J].Count;
  343. V<<=7;
  344. for(I=0;I<SND_BUFSIZE;I++)
  345. {
  346. L2=L1+K;
  347. Wave[I]+= L1&0x2000?V:-V /*(L2&0x8000? V:0):(L2&0x8000? 0:-V)*/;
  348. L1=L2;
  349. }
  350. CH[J].Count=L1;
  351. break;
  352. }
  353. /* Mix and convert waveforms */
  354. for(J=0;J<SND_BUFSIZE;J++)
  355. {
  356. I=(Wave[J]*MasterVolume)>>16;
  357. I=I<-128? -128:I>127? 127:I;
  358. Buf[J]=AUDIO_CONV(I);
  359. Wave[J]=0;
  360. }
  361. if(SoundFD==-1) sleep(1);
  362. else
  363. {
  364. #ifdef SUN_AUDIO
  365. /* Flush output first, don't care about return status. After this
  366. ** write next buffer of audio data. This method produces a horrible
  367. ** click on each buffer :( Any ideas, how to fix this?
  368. */
  369. ioctl(SoundFD,AUDIO_DRAIN);
  370. write(SoundFD,Buf,SND_BUFSIZE);
  371. #else
  372. /* We'll block here until next DMA buffer becomes free. It happens
  373. ** once per (1<<SND_BITS)/SoundRate seconds.
  374. */
  375. write(SoundFD,Buf,SND_BUFSIZE);
  376. #endif
  377. }
  378. }
  379. return(0);
  380. }
  381. /** InitSound() **********************************************/
  382. /** Initialize DSP. Returns Rate on success, 0 otherwise. **/
  383. /** Mode is 0 to skip initialization (will be silent). **/
  384. /*************************************************************/
  385. int InitSound(int Rate,int Verbose)
  386. {
  387. /* If sound was initialized, kill it */
  388. TrashSound();
  389. /* Silence requested */
  390. if(Rate<=0) return(0);
  391. /* Synthesis rate should be at least 8kHz */
  392. if(Rate<8192) Rate=44100;
  393. /* Initialize things */
  394. SoundRate = 0;
  395. SoundFD = -1;
  396. ThreadID = 0;
  397. Suspended = 0;
  398. /* Set driver functions */
  399. SndDriver.SetSound = UnixSetSound;
  400. SndDriver.Drum = UnixDrum;
  401. SndDriver.SetChannels = UnixSetChannels;
  402. SndDriver.Sound = UnixSound;
  403. SndDriver.SetWave = UnixSetWave;
  404. /* Open sound device */
  405. if(Verbose) puts("Starting sound server:");
  406. if(!(Rate=OpenSoundDevice(Rate,Verbose))) return(0);
  407. /* Create DSPLoop() thread */
  408. if(Verbose) console_printf(Console_Default, " Creating thread...");
  409. if(pthread_create(&ThreadID,0,DSPLoop,0))
  410. { if(Verbose) puts("FAILED");return(0); }
  411. /* Detach the thread */
  412. pthread_detach(ThreadID);
  413. /* Done */
  414. if(Verbose) puts("OK");
  415. return(SoundRate=Rate);
  416. }
  417. /** TrashSound() *********************************************/
  418. /** Shut DSP down. **/
  419. /*************************************************************/
  420. void TrashSound(void)
  421. {
  422. StopSound();
  423. console_printf(Console_Default, "%s: Kill thread...\n", __func__);
  424. if(ThreadID) pthread_cancel(ThreadID);
  425. console_printf(Console_Default, "%s: close /dev/xxx ...\n", __func__);
  426. if(SoundFD!=-1) close(SoundFD);
  427. SoundRate = 0;
  428. SoundFD = -1;
  429. ThreadID = 0;
  430. }
  431. /** UnixSound() **********************************************/
  432. /** Generate sound of given frequency (Hz) and volume **/
  433. /** (0..255) via given channel. **/
  434. /*************************************************************/
  435. void UnixSound(int Channel,int NewFreq,int NewVolume)
  436. {
  437. if((Channel<0)||(Channel>=SND_CHANNELS)) return;
  438. if(!NewVolume||!NewFreq) { NewVolume=0;NewFreq=0; }
  439. CH[Channel].Volume = NewVolume;
  440. CH[Channel].Freq = NewFreq;
  441. }
  442. /** UnixSetChannels() ****************************************/
  443. /** Set master volume (0..255) and turn channels on/off. **/
  444. /** Each bit in Toggle corresponds to a channel (1=on). **/
  445. /*************************************************************/
  446. void UnixSetChannels(int MVolume,int MSwitch)
  447. {
  448. /* Set new MasterSwitch value */
  449. MasterSwitch = MSwitch;
  450. MasterVolume = MVolume;
  451. }
  452. /** UnixSetSound() *******************************************/
  453. /** Set sound type (SND_NOISE/SND_MELODIC) for a given **/
  454. /** channel. **/
  455. /*************************************************************/
  456. void UnixSetSound(int Channel,int NewType)
  457. {
  458. if((Channel<0)||(Channel>=SND_CHANNELS)) return;
  459. CH[Channel].Type = NewType;
  460. }
  461. /** UnixSetWave() ********************************************/
  462. /** Set waveform for a given channel. The channel will be **/
  463. /** marked with sound type SND_WAVE. Set Rate=0 if you want **/
  464. /** waveform to be an instrument or set it to the waveform **/
  465. /** own playback rate. **/
  466. /*************************************************************/
  467. void UnixSetWave(int Channel,signed char *Data,int Length,int Rate)
  468. {
  469. if((Channel<0)||(Channel>=SND_CHANNELS)||(Length<=0)) return;
  470. CH[Channel].Type = SND_WAVE;
  471. CH[Channel].Length = Length;
  472. CH[Channel].Rate = Rate;
  473. CH[Channel].Pos = 0;
  474. CH[Channel].Count = 0;
  475. CH[Channel].Data = Data;
  476. }
  477. /** UnixDrum() ***********************************************/
  478. /** Hit a drum of a given type with given force. **/
  479. /*************************************************************/
  480. void UnixDrum(int Type,int Force)
  481. {
  482. /* This function is currently empty */
  483. }
  484. #endif /* UNIX */