Sound.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /** EMULib Emulation Library *********************************/
  2. /** **/
  3. /** Sound.h **/
  4. /** **/
  5. /** This file defines standard sound generation API and **/
  6. /** functions needed to log soundtrack into a MIDI file. **/
  7. /** See Sound.c and the sound drivers for the code. **/
  8. /** **/
  9. /** Copyright (C) Marat Fayzullin 1996-2007 **/
  10. /** You are not allowed to distribute this software **/
  11. /** commercially. Please, notify me, if you make any **/
  12. /** changes to this file. **/
  13. /*************************************************************/
  14. /*
  15. * $LastChangedDate$
  16. * $Author$
  17. * $HeadURL$
  18. * $Revision$
  19. */
  20. #ifndef SOUND_H
  21. #define SOUND_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* SetSound() arguments: */
  26. #define SND_MELODIC 0 /* Melodic sound (default) */
  27. #define SND_RECTANGLE 0 /* Rectangular wave */
  28. #define SND_QS_DU0 5
  29. #define SND_QS_DU1 6
  30. #define SND_QS_DU2 7
  31. #define SND_QS_DU3 8
  32. #define SND_TRIANGLE 1 /* Triangular wave (1/2 rect.)*/
  33. #define SND_NOISE 2 /* White noise */
  34. #define SND_PERIODIC 3 /* Periodic noise (not im-ed) */
  35. #define SND_WAVE 4 /* Wave sound set by SetWave()*/
  36. #define SND_MIDI 0x100 /* MIDI instrument (ORable) */
  37. /* Drum() arguments: */
  38. #define DRM_CLICK 0 /* Click (default) */
  39. #define DRM_MIDI 0x100 /* MIDI drum (ORable) */
  40. /* MIDI characteristics: */
  41. #define MIDI_CHANNELS 16 /* Number of MIDI channels */
  42. #define MIDI_MINFREQ 9 /* Min MIDI frequency (Hz) */
  43. #define MIDI_MAXFREQ 12285 /* Max MIDI frequency (Hz) */
  44. #define MIDI_DIVISIONS 1000 /* Number of ticks per second */
  45. /* MIDILogging() arguments: */
  46. #define MIDI_OFF 0 /* Turn MIDI logging off */
  47. #define MIDI_ON 1 /* Turn MIDI logging on */
  48. #define MIDI_TOGGLE 2 /* Toggle MIDI logging */
  49. #define MIDI_QUERY 3 /* Query MIDI logging status */
  50. /** TrashSound() *********************************************/
  51. /** Shut down sound driver. Each driver implements its own **/
  52. /** TrashSound() function. **/
  53. /*************************************************************/
  54. void TrashSound(void);
  55. /** Sound() **************************************************/
  56. /** Generate sound of given frequency (Hz) and volume **/
  57. /** (0..255) via given channel. Setting Freq=0 or Volume=0 **/
  58. /** turns sound off. **/
  59. /*************************************************************/
  60. void Sound(int Channel,int Freq,int Volume);
  61. /** Drum() ***************************************************/
  62. /** Hit a drum of given type with given force (0..255). **/
  63. /** MIDI drums can be used by ORing their numbers with **/
  64. /** SND_MIDI. **/
  65. /*************************************************************/
  66. void Drum(int Type,int Force);
  67. /** SetSound() ***********************************************/
  68. /** Set sound type at a given channel. MIDI instruments can **/
  69. /** be set directly by ORing their numbers with SND_MIDI. **/
  70. /*************************************************************/
  71. void SetSound(int Channel,int NewType);
  72. /** SetChannels() ********************************************/
  73. /** Set master volume (0..255) and switch channels on/off. **/
  74. /** Each channel N has corresponding bit 2^N in Switch. Set **/
  75. /** or reset this bit to turn the channel on or off. **/
  76. /*************************************************************/
  77. void SetChannels(int Volume,int Switch);
  78. /** SetWave() ************************************************/
  79. /** Set waveform for a given channel. The channel will be **/
  80. /** marked with sound type SND_WAVE. Set Rate=0 if you want **/
  81. /** waveform to be an instrument or set it to the waveform **/
  82. /** own playback rate. **/
  83. /*************************************************************/
  84. void SetWave(int Channel,const signed char *Data,int Length,int Rate);
  85. /** GetWave() ************************************************/
  86. /** Get current read position for the buffer set with the **/
  87. /** SetWave() call. Returns 0 if no buffer has been set, or **/
  88. /** if there is no playrate set (i.e. wave is instrument). **/
  89. /*************************************************************/
  90. const signed char *GetWave(int Channel);
  91. /** InitMIDI() ***********************************************/
  92. /** Initialize soundtrack logging into MIDI file FileName. **/
  93. /** Repeated calls to InitMIDI() will close current MIDI **/
  94. /** file and continue logging into a new one. **/
  95. /*************************************************************/
  96. void InitMIDI(const char *FileName);
  97. /** TrashMIDI() **********************************************/
  98. /** Finish logging soundtrack and close the MIDI file. **/
  99. /*************************************************************/
  100. void TrashMIDI(void);
  101. /** MIDILogging() ********************************************/
  102. /** Turn soundtrack logging on/off and return its current **/
  103. /** status. Possible values of Switch are MIDI_OFF (turn **/
  104. /** logging off), MIDI_ON (turn logging on), MIDI_TOGGLE **/
  105. /** (toggle logging), and MIDI_QUERY (just return current **/
  106. /** state of logging). **/
  107. /*************************************************************/
  108. int MIDILogging(int Switch);
  109. /** MIDITicks() **********************************************/
  110. /** Log N 1ms MIDI ticks. **/
  111. /*************************************************************/
  112. void MIDITicks(int N);
  113. //#ifdef UNIX
  114. #define SND_CHANNELS 4 /* Number of channels */
  115. #define SND_SAMPLESIZE 256 /* Max. SetWave() sample size */
  116. #define SND_BUFSIZE 256 /* Buffer size, <= 2^SND_BITS */
  117. #define SND_BITS 8 /* Number of bits in a fragment */
  118. #define SND_BUFFERS 64 /* Number of fragments, >= 2 */
  119. /* Bigger value results in better behaviour on loaded */
  120. /* but output gets more delayed. */
  121. /** InitSound() **********************************************/
  122. /** Initialize Unix sound driver with given synthesis rate. **/
  123. /** Returns Rate on success, 0 otherwise. Pass Rate=0 to **/
  124. /** skip initialization and be silent. Pass Verbose!=0 to **/
  125. /** see initialization messages. **/
  126. /*************************************************************/
  127. int InitSound(int Rate,int Verbose);
  128. /** StopSound() **********************************************/
  129. /** Temporarily suspend sound. **/
  130. /*************************************************************/
  131. void StopSound(void);
  132. /** ResumeSound() ********************************************/
  133. /** Resume sound after StopSound(). **/
  134. /*************************************************************/
  135. void ResumeSound(void);
  136. //#endif /* UNIX */
  137. #ifdef MSDOS
  138. #define SND_CHANNELS 16 /* Number of sound channels */
  139. #define OPL_CHANNELS 7 /* Number of Adlib channels */
  140. #define SND_SAMPLESIZE 256 /* Max. SetWave() sample size */
  141. #define SND_BUFSIZE 512 /* Buffer size for DMA */
  142. #define SND_MAXDELAY 10 /* Maximal sound delay 1/n s */
  143. /** InitSound() **********************************************/
  144. /** Initialize sound. Returns Rate on success, 0 otherwise. **/
  145. /** Rate=0 to skip initialization (will be silent). **/
  146. /*************************************************************/
  147. int InitSound(unsigned int Rate,unsigned int Latency);
  148. #endif /* MSDOS */
  149. #ifdef WINDOWS
  150. #define SND_CHANNELS 16 /* Number of channels */
  151. #define SND_SAMPLESIZE 256 /* Max. SetWave() sample size */
  152. #define SND_BUFSIZE 512 /* Size of a wave buffer */
  153. #define SND_BUFFERS 32 /* Number of wave buffers */
  154. #include <Windows.h>
  155. /** InitSound() **********************************************/
  156. /** Initialize Windows sound driver with given synthesis **/
  157. /** rate. Returns Rate on success, 0 otherwise. Pass Rate=0 **/
  158. /** to skip initialization and be silent. Pass Rate=1 to **/
  159. /** use MIDI (midiOut). Pass Rate=8192..44100 to use wave **/
  160. /** synthesis (waveOut). Number of wave synthesis buffers **/
  161. /** must be in 2..SND_BUFFERS range. **/
  162. /*************************************************************/
  163. unsigned int InitSound(unsigned int Rate,unsigned int Delay);
  164. #endif /* WINDOWS */
  165. #if 0
  166. #ifndef MSDOS
  167. #ifndef WINDOWS
  168. #ifndef UNIX
  169. #define SND_CHANNELS MIDI_CHANNELS /* Default number */
  170. #endif
  171. #endif
  172. #endif
  173. /** InitSound() **********************************************/
  174. /** Initialize Series60 sound driver with given synthesis **/
  175. /** rate. Returns Rate on success, 0 otherwise. Pass Rate=0 **/
  176. /** to skip initialization and be silent. **/
  177. /*************************************************************/
  178. unsigned int InitSound(unsigned int Rate,unsigned int Delay);
  179. #endif
  180. /** RenderAudio() ********************************************/
  181. /** Render given number of melodic sound samples. Returns **/
  182. /** number of samples actually rendered. **/
  183. /*************************************************************/
  184. unsigned int RenderAudio(unsigned int Samples);
  185. /** SndDriver ************************************************/
  186. /** Each sound driver should fill this structure with **/
  187. /** pointers to hardware-dependent handlers. This has to be **/
  188. /** done inside the InitSound() function. **/
  189. /*************************************************************/
  190. struct SndDriverStruct
  191. {
  192. void (*SetSound)(int Channel,int NewType);
  193. void (*Drum)(int Type,int Force);
  194. void (*SetChannels)(int Volume,int Switch);
  195. void (*Sound)(int Channel,int NewFreq,int NewVolume);
  196. void (*SetWave)(int Channel,const signed char *Data,int Length,int Freq);
  197. const signed char *(*GetWave)(int Channel);
  198. };
  199. extern struct SndDriverStruct SndDriver;
  200. #ifdef __cplusplus
  201. }
  202. #endif
  203. #endif /* SOUND_H */