audio.bmx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. Strict
  2. Rem
  3. bbdoc: Audio/Audio playback
  4. End Rem
  5. Module BRL.Audio
  6. ModuleInfo "Version: 1.07"
  7. ModuleInfo "Author: Mark Sibly"
  8. ModuleInfo "License: zlib/libpng"
  9. ModuleInfo "Copyright: Blitz Research Ltd"
  10. ModuleInfo "Modserver: BRL"
  11. ModuleInfo "History: 1.07 Release"
  12. ModuleInfo "History: Added flags to LoadSound"
  13. ModuleInfo "History: Driver now set to Null if SetAudioDriver fails"
  14. ModuleInfo "History: 1.06 Release"
  15. ModuleInfo "History: Changed default device to FreeAudio"
  16. ModuleInfo "History: 1.05 Release"
  17. ModuleInfo "History: Added driver list and SetAudioDriver"
  18. Import BRL.AudioSample
  19. Private
  20. Global _nullDriver:TAudioDriver=New TAudioDriver
  21. Global _driver:TAudioDriver,_drivers:TAudioDriver
  22. Function Driver:TAudioDriver()
  23. If _driver Return _driver
  24. ?Win32
  25. If SetAudioDriver( "DirectSound" ) Return _driver
  26. ?Not Win32
  27. If SetAudioDriver( "FreeAudio" ) Return _driver
  28. ?
  29. SetAudioDriver "Null"
  30. Return _driver
  31. End Function
  32. Function Shutdown()
  33. If Not _driver Return
  34. _driver.Shutdown
  35. _driver=Null
  36. End Function
  37. atexit_ Shutdown
  38. Public
  39. Const SOUND_LOOP=1
  40. Const SOUND_HARDWARE=2
  41. Rem
  42. bbdoc: Audio sound type
  43. End Rem
  44. Type TSound
  45. Rem
  46. bbdoc: Play the sound
  47. returns: An audio channel object
  48. about:
  49. Starts a sound playing through an audio channel.
  50. If no channel is specified, #Play automatically allocates a channel for you.
  51. End Rem
  52. Method Play:TChannel( alloced_channel:TChannel=Null )
  53. Return New TChannel
  54. End Method
  55. Rem
  56. bbdoc: Cue the sound for playback
  57. returns: An audio channel object
  58. about:
  59. Prepares an audio channel for playback of a sound.
  60. To actually start the sound, you must use the channel's #SetPaused method.
  61. If no channel is specified, #Cue automatically allocates a channel for you.
  62. #Cue allows you to setup various audio channel states such as volume, pan, depth and rate before a sound
  63. actually starts playing.
  64. End Rem
  65. Method Cue:TChannel( alloced_channel:TChannel=Null )
  66. Return New TChannel
  67. End Method
  68. Rem
  69. bbdoc: Load sound
  70. returns: A sound object
  71. about:
  72. @url can be either a string, a stream or an audio sample object.
  73. The returned sound object can be played using #Play or #Cue.
  74. End Rem
  75. Function Load:TSound( url:Object,loop_flag )
  76. Local sample:TAudioSample
  77. sample=TAudioSample( url )
  78. If Not sample sample=LoadAudioSample( url )
  79. If sample Return Driver().CreateSound( sample,loop_flag )
  80. End Function
  81. End Type
  82. Rem
  83. bbdoc: Audio channel Type
  84. End Rem
  85. Type TChannel
  86. Rem
  87. bbdoc: Stop audio channel playback
  88. about:
  89. Shuts down the audio channel. Further commands on this audio channel will have no effect.
  90. End Rem
  91. Method Stop()
  92. End Method
  93. Rem
  94. bbdoc: Pause or unpause audio channel playback
  95. about:
  96. If @paused is True, the audio channel is paused. Otherwise, the audio channel is unpaused.
  97. End Rem
  98. Method SetPaused( paused )
  99. End Method
  100. Rem
  101. bbdoc: Set audio channel volume
  102. about:
  103. @volume should be in the range 0 (silence) to 1 (full volume).
  104. End Rem
  105. Method SetVolume( volume# )
  106. End Method
  107. Rem
  108. bbdoc: Set audio channel stereo pan
  109. about:
  110. @pan should be in the range -1 (full left) to 1 (full right).
  111. End Rem
  112. Method SetPan( pan# )
  113. End Method
  114. Rem
  115. bbdoc: Set audio channel depth
  116. about:
  117. @depth should be in the range -1 (back) to 1 (front).
  118. End Rem
  119. Method SetDepth( depth# )
  120. End Method
  121. Rem
  122. bbdoc: Set audio channel playback rate
  123. about:
  124. @rate is a multiplier used to modify the audio channel's frequency.
  125. For example, a rate of .5 will cause the audio channel
  126. to play at half speed (ie: an octave down) while a rate of 2 will
  127. cause the audio channel to play at double speed (ie: an octave up).
  128. End Rem
  129. Method SetRate( rate# )
  130. End Method
  131. Rem
  132. bbdoc: Determine whether audio channel is playing
  133. returns: True if @channel is currently playing
  134. about:
  135. #Playing will return False if the audio channel is either paused, or has been stopped
  136. using #Stop.
  137. End Rem
  138. Method Playing()
  139. End Method
  140. End Type
  141. Type TAudioDriver
  142. Method New()
  143. _succ=_drivers
  144. _drivers=Self
  145. End Method
  146. Method Name$()
  147. Return "Null"
  148. End Method
  149. Method Startup()
  150. Return True
  151. End Method
  152. Method Shutdown()
  153. End Method
  154. Method CreateSound:TSound( sample:TAudioSample,loop_flag )
  155. Return New TSound
  156. End Method
  157. Method AllocChannel:TChannel()
  158. Return New TChannel
  159. End Method
  160. Method LoadSound:TSound( url:Object, flags:Int = 0)
  161. Return TSound.Load(url, flags)
  162. End Method
  163. Field _succ:TAudioDriver
  164. End Type
  165. Rem
  166. bbdoc: Load a sound
  167. returns: A sound object
  168. about:
  169. @url can be either a string, a stream or a #TAudioSample object.
  170. The returned sound can be played using #PlaySound or #CueSound.
  171. The @flags parameter can be any combination of:
  172. [ @{Flag value} | @Effect
  173. * SOUND_LOOP | The sound should loop when played back.
  174. * SOUND_HARDWARE | The sound should be placed in onboard soundcard memory if possible.
  175. ]
  176. To combine flags, use the binary 'or' operator: '|'.
  177. End Rem
  178. Function LoadSound:TSound( url:Object,flags=0 )
  179. Return Driver().LoadSound( url,flags )
  180. End Function
  181. Rem
  182. bbdoc: Play a sound
  183. returns: An audio channel object
  184. about:
  185. #PlaySound starts a sound playing through an audio channel.
  186. If no @channel is specified, #PlaySound automatically allocates a channel for you.
  187. end rem
  188. Function PlaySound:TChannel( sound:TSound,channel:TChannel=Null )
  189. Return sound.Play( channel )
  190. End Function
  191. Rem
  192. bbdoc: Cue a sound
  193. returns: An audio channel object
  194. about:
  195. Prepares a sound for playback through an audio channel.
  196. To actually start the sound, you must use #ResumeChannel.
  197. If no @channel is specified, #CueSound automatically allocates a channel for you.
  198. #CueSound allows you to setup various audio channel states such as volume, pan, depth
  199. and rate before a sound actually starts playing.
  200. End Rem
  201. Function CueSound:TChannel( sound:TSound,channel:TChannel=Null )
  202. Return sound.Cue( channel )
  203. End Function
  204. Rem
  205. bbdoc: Allocate audio channel
  206. returns: An audio channel object
  207. about:
  208. Allocates an audio channel for use with #PlaySound and #CueSound.
  209. Once you are finished with an audio channel, you should use #StopChannel.
  210. end rem
  211. Function AllocChannel:TChannel()
  212. Return Driver().AllocChannel()
  213. End Function
  214. Rem
  215. bbdoc: Stop an audio channel
  216. about:
  217. Shuts down an audio channel. Further commands using this channel will have no effect.
  218. end rem
  219. Function StopChannel( channel:TChannel )
  220. channel.Stop
  221. End Function
  222. Rem
  223. bbdoc: Determine whether an audio channel is playing
  224. returns: #True if @channel is currently playing
  225. about:
  226. #ChannelPlaying will return #False if either the channel has been paused using #PauseChannel,
  227. or stopped using #StopChannel.
  228. end rem
  229. Function ChannelPlaying( channel:TChannel )
  230. Return channel.Playing()
  231. End Function
  232. Rem
  233. bbdoc: Set playback volume of an audio channel
  234. about:
  235. @volume should be in the range 0 (silent) to 1 (full volume)
  236. end rem
  237. Function SetChannelVolume( channel:TChannel,volume# )
  238. channel.SetVolume volume
  239. End Function
  240. Rem
  241. bbdoc: Set stereo balance of an audio channel
  242. about:
  243. @pan should be in the range -1 (left) to 1 (right)
  244. end rem
  245. Function SetChannelPan( channel:TChannel,pan# )
  246. channel.SetPan pan
  247. End Function
  248. Rem
  249. bbdoc: Set surround sound depth of an audio channel
  250. about:
  251. @depth should be in the range -1 (back) to 1 (front)
  252. end rem
  253. Function SetChannelDepth( channel:TChannel,depth# )
  254. channel.SetDepth depth
  255. End Function
  256. Rem
  257. bbdoc: Set playback rate of an audio channel
  258. about:
  259. @rate is a multiplier used to modify the audio channel's frequency.
  260. For example, a rate of .5 will cause the audio channel
  261. to play at half speed (ie: an octave down) while a rate of 2 will
  262. cause the audio channel to play at double speed (ie: an octave up).
  263. end rem
  264. Function SetChannelRate( channel:TChannel,rate# )
  265. channel.SetRate rate
  266. End Function
  267. Rem
  268. bbdoc: Pause audio channel playback
  269. about:
  270. Pauses audio channel playback.
  271. end rem
  272. Function PauseChannel( channel:TChannel )
  273. channel.SetPaused True
  274. End Function
  275. Rem
  276. bbdoc: Resume audio channel playback
  277. about:
  278. Resumes audio channel playback after it has been paused by #CueSound or #PauseChannel.
  279. end rem
  280. Function ResumeChannel( channel:TChannel )
  281. channel.SetPaused False
  282. End Function
  283. Rem
  284. bbdoc: Get audio drivers
  285. about:
  286. Returns an array of strings, where each string describes an audio driver.
  287. End Rem
  288. Function AudioDrivers$[]()
  289. Local devs$[100],n
  290. Local t:TAudioDriver=_drivers
  291. While t And n<100
  292. devs[n]=t.Name()
  293. n:+1
  294. t=t._succ
  295. Wend
  296. Return devs[..n]
  297. End Function
  298. Rem
  299. bbdoc: Determine if an audio driver exists
  300. about:
  301. Returns True if the audio drvier specified by @driver exists.
  302. End Rem
  303. Function AudioDriverExists( name$ )
  304. name=name.ToLower()
  305. Local t:TAudioDriver=_drivers
  306. While t
  307. If t.Name().ToLower()=name Return True
  308. t=t._succ
  309. Wend
  310. End Function
  311. Rem
  312. bbdoc: Set current audio driver
  313. about:
  314. Returns true if the audio driver was successfully set.
  315. End Rem
  316. Function SetAudioDriver( name$ )
  317. name=name.ToLower()
  318. Shutdown
  319. _driver=_nullDriver
  320. Local t:TAudioDriver=_drivers
  321. While t
  322. If t.Name().ToLower()=name
  323. If t.Startup()
  324. _driver=t
  325. Return True
  326. EndIf
  327. Return False
  328. EndIf
  329. t=t._succ
  330. Wend
  331. End Function