audio.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. function Audio::create(%this)
  2. {
  3. if(OpenALInitDriver())
  4. {
  5. %this.MusicOn = true;
  6. %this.SoundOn = true;
  7. %this.setMasterVolume(1);
  8. %this.SetMusicVolume(1);
  9. %this.SetSoundVolume(1);
  10. %this.CurrentSong = "";
  11. }
  12. }
  13. function Audio::destroy( %this )
  14. {
  15. alxStopAll();
  16. OpenALShutdownDriver();
  17. }
  18. function Audio::setMasterVolume(%this, %volume)
  19. {
  20. %this.MasterVolume = mClamp(%volume, 0, 1);
  21. alxListenerf(AL_GAIN_LINEAR, %this.MasterVolume);
  22. }
  23. function Audio::SetMusicVolume(%this, %volume)
  24. {
  25. %this.MusicVolume = mClamp(%volume, 0, 1);
  26. alxSetChannelVolume(0, %this.MusicVolume);
  27. }
  28. function Audio::SetSoundVolume(%this, %volume)
  29. {
  30. %this.SoundVolume = mClamp(%volume, 0,1);
  31. alxSetChannelVolume(1, %this.SoundVolume);
  32. }
  33. function Audio::SetPitch(%this, %noise, %pitch)
  34. {
  35. alxSourcef(%noise, AL_PITCH, %pitch);
  36. }
  37. function Audio::StopAllAndPlayMusic(%this, %song)
  38. {
  39. if(%song !$= %this.CurrentSong)
  40. {
  41. alxStopAll();
  42. %this.CurrentSong = %song;
  43. if(%this.MusicOn)
  44. {
  45. cancel(%this.fadeMusicSchedule);
  46. %this.Music = alxPlay(%song);
  47. }
  48. }
  49. }
  50. function Audio::PlayMusic(%this, %song)
  51. {
  52. if(%song !$= %this.CurrentSong)
  53. {
  54. %this.CurrentSong = %song;
  55. if(%this.MusicOn)
  56. {
  57. cancel(%this.fadeMusicSchedule);
  58. alxStop(%this.Music);
  59. %this.Music = alxPlay(%song);
  60. }
  61. }
  62. }
  63. function Audio::RestartMusic(%this)
  64. {
  65. cancel(%this.fadeMusicSchedule);
  66. alxStop(%this.Music);
  67. %this.Music = alxPlay(%this.CurrentSong);
  68. }
  69. function Audio::StopMusic(%this)
  70. {
  71. alxStop(%this.Music);
  72. }
  73. function Audio::PlaySound(%this, %name)
  74. {
  75. if(%this.SoundOn)
  76. {
  77. %sound = alxPlay(%name);
  78. }
  79. return %sound;
  80. }
  81. function Audio::FadeMusicVolumeTo(%this, %time, %volume)
  82. {
  83. //Time is in milliseconds
  84. %volume = mClamp(%volume, 0,1);
  85. if(%volume == %this.MusicVolume)
  86. {
  87. return;
  88. }
  89. %difference = %volume - %this.MusicVolume;
  90. %rate = 50;
  91. %steps = mCeil(%time / %rate);
  92. %delta = %difference / %steps;
  93. %this.fadeMusicSchedule = %this.schedule(%rate, "FadeMusicStep", %volume, %rate, %delta);
  94. }
  95. function Audio::FadeMusicStep(%this, %targetVolume, %rate, %delta)
  96. {
  97. if(mAbs(%targetVolume - %this.MusicVolume) < mAbs(%delta))
  98. {
  99. %this.SetMusicVolume(%targetVolume);
  100. return;
  101. }
  102. %this.SetMusicVolume(%this.MusicVolume + %delta);
  103. %this.fadeMusicSchedule = %this.schedule(%rate, "FadeMusicStep", %targetVolume, %rate, %delta);
  104. }