SndAlleg.c 13 KB

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