Sounds.BASS.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // The multimedia graphics platform GLScene https://github.com/glscene
  3. //
  4. unit Sounds.BASS;
  5. (*
  6. BASS based sound-manager http://www.un4seen.com/music/, free for freeware
  7. Unsupported feature(s) : sound source velocity,
  8. looping (sounds are played either once or forever)
  9. source priorities (not relevant, channels are not limited)
  10. *)
  11. interface
  12. {$I GLScene.inc}
  13. uses
  14. System.Classes,
  15. System.SysUtils,
  16. Vcl.Forms,
  17. GLS.Sound,
  18. GLS.Scene,
  19. GLS.VectorTypes,
  20. GLS.VectorGeometry,
  21. Sounds.BASSImport;
  22. type
  23. TBASS3DAlgorithm = (algDefault, algOff, algFull, algLight);
  24. TGLSMBASS = class(TGLSoundManager)
  25. private
  26. FActivated: Boolean;
  27. FAlgorithm3D: TBASS3DAlgorithm;
  28. protected
  29. function DoActivate: Boolean; override;
  30. procedure DoDeActivate; override;
  31. procedure NotifyMasterVolumeChange; override;
  32. procedure Notify3DFactorsChanged; override;
  33. procedure NotifyEnvironmentChanged; override;
  34. procedure KillSource(aSource: TGLBaseSoundSource); override;
  35. procedure UpdateSource(aSource: TGLBaseSoundSource); override;
  36. procedure MuteSource(aSource: TGLBaseSoundSource; muted: Boolean); override;
  37. procedure PauseSource(aSource: TGLBaseSoundSource;
  38. paused: Boolean); override;
  39. function GetDefaultFrequency(aSource: TGLBaseSoundSource): Integer;
  40. public
  41. constructor Create(AOwner: TComponent); override;
  42. destructor Destroy; override;
  43. procedure UpdateSources; override;
  44. function CPUUsagePercent: Single; override;
  45. function EAXSupported: Boolean; override;
  46. published
  47. property Algorithm3D: TBASS3DAlgorithm read FAlgorithm3D write FAlgorithm3D
  48. default algDefault;
  49. end;
  50. // ---------------------------------------------------------------------
  51. implementation
  52. // ---------------------------------------------------------------------
  53. type
  54. TBASSInfo = record
  55. channel: HCHANNEL;
  56. sample: HSAMPLE;
  57. end;
  58. PBASSInfo = ^TBASSInfo;
  59. procedure VectorToBASSVector(const aVector: TGLVector;
  60. var aBASSVector: BASS_3DVECTOR);
  61. begin
  62. aBASSVector.x := aVector.x;
  63. aBASSVector.y := aVector.y;
  64. aBASSVector.z := -aVector.z;
  65. end;
  66. // ------------------
  67. // ------------------ TGLSMBASS ------------------
  68. // ------------------
  69. constructor TGLSMBASS.Create(AOwner: TComponent);
  70. begin
  71. inherited Create(AOwner);
  72. BASS_Load(bassdll);
  73. MaxChannels := 32;
  74. end;
  75. destructor TGLSMBASS.Destroy;
  76. begin
  77. inherited Destroy;
  78. BASS_UnLoad;
  79. end;
  80. function TGLSMBASS.DoActivate: Boolean;
  81. const
  82. c3DAlgo: array [algDefault .. algLight] of Integer = (BASS_3DALG_DEFAULT,
  83. BASS_3DALG_OFF, BASS_3DALG_FULL, BASS_3DALG_LIGHT);
  84. begin
  85. Assert(bass_isloaded, 'BASS DLL is not present');
  86. if not BASS_Init(1, OutputFrequency, BASS_DEVICE_3D, Application.Handle, nil)
  87. then
  88. begin
  89. Result := False;
  90. Exit;
  91. end;
  92. if not BASS_Start then
  93. begin
  94. Result := False;
  95. Exit;
  96. end;
  97. FActivated := True;
  98. BASS_SetConfig(BASS_CONFIG_3DALGORITHM, c3DAlgo[FAlgorithm3D]);
  99. NotifyMasterVolumeChange;
  100. Notify3DFactorsChanged;
  101. if Environment <> seDefault then
  102. NotifyEnvironmentChanged;
  103. Result := True;
  104. end;
  105. procedure TGLSMBASS.DoDeActivate;
  106. begin
  107. FActivated := False;
  108. BASS_Stop;
  109. BASS_Free;
  110. end;
  111. procedure TGLSMBASS.NotifyMasterVolumeChange;
  112. begin
  113. if FActivated then
  114. BASS_SetVolume(Round(MasterVolume * 100));
  115. end;
  116. procedure TGLSMBASS.Notify3DFactorsChanged;
  117. begin
  118. if FActivated then
  119. BASS_Set3DFactors(DistanceFactor, RollOffFactor, DopplerFactor);
  120. end;
  121. procedure TGLSMBASS.NotifyEnvironmentChanged;
  122. const
  123. cEnvironmentToBASSConstant: array [seDefault .. sePsychotic] of Integer =
  124. (EAX_ENVIRONMENT_GENERIC, EAX_ENVIRONMENT_PADDEDCELL, EAX_ENVIRONMENT_ROOM,
  125. EAX_ENVIRONMENT_BATHROOM, EAX_ENVIRONMENT_LIVINGROOM,
  126. EAX_ENVIRONMENT_STONEROOM, EAX_ENVIRONMENT_AUDITORIUM,
  127. EAX_ENVIRONMENT_CONCERTHALL, EAX_ENVIRONMENT_CAVE, EAX_ENVIRONMENT_ARENA,
  128. EAX_ENVIRONMENT_HANGAR, EAX_ENVIRONMENT_CARPETEDHALLWAY,
  129. EAX_ENVIRONMENT_HALLWAY, EAX_ENVIRONMENT_STONECORRIDOR,
  130. EAX_ENVIRONMENT_ALLEY, EAX_ENVIRONMENT_FOREST, EAX_ENVIRONMENT_CITY,
  131. EAX_ENVIRONMENT_MOUNTAINS, EAX_ENVIRONMENT_QUARRY, EAX_ENVIRONMENT_PLAIN,
  132. EAX_ENVIRONMENT_PARKINGLOT, EAX_ENVIRONMENT_SEWERPIPE,
  133. EAX_ENVIRONMENT_UNDERWATER, EAX_ENVIRONMENT_DRUGGED, EAX_ENVIRONMENT_DIZZY,
  134. EAX_ENVIRONMENT_PSYCHOTIC);
  135. begin
  136. if FActivated and EAXSupported then
  137. BASS_SetEAXParameters(cEnvironmentToBASSConstant[Environment], -1, -1, -1);
  138. end;
  139. procedure TGLSMBASS.KillSource(aSource: TGLBaseSoundSource);
  140. var
  141. p: PBASSInfo;
  142. begin
  143. if aSource.ManagerTag <> 0 then
  144. begin
  145. p := PBASSInfo(aSource.ManagerTag);
  146. if p.channel <> 0 then
  147. if not BASS_ChannelStop(p.channel) then
  148. Assert(False);
  149. BASS_SampleFree(p.sample);
  150. FreeMem(p);
  151. aSource.ManagerTag := 0;
  152. end;
  153. end;
  154. procedure TGLSMBASS.UpdateSource(aSource: TGLBaseSoundSource);
  155. var
  156. i: Integer;
  157. p: PBASSInfo;
  158. objPos, objOri, objVel: TGLVector;
  159. position, orientation, velocity: BASS_3DVECTOR;
  160. res: Boolean;
  161. begin
  162. if (sscSample in aSource.Changes) then
  163. begin
  164. KillSource(aSource);
  165. end;
  166. if (aSource.sample = nil) or (aSource.sample.Data = nil) or
  167. (aSource.sample.Data.WAVDataSize = 0) then
  168. Exit;
  169. if aSource.ManagerTag <> 0 then
  170. begin
  171. p := PBASSInfo(aSource.ManagerTag);
  172. if BASS_ChannelIsActive(p.channel) = 0 then
  173. begin
  174. p.channel := 0;
  175. aSource.Free;
  176. Exit;
  177. end;
  178. end
  179. else
  180. begin
  181. p := AllocMem(SizeOf(TBASSInfo));
  182. p.channel := 0;
  183. i := BASS_SAMPLE_VAM + BASS_SAMPLE_3D + BASS_SAMPLE_OVER_DIST;
  184. if aSource.NbLoops > 1 then
  185. i := i + BASS_SAMPLE_LOOP;
  186. p.sample := BASS_SampleLoad(True, aSource.sample.Data.WAVData, 0,
  187. aSource.sample.Data.WAVDataSize, MaxChannels, i);
  188. Assert(p.sample <> 0, 'BASS Error ' + IntToStr(Integer(BASS_ErrorGetCode)));
  189. aSource.ManagerTag := Integer(p);
  190. if aSource.Frequency <= 0 then
  191. aSource.Frequency := -1;
  192. end;
  193. if aSource.Origin <> nil then
  194. begin
  195. objPos := aSource.Origin.AbsolutePosition;
  196. objOri := aSource.Origin.AbsoluteZVector;
  197. objVel := NullHmgVector;
  198. end
  199. else
  200. begin
  201. objPos := NullHmgPoint;
  202. objOri := ZHmgVector;
  203. objVel := NullHmgVector;
  204. end;
  205. VectorToBASSVector(objPos, position);
  206. VectorToBASSVector(objVel, velocity);
  207. VectorToBASSVector(objOri, orientation);
  208. if p.channel = 0 then
  209. begin
  210. p.channel := BASS_SampleGetChannel(p.sample, False);
  211. Assert(p.channel <> 0);
  212. BASS_ChannelSet3DPosition(p.channel, position, orientation, velocity);
  213. BASS_ChannelSet3DAttributes(p.channel, BASS_3DMODE_NORMAL,
  214. aSource.MinDistance, aSource.MaxDistance, Round(aSource.InsideConeAngle),
  215. Round(aSource.OutsideConeAngle), Round(aSource.ConeOutsideVolume * 100));
  216. if not aSource.Pause then
  217. BASS_ChannelPlay(p.channel, True);
  218. end
  219. else
  220. BASS_ChannelSet3DPosition(p.channel, position, orientation, velocity);
  221. if p.channel <> 0 then
  222. begin
  223. res := BASS_ChannelSetAttribute(p.channel, BASS_ATTRIB_FREQ, 0);
  224. Assert(res);
  225. if aSource.Mute then
  226. res := BASS_ChannelSetAttribute(p.channel, BASS_ATTRIB_VOL, 0)
  227. else
  228. res := BASS_ChannelSetAttribute(p.channel, BASS_ATTRIB_VOL,
  229. aSource.Volume);
  230. Assert(res);
  231. end
  232. else
  233. aSource.Free;
  234. inherited UpdateSource(aSource);
  235. end;
  236. procedure TGLSMBASS.MuteSource(aSource: TGLBaseSoundSource; muted: Boolean);
  237. var
  238. p: PBASSInfo;
  239. res: Boolean;
  240. begin
  241. if aSource.ManagerTag <> 0 then
  242. begin
  243. p := PBASSInfo(aSource.ManagerTag);
  244. if muted then
  245. res := BASS_ChannelSetAttribute(p.channel, BASS_ATTRIB_VOL, 0)
  246. else
  247. res := BASS_ChannelSetAttribute(p.channel, BASS_ATTRIB_VOL,
  248. aSource.Volume);
  249. Assert(res);
  250. end;
  251. end;
  252. procedure TGLSMBASS.PauseSource(aSource: TGLBaseSoundSource; paused: Boolean);
  253. var
  254. p: PBASSInfo;
  255. begin
  256. if aSource.ManagerTag <> 0 then
  257. begin
  258. p := PBASSInfo(aSource.ManagerTag);
  259. if paused then
  260. BASS_ChannelPause(p.channel)
  261. else
  262. BASS_ChannelPlay(p.channel, False);
  263. end;
  264. end;
  265. procedure TGLSMBASS.UpdateSources;
  266. var
  267. objPos, objVel, objDir, objUp: TGLVector;
  268. position, velocity, fwd, top: BASS_3DVECTOR;
  269. begin
  270. // update listener
  271. ListenerCoordinates(objPos, objVel, objDir, objUp);
  272. VectorToBASSVector(objPos, position);
  273. VectorToBASSVector(objVel, velocity);
  274. VectorToBASSVector(objDir, fwd);
  275. VectorToBASSVector(objUp, top);
  276. if not BASS_Set3DPosition(position, velocity, fwd, top) then
  277. Assert(False);
  278. // update sources
  279. inherited;
  280. (* if not *) BASS_Apply3D; (* then Assert(False); *)
  281. end;
  282. function TGLSMBASS.CPUUsagePercent: Single;
  283. begin
  284. Result := BASS_GetCPU * 100;
  285. end;
  286. function TGLSMBASS.EAXSupported: Boolean;
  287. var
  288. c: Cardinal;
  289. s: Single;
  290. begin
  291. Result := BASS_GetEAXParameters(c, s, s, s);
  292. end;
  293. function TGLSMBASS.GetDefaultFrequency(aSource: TGLBaseSoundSource): Integer;
  294. var
  295. p: PBASSInfo;
  296. sampleInfo: BASS_Sample;
  297. begin
  298. try
  299. p := PBASSInfo(aSource.ManagerTag);
  300. BASS_SampleGetInfo(p.sample, sampleInfo);
  301. Result := sampleInfo.freq;
  302. except
  303. Result := -1;
  304. end;
  305. end;
  306. end.