SndAlleg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 <allegro.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. #define AUDIO_CONV(A) (128+(A))
  22. AUDIOSTREAM *stream;
  23. static pthread_t ThreadID;
  24. static int SoundFD;
  25. static int SoundRate = 0;
  26. static int MasterVolume = 64;
  27. static int MasterSwitch = (1<<SND_CHANNELS)-1;
  28. static int LoopFreq = 25;
  29. static int NoiseGen = 1;
  30. static int Suspended = 0;
  31. static struct
  32. {
  33. int Type; /* Channel type (SND_*) */
  34. int Freq; /* Channel frequency (Hz) */
  35. int Volume; /* Channel volume (0..255) */
  36. signed char *Data; /* Wave data (-128..127 each) */
  37. int Length; /* Wave length in Data */
  38. int Rate; /* Wave playback rate (or 0Hz) */
  39. int Pos; /* Wave current position in Data */
  40. int Count; /* Phase counter */
  41. } CH[SND_CHANNELS];
  42. static void UnixSetWave(int Channel,signed char *Data,int Length,int Rate);
  43. static void UnixSetSound(int Channel,int NewType);
  44. static void UnixDrum(int Type,int Force);
  45. static void UnixSetChannels(int Volume,int Switch);
  46. static void UnixSound(int Channel,int NewFreq,int NewVolume);
  47. static int OpenSoundDevice(int Rate,int Verbose);
  48. static void *DSPLoop(void *Arg);
  49. /** StopSound() **********************************************/
  50. /** Temporarily suspend sound. **/
  51. /*************************************************************/
  52. void StopSound(void) { Suspended=1; }
  53. /** ResumeSound() ********************************************/
  54. /** Resume sound after StopSound(). **/
  55. /*************************************************************/
  56. void ResumeSound(void) { Suspended=0; }
  57. /** OpenSoundDevice() ****************************************/
  58. /** Open /dev/dsp with a given level of sound quality. **/
  59. /** Returns 0 if failed or sound quality (Mode). **/
  60. /*************************************************************/
  61. static int OpenSoundDevice(int Rate,int Verbose)
  62. {
  63. voice_start(stream->voice);
  64. if(Verbose) puts("OK");
  65. return(Rate);
  66. }
  67. /** DSPLoop() ************************************************/
  68. /** Main loop of the sound server. **/
  69. /*************************************************************/
  70. static void *DSPLoop(void *Arg)
  71. {
  72. int Wave[SND_BUFSIZE];
  73. unsigned char *Buf;
  74. register int J,I,K,L,M,N,L1,L2,A1,A2,V;
  75. int FreqCount;
  76. for(J=0;J<SND_CHANNELS;J++)
  77. {
  78. CH[J].Type = SND_MELODIC;
  79. CH[J].Count = 0;
  80. CH[J].Volume = 0;
  81. CH[J].Freq = 0;
  82. }
  83. FreqCount=SoundRate/SND_BUFSIZE;
  84. for(;;)
  85. {
  86. Buf = get_audio_stream_buffer(stream);
  87. if (Buf) {
  88. FreqCount-=LoopFreq;
  89. /* If suspending sound... */
  90. if(Suspended)
  91. {
  92. /* Close sound device */
  93. while(Suspended) sleep(1);
  94. /* Reopen sound device */
  95. SoundRate=OpenSoundDevice(SoundRate,0);
  96. }
  97. /* Waveform generator */
  98. for(J=0,M=MasterSwitch;M&&(J<SND_CHANNELS);J++,M>>=1)
  99. if(CH[J].Freq&&(V=CH[J].Volume)&&(M&1))
  100. switch(CH[J].Type)
  101. {
  102. case SND_NOISE: /* White Noise */
  103. /* For high frequencies, recompute volume */
  104. if(CH[J].Freq<=SoundRate) K=0x10000*CH[J].Freq/SoundRate;
  105. else { V=V*SoundRate/CH[J].Freq;K=0x10000; }
  106. L1=CH[J].Count;
  107. V<<=7;
  108. for(I=0;I<SND_BUFSIZE;I++)
  109. {
  110. L1+=K;
  111. if(L1&0xFFFF0000)
  112. {
  113. L1&=0xFFFF;
  114. if((NoiseGen<<=1)&0x80000000) NoiseGen^=0x08000001;
  115. }
  116. Wave[I]+=NoiseGen&1? V:-V;
  117. }
  118. CH[J].Count=L1;
  119. break;
  120. case SND_WAVE: /* Custom Waveform */
  121. /* Waveform data must have correct length! */
  122. if(CH[J].Length<=0) break;
  123. /* Start counting */
  124. K = CH[J].Rate>0? (SoundRate<<15)/CH[J].Freq/CH[J].Rate
  125. : (SoundRate<<15)/CH[J].Freq/CH[J].Length;
  126. L1 = CH[J].Pos%CH[J].Length;
  127. L2 = CH[J].Count;
  128. A1 = CH[J].Data[L1]*V;
  129. /* If expecting interpolation... */
  130. if(L2<K)
  131. {
  132. /* Compute interpolation parameters */
  133. A2 = CH[J].Data[(L1+1)%CH[J].Length]*V;
  134. L = (L2>>15)+1;
  135. N = ((K-(L2&0x7FFF))>>15)+1;
  136. }
  137. /* Add waveform to the buffer */
  138. for(I=0;I<SND_BUFSIZE;I++)
  139. if(L2<K)
  140. {
  141. /* Interpolate linearly */
  142. Wave[I]+=A1+L*(A2-A1)/N;
  143. /* Next waveform step */
  144. L2+=0x8000;
  145. /* Next interpolation step */
  146. L++;
  147. }
  148. else
  149. {
  150. L1 = (L1+L2/K)%CH[J].Length;
  151. L2 = (L2%K)+0x8000;
  152. A1 = CH[J].Data[L1]*V;
  153. Wave[I]+=A1;
  154. /* If expecting interpolation... */
  155. if(L2<K)
  156. {
  157. /* Compute interpolation parameters */
  158. A2 = CH[J].Data[(L1+1)%CH[J].Length]*V;
  159. L = 1;
  160. N = ((K-L2)>>15)+1;
  161. }
  162. }
  163. /* End counting */
  164. CH[J].Pos = L1;
  165. CH[J].Count = L2;
  166. break;
  167. case SND_QS_DU0:
  168. /* Do not allow frequencies that are too high */
  169. if(CH[J].Freq>=SoundRate/3) break;
  170. K=0x10000*CH[J].Freq/SoundRate;
  171. L1=CH[J].Count;
  172. V<<=7;
  173. for(I=0;I<SND_BUFSIZE;I++)
  174. {
  175. L2=L1+K;
  176. Wave[I]+=L1&0x2000?(L2&0x8000? V:0):(L2&0x8000? 0:-V);
  177. L1=L2;
  178. }
  179. CH[J].Count=L1;
  180. break;
  181. case SND_QS_DU1:
  182. /* Do not allow frequencies that are too high */
  183. if(CH[J].Freq>=SoundRate/3) break;
  184. K=0x10000*CH[J].Freq/SoundRate;
  185. L1=CH[J].Count;
  186. V<<=7;
  187. for(I=0;I<SND_BUFSIZE;I++)
  188. {
  189. L2=L1+K;
  190. Wave[I]+=L1&0x4000?(L2&0x8000? V:0):(L2&0x8000? 0:-V);
  191. L1=L2;
  192. }
  193. CH[J].Count=L1;
  194. break;
  195. case SND_QS_DU3:
  196. /* Do not allow frequencies that are too high */
  197. if(CH[J].Freq>=SoundRate/3) break;
  198. K=0x10000*CH[J].Freq/SoundRate;
  199. L1=CH[J].Count;
  200. V<<=7;
  201. for(I=0;I<SND_BUFSIZE;I++)
  202. {
  203. L2=L1+K;
  204. Wave[I]+=L1&0xC000?(L2&0x4000? V:0):(L2&0xC000? 0:-V);
  205. L1=L2;
  206. }
  207. CH[J].Count=L1;
  208. break;
  209. case SND_QS_DU2:
  210. case SND_MELODIC: /* Melodic Sound */
  211. default: /* Default Sound */
  212. /* Do not allow frequencies that are too high */
  213. if(CH[J].Freq>=SoundRate/3) break;
  214. K=0x10000*CH[J].Freq/SoundRate;
  215. L1=CH[J].Count;
  216. V<<=7;
  217. for(I=0;I<SND_BUFSIZE;I++)
  218. {
  219. L2=L1+K;
  220. Wave[I]+=L1&0x8000? (L2&0x8000? V:0):(L2&0x8000? 0:-V);
  221. L1=L2;
  222. }
  223. CH[J].Count=L1;
  224. break;
  225. case SND_TRIANGLE: /* Default Sound */
  226. /* Do not allow frequencies that are too high */
  227. if(CH[J].Freq>=SoundRate/3) break;
  228. K=0x10000*CH[J].Freq/SoundRate;
  229. L1=CH[J].Count;
  230. V<<=7;
  231. for(I=0;I<SND_BUFSIZE;I++)
  232. {
  233. L2=L1+K;
  234. Wave[I]+= L1&0x2000?V:-V /*(L2&0x8000? V:0):(L2&0x8000? 0:-V)*/;
  235. L1=L2;
  236. }
  237. CH[J].Count=L1;
  238. break;
  239. }
  240. /* Mix and convert waveforms */
  241. for(J=0;J<SND_BUFSIZE;J++)
  242. {
  243. I=(Wave[J]*MasterVolume)>>16;
  244. I=I<-128? -128:I>127? 127:I;
  245. Buf[J]=AUDIO_CONV(I);
  246. Wave[J]=0;
  247. }
  248. free_audio_stream_buffer(stream);
  249. }
  250. }
  251. return(0);
  252. }
  253. /** InitSound() **********************************************/
  254. /** Initialize DSP. Returns Rate on success, 0 otherwise. **/
  255. /** Mode is 0 to skip initialization (will be silent). **/
  256. /*************************************************************/
  257. int InitSound(int Rate,int Verbose)
  258. {
  259. /* If sound was initialized, kill it */
  260. TrashSound();
  261. /* Silence requested */
  262. if(Rate<=0) return(0);
  263. /* Synthesis rate should be at least 8kHz */
  264. if(Rate<8192) Rate=44100;
  265. /* Initialize things */
  266. SoundRate = 0;
  267. ThreadID = 0;
  268. Suspended = 0;
  269. /* Set driver functions */
  270. SndDriver.SetSound = UnixSetSound;
  271. SndDriver.Drum = UnixDrum;
  272. SndDriver.SetChannels = UnixSetChannels;
  273. SndDriver.Sound = UnixSound;
  274. SndDriver.SetWave = UnixSetWave;
  275. if (install_sound(DIGI_AUTODETECT, MIDI_NONE, "") != 0)
  276. {
  277. fprintf(stderr, "%s!\n", allegro_error);
  278. return 1;
  279. }
  280. stream = play_audio_stream(SND_BUFSIZE, 8, FALSE, Rate, 255, 128);
  281. if (!stream) {
  282. fprintf(stderr, "Error creating audio stream!\n");
  283. return 1;
  284. }
  285. voice_stop(stream->voice);
  286. /* Open sound device */
  287. if(Verbose) puts("Starting sound server:");
  288. if(!(Rate=OpenSoundDevice(Rate,Verbose))) return(0);
  289. /* Create DSPLoop() thread */
  290. if(Verbose) printf(" Creating thread...");
  291. if(pthread_create(&ThreadID,0,DSPLoop,0))
  292. { if(Verbose) puts("FAILED");return(0); }
  293. /* Detach the thread */
  294. pthread_detach(ThreadID);
  295. /* Done */
  296. if(Verbose) puts("OK");
  297. return(SoundRate=Rate);
  298. }
  299. /** TrashSound() *********************************************/
  300. /** Shut DSP down. **/
  301. /*************************************************************/
  302. void TrashSound(void)
  303. {
  304. StopSound();
  305. printf("%s: Kill thread...\n", __func__);
  306. if(ThreadID) pthread_cancel(ThreadID);
  307. SoundRate = 0;
  308. ThreadID = 0;
  309. }
  310. /** UnixSound() **********************************************/
  311. /** Generate sound of given frequency (Hz) and volume **/
  312. /** (0..255) via given channel. **/
  313. /*************************************************************/
  314. void UnixSound(int Channel,int NewFreq,int NewVolume)
  315. {
  316. if((Channel<0)||(Channel>=SND_CHANNELS)) return;
  317. if(!NewVolume||!NewFreq) { NewVolume=0;NewFreq=0; }
  318. CH[Channel].Volume = NewVolume;
  319. CH[Channel].Freq = NewFreq;
  320. }
  321. /** UnixSetChannels() ****************************************/
  322. /** Set master volume (0..255) and turn channels on/off. **/
  323. /** Each bit in Toggle corresponds to a channel (1=on). **/
  324. /*************************************************************/
  325. void UnixSetChannels(int MVolume,int MSwitch)
  326. {
  327. /* Set new MasterSwitch value */
  328. MasterSwitch = MSwitch;
  329. MasterVolume = MVolume;
  330. }
  331. /** UnixSetSound() *******************************************/
  332. /** Set sound type (SND_NOISE/SND_MELODIC) for a given **/
  333. /** channel. **/
  334. /*************************************************************/
  335. void UnixSetSound(int Channel,int NewType)
  336. {
  337. if((Channel<0)||(Channel>=SND_CHANNELS)) return;
  338. CH[Channel].Type = NewType;
  339. }
  340. /** UnixSetWave() ********************************************/
  341. /** Set waveform for a given channel. The channel will be **/
  342. /** marked with sound type SND_WAVE. Set Rate=0 if you want **/
  343. /** waveform to be an instrument or set it to the waveform **/
  344. /** own playback rate. **/
  345. /*************************************************************/
  346. void UnixSetWave(int Channel,signed char *Data,int Length,int Rate)
  347. {
  348. if((Channel<0)||(Channel>=SND_CHANNELS)||(Length<=0)) return;
  349. CH[Channel].Type = SND_WAVE;
  350. CH[Channel].Length = Length;
  351. CH[Channel].Rate = Rate;
  352. CH[Channel].Pos = 0;
  353. CH[Channel].Count = 0;
  354. CH[Channel].Data = Data;
  355. }
  356. /** UnixDrum() ***********************************************/
  357. /** Hit a drum of a given type with given force. **/
  358. /*************************************************************/
  359. void UnixDrum(int Type,int Force)
  360. {
  361. /* This function is currently empty */
  362. }