SndAlleg.c 12 KB

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