freeaudioaudio.bmx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. SuperStrict
  2. Rem
  3. bbdoc: Audio/FreeAudio audio
  4. about:
  5. The FreeAudio audio module provides FreeAudio drivers for use with the #audio module.
  6. End Rem
  7. Module BRL.FreeAudioAudio
  8. ModuleInfo "Version: 1.14"
  9. ModuleInfo "Author: Mark Sibly"
  10. ModuleInfo "License: zlib/libpng"
  11. ModuleInfo "Copyright: Blitz Research Ltd"
  12. ModuleInfo "Modserver: BRL"
  13. ModuleInfo "History: 1.14"
  14. ModuleInfo "History: Added Pulse audio on Linux."
  15. ModuleInfo "History: Use ALSA instead of OSS on Linux."
  16. ModuleInfo "History: 1.13 Release"
  17. ModuleInfo "History: Fixed bug in FreeChannel Playing()"
  18. ModuleInfo "History: 1.12 Release"
  19. ModuleInfo "History: Added call to fa_FreeChannel to TFreeAudioChannel destructor"
  20. ModuleInfo "History: 1.11 Release"
  21. ModuleInfo "History: Nudge"
  22. ModuleInfo "History: 1.10 Release"
  23. ModuleInfo "History: Bumped"
  24. ModuleInfo "History: 1.08 Release"
  25. ModuleInfo "History: Added src sample reference to TFreeAudioSound"
  26. ModuleInfo "History: Channel playing now returns sample playback position"
  27. ModuleInfo "History: 1.07 Release"
  28. ModuleInfo "History: Added DirectSound driver"
  29. ModuleInfo "History: 1.06 Release"
  30. ModuleInfo "History: Added Method TFreeAudioChannel.Position"
  31. ModuleInfo "History: 1.05 Release"
  32. ModuleInfo "History: Mono and 8 bit sample support added"
  33. Import BRL.Audio
  34. Import Pub.FreeAudio
  35. Import Pub.DirectX
  36. Private
  37. Const CLOG:Int=False
  38. Public
  39. Type TFreeAudioSound Extends TSound
  40. Field fa_sound:Byte Ptr
  41. Method Delete()
  42. fa_FreeSound fa_sound
  43. If CLOG WriteStdout "Deleted FreeAudio sound "+Long(fa_sound)+"~n"
  44. End Method
  45. Method Play:TFreeAudioChannel( alloced_channel:TChannel ) Override
  46. Local channel:TFreeAudioChannel,fa_channel:Int
  47. If alloced_channel
  48. channel=TFreeAudioChannel( alloced_channel )
  49. If Not channel Return Null
  50. fa_channel=channel.fa_channel
  51. EndIf
  52. fa_channel=fa_PlaySound(fa_sound,False,fa_channel)
  53. If Not fa_channel Return Null
  54. If channel And channel.fa_channel=fa_channel Return channel
  55. Return TFreeAudioChannel.CreateWithChannel( fa_channel )
  56. End Method
  57. Method Cue:TFreeAudioChannel( alloced_channel:TChannel ) Override
  58. Local channel:TFreeAudioChannel,fa_channel:Int
  59. If alloced_channel
  60. channel=TFreeAudioChannel( alloced_channel )
  61. If Not channel Return Null
  62. fa_channel=channel.fa_channel
  63. EndIf
  64. fa_channel=fa_PlaySound( fa_sound,True,fa_channel )
  65. If Not fa_channel Return Null
  66. If channel And channel.fa_channel=fa_channel Return channel
  67. Return TFreeAudioChannel.CreateWithChannel( fa_channel )
  68. End Method
  69. Function CreateWithSound:TFreeAudioSound( fa_sound:Byte Ptr,src:TAudioSample )
  70. Local t:TFreeAudioSound=New TFreeAudioSound
  71. t.fa_sound=fa_sound
  72. ' t.src=src
  73. Return t
  74. End Function
  75. End Type
  76. Type TFreeAudioChannel Extends TChannel
  77. Field fa_channel:Int
  78. Method Delete()
  79. If fa_channel fa_FreeChannel fa_channel
  80. End Method
  81. Method Stop() Override
  82. fa_StopChannel fa_channel
  83. fa_channel=0
  84. End Method
  85. Method SetPaused( paused:Int ) Override
  86. fa_SetChannelPaused fa_channel,paused
  87. End Method
  88. Method SetVolume( volume# ) Override
  89. fa_SetChannelVolume fa_channel,volume
  90. End Method
  91. Method SetPan( pan# ) Override
  92. fa_SetChannelPan fa_channel,pan
  93. End Method
  94. Method SetDepth( depth# ) Override
  95. fa_SetChannelDepth fa_channel,depth
  96. End Method
  97. Method SetRate( rate# ) Override
  98. fa_SetChannelRate fa_channel,rate
  99. End Method
  100. Method Playing:Int() Override
  101. Local status:Int=fa_ChannelStatus( fa_channel )
  102. If status=FA_CHANNELSTATUS_FREE Return False
  103. If status&FA_CHANNELSTATUS_STOPPED Return False
  104. If status&FA_CHANNELSTATUS_PAUSED Return False
  105. Return True
  106. End Method
  107. Method Position:Int()
  108. Return fa_ChannelPosition( fa_channel )
  109. End Method
  110. Function CreateWithChannel:TFreeAudioChannel( fa_channel:Int )
  111. Local t:TFreeAudioChannel=New TFreeAudioChannel
  112. t.fa_channel=fa_channel
  113. Return t
  114. End Function
  115. End Type
  116. Type TFreeAudioAudioDriver Extends TAudioDriver
  117. Method Name$() Override
  118. Return _name
  119. End Method
  120. Method Startup:Int() Override
  121. If _mode<>-1 Return fa_Init( _mode )<>-1
  122. If fa_Init( 0 )<>-1 Return True
  123. ?Not MacOS
  124. Return fa_Init( 1 )<>-1
  125. ?
  126. End Method
  127. Method Shutdown() Override
  128. fa_Close
  129. End Method
  130. Method CreateSound:TFreeAudioSound( sample:TAudioSample,flags:Int ) Override
  131. Local channels:Int,bits:Int
  132. Select sample.format
  133. ?BigEndian
  134. Case SF_MONO16LE
  135. sample=sample.Convert(SF_MONO16BE)
  136. Case SF_STEREO16LE
  137. sample=sample.Convert(SF_STEREO16BE)
  138. ?LittleEndian
  139. Case SF_MONO16BE
  140. sample=sample.Convert(SF_MONO16LE)
  141. Case SF_STEREO16BE
  142. sample=sample.Convert(SF_STEREO16LE)
  143. ?
  144. End Select
  145. Local loop_flag:Int
  146. If (flags & 1) loop_flag=-1
  147. channels=ChannelsPerSample[sample.format]
  148. If Not channels Return Null
  149. bits=8*BytesPerSample[sample.format]/channels
  150. Local fa_sound:Byte Ptr=fa_CreateSound( sample.length,bits,channels,sample.hertz,sample.samples,loop_flag )
  151. If CLOG WriteStdout "Generated FreeAudio sound "+Long(fa_sound)+"~n"
  152. Return TFreeAudioSound.CreateWithSound( fa_sound,sample )
  153. End Method
  154. Method AllocChannel:TFreeAudioChannel() Override
  155. Local fa_channel:Int=fa_AllocChannel()
  156. If fa_channel Return TFreeAudioChannel.CreateWithChannel( fa_channel )
  157. End Method
  158. Function Create:TFreeAudioAudioDriver( name$,Mode:Int )
  159. Local t:TFreeAudioAudioDriver=New TFreeAudioAudioDriver
  160. t._name=name
  161. t._mode=Mode
  162. Return t
  163. End Function
  164. Field _name$,_mode:Int
  165. End Type
  166. ?Win32
  167. 'If DirectSoundCreate TFreeAudioAudioDriver.Create "FreeAudio DirectSound",1
  168. TFreeAudioAudioDriver.Create "FreeAudio Multimedia",0
  169. ?MacOS
  170. TFreeAudioAudioDriver.Create "FreeAudio CoreAudio",0
  171. ?Linuxx86
  172. TFreeAudioAudioDriver.Create "FreeAudio Pulse Audio",0
  173. TFreeAudioAudioDriver.Create "FreeAudio ALSA",1
  174. ?Linuxx64
  175. TFreeAudioAudioDriver.Create "FreeAudio Pulse Audio",0
  176. TFreeAudioAudioDriver.Create "FreeAudio ALSA",1
  177. ?raspberrypi
  178. TFreeAudioAudioDriver.Create "FreeAudio Pulse Audio",0
  179. TFreeAudioAudioDriver.Create "FreeAudio ALSA",1
  180. ?
  181. TFreeAudioAudioDriver.Create "FreeAudio",-1