Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : #include "precompiled.h" 18 : 19 : #include "JSInterface_Sound.h" 20 : 21 : #include "lib/config2.h" 22 : #include "lib/file/vfs/vfs_path.h" 23 : #include "scriptinterface/FunctionWrapper.h" 24 : #include "soundmanager/ISoundManager.h" 25 : #include "soundmanager/SoundManager.h" 26 : 27 : #include <string> 28 : 29 : namespace JSI_Sound 30 : { 31 : #if CONFIG2_AUDIO 32 : 33 0 : void StartMusic() 34 : { 35 0 : if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager) 36 0 : sndManager->SetMusicEnabled(true); 37 0 : } 38 : 39 0 : void StopMusic() 40 : { 41 0 : if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager) 42 0 : sndManager->SetMusicEnabled(false); 43 0 : } 44 : 45 0 : void ClearPlaylist() 46 : { 47 0 : if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager) 48 0 : sndManager->ClearPlayListItems(); 49 0 : } 50 : 51 0 : void AddPlaylistItem(const std::wstring& filename) 52 : { 53 0 : if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager) 54 0 : sndManager->AddPlayListItem(VfsPath(filename)); 55 0 : } 56 : 57 0 : void StartPlaylist(bool looping) 58 : { 59 0 : if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager) 60 0 : sndManager->StartPlayList(looping ); 61 0 : } 62 : 63 0 : void PlayMusic(const std::wstring& filename, bool looping) 64 : { 65 0 : if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager) 66 0 : sndManager->PlayAsMusic(filename, looping); 67 0 : } 68 : 69 0 : void PlayUISound(const std::wstring& filename, bool looping) 70 : { 71 0 : if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager) 72 0 : sndManager->PlayAsUI(filename, looping); 73 0 : } 74 : 75 0 : void PlayAmbientSound(const std::wstring& filename, bool looping) 76 : { 77 0 : if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager) 78 0 : sndManager->PlayAsAmbient(filename, looping); 79 0 : } 80 : 81 0 : bool MusicPlaying() 82 : { 83 0 : return true; 84 : } 85 : 86 0 : void SetMasterGain(float gain) 87 : { 88 0 : if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager) 89 0 : sndManager->SetMasterGain(gain); 90 0 : } 91 : 92 0 : void SetMusicGain(float gain) 93 : { 94 0 : if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager) 95 0 : sndManager->SetMusicGain(gain); 96 0 : } 97 : 98 0 : void SetAmbientGain(float gain) 99 : { 100 0 : if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager) 101 0 : sndManager->SetAmbientGain(gain); 102 0 : } 103 : 104 0 : void SetActionGain(float gain) 105 : { 106 0 : if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager) 107 0 : sndManager->SetActionGain(gain); 108 0 : } 109 : 110 0 : void SetUIGain(float gain) 111 : { 112 0 : if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager) 113 0 : sndManager->SetUIGain(gain); 114 0 : } 115 : 116 : #else 117 : 118 : bool MusicPlaying( ){ return false; } 119 : void PlayAmbientSound(const std::wstring& /*filename*/, bool /*looping*/ ){} 120 : void PlayUISound(const std::wstring& /*filename*/, bool /*looping*/ ) {} 121 : void PlayMusic(const std::wstring& /*filename*/, bool /*looping*/ ) {} 122 : void StartPlaylist(bool /*looping*/ ){} 123 : void AddPlaylistItem(const std::wstring& /*filename*/ ){} 124 : void ClearPlaylist( ){} 125 : void StopMusic( ){} 126 : void StartMusic( ){} 127 : void SetMasterGain(float /*gain*/){} 128 : void SetMusicGain(float /*gain*/){} 129 : void SetAmbientGain(float /*gain*/){} 130 : void SetActionGain(float /*gain*/){} 131 : void SetUIGain(float /*gain*/){} 132 : 133 : #endif 134 : 135 26 : void RegisterScriptFunctions(const Script::Request& rq) 136 : { 137 26 : Script::Function::Register<&StartMusic>(rq, "StartMusic"); 138 26 : Script::Function::Register<&StopMusic>(rq, "StopMusic"); 139 26 : Script::Function::Register<&ClearPlaylist>(rq, "ClearPlaylist"); 140 26 : Script::Function::Register<&AddPlaylistItem>(rq, "AddPlaylistItem"); 141 26 : Script::Function::Register<&StartPlaylist>(rq, "StartPlaylist"); 142 26 : Script::Function::Register<&PlayMusic>(rq, "PlayMusic"); 143 26 : Script::Function::Register<&PlayUISound>(rq, "PlayUISound"); 144 26 : Script::Function::Register<&PlayAmbientSound>(rq, "PlayAmbientSound"); 145 26 : Script::Function::Register<&MusicPlaying>(rq, "MusicPlaying"); 146 26 : Script::Function::Register<&SetMasterGain>(rq, "SetMasterGain"); 147 26 : Script::Function::Register<&SetMusicGain>(rq, "SetMusicGain"); 148 26 : Script::Function::Register<&SetAmbientGain>(rq, "SetAmbientGain"); 149 26 : Script::Function::Register<&SetActionGain>(rq, "SetActionGain"); 150 26 : Script::Function::Register<&SetUIGain>(rq, "SetUIGain"); 151 26 : } 152 : }