Sound.h 10 KB

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